forked from mozilla-services/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbor_test.go
555 lines (506 loc) · 17 KB
/
cbor_test.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package cose
import (
"errors"
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/assert"
"testing"
)
// Tests for encoding and decoding go-cose objects to and from CBOR
type CBORTestCase struct {
name string
obj interface{}
bytes []byte
}
var CBORTestCases = []CBORTestCase{
// golang data structures
{
"empty bstr",
[]byte(""),
[]byte("\x40"), // bytes(0) i.e. ""
},
{
"generic interface map",
map[interface{}]interface{}{1: -7},
[]byte("\xA1\x01\x26"),
},
// SignMessage Headers
{
"sign message with empty headers",
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{},
Unprotected: map[interface{}]interface{}{},
},
Payload: nil,
Signatures: nil,
},
// D8 62 # tag(98) COSE SignMessage tag
// 84 # array(4)
// 40 # bytes(0) empty protected headers
// # ""
// A0 # map(0) empty unprotectd headers
// F6 # primitive(22) nil / null payload
// 80 # array(0) no signatures
[]byte("\xd8\x62\x84\x40\xa0\xf6\x80"),
},
{
"sign message with alg in protected header",
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{"alg": "ES256"},
Unprotected: map[interface{}]interface{}{},
},
Payload: nil,
Signatures: nil,
},
// D8 62 # tag(98) COSE SignMessage tag
// 84 # array(4)
// 43 # bytes(3) bstr protected header
// A10126 # "\xA1\x01&"
// A0 # map(0) empty unprotected headers
// F6 # primitive(22) nil / null payload
// 80 # array(0) no signatures
//
// where bstr h'A10126' is:
// A1 # map(1)
// 01 # unsigned(1) common header ID for alg
// 26 # negative(7) ES256 alg ID
[]byte("\xd8\x62\x84\x43\xa1\x01\x26\xa0\xf6\x80"),
},
{
"sign message with alg in unprotected header",
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{},
Unprotected: map[interface{}]interface{}{"alg": "ES256"},
},
Payload: nil,
Signatures: nil,
},
// D8 62 # tag(98) COSE SignMessage tag
// 84 # array(4)
// 40 # bytes(0) empty protected headers
// # ""
// A1 # map(1) unprotected headers
// 01 # unsigned(1) common header ID for alg
// 26 # negative(7) ES256 alg ID
// F6 # primitive(22) nil / null payload
// 80 # array(0) no signatures
[]byte("\xd8\x62\x84\x40\xa1\x01\x26\xf6\x80"),
},
}
func MarshalsToExpectedBytes(t *testing.T, testCase CBORTestCase) {
assert := assert.New(t)
bytes, err := Marshal(testCase.obj)
assert.Nil(err)
assert.Equal(testCase.bytes, bytes)
}
func UnmarshalsWithoutErr(t *testing.T, testCase CBORTestCase) {
assert := assert.New(t)
_, err := Unmarshal(testCase.bytes)
assert.Nil(err)
}
func RoundtripsToExpectedBytes(t *testing.T, testCase CBORTestCase) {
assert := assert.New(t)
obj, err := Unmarshal(testCase.bytes)
assert.Nil(err)
bytes, err := Marshal(obj)
assert.Nil(err)
assert.Equal(testCase.bytes, bytes)
}
func TestCBOREncoding(t *testing.T) {
for _, testCase := range CBORTestCases {
t.Run(fmt.Sprintf("%s: MarshalsToExpectedBytes", testCase.name), func(t *testing.T) {
MarshalsToExpectedBytes(t, testCase)
})
t.Run(fmt.Sprintf("%s: UnmarshalsToExpectedInterface", testCase.name), func(t *testing.T) {
UnmarshalsWithoutErr(t, testCase)
})
t.Run(fmt.Sprintf("%s: RoundtripsToExpectedBytes", testCase.name), func(t *testing.T) {
RoundtripsToExpectedBytes(t, testCase)
})
}
}
func TestCBORMarshalSignMessageWithNilHeadersErrors(t *testing.T) {
assert := assert.New(t)
msg := NewSignMessage()
msg.Payload = nil
msg.Headers = nil
_, err := Marshal(msg)
assert.Equal("cbor: SignMessage has nil Headers", err.Error())
}
func TestCBORMarshalDuplicateKeysErrs(t *testing.T) {
assert := assert.New(t)
// NB: golang does not allow duplicate keys in a map literal
// so we don't test Marshalling duplicate entries both in
// Protected or Unprotected
// uncompressed one in each
msg := NewSignMessage()
msg.Payload = nil
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{
"alg": "ES256",
},
Unprotected: map[interface{}]interface{}{
"alg": "PS256",
},
}
_, err := Marshal(msg)
assert.Equal(errors.New("cbor: Duplicate header 1 found"), err)
// compressed one in each
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{
1: -7,
},
Unprotected: map[interface{}]interface{}{
1: -37,
},
}
_, err = Marshal(msg)
assert.Equal(errors.New("cbor: Duplicate header 1 found"), err)
// compressed and uncompressed both in Protected
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{
"alg": "ES256",
1: -37,
},
Unprotected: map[interface{}]interface{}{},
}
_, err = Marshal(msg)
assert.Equal(errors.New("cbor: Duplicate compressed and uncompressed common header 1 found in headers"), err)
// compressed and uncompressed both in Unprotected
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{},
Unprotected: map[interface{}]interface{}{
"alg": "ES256",
1: -37,
},
}
_, err = Marshal(msg)
assert.Equal(errors.New("cbor: Duplicate compressed and uncompressed common header 1 found in headers"), err)
// compressed and uncompressed one in each
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{
"alg": "ES256",
},
Unprotected: map[interface{}]interface{}{
1: -37,
},
}
_, err = Marshal(msg)
assert.Equal(errors.New("cbor: Duplicate header 1 found"), err)
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{
1: -37,
},
Unprotected: map[interface{}]interface{}{
"alg": "ES256",
},
}
_, err = Marshal(msg)
assert.Equal(errors.New("cbor: Duplicate header 1 found"), err)
// duplicate headers in a SignMessage Signature
msg.Headers = &Headers{
Protected: map[interface{}]interface{}{},
Unprotected: map[interface{}]interface{}{},
}
msg.AddSignature(&Signature{
Headers: &Headers{
Protected: map[interface{}]interface{}{
1: -37,
},
Unprotected: map[interface{}]interface{}{
"alg": "ES256",
},
},
SignatureBytes: []byte(""),
})
_, err = Marshal(msg)
assert.Equal("cbor: Duplicate signature header 1 found", err.Error())
}
func TestCBORDecodeNilSignMessagePayload(t *testing.T) {
assert := assert.New(t)
msg := NewSignMessage()
msg.Payload = nil
// tag(98) + array(4) [ bytes(0), map(0), nil/null, array(0) ]
b := HexToBytesOrDie("D862" + "84" + "40" + "A0" + "F6" + "80")
result, err := Unmarshal(b)
assert.Nil(err)
assert.Equal(result, *msg)
bytes, err := Marshal(result)
assert.Nil(err)
assert.Equal(bytes, b)
}
func TestCBORDecodingDuplicateKeys(t *testing.T) {
assert := assert.New(t)
type DecodeTestCase struct {
bytes []byte
result SignMessage
}
var cases = []DecodeTestCase{
{
// duplicate compressed key in protected
// tag(98) + array(4) [ bytes(5), map(0), bytes(0), array(0) ]
//
// where our bytes(5) is A201260128 or
// A2 # map(2)
// 01 # unsigned(1)
// 26 # negative(6)
// 01 # unsigned(1)
// 29 # negative(10)
//
// and decodes to map[1:-10] so last/rightmost value wins
HexToBytesOrDie("D862" + "84" + "45A201260129" + "A0" + "40" + "80"),
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{1: -10},
Unprotected: map[interface{}]interface{}{},
},
Payload: []byte(""),
Signatures: nil,
},
},
{
// duplicate compressed key in unprotected
// tag(98) + array(4) [ bytes(0), map(2), bytes(0), array(0) ]
//
// where our map(2) is
// 01 # unsigned(1)
// 26 # negative(6)
// 01 # unsigned(1)
// 29 # negative(10)
//
// and decodes to map[1:-10] so last/rightmost value wins
HexToBytesOrDie("D862" + "84" + "40" + "A201260129" + "40" + "80"),
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{},
Unprotected: map[interface{}]interface{}{1: -10},
},
Payload: []byte(""),
Signatures: nil,
},
},
{
// duplicate uncompressed key in protected
// tag(98) + array(4) [ bytes(21), map(0), bytes(0), array(0) ]
//
// see next test for what bytes(21) represents
HexToBytesOrDie("D862" + "84" + "55" + "A2" + "63" + "616C67" + "65" + "4553323536" + "63" + "616C67" + "65" + "5053323536" + "A0" + "40" + "80"),
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{
1: -37, // decoding compresses to check for duplicate keys
},
Unprotected: map[interface{}]interface{}{},
},
Payload: []byte(""),
Signatures: nil,
},
},
{
// duplicate uncompressed key in unprotected
// tag(98) + array(4) [ bytes(0), map(2), bytes(0), array(0) ]
//
// where our map(2) is
//
// A2 # map(2)
// 63 # text(3)
// 616C67 # "alg"
// 65 # text(5)
// 4553323536 # "ES256"
// 63 # text(3)
// 616C67 # "alg"
// 65 # text(5)
// 5053323536 # "PS256"
//
HexToBytesOrDie("D862" + "84" + "40" + "A2" + "63" + "616C67" + "65" + "4553323536" + "63" + "616C67" + "65" + "5053323536" + "40" + "80"),
SignMessage{
Headers: &Headers{
Protected: map[interface{}]interface{}{},
Unprotected: map[interface{}]interface{}{
1: -37, // decoding compresses to check for duplicate keys
},
},
Payload: []byte(""),
Signatures: nil,
},
},
}
for _, testCase := range cases {
result, err := Unmarshal(testCase.bytes)
assert.Nil(err)
assert.Equal(testCase.result, result)
}
}
func TestCBORDecodingErrors(t *testing.T) {
assert := assert.New(t)
type DecodeErrorTestCase struct {
bytes []byte
errorMessage string
}
var cases = []DecodeErrorTestCase{
{
HexToBytesOrDie("D862" + "60"), // tag(98) + text(0)
"cbor: cannot unmarshal UTF-8 text string into Go value of type cose.signMessage",
},
{
HexToBytesOrDie("D862" + "80"), // tag(98) + array(0)
"cbor: cannot unmarshal array into Go value of type cose.signMessage (cannot decode CBOR array to struct with different number of elements)",
},
{
// tag(98) + array(4) [ 4 * text(0) ]
HexToBytesOrDie("D862" + "84" + "60" + "60" + "60" + "60"),
"cbor: cannot unmarshal UTF-8 text string into Go struct field cose.signMessage.Protected of type []uint8",
},
{
// tag(98) + array(4) [ bytes(0), map(0), 2 * text(0) ]
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "60" + "60"),
"cbor: cannot unmarshal UTF-8 text string into Go struct field cose.signMessage.Payload of type []uint8",
},
{
// tag(98) + array(4) [ bytes(0), map(0), bytes(0), text(0) ]
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "40" + "60"),
"cbor: cannot unmarshal UTF-8 text string into Go struct field cose.signMessage.Signatures of type []cose.signature",
},
{
// wrong # of protected header bytes
// tag(98) + array(4) [ bytes(2) (but actually 1), map(0), bytes(0), text(0) ]
HexToBytesOrDie("D862" + "84" + "4263" + "A0" + "40" + "60"),
"unexpected EOF",
},
{
// protected header is serialized array
// tag(98) + array(4) [ bytes(3), map(2), bytes(0), array(0) ]
// protected header is bytes(3) is [2, -7]
HexToBytesOrDie("D862" + "84" + "43820226" + "A10224" + "40" + "80"),
"cbor: error casting protected to map; got []interface {}",
},
{
// duplicate compressed key in protected and unprotected
// tag(98) + array(4) [ bytes(3), map(2), bytes(0), array(0) ]
// bytes(3) is protected {2: -7}
// map(1) is {2: -5}
HexToBytesOrDie("D862" + "84" + "43A10226" + "A10224" + "40" + "80"),
"cbor: Duplicate header 2 found",
},
{
// duplicate uncompressed key in protected and unprotected
// tag(98) + array(4) [ bytes(11), map(1), bytes(0), array(0) ]
// bytes(11) is protected {"alg": "ES256"}
// map(1) is unprotected {"alg": "ES256"}
HexToBytesOrDie("D862" + "84" + "4B" + "A1" + "63" + "616C67" + "65" + "4553323536" + "A1" + "63" + "616C67" + "65" + "4553323536" + "40" + "80"),
"cbor: Duplicate header 1 found",
},
{
// duplicate key compressed in protected and uncompressed in unprotected
// tag(98) + array(4) [ bytes(3), map(1), bytes(0), array(0) ]
// bytes(3) is protected {1: -7}
// map(1) is unprotected {"alg": "PS256"}
HexToBytesOrDie("D862" + "84" + "43" + "A10126" + "A1" + "63" + "616C67" + "65" + "4553323536" + "40" + "80"),
"cbor: Duplicate header 1 found",
},
{
// duplicate key uncompressed in protected and compressed in unprotected
// tag(98) + array(4) [ bytes(11), map(1), bytes(0), array(0) ]
// bytes(11) is protected {"alg": "ES256"}
// map(1) is unprotected {1: -7}
HexToBytesOrDie("D862" + "84" + "4B" + "A1" + "63" + "616C67" + "65" + "4553323536" + "A10126" + "40" + "80"),
"cbor: Duplicate header 1 found",
},
{
// Signature's protected header is serialized array
// tag(98) + array(4) [ bytes(0), map(0), bytes(0), array(1) ]
// Signature is array(3) [ bytes(3), map(0), bytes(0)]
// Signature protected header is bytes(3) is [2, -7]
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "40" + "81" + "83" + "43820226" + "A0" + "40"),
"cbor: error casting protected to map; got []interface {}",
},
{
// Signature duplicate compressed key in protected and unprotected
// tag(98) + array(4) [ bytes(0), map(0), bytes(0), array(1) ]
// Signature is array(3) [ bytes(3), map(1), bytes(0)]
// Signature bytes(3) is protected {2: -7}
// Signature map(1) is {2: -5}
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "40" + "81" + "83" + "43A10226" + "A10224" + "40"),
"cbor: Duplicate header 2 found",
},
{
// Signature duplicate uncompressed key in protected and unprotected
// tag(98) + array(4) [ bytes(0), map(0), bytes(0), array(1) ]
// Signature is array(3) [ bytes(11), map(1), bytes(0)]
// Signature bytes(11) is protected {"alg": "ES256"}
// Signature map(1) is unprotected {"alg": "ES256"}
//HexToBytesOrDie("D862" + "84" + "4B" + "A1" + "63" + "616C67" + "65" + "4553323536" + "A1" + "63" + "616C67" + "65" + "4553323536" + "40" + "80"),
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "40" + "81" + "83" + "4B" + "A1" + "63" + "616C67" + "65" + "4553323536" + "A1" + "63" + "616C67" + "65" + "4553323536" + "40"),
"cbor: Duplicate header 1 found",
},
{
// Signature duplicate key compressed in protected and uncompressed in unprotected
// tag(98) + array(4) [ bytes(0), map(0), bytes(0), array(1) ]
// Signature is array(3) [ bytes(3), map(1), bytes(0)]
// Signature bytes(3) is protected {1: -7}
// Signature map(1) is unprotected {"alg": "PS256"}
//HexToBytesOrDie("D862" + "84" + "43" + "A10126" + "A1" + "63" + "616C67" + "65" + "4553323536" + "40" + "80"),
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "40" + "81" + "83" + "43" + "A10126" + "A1" + "63" + "616C67" + "65" + "4553323536" + "40"),
"cbor: Duplicate header 1 found",
},
{
// Signature duplicate key uncompressed in protected and compressed in unprotected
// tag(98) + array(4) [ bytes(0), map(0), bytes(0), array(1) ]
// Signature is array(3) [ bytes(11), map(1), bytes(0)]
// Signature bytes(11) is protected {"alg": "ES256"}
// Signature map(1) is unprotected {1: -7}
//HexToBytesOrDie("D862" + "84" + "4B" + "A1" + "63" + "616C67" + "65" + "4553323536" + "A10126" + "40" + "80"),
HexToBytesOrDie("D862" + "84" + "40" + "A0" + "40" + "81" + "83" + "4B" + "A1" + "63" + "616C67" + "65" + "4553323536" + "A10126" + "40"),
"cbor: Duplicate header 1 found",
},
}
for _, testCase := range cases {
result, err := Unmarshal(testCase.bytes)
assert.Nil(result)
assert.Equal(testCase.errorMessage, err.Error())
}
}
// TestCBORDecodingToSignMessageErrors tests unmarshaling COSE data to SignMessage,
// while TestCBORDecodingErrors tests unmarshaling COSE data to interface{}.
func TestCBORDecodingToSignMessageErrors(t *testing.T) {
assert := assert.New(t)
type DecodeErrorTestCase struct {
name string
bytes []byte
errorMessage string
}
var cases = []DecodeErrorTestCase{
{
"missing tag number",
HexToBytesOrDie("8440A0F680"), // array(4) [ bytes(0), map(0), nil, array(0)]
"cbor: cannot unmarshal array into Go value of type cbor.RawTag",
},
{
"wrong tag number",
HexToBytesOrDie("D8638440A0F680"), // tag(99) + array(4) [ bytes(0), map(0), nil, array(0)]
"cbor: wrong tag number 99",
},
}
for _, testCase := range cases {
var msg SignMessage
err := cbor.Unmarshal(testCase.bytes, &msg)
assert.Equal(testCase.errorMessage, err.Error())
}
}
func TestIsSignMessage(t *testing.T) {
assert := assert.New(t)
assert.Equal(IsSignMessage([]byte("deadbeef")), false)
msgBytes, err := Marshal(NewSignMessage())
assert.Nil(err)
assert.Equal(IsSignMessage(msgBytes), true)
}
func TestUnmarshalToNilSignMessage(t *testing.T) {
assert := assert.New(t)
b := []byte("\xd8\x62\x84\x40\xa0\xf6\x80")
var msg *SignMessage
err := msg.UnmarshalCBOR(b)
assert.Equal("cbor: UnmarshalCBOR on nil SignMessage pointer", err.Error())
}