forked from mozilla-services/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
380 lines (334 loc) · 11.1 KB
/
core.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
package cose
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/subtle"
"encoding/base64"
"github.com/pkg/errors"
"io"
"math/big"
)
// ContextSignature identifies the context of the signature as a
// COSE_Signature structure per
// https://tools.ietf.org/html/rfc8152#section-4.4
const ContextSignature = "Signature"
// Supported Algorithms
var (
// PS256 is RSASSA-PSS w/ SHA-256 from [RFC8230]
PS256 = getAlgByNameOrPanic("PS256")
// ES256 is ECDSA w/ SHA-256 from [RFC8152]
ES256 = getAlgByNameOrPanic("ES256")
// ES384 is ECDSA w/ SHA-384 from [RFC8152]
ES384 = getAlgByNameOrPanic("ES384")
// ES512 is ECDSA w/ SHA-512 from [RFC8152]
ES512 = getAlgByNameOrPanic("ES512")
)
// ByteSigner take a signature digest and returns COSE signature bytes
type ByteSigner interface {
// Sign returns the COSE signature as a byte slice
Sign(rand io.Reader, digest []byte) (signature []byte, err error)
}
// ByteVerifier checks COSE signatures
type ByteVerifier interface {
// Verify returns nil for a successfully verified signature or an error
Verify(digest []byte, signature []byte) (err error)
}
// Signer holds a COSE Algorithm and private key for signing messages
type Signer struct {
PrivateKey crypto.PrivateKey
alg *Algorithm
}
// RSAOptions are options for NewSigner currently just the RSA Key
// size
type RSAOptions struct {
Size int
}
// NewSigner returns a Signer with a generated key
func NewSigner(alg *Algorithm, options interface{}) (signer *Signer, err error) {
var privateKey crypto.PrivateKey
if alg.privateKeyType == KeyTypeECDSA {
if alg.privateKeyECDSACurve == nil {
err = errors.Errorf("No ECDSA curve found for algorithm")
return nil, err
}
privateKey, err = ecdsa.GenerateKey(alg.privateKeyECDSACurve, rand.Reader)
if err != nil {
err = errors.Wrapf(err, "error generating ecdsa signer private key")
return nil, err
}
} else if alg.privateKeyType == KeyTypeRSA {
var keyBitLen int = alg.minRSAKeyBitLen
if opts, ok := options.(RSAOptions); ok {
if opts.Size > alg.minRSAKeyBitLen {
keyBitLen = opts.Size
} else {
err = errors.Errorf("error generating rsa signer private key RSA key size must be at least %d", alg.minRSAKeyBitLen)
return nil, err
}
}
privateKey, err = rsa.GenerateKey(rand.Reader, keyBitLen)
if err != nil {
err = errors.Wrapf(err, "error generating rsa signer private key")
return nil, err
}
} else {
return nil, ErrUnknownPrivateKeyType
}
return &Signer{
PrivateKey: privateKey,
alg: alg,
}, nil
}
// NewSignerFromKey checks whether the privateKey is supported and
// returns a Signer using the provided key
func NewSignerFromKey(alg *Algorithm, privateKey crypto.PrivateKey) (signer *Signer, err error) {
switch privateKey.(type) {
case *rsa.PrivateKey:
case *ecdsa.PrivateKey:
default:
return nil, ErrUnknownPrivateKeyType
}
return &Signer{
PrivateKey: privateKey,
alg: alg,
}, nil
}
// Public returns the crypto.PublicKey for the Signer's privateKey
func (s *Signer) Public() (publicKey crypto.PublicKey) {
switch key := s.PrivateKey.(type) {
case *rsa.PrivateKey:
return key.Public()
case *ecdsa.PrivateKey:
return key.Public()
default:
panic("Could not return public key for Unrecognized private key type.")
}
}
// Sign returns the COSE signature as a byte slice
func (s *Signer) Sign(rand io.Reader, digest []byte) (signature []byte, err error) {
switch key := s.PrivateKey.(type) {
case *rsa.PrivateKey:
if s.alg.privateKeyType != KeyTypeRSA {
return nil, errors.Errorf("Key type must be RSA")
}
if key.N.BitLen() < s.alg.minRSAKeyBitLen {
return nil, errors.Errorf("RSA key must be at least %d bits long", s.alg.minRSAKeyBitLen)
}
sig, err := rsa.SignPSS(rand, key, s.alg.HashFunc, digest, &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
Hash: s.alg.HashFunc,
})
if err != nil {
return nil, errors.Errorf("rsa.SignPSS error %s", err)
}
return sig, nil
case *ecdsa.PrivateKey:
if s.alg.privateKeyType != KeyTypeECDSA {
return nil, errors.Errorf("Key type must be ECDSA")
}
// https://tools.ietf.org/html/rfc8152#section-8.1
r, s, err := ecdsa.Sign(rand, key, digest)
if err != nil {
return nil, errors.Errorf("ecdsa.Sign error %s", err)
}
// These integers (r and s) will be the same length as
// the length of the key used for the signature
// process.
const tolerance = uint(1)
rByteLen, sByteLen, dByteLen := len(s.Bits()), len(r.Bits()), len(key.D.Bits())
if !(approxEqual(sByteLen, rByteLen, tolerance) && approxEqual(sByteLen, dByteLen, tolerance) && approxEqual(dByteLen, rByteLen, tolerance)) {
return nil, errors.Errorf("Byte lengths of integers r and s (%d and %d) do not match the key length %d±%d\n", sByteLen, rByteLen, dByteLen, tolerance)
}
// The signature is encoded by converting the integers
// into byte strings of the same length as the key
// size. The length is rounded up to the nearest byte
// and is left padded with zero bits to get to the
// correct length. The two integers are then
// concatenated together to form a byte string that is
// the resulting signature.
n := ecdsaCurveKeyBytesSize(key.Curve)
sig := make([]byte, 0)
sig = append(sig, I2OSP(r, n)...)
sig = append(sig, I2OSP(s, n)...)
return sig, nil
default:
return nil, ErrUnknownPrivateKeyType
}
}
// Verifier returns a Verifier using the Signer's public key and
// Algorithm
func (s *Signer) Verifier() (verifier *Verifier) {
return &Verifier{
PublicKey: s.Public(),
Alg: s.alg,
}
}
// Verifier holds a PublicKey and Algorithm to verify signatures
type Verifier struct {
PublicKey crypto.PublicKey
Alg *Algorithm
}
// Verify verifies a signature returning nil for success or an error
func (v *Verifier) Verify(digest []byte, signature []byte) (err error) {
if v.Alg.Value > -1 { // Negative numbers are used for second layer objects (COSE_Signature and COSE_recipient)
return ErrInvalidAlg
}
switch key := v.PublicKey.(type) {
case *rsa.PublicKey:
hashFunc := v.Alg.HashFunc
err = rsa.VerifyPSS(key, hashFunc, digest, signature, &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
Hash: hashFunc,
})
if err != nil {
return errors.Errorf("verification failed rsa.VerifyPSS err %s", err)
}
return nil
case *ecdsa.PublicKey:
if v.Alg.privateKeyECDSACurve == nil {
return errors.Errorf("Could not find an elliptic curve for the ecdsa algorithm")
}
algCurveBitSize := v.Alg.privateKeyECDSACurve.Params().BitSize
keyCurveBitSize := key.Curve.Params().BitSize
if algCurveBitSize != keyCurveBitSize {
return errors.Errorf("Expected %d bit key, got %d bits instead", algCurveBitSize, keyCurveBitSize)
}
algKeyBytesSize := ecdsaCurveKeyBytesSize(v.Alg.privateKeyECDSACurve)
// signature bytes is the keys with padding r and s
if len(signature) != 2*algKeyBytesSize {
return errors.Errorf("invalid signature length: %d", len(signature))
}
r := big.NewInt(0).SetBytes(signature[:algKeyBytesSize])
s := big.NewInt(0).SetBytes(signature[algKeyBytesSize:])
ok := ecdsa.Verify(key, digest, r, s)
if ok {
return nil
}
return ErrECDSAVerification
default:
return ErrUnknownPublicKeyType
}
}
// buildAndMarshalSigStructure creates a Sig_structure, populates it
// with the appropriate fields, and marshals it to CBOR bytes
func buildAndMarshalSigStructure(bodyProtected, signProtected, external, payload []byte) (ToBeSigned []byte, err error) {
// 1. Create a Sig_structure and populate it with the appropriate fields.
//
// Sig_structure = [
// context : "Signature" / "Signature1" / "CounterSignature",
// body_protected : empty_or_serialized_map,
// ? sign_protected : empty_or_serialized_map,
// external_aad : bstr,
// payload : bstr
// ]
sigStructure := []interface{}{
ContextSignature,
bodyProtected, // message.headers.EncodeProtected(),
signProtected, // message.signatures[0].headers.EncodeProtected(),
external,
payload,
}
// 2. Create the value ToBeSigned by encoding the Sig_structure to a
// byte string, using the encoding described in Section 14.
ToBeSigned, err = Marshal(sigStructure)
if err != nil {
return nil, errors.Errorf("Error marshaling Sig_structure: %s", err)
}
return ToBeSigned, nil
}
// hashSigStructure computes the crypto.Hash digest of a byte slice
func hashSigStructure(ToBeSigned []byte, hash crypto.Hash) (digest []byte, err error) {
if !hash.Available() {
return []byte(""), ErrUnavailableHashFunc
}
hasher := hash.New()
_, _ = hasher.Write(ToBeSigned) // Write() on hash never fails
digest = hasher.Sum(nil)
return digest, nil
}
// ecdsaCurveKeyBytesSize returns the ECDSA key size in bytes with padding
func ecdsaCurveKeyBytesSize(curve elliptic.Curve) (keyBytesSize int) {
curveBits := curve.Params().BitSize
keyBytesSize = curveBits / 8
// add a byte of padding for curves like P521
if curveBits%8 > 0 {
keyBytesSize++
}
return
}
// I2OSP "Integer-to-Octet-String" converts a nonnegative integer to
// an octet string of a specified length
//
// https://tools.ietf.org/html/rfc8017#section-4.1
func I2OSP(b *big.Int, n int) []byte {
var (
octetString = b.Bytes()
octetStringSize = len(octetString)
result = make([]byte, n)
)
if !(b.Sign() == 0 || b.Sign() == 1) {
panic("I2OSP error: integer must be zero or positive")
}
if n == 0 || octetStringSize > n {
panic("I2OSP error: integer too large")
}
subtle.ConstantTimeCopy(1, result[:n-octetStringSize], result[:n-octetStringSize])
subtle.ConstantTimeCopy(1, result[n-octetStringSize:], octetString)
return result
}
// FromBase64Int decodes a base64-encoded string into a big.Int or panics
//
// from https://github.com/square/go-jose/blob/789a4c4bd4c118f7564954f441b29c153ccd6a96/utils_test.go#L45
// Apache License 2.0
func FromBase64Int(data string) *big.Int {
val, err := base64.RawURLEncoding.DecodeString(data)
if err != nil {
panic("Invalid test data")
}
return new(big.Int).SetBytes(val)
}
// Sign returns the SignatureBytes for each Signer in the same order
// on the digest or the error from the first failing Signer
func Sign(rand io.Reader, digest []byte, signers []ByteSigner) (signatures [][]byte, err error) {
var signatureBytes []byte
for _, signer := range signers {
signatureBytes, err = signer.Sign(rand, digest)
if err != nil {
return
}
signatures = append(signatures, signatureBytes)
}
return
}
// Verify returns nil if all Verifier verify the SignatureBytes or the
// error from the first failing Verifier
func Verify(digest []byte, signatures [][]byte, verifiers []ByteVerifier) (err error) {
if len(signatures) != len(verifiers) {
return errors.Errorf("Wrong number of signatures %d and verifiers %d", len(signatures), len(verifiers))
}
for i, verifier := range verifiers {
err = verifier.Verify(digest, signatures[i])
if err != nil {
return
}
}
return nil
}
// approxEquals returns a bool of whether x and y are equal to within
// a given tolerance
func approxEqual(x, y int, tolerance uint) bool {
var (
larger, smaller int
)
if x > y {
larger = x
smaller = y
} else {
larger = y
smaller = x
}
return uint(larger-smaller) <= tolerance
}