forked from veraison/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conformance_test.go
319 lines (297 loc) · 8.01 KB
/
conformance_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
package cose_test
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"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-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-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 \"1\" at map element index 1"},
{name: "sign1-verify-negative-0003", err: "cbor: invalid unprotected header: cbor: found duplicate map key \"4\" at map element index 1"},
}
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)
}
err = sigMsg.Sign(new(zeroSource), 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) {
pkey, err := getKey(tc.Key, private)
if err != nil {
return nil, nil, err
}
alg := mustNameToAlg(tc.Alg)
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
}
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)
}
// 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 "ES256":
return cose.AlgorithmES256
case "ES384":
return cose.AlgorithmES384
case "ES512":
return cose.AlgorithmES512
}
panic("algorithm name not found: " + name)
}