forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
155 lines (136 loc) · 3.86 KB
/
rsa.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
package jws
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"github.com/lestrrat-go/jwx/jwa"
"github.com/pkg/errors"
)
var rsaSignFuncs = map[jwa.SignatureAlgorithm]rsaSignFunc{}
var rsaVerifyFuncs = map[jwa.SignatureAlgorithm]rsaVerifyFunc{}
func init() {
algs := map[jwa.SignatureAlgorithm]struct {
Hash crypto.Hash
SignFunc func(crypto.Hash) rsaSignFunc
VerifyFunc func(crypto.Hash) rsaVerifyFunc
}{
jwa.RS256: {
Hash: crypto.SHA256,
SignFunc: makeSignPKCS1v15,
VerifyFunc: makeVerifyPKCS1v15,
},
jwa.RS384: {
Hash: crypto.SHA384,
SignFunc: makeSignPKCS1v15,
VerifyFunc: makeVerifyPKCS1v15,
},
jwa.RS512: {
Hash: crypto.SHA512,
SignFunc: makeSignPKCS1v15,
VerifyFunc: makeVerifyPKCS1v15,
},
jwa.PS256: {
Hash: crypto.SHA256,
SignFunc: makeSignPSS,
VerifyFunc: makeVerifyPSS,
},
jwa.PS384: {
Hash: crypto.SHA384,
SignFunc: makeSignPSS,
VerifyFunc: makeVerifyPSS,
},
jwa.PS512: {
Hash: crypto.SHA512,
SignFunc: makeSignPSS,
VerifyFunc: makeVerifyPSS,
},
}
for alg, item := range algs {
rsaSignFuncs[alg] = item.SignFunc(item.Hash)
rsaVerifyFuncs[alg] = item.VerifyFunc(item.Hash)
}
}
func makeSignPKCS1v15(hash crypto.Hash) rsaSignFunc {
return func(payload []byte, key *rsa.PrivateKey) ([]byte, error) {
h := hash.New()
if _, err := h.Write(payload); err != nil {
return nil, errors.Wrap(err, "failed to write payload using SignPKCS1v15")
}
return rsa.SignPKCS1v15(rand.Reader, key, hash, h.Sum(nil))
}
}
func makeSignPSS(hash crypto.Hash) rsaSignFunc {
return func(payload []byte, key *rsa.PrivateKey) ([]byte, error) {
h := hash.New()
if _, err := h.Write(payload); err != nil {
return nil, errors.Wrap(err, "failed to write payload using SignPSS")
}
return rsa.SignPSS(rand.Reader, key, hash, h.Sum(nil), &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthAuto,
})
}
}
func newRSASigner(alg jwa.SignatureAlgorithm) Signer {
return &RSASigner{
alg: alg,
sign: rsaSignFuncs[alg], // we know this will succeed
}
}
func (s RSASigner) Algorithm() jwa.SignatureAlgorithm {
return s.alg
}
// Sign creates a signature using crypto/rsa. key must be a non-nil instance of
// `*"crypto/rsa".PrivateKey`.
func (s RSASigner) Sign(payload []byte, key interface{}) ([]byte, error) {
if key == nil {
return nil, errors.New(`missing private key while signing payload`)
}
var privkey *rsa.PrivateKey
switch v := key.(type) {
case rsa.PrivateKey:
privkey = &v
case *rsa.PrivateKey:
privkey = v
default:
return nil, errors.Errorf(`invalid key type %T. *rsa.PrivateKey is required`, key)
}
return s.sign(payload, privkey)
}
func makeVerifyPKCS1v15(hash crypto.Hash) rsaVerifyFunc {
return func(payload, signature []byte, key *rsa.PublicKey) error {
h := hash.New()
if _, err := h.Write(payload); err != nil {
return errors.Wrap(err, "failed to write payload using PKCS1v15")
}
return rsa.VerifyPKCS1v15(key, hash, h.Sum(nil), signature)
}
}
func makeVerifyPSS(hash crypto.Hash) rsaVerifyFunc {
return func(payload, signature []byte, key *rsa.PublicKey) error {
h := hash.New()
if _, err := h.Write(payload); err != nil {
return errors.Wrap(err, "failed to write payload using PSS")
}
return rsa.VerifyPSS(key, hash, h.Sum(nil), signature, nil)
}
}
func newRSAVerifier(alg jwa.SignatureAlgorithm) Verifier {
return &RSAVerifier{
verify: rsaVerifyFuncs[alg], // we know this will succeed
}
}
func (v RSAVerifier) Verify(payload, signature []byte, key interface{}) error {
if key == nil {
return errors.New(`missing public key while verifying payload`)
}
var pubkey *rsa.PublicKey
switch v := key.(type) {
case rsa.PublicKey:
pubkey = &v
case *rsa.PublicKey:
pubkey = v
default:
return errors.Errorf(`invalid key type %T. *rsa.PublicKey is required`, key)
}
return v.verify(payload, signature, pubkey)
}