-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathconformance_test.go
More file actions
420 lines (389 loc) · 11.1 KB
/
conformance_test.go
File metadata and controls
420 lines (389 loc) · 11.1 KB
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
package cose_test
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"io"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"github.com/fxamacker/cbor/v2"
"github.com/veraison/go-cose"
)
type TestCase struct {
UUID string `json:"uuid"`
Title string `json:"title"`
Description string `json:"description"`
Key Key `json:"key"`
Alg string `json:"alg"`
Sign1 *Sign1 `json:"sign1::sign"`
Verify1 *Verify1 `json:"sign1::verify"`
}
type Key map[string]string
type Sign1 struct {
Payload string `json:"payload"`
ProtectedHeaders *CBOR `json:"protectedHeaders"`
UnprotectedHeaders *CBOR `json:"unprotectedHeaders"`
External string `json:"external"`
Detached bool `json:"detached"`
TBS CBOR `json:"tbsHex"`
Output CBOR `json:"expectedOutput"`
OutputLength int `json:"fixedOutputLength"`
}
type Verify1 struct {
TaggedCOSESign1 CBOR `json:"taggedCOSESign1"`
External string `json:"external"`
Verify bool `json:"shouldVerify"`
}
type CBOR struct {
CBORHex string `json:"cborHex"`
CBORDiag string `json:"cborDiag"`
}
// Conformance samples are taken from
// https://github.com/gluecose/test-vectors.
var testCases = []struct {
name string
deterministic bool
err string
skip bool
}{
{name: "sign1-sign-0000"},
{name: "sign1-sign-0001"},
{name: "sign1-sign-0002"},
{name: "sign1-sign-0003"},
{name: "sign1-sign-0004", deterministic: true},
{name: "sign1-sign-0005", deterministic: true},
{name: "sign1-sign-0006", deterministic: true},
{name: "sign1-sign-0007", deterministic: true}, // EdDSA Ed25519
{name: "sign1-verify-0000"},
{name: "sign1-verify-0001"},
{name: "sign1-verify-0002"},
{name: "sign1-verify-0003"},
{name: "sign1-verify-0004"},
{name: "sign1-verify-0005"},
{name: "sign1-verify-0006"},
{name: "sign1-verify-0007"}, // EdDSA Ed25519
{name: "sign1-verify-negative-0000", err: "cbor: invalid protected header: cbor: require bstr type"},
{name: "sign1-verify-negative-0001", err: "cbor: invalid protected header: cbor: protected header: require map type"},
{name: "sign1-verify-negative-0002", err: "cbor: invalid protected header: cbor: found duplicate map key cose.headerLabelValidator{value:1} at map element index 1"},
{name: "sign1-verify-negative-0003", err: "cbor: invalid unprotected header: cbor: found duplicate map key cose.headerLabelValidator{value:4} at map element index 1"},
{name: "sign1-verify-negative-0004"}, // EdDSA Ed25519 invalid signature
}
func TestConformance(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
if tt.skip {
t.SkipNow()
}
data, err := os.ReadFile(filepath.Join("testdata", tt.name+".json"))
if err != nil {
t.Fatal(err)
}
var tc TestCase
err = json.Unmarshal(data, &tc)
if err != nil {
t.Fatal(err)
}
if tc.Sign1 != nil {
testSign1(t, &tc, tt.deterministic)
} else if tc.Verify1 != nil {
testVerify1(t, &tc, tt.err)
} else {
t.Fatal("test case not supported")
}
})
}
}
func testVerify1(t *testing.T, tc *TestCase, wantErr string) {
var err error
defer func() {
if tc.Verify1.Verify && err != nil {
t.Fatal(err)
} else if !tc.Verify1.Verify {
if err == nil {
t.Fatal("Verify1 should have failed")
}
if wantErr != "" {
if got := err.Error(); !strings.Contains(got, wantErr) {
t.Fatalf("error mismatch; want %q, got %q", wantErr, got)
}
}
}
}()
var verifier cose.Verifier
_, verifier, err = getSigner(tc, false)
if err != nil {
return
}
var sigMsg cose.Sign1Message
err = sigMsg.UnmarshalCBOR(mustHexToBytes(tc.Verify1.TaggedCOSESign1.CBORHex))
if err != nil {
return
}
var external []byte
if tc.Verify1.External != "" {
external = mustHexToBytes(tc.Verify1.External)
}
err = sigMsg.Verify(external, verifier)
if tc.Verify1.Verify && err != nil {
t.Fatal(err)
} else if !tc.Verify1.Verify && err == nil {
t.Fatal("Verify1 should have failed")
}
}
func testSign1(t *testing.T, tc *TestCase, deterministic bool) {
signer, verifier, err := getSigner(tc, true)
if err != nil {
t.Fatal(err)
}
sig := tc.Sign1
sigMsg := cose.NewSign1Message()
sigMsg.Payload = mustHexToBytes(sig.Payload)
sigMsg.Headers, err = decodeHeaders(mustHexToBytes(sig.ProtectedHeaders.CBORHex), mustHexToBytes(sig.UnprotectedHeaders.CBORHex))
if err != nil {
t.Fatal(err)
}
var external []byte
if sig.External != "" {
external = mustHexToBytes(sig.External)
}
// Ed25519 signatures are deterministic and should use nil for rand
// Other algorithms use zeroSource for reproducible test results
var rand io.Reader = new(zeroSource)
if tc.Alg == "EdDSA" {
rand = nil
}
err = sigMsg.Sign(rand, external, signer)
if err != nil {
t.Fatal(err)
}
err = sigMsg.Verify(external, verifier)
if err != nil {
t.Fatal(err)
}
got, err := sigMsg.MarshalCBOR()
if err != nil {
t.Fatal(err)
}
want := mustHexToBytes(sig.Output.CBORHex)
if !deterministic {
got = got[:sig.OutputLength]
want = want[:sig.OutputLength]
}
if !bytes.Equal(want, got) {
t.Fatalf("unexpected output:\nwant: %x\n got: %x", want, got)
}
}
func getSigner(tc *TestCase, private bool) (cose.Signer, cose.Verifier, error) {
alg := mustNameToAlg(tc.Alg)
// Special handling for OKP keys
if tc.Key["kty"] == "OKP" {
switch tc.Key["crv"] {
case "Ed25519":
// Note: Ed448 would require different key size validation (57 bytes)
verifierKey, privateKeySigner, err := createEd25519Key(tc.Key, private)
if err != nil {
return nil, nil, err
}
var signer cose.Signer
if private {
signer, err = cose.NewSigner(alg, privateKeySigner)
if err != nil {
return nil, nil, err
}
}
verifier, err := cose.NewVerifier(alg, verifierKey)
if err != nil {
return nil, nil, err
}
return signer, verifier, nil
case "Ed448":
// Ed448 support would go here - requires golang.org/x/crypto/ed448
return nil, nil, errors.New("Ed448 not yet implemented")
default:
return nil, nil, errors.New("unsupported OKP curve: " + tc.Key["crv"])
}
}
// Original implementation for RSA and EC keys
pkey, err := getKey(tc.Key, private)
if err != nil {
return nil, nil, err
}
signer, err := cose.NewSigner(alg, pkey)
if err != nil {
return nil, nil, err
}
verifier, err := cose.NewVerifier(alg, pkey.Public())
if err != nil {
return nil, nil, err
}
return signer, verifier, nil
}
func getKey(key Key, private bool) (crypto.Signer, error) {
switch key["kty"] {
case "RSA":
pkey := &rsa.PrivateKey{
PublicKey: rsa.PublicKey{
N: mustBase64ToBigInt(key["n"]),
E: mustBase64ToInt(key["e"]),
},
}
if private {
pkey.D = mustBase64ToBigInt(key["d"])
pkey.Primes = []*big.Int{mustBase64ToBigInt(key["p"]), mustBase64ToBigInt(key["q"])}
pkey.Precomputed = rsa.PrecomputedValues{
Dp: mustBase64ToBigInt(key["dp"]),
Dq: mustBase64ToBigInt(key["dq"]),
Qinv: mustBase64ToBigInt(key["qi"]),
CRTValues: make([]rsa.CRTValue, 0),
}
}
return pkey, nil
case "EC":
var c elliptic.Curve
switch key["crv"] {
case "P-224":
c = elliptic.P224()
case "P-256":
c = elliptic.P256()
case "P-384":
c = elliptic.P384()
case "P-521":
c = elliptic.P521()
default:
return nil, errors.New("unsupported EC curve: " + key["crv"])
}
pkey := &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
X: mustBase64ToBigInt(key["x"]),
Y: mustBase64ToBigInt(key["y"]),
Curve: c,
},
}
if private {
pkey.D = mustBase64ToBigInt(key["d"])
}
return pkey, nil
case "OKP":
// Only Ed25519 is supported for now
switch key["crv"] {
case "Ed25519":
_, privateKeySigner, err := createEd25519Key(key, private)
if err != nil {
return nil, err
}
if !private {
// For public key only operations, we need to return a type that satisfies crypto.Signer
// but this won't work for verify-only operations. We'll handle this in getSigner.
return nil, errors.New("OKP public-only key not supported in this context")
}
return privateKeySigner, nil
case "Ed448":
// Ed448 support would require golang.org/x/crypto/ed448
return nil, errors.New("Ed448 not yet implemented")
default:
return nil, errors.New("unsupported OKP curve: " + key["crv"])
}
}
return nil, errors.New("unsupported key type: " + key["kty"])
}
// zeroSource is an io.Reader that returns an unlimited number of zero bytes.
type zeroSource struct{}
func (zeroSource) Read(b []byte) (n int, err error) {
for i := range b {
b[i] = 0
}
return len(b), nil
}
var encMode, _ = cbor.CanonicalEncOptions().EncMode()
func decodeHeaders(protected, unprotected []byte) (hdr cose.Headers, err error) {
// test-vectors encodes the protected header as a map instead of a map wrapped in a bstr.
// UnmarshalFromRaw expects the former, so wrap the map here before passing it to UnmarshalFromRaw.
hdr.RawProtected, err = encMode.Marshal(protected)
if err != nil {
return
}
hdr.RawUnprotected = unprotected
err = hdr.UnmarshalFromRaw()
return hdr, err
}
func mustBase64ToInt(s string) int {
return int(mustBase64ToBigInt(s).Int64())
}
func mustHexToBytes(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
func mustBase64ToBigInt(s string) *big.Int {
val, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return new(big.Int).SetBytes(val)
}
func mustBase64ToBytes(s string) []byte {
val, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return val
}
// createEd25519Key creates an Ed25519 key from test case data
func createEd25519Key(key Key, private bool) (ed25519.PublicKey, crypto.Signer, error) {
publicKey := mustBase64ToBytes(key["x"])
if len(publicKey) != ed25519.PublicKeySize {
return nil, nil, errors.New("invalid Ed25519 public key size")
}
if !private {
return ed25519.PublicKey(publicKey), nil, nil
}
privateKey := mustBase64ToBytes(key["d"])
if len(privateKey) != ed25519.SeedSize {
return nil, nil, errors.New("invalid Ed25519 private key size")
}
fullPrivateKey := ed25519.NewKeyFromSeed(privateKey)
publicKeyFromPrivate := fullPrivateKey.Public().(ed25519.PublicKey)
return publicKeyFromPrivate, fullPrivateKey, nil
}
// mustNameToAlg returns the algorithm associated to name.
// The content of name is not defined in any RFC,
// but it's what the test cases use to identify algorithms.
func mustNameToAlg(name string) cose.Algorithm {
switch name {
case "PS256":
return cose.AlgorithmPS256
case "PS384":
return cose.AlgorithmPS384
case "PS512":
return cose.AlgorithmPS512
case "RS256":
return cose.AlgorithmRS256
case "RS384":
return cose.AlgorithmRS384
case "RS512":
return cose.AlgorithmRS512
case "ES256":
return cose.AlgorithmES256
case "ES384":
return cose.AlgorithmES384
case "ES512":
return cose.AlgorithmES512
case "EdDSA":
return cose.AlgorithmEdDSA
}
panic("algorithm name not found: " + name)
}