/
verifier.go
299 lines (260 loc) · 9.44 KB
/
verifier.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
293
294
295
296
297
298
299
package authentication
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"fmt"
"sync"
"time"
"google.golang.org/protobuf/proto"
"github.com/k-katsuda/vehicle-command/pkg/protocol/protobuf/signatures"
universal "github.com/k-katsuda/vehicle-command/pkg/protocol/protobuf/universalmessage"
)
// A Verifier checks the authenticity of commands sent by a Signer.
type Verifier struct {
Peer
lock sync.Mutex
window uint64
}
// NewVerifier returns a Verifier.
// Set domain to universal.Domain_DOMAIN_BROADCAST if the Verifier shouldn't enforce domain
// checking. The Verifier's domain must be known in advance by the Signer.
func NewVerifier(private ECDHPrivateKey, id []byte, domain universal.Domain, signerPublicBytes []byte) (*Verifier, error) {
session, err := private.Exchange(signerPublicBytes)
if err != nil {
return nil, err
}
verifier := Verifier{
Peer: Peer{
domain: domain,
verifierName: id,
session: session,
},
window: 0,
}
if len(id) > 255 {
return nil, ErrMetadataFieldTooLong
}
if err := verifier.rotateEpochIfNeeded(); err != nil {
return nil, err
}
return &verifier, nil
}
func (v *Verifier) signatureError(code universal.MessageFault_E, challenge []byte) error {
if info, tag, err := v.signedSessionInfo(challenge); err == nil {
return &InvalidSignatureError{
Code: code,
EncodedInfo: info,
Tag: tag,
}
}
return newError(errCodeInternal, fmt.Sprintf("Error collecting session info after encountering %s", code))
}
func (v *Verifier) rotateEpochIfNeeded() error {
if v.timeZero.IsZero() || v.counter == 0xFFFFFFFF || v.timestamp() > uint32(epochLength/time.Second) {
if _, err := rand.Read(v.epoch[:]); err != nil {
v.counter = 0xFFFFFFFF
return newError(errCodeInternal, "RNG failure")
} else {
v.timeZero = time.Now()
v.counter = 0
}
}
return nil
}
func (v *Verifier) sessionInfo() (*signatures.SessionInfo, error) {
if err := v.rotateEpochIfNeeded(); err != nil {
return nil, err
}
info := &signatures.SessionInfo{
Counter: v.counter,
PublicKey: v.session.LocalPublicBytes(),
Epoch: v.epoch[:],
ClockTime: v.timestamp(),
}
return info, nil
}
// SessionInfo contains metadata used to prevent replay and similar attacks.
// A Signer must have the Verifier's SessionInfo on initialization.
func (v *Verifier) SessionInfo() (*signatures.SessionInfo, error) {
v.lock.Lock()
defer v.lock.Unlock()
return v.sessionInfo()
}
func (v *Verifier) signedSessionInfo(challenge []byte) (encodedInfo, tag []byte, err error) {
info, err := v.sessionInfo()
if err != nil {
return
}
encodedInfo, err = proto.Marshal(info)
if err != nil {
return
}
tag, err = v.session.SessionInfoHMAC(v.verifierName, challenge, encodedInfo)
return
}
// SignedSessionInfo returns a protobuf-encoded signatures.SessionInfo along with an authentication
// tag that a Signer may use to verify the info has not been tampered with.
func (v *Verifier) SignedSessionInfo(challenge []byte) (encodedInfo, tag []byte, err error) {
v.lock.Lock()
defer v.lock.Unlock()
return v.signedSessionInfo(challenge)
}
// SetSessionInfo attaches up-to-date session info to a message.
// This is useful when v encounters an error while authenticating a message and wishes to include
// session info in the error response, thereby allowing the Signer to resync.
func (v *Verifier) SetSessionInfo(challenge []byte, message *universal.RoutableMessage) error {
encodedInfo, tag, err := v.SignedSessionInfo(challenge)
if err != nil {
return err
}
message.Payload = &universal.RoutableMessage_SessionInfo{
SessionInfo: encodedInfo,
}
message.SubSigData = &universal.RoutableMessage_SignatureData{
SignatureData: &signatures.SignatureData{
SigType: &signatures.SignatureData_SessionInfoTag{
SessionInfoTag: &signatures.HMAC_Signature_Data{
Tag: tag,
},
},
},
}
return nil
}
// Verify message.
// If payload is encrypted, returns the plaintext. Otherwise extracts and returns the payload as-is.
func (v *Verifier) Verify(message *universal.RoutableMessage) (plaintext []byte, err error) {
v.lock.Lock()
defer v.lock.Unlock()
if err = v.rotateEpochIfNeeded(); err != nil {
return nil, err
}
if message.GetSignatureData() == nil {
return nil, newError(errCodeBadParameter, "signature data missing")
}
var counter uint32
switch sigData := message.GetSignatureData().SigType.(type) {
case *signatures.SignatureData_AES_GCM_PersonalizedData:
counter = sigData.AES_GCM_PersonalizedData.GetCounter()
plaintext, err = v.verifyGCM(message, sigData.AES_GCM_PersonalizedData)
case *signatures.SignatureData_HMAC_PersonalizedData:
counter = sigData.HMAC_PersonalizedData.GetCounter()
plaintext, err = v.verifyHMAC(message, sigData.HMAC_PersonalizedData)
default:
return nil, newError(errCodeBadParameter, "unrecognized authentication method")
}
if err != nil {
return nil, err
}
if counter > 0 {
var ok bool
if v.counter, v.window, ok = updateSlidingWindow(v.counter, v.window, counter); !ok {
return nil, v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_INVALID_TOKEN_OR_COUNTER, message.GetUuid())
}
}
return
}
// updateSlidingWindow takes the current counter value (i.e., the highest
// counter value of any authentic message received so far), the current sliding
// window, and the newCounter value from an incoming message. The function
// returns the updated counter and window values and sets ok to true if it
// could confirm that newCounter has never been previously used. If ok is
// false, then updatedCounter = counter and updatedWindow = window.
func updateSlidingWindow(counter uint32, window uint64, newCounter uint32) (updatedCounter uint32, updatedWindow uint64, ok bool) {
// If we exit early due to an error, we want to leave the counter/window
// state unchanged. Therefore we initialize return values to the current
// state.
updatedCounter = counter
updatedWindow = window
ok = false
if counter == newCounter {
// This counter value has been used before.
return
}
if newCounter < counter {
// This message arrived out of order.
age := counter - newCounter
if age > windowSize {
// Our history doesn't go back this far, so we can't determine if
// we've seen this newCounter value before.
return
}
if window>>(age-1)&1 == 1 {
// The newCounter value has been used before.
return
}
// Everything looks good.
ok = true
updatedWindow |= (1 << (age - 1))
return
}
// If we've reached this point, newCounter > counter, so newCounter is valid.
ok = true
updatedCounter = newCounter
// Compute how far we need to shift our sliding window.
shiftCount := newCounter - counter
updatedWindow <<= shiftCount
// We need to set the bit in our window that corresponds to counter (if
// newCounter = counter + 1, then this is the first [LSB] of the window).
updatedWindow |= uint64(1) << (shiftCount - 1)
return
}
func (v *Verifier) verifyGCM(message *universal.RoutableMessage, gcmData *signatures.AES_GCM_Personalized_Signature_Data) (plaintext []byte, err error) {
if err = v.verifySessionInfo(message, gcmData); err != nil {
return nil, err
}
meta := newMetadata()
if err := v.extractMetadata(meta, message, gcmData, signatures.SignatureType_SIGNATURE_TYPE_AES_GCM_PERSONALIZED); err != nil {
return nil, err
}
plaintext, err = v.session.Decrypt(
gcmData.GetNonce(), message.GetProtobufMessageAsBytes(), meta.Checksum(nil), gcmData.GetTag())
if err != nil {
return nil, v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_INVALID_SIGNATURE, message.GetUuid())
}
return
}
func (v *Verifier) verifyHMAC(message *universal.RoutableMessage, hmacData *signatures.HMAC_Personalized_Signature_Data) (plaintext []byte, err error) {
if err = v.verifySessionInfo(message, hmacData); err != nil {
return nil, err
}
expectedTag, err := v.hmacTag(message, hmacData)
if err != nil {
return nil, err
}
if !hmac.Equal(hmacData.Tag, expectedTag) {
return nil, v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_INVALID_SIGNATURE, message.GetUuid())
}
return message.GetProtobufMessageAsBytes(), nil
}
func (v *Verifier) verifySessionInfo(message *universal.RoutableMessage, info sessionInfo) error {
if domain := message.GetToDestination().GetDomain(); domain != v.domain && v.domain != universal.Domain_DOMAIN_BROADCAST {
return newError(errCodeInvalidDomain, "wrong domain")
}
epoch := info.GetEpoch()
if epoch != nil {
if !bytes.Equal(epoch[:], v.epoch[:]) {
return v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_INCORRECT_EPOCH, message.GetUuid())
}
}
expiresAt := info.GetExpiresAt()
if expiresAt != 0 && expiresAt < v.timestamp() {
return v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_TIME_EXPIRED, message.GetUuid())
}
if time.Duration(expiresAt) > epochLength/time.Second {
return v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_BAD_PARAMETER, message.GetUuid())
}
// A counter value of zero disables the counter check, allow messages to
// arrive out-of-order. This should only be used for messages that can
// safely be replayed, that are likely to arrive out of order, and that
// have a short expiration time.
counter := info.GetCounter()
if counter == 0 || counter < v.counter {
// A message that arrives out of order must have a short expiration time remaining.
if expiresAt == 0 || expiresAt-v.timestamp() > maxSecondsWithoutCounter {
return v.signatureError(universal.MessageFault_E_MESSAGEFAULT_ERROR_TIME_TO_LIVE_TOO_LONG, message.GetUuid())
}
}
return nil
}