-
Notifications
You must be signed in to change notification settings - Fork 376
/
crypto_sigchain.go
174 lines (134 loc) · 3.97 KB
/
crypto_sigchain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cryptosigchain
import (
"errors"
"time"
"berty.tech/go/pkg/iface"
"github.com/libp2p/go-libp2p-core/crypto"
)
var theFuture = time.Date(2199, time.December, 31, 0, 0, 0, 0, time.UTC)
func (m *SigChain) GetInitialEntry() iface.SigChainEntry {
return m.ListEntries()[0]
}
func (m *SigChain) GetLastEntry() iface.SigChainEntry {
entries := m.ListEntries()
if len(entries) == 0 {
return nil
}
return entries[len(entries)-1]
}
func (m *SigChain) ListEntries() []iface.SigChainEntry {
var entries []iface.SigChainEntry
for _, e := range m.Entries {
entries = append(entries, NewWrappedSigChainEntry(m, e))
}
return entries
}
func (m *SigChain) ListCurrentPubKeys() []crypto.PubKey {
pubKeys := map[string][]byte{}
var pubKeysSlice []crypto.PubKey
for _, e := range m.Entries {
entryType := iface.SigChainEntryType(e.EntryTypeCode)
if entryType == iface.SigChainEntryTypeUndefined {
continue
} else if entryType == iface.SigChainEntryTypeRemoveKey {
delete(pubKeys, string(e.SubjectPublicKeyBytes))
} else {
pubKeys[string(e.SubjectPublicKeyBytes)] = e.SubjectPublicKeyBytes
}
}
for _, p := range pubKeys {
pubKey, err := crypto.UnmarshalPublicKey(p)
if err != nil {
continue
}
pubKeysSlice = append(pubKeysSlice, pubKey)
}
return pubKeysSlice
}
func (m *SigChain) Init(privKey crypto.PrivKey) (iface.SigChainEntry, error) {
if len(m.Entries) > 0 {
return nil, errors.New("sig chain already initialized")
}
subjectKeyBytes, err := privKey.GetPublic().Bytes()
if err != nil {
return nil, err
}
return m.appendEntry(privKey, &SigChainEntry{
EntryTypeCode: uint32(iface.SigChainEntryTypeInitChain),
SubjectPublicKeyBytes: subjectKeyBytes,
})
}
func (m *SigChain) AddEntry(privKey crypto.PrivKey, pubKey crypto.PubKey) (iface.SigChainEntry, error) {
if !m.isKeyCurrentlyPresent(privKey.GetPublic()) {
return nil, errors.New("not allowed to add entry")
}
if m.isKeyCurrentlyPresent(pubKey) {
return nil, errors.New("pub key is already listed in the sig chain")
}
subjectKeyBytes, err := pubKey.Bytes()
if err != nil {
return nil, err
}
if len(m.Entries) == 0 {
return nil, errors.New("sig chain has not been initialized yet")
}
return m.appendEntry(privKey, &SigChainEntry{
EntryTypeCode: uint32(iface.SigChainEntryTypeAddKey),
SubjectPublicKeyBytes: subjectKeyBytes,
})
}
func (m *SigChain) RemoveEntry(privKey crypto.PrivKey, pubKey crypto.PubKey) (iface.SigChainEntry, error) {
if !m.isKeyCurrentlyPresent(privKey.GetPublic()) {
return nil, errors.New("not allowed to remove entry")
}
if !m.isKeyCurrentlyPresent(pubKey) {
return nil, errors.New("pub key is not currently listed in the sig chain")
}
subjectKeyBytes, err := pubKey.Bytes()
if err != nil {
return nil, err
}
if len(m.Entries) == 0 {
return nil, errors.New("sig chain has not been initialized yet")
}
return m.appendEntry(privKey, &SigChainEntry{
EntryTypeCode: uint32(iface.SigChainEntryTypeRemoveKey),
SubjectPublicKeyBytes: subjectKeyBytes,
})
}
func (m *SigChain) isKeyCurrentlyPresent(pubKey crypto.PubKey) bool {
for _, allowedPubKey := range m.ListCurrentPubKeys() {
if allowedPubKey.Equals(pubKey) {
return true
}
}
return false
}
func (m *SigChain) appendEntry(privKey crypto.PrivKey, entry *SigChainEntry) (iface.SigChainEntry, error) {
lastEntry := m.GetLastEntry()
if lastEntry != nil {
entry.ParentEntryHash = lastEntry.GetEntryHash()
}
signerPubKeyBytes, err := privKey.GetPublic().Bytes()
if err != nil {
return nil, err
}
entry.CreatedAt = time.Now()
entry.ExpiringAt = theFuture
entry.SignerPublicKeyBytes = signerPubKeyBytes
wrappedEntry := NewWrappedSigChainEntry(m, entry)
err = wrappedEntry.Sign(privKey)
if err != nil {
return nil, err
}
m.Entries = append(m.Entries, entry)
return wrappedEntry, nil
}
func (m *SigChain) Check() error {
// TODO: implement me
return nil
}
func NewSigChain() iface.SigChain {
return &SigChain{}
}
var _ iface.SigChain = (*SigChain)(nil)