-
Notifications
You must be signed in to change notification settings - Fork 0
/
authority.go
292 lines (261 loc) · 8.38 KB
/
authority.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"fmt"
"github.com/DCNT-developer/dcnt/common/constants"
. "github.com/DCNT-developer/dcnt/common/identity"
"github.com/DCNT-developer/dcnt/common/interfaces"
)
// Checks the signature of a message. Returns an int based on who signed it:
// 1 -> Federated Signature
// 0 -> Audit Signature
// -1 -> Neither Fed or Audit Signature
func (st *State) VerifyAuthoritySignature(msg []byte, sig *[constants.SIGNATURE_LENGTH]byte, dbheight uint32) (rval int, err error) {
//defer func() { // debug code
// //st.LogMessage("executeMsg", "Signature Fail", msg)
// if rval <= 0 {
// m, err := messages.General.UnmarshalMessage(append(msg[:], make([]byte, 256)[:]...))
// if err != nil {
// st.LogPrintf("executeMsg", "Unable to unmarshal message")
// } else {
// st.LogMessage("executeMsg", "VerifyAuthoritySignature", m)
// }
// st.LogPrintf("executeMsg", "VerifyAuthoritySignature failed signature")
//
// feds := st.GetFedServers(dbheight)
// for _, fed := range feds {
// st.LogPrintf("executeMsg", "L %s:%s", messages.LookupName(fed.GetChainID().String()), fed.GetChainID().String()[6:12])
// }
//
// auds := st.GetAuditServers(dbheight)
// if auds == nil {
// st.LogPrintf("executeMsg", "Audit Servers are unknown at directory block height %d", dbheight)
// }
// for _, aud := range auds {
// st.LogPrintf("executeMsg", "A %s:%s", messages.LookupName(aud.GetChainID().String()), aud.GetChainID().String()[6:12])
// }
//
// st.LogPrintf("executeMsg", "auth.VerifySignature(msg:%x, sig:%x)", msg, sig)
// for _, s := range feds {
// auth, _ := st.GetAuthority(s.GetChainID())
// valid, err := auth.VerifySignature2(msg, sig)
// st.LogPrintf("executeMsg", "L-%x valid:%v, err:%v", s.GetChainID().Bytes()[3:6], valid, err)
// }
// for _, s := range auds {
// auth, _ := st.GetAuthority(s.GetChainID())
// valid, err := auth.VerifySignature2(msg, sig)
// st.LogPrintf("executeMsg", "A-%x valid:%v, err:%v", s.GetChainID().Bytes()[3:6], valid, err)
// }
//
// }
//}() // end debug code
feds := st.GetFedServers(dbheight)
if feds == nil {
return -1, fmt.Errorf("Federated Servers are unknown at directory block height %d", dbheight)
}
for _, fed := range feds {
auth, _ := st.GetAuthority(fed.GetChainID())
if auth == nil {
continue
}
valid, err := auth.VerifySignature(msg, sig)
if err == nil && valid {
return 1, nil
}
}
auds := st.GetAuditServers(dbheight)
if auds == nil {
return -1, fmt.Errorf("Audit Servers are unknown at directory block height %d", dbheight)
}
for _, aud := range auds {
auth, _ := st.GetAuthority(aud.GetChainID())
if auth == nil {
continue
}
valid, err := auth.VerifySignature(msg, sig)
if err == nil && valid {
return 0, nil
}
}
if st.CurrentMinute == 0 {
// Also allow leaders who were demoted if we are in minute 0
feds := st.LeaderPL.StartingFedServers
for _, fed := range feds {
auth, _ := st.GetAuthority(fed.GetChainID())
if auth == nil {
continue
}
valid, err := auth.VerifySignature(msg, sig)
if err == nil && valid {
return 1, nil
}
}
}
//fmt.Println("WARNING: A signature failed to validate.")
return -1, fmt.Errorf("%s", "Signature Key Invalid or not Federated Server Key")
}
// Checks the signature of a message. Returns an int based on who signed it:
// 1 -> Federated Signature
// 0 -> Audit Signature
// -1 -> Neither Fed or Audit Signature
func (st *State) FastVerifyAuthoritySignature(msg []byte, sig interfaces.IFullSignature, dbheight uint32) (int, error) {
if sig == nil { // no signature = not valid
return -1, fmt.Errorf("Message contained no signature")
}
feds := st.GetFedServers(dbheight)
if feds == nil {
return 0, fmt.Errorf("Federated Servers are unknown at directory block height %d", dbheight)
}
auds := st.GetAuditServers(dbheight)
for _, fed := range feds {
auth, _ := st.GetAuthority(fed.GetChainID())
if auth == nil {
continue
}
compareKey, err := auth.SigningKey.MarshalBinary()
if err == nil {
if pkEq(sig.GetKey(), compareKey) {
valid, err := auth.VerifySignature(msg, sig.GetSignature())
if err == nil && valid {
return 1, nil
}
}
}
}
for _, aud := range auds {
auth, _ := st.GetAuthority(aud.GetChainID())
if auth == nil {
continue
}
compareKey, err := auth.SigningKey.MarshalBinary()
if err == nil {
if pkEq(sig.GetKey(), compareKey) {
valid, err := auth.VerifySignature(msg, sig.GetSignature())
if err == nil && valid {
return 0, nil
}
}
}
}
//fmt.Println("WARNING: A signature failed to validate.")
// The checking pl for nil happens for unit testing
if st.CurrentMinute == 0 && st.LeaderPL != nil {
// Also allow leaders who were demoted if we are in minute 0
feds := st.LeaderPL.StartingFedServers
for _, fed := range feds {
auth, _ := st.GetAuthority(fed.GetChainID())
if auth == nil {
continue
}
compareKey, err := auth.SigningKey.MarshalBinary()
if err == nil {
if pkEq(sig.GetKey(), compareKey) {
valid, err := auth.VerifySignature(msg, sig.GetSignature())
if err == nil && valid {
return 1, nil
}
}
}
}
}
for i, f := range st.GetFedServers(st.LLeaderHeight) {
st.LogPrintf("executeMsg", "%d - %x", i, f.GetChainID().Bytes()[3:6])
}
return -1, fmt.Errorf("%s", "Signature Key Invalid or not Federated Server Key")
}
func pkEq(a, b []byte) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// Gets the authority matching the identity ChainID.
// Returns the authority and the int of its type:
// 1 -> Federated
// 0 -> Audit
// -1 -> Not fed or audit
// -2 -> Not found
func (st *State) GetAuthority(serverID interfaces.IHash) (*Authority, int) {
auth := st.IdentityControl.GetAuthority(serverID)
if auth == nil {
return nil, -2
}
return auth, auth.Type()
}
// We keep a 2 block history of their keys, this is so if we change their key and need to verify
// a message from 1 block ago, we still can. This function garbage collects old keys
func (st *State) UpdateAuthSigningKeys(height uint32) {
for key, auth := range st.IdentityControl.Authorities {
chopOffIndex := 0 // Index of the keys we should chop off
for i, key := range auth.KeyHistory {
// Keeping 2 heights worth.
if key.ActiveDBHeight <= height-2 {
chopOffIndex = i
}
}
if chopOffIndex > 0 {
if len(st.IdentityControl.Authorities[key].KeyHistory) == chopOffIndex+1 {
st.IdentityControl.Authorities[key].KeyHistory = nil
} else {
// This could be a memory leak if the authority keeps updating his keys every block,
// but the line above sets to nil if there is only 1 item left, so it will eventually
// garbage collect the whole slice
st.IdentityControl.Authorities[key].KeyHistory = st.IdentityControl.Authorities[auth.AuthorityChainID.Fixed()].KeyHistory[chopOffIndex+1:]
}
}
}
st.RepairAuthorities()
}
func (st *State) UpdateAuthorityFromABEntry(entry interfaces.IABEntry) error {
err := st.IdentityControl.ProcessABlockEntry(entry, st)
if err != nil {
return err
}
return nil
}
func (st *State) GetAuthorityServerType(chainID interfaces.IHash) int { // 0 = Federated, 1 = Audit
auth := st.IdentityControl.GetAuthority(chainID)
if auth == nil {
return -1
}
status := auth.Status
if status == constants.IDENTITY_FEDERATED_SERVER ||
status == constants.IDENTITY_PENDING_FEDERATED_SERVER {
return 0
}
if status == constants.IDENTITY_AUDIT_SERVER ||
status == constants.IDENTITY_PENDING_AUDIT_SERVER {
return 1
}
return -1
}
// RepairAuthorities will put the management chain of an identity in the authority if it
// is missing.
func (s *State) RepairAuthorities() {
// Fix any missing management chains
for _, iAuth := range s.IdentityControl.GetAuthorities() {
auth := iAuth.(*Authority)
if auth.ManagementChainID == nil || auth.ManagementChainID.IsZero() {
id := s.IdentityControl.GetIdentity(auth.AuthorityChainID)
if id != nil {
auth.ManagementChainID = id.ManagementChainID
id.Status = auth.Status
s.IdentityControl.SetAuthority(auth.AuthorityChainID, auth)
s.IdentityControl.SetIdentity(auth.AuthorityChainID, id)
}
}
}
}