-
Notifications
You must be signed in to change notification settings - Fork 26
/
signer.go
100 lines (94 loc) · 3.07 KB
/
signer.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
package cose
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"errors"
"fmt"
"io"
)
// Signer is an interface for private keys to sign COSE signatures.
type Signer interface {
// Algorithm returns the signing algorithm associated with the private key.
Algorithm() Algorithm
// Sign signs message content with the private key, possibly using entropy
// from rand.
// The resulting signature should follow RFC 8152 section 8.
//
// Reference: https://datatracker.ietf.org/doc/html/rfc8152#section-8
Sign(rand io.Reader, content []byte) ([]byte, error)
}
// DigestSigner is an interface for private keys to sign digested COSE signatures.
type DigestSigner interface {
// Algorithm returns the signing algorithm associated with the private key.
Algorithm() Algorithm
// SignDigest signs message digest with the private key, possibly using
// entropy from rand.
// The resulting signature should follow RFC 8152 section 8.
SignDigest(rand io.Reader, digest []byte) ([]byte, error)
}
// NewSigner returns a signer with a given signing key.
// The signing key can be a golang built-in crypto private key, a key in HSM, or
// a remote KMS.
//
// Developers are encouraged to implement the `cose.Signer` interface instead of
// the `crypto.Signer` interface for better performance.
//
// All signing keys implementing `crypto.Signer` with `Public()` returning a
// public key of type `*rsa.PublicKey`, `*ecdsa.PublicKey`, or
// `ed25519.PublicKey` are accepted.
//
// The returned signer for rsa and ecdsa keys also implements `cose.DigestSigner`.
//
// Note: `*rsa.PrivateKey`, `*ecdsa.PrivateKey`, and `ed25519.PrivateKey`
// implement `crypto.Signer`.
func NewSigner(alg Algorithm, key crypto.Signer) (Signer, error) {
var errReason string
switch alg {
case AlgorithmPS256, AlgorithmPS384, AlgorithmPS512:
vk, ok := key.Public().(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("%v: %w", alg, ErrInvalidPubKey)
}
// RFC 8230 6.1 requires RSA keys having a minimum size of 2048 bits.
// Reference: https://www.rfc-editor.org/rfc/rfc8230.html#section-6.1
if vk.N.BitLen() < 2048 {
return nil, errors.New("RSA key must be at least 2048 bits long")
}
return &rsaSigner{
alg: alg,
key: key,
}, nil
case AlgorithmES256, AlgorithmES384, AlgorithmES512:
vk, ok := key.Public().(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("%v: %w", alg, ErrInvalidPubKey)
}
if sk, ok := key.(*ecdsa.PrivateKey); ok {
return &ecdsaKeySigner{
alg: alg,
key: sk,
}, nil
}
return &ecdsaCryptoSigner{
alg: alg,
key: vk,
signer: key,
}, nil
case AlgorithmEdDSA:
if _, ok := key.Public().(ed25519.PublicKey); !ok {
return nil, fmt.Errorf("%v: %w", alg, ErrInvalidPubKey)
}
return &ed25519Signer{
key: key,
}, nil
case AlgorithmReserved:
errReason = "can't be implemented"
case AlgorithmRS256, AlgorithmRS384, AlgorithmRS512:
errReason = "no built-in implementation available"
default:
errReason = "unknown algorithm"
}
return nil, fmt.Errorf("can't create new Signer for %s: %s: %w", alg, errReason, ErrAlgorithmNotSupported)
}