Repository navigation
Expand file tree
/
Copy pathjwskey.go
More file actions
328 lines (314 loc) · 10.6 KB
/
Copy pathjwskey.go
File metadata and controls
328 lines (314 loc) · 10.6 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
package httpsign
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/mldsa"
"crypto/rsa"
"fmt"
"github.com/lestrrat-go/jwx/v4/jwa"
)
// validateJWSKeyAlg checks that key is an appropriate Go type for alg, without using
// jws.AlgorithmsForKey (deprecated; not a compatibility API; over-broad for ECDSA/ML-DSA).
// When signing is true, asymmetric keys must be private; when false, they must be public.
// HMAC keys are symmetric and accepted for either role.
//
// Classical algs (RSA, ECDSA, Ed25519) and ML-DSA also accept an opaque crypto.Signer
// (HSM/KMS-backed keys, etc.) whose Public() reports the expected key shape — valid for
// both sign and verify (verify is local via Public()). Default jwx ML-DSA signing still
// requires *mldsa.PrivateKey; callers using an opaque Signer for ML-DSA must register a
// custom jws.Signer (jws.RegisterSigner) that accepts crypto.Signer. ML-DSA is foreign-JWS
// only — not a native RFC 9421 algorithm.
// JWK wrappers are rejected here so callers convert first (NewJWSSignerFromJWK / preferred verify).
func validateJWSKeyAlg(alg jwa.SignatureAlgorithm, key any, signing bool) error {
switch alg {
case jwa.HS256(), jwa.HS384(), jwa.HS512():
return validateHMACKey(alg, key)
case jwa.RS256(), jwa.RS384(), jwa.RS512(), jwa.PS256(), jwa.PS384(), jwa.PS512():
return validateRSAKey(alg, key, signing)
case jwa.ES256(), jwa.ES384(), jwa.ES512():
return validateECDSAKey(alg, key, signing)
case jwa.EdDSA(), jwa.EdDSAEd25519():
return validateEd25519Key(alg, key, signing)
case jwa.MLDSA44(), jwa.MLDSA65(), jwa.MLDSA87():
return validateMLDSAKey(alg, key, signing)
default:
// Passthrough: unknown/new jwx algs are left to jws.SignerFor / VerifierFor.
// Keep hardening above for known classical/PQ algs only.
return nil
}
}
func validateHMACKey(alg jwa.SignatureAlgorithm, key any) error {
k, ok := key.([]byte)
if !ok {
return fmt.Errorf("algorithm %s requires []byte key, got %T", alg, key)
}
// RFC 7518 §3.2: key at least as large as the hash output.
var minLen int
switch alg {
case jwa.HS256():
minLen = 32
case jwa.HS384():
minLen = 48
case jwa.HS512():
minLen = 64
default:
return fmt.Errorf("unsupported HMAC algorithm %s", alg)
}
if len(k) < minLen {
return fmt.Errorf("algorithm %s requires a key of at least %d bytes (RFC 7518), got %d", alg, minLen, len(k))
}
return nil
}
// publicFromSigner returns Public() for a crypto.Signer. Recovers from panics
// (e.g. malformed ed25519.PrivateKey) so cross-family probes return a normal miss.
func publicFromSigner(key any) (pub crypto.PublicKey, ok bool) {
signer, isSigner := key.(crypto.Signer)
if !isSigner || signer == nil {
return nil, false
}
defer func() {
if recover() != nil {
pub = nil
ok = false
}
}()
pub = signer.Public()
if pub == nil {
return nil, false
}
return pub, true
}
func errNeedKeyType(alg jwa.SignatureAlgorithm, kind, gotType string) error {
return fmt.Errorf("algorithm %s requires an %s key, got %s", alg, kind, gotType)
}
func errNeedKeyTypeFromSigner(alg jwa.SignatureAlgorithm, kind, gotType, pubType string) error {
return fmt.Errorf("algorithm %s requires an %s key, got %s (Public %s)", alg, kind, gotType, pubType)
}
func typeString(v any) string { return fmt.Sprintf("%T", v) }
func validateRSAKey(alg jwa.SignatureAlgorithm, key any, signing bool) error {
switch k := key.(type) {
case *rsa.PrivateKey:
if k == nil {
return fmt.Errorf("algorithm %s: nil RSA private key", alg)
}
if !signing {
return fmt.Errorf("algorithm %s requires an RSA public key for verification", alg)
}
case rsa.PrivateKey:
if !signing {
return fmt.Errorf("algorithm %s requires an RSA public key for verification", alg)
}
case *rsa.PublicKey:
if k == nil {
return fmt.Errorf("algorithm %s: nil RSA public key", alg)
}
if signing {
return fmt.Errorf("algorithm %s requires an RSA private key for signing", alg)
}
case rsa.PublicKey:
if signing {
return fmt.Errorf("algorithm %s requires an RSA private key for signing", alg)
}
default:
pub, sok := publicFromSigner(key)
if !sok {
return errNeedKeyType(alg, "RSA", typeString(key))
}
switch rp := pub.(type) {
case *rsa.PublicKey:
if rp == nil {
return fmt.Errorf("algorithm %s: nil RSA public key", alg)
}
case rsa.PublicKey:
// ok
default:
return errNeedKeyTypeFromSigner(alg, "RSA", typeString(key), typeString(pub))
}
// Opaque crypto.Signer with RSA Public(): allowed for sign and verify.
}
return nil
}
// validateECDSAKey enforces RFC 7518 §3.4: ES256/ES384/ES512 bind to P-256/P-384/P-521.
// jwx's SignerFor path does not enforce this.
func validateECDSAKey(alg jwa.SignatureAlgorithm, key any, signing bool) error {
curve, err := ecdsaCurveFor(alg, key, signing)
if err != nil {
return err
}
if curve == nil {
return fmt.Errorf("algorithm %s: ECDSA key has nil curve", alg)
}
var want elliptic.Curve
switch alg {
case jwa.ES256():
want = elliptic.P256()
case jwa.ES384():
want = elliptic.P384()
case jwa.ES512():
want = elliptic.P521()
default:
return fmt.Errorf("unsupported ECDSA algorithm %s", alg)
}
if curve != want {
return fmt.Errorf("algorithm %s requires curve %s, got %s", alg, want.Params().Name, curve.Params().Name)
}
return nil
}
// ecdsaCurveFor returns the curve for a concrete ECDSA key or an opaque crypto.Signer
// whose Public() is ECDSA. Role checks (private vs public) apply only to concrete keys;
// Signers are accepted for both sign and verify.
func ecdsaCurveFor(alg jwa.SignatureAlgorithm, key any, signing bool) (elliptic.Curve, error) {
switch k := key.(type) {
case *ecdsa.PrivateKey:
if k == nil {
return nil, errNeedKeyType(alg, "ECDSA", typeString(key))
}
if !signing {
return nil, fmt.Errorf("algorithm %s requires an ECDSA public key for verification", alg)
}
return k.Curve, nil
case ecdsa.PrivateKey:
if !signing {
return nil, fmt.Errorf("algorithm %s requires an ECDSA public key for verification", alg)
}
return k.Curve, nil
case *ecdsa.PublicKey:
if k == nil {
return nil, errNeedKeyType(alg, "ECDSA", typeString(key))
}
if signing {
return nil, fmt.Errorf("algorithm %s requires an ECDSA private key for signing", alg)
}
return k.Curve, nil
case ecdsa.PublicKey:
if signing {
return nil, fmt.Errorf("algorithm %s requires an ECDSA private key for signing", alg)
}
return k.Curve, nil
default:
pub, ok := publicFromSigner(key)
if !ok {
return nil, errNeedKeyType(alg, "ECDSA", typeString(key))
}
switch ep := pub.(type) {
case *ecdsa.PublicKey:
if ep == nil {
return nil, errNeedKeyTypeFromSigner(alg, "ECDSA", typeString(key), typeString(pub))
}
return ep.Curve, nil
case ecdsa.PublicKey:
return ep.Curve, nil
default:
return nil, errNeedKeyTypeFromSigner(alg, "ECDSA", typeString(key), typeString(pub))
}
}
}
func validateEd25519Key(alg jwa.SignatureAlgorithm, key any, signing bool) error {
switch k := key.(type) {
case ed25519.PrivateKey:
if len(k) != ed25519.PrivateKeySize {
return fmt.Errorf("algorithm %s: Ed25519 private key must be %d bytes, got %d", alg, ed25519.PrivateKeySize, len(k))
}
if !signing {
return fmt.Errorf("algorithm %s requires an Ed25519 public key for verification", alg)
}
case ed25519.PublicKey:
if len(k) != ed25519.PublicKeySize {
return fmt.Errorf("algorithm %s: Ed25519 public key must be %d bytes, got %d", alg, ed25519.PublicKeySize, len(k))
}
if signing {
return fmt.Errorf("algorithm %s requires an Ed25519 private key for signing", alg)
}
case *ed25519.PrivateKey:
if k == nil || len(*k) != ed25519.PrivateKeySize {
return fmt.Errorf("algorithm %s: invalid Ed25519 private key", alg)
}
if !signing {
return fmt.Errorf("algorithm %s requires an Ed25519 public key for verification", alg)
}
case *ed25519.PublicKey:
if k == nil || len(*k) != ed25519.PublicKeySize {
return fmt.Errorf("algorithm %s: invalid Ed25519 public key", alg)
}
if signing {
return fmt.Errorf("algorithm %s requires an Ed25519 private key for signing", alg)
}
default:
pub, sok := publicFromSigner(key)
if !sok {
return errNeedKeyType(alg, "Ed25519", typeString(key))
}
switch ep := pub.(type) {
case ed25519.PublicKey:
if len(ep) != ed25519.PublicKeySize {
return fmt.Errorf("algorithm %s: Ed25519 public key must be %d bytes, got %d", alg, ed25519.PublicKeySize, len(ep))
}
case *ed25519.PublicKey:
if ep == nil || len(*ep) != ed25519.PublicKeySize {
return fmt.Errorf("algorithm %s: invalid Ed25519 public key", alg)
}
default:
return errNeedKeyTypeFromSigner(alg, "Ed25519", typeString(key), typeString(pub))
}
// Opaque crypto.Signer with Ed25519 Public(): allowed for sign and verify.
}
return nil
}
// validateMLDSAKey enforces that an ML-DSA JWS algorithm matches the key's parameter set.
// jwx also rejects mismatches at Sign/Verify for raw keys; this fails earlier at NewJWS* construction.
func validateMLDSAKey(alg jwa.SignatureAlgorithm, key any, signing bool) error {
got, err := mldsaParamsFor(alg, key, signing)
if err != nil {
return err
}
var want mldsa.Parameters
switch alg {
case jwa.MLDSA44():
want = mldsa.MLDSA44()
case jwa.MLDSA65():
want = mldsa.MLDSA65()
case jwa.MLDSA87():
want = mldsa.MLDSA87()
default:
return fmt.Errorf("unsupported ML-DSA algorithm %s", alg)
}
if got != want {
return fmt.Errorf("algorithm %s requires ML-DSA parameter set %s, got %s", alg, want, got)
}
return nil
}
// mldsaParamsFor returns the parameter set for a concrete crypto/mldsa key or an opaque
// crypto.Signer whose Public() is *mldsa.PublicKey. Role checks apply only to concrete keys;
// Signers are accepted for both sign and verify.
func mldsaParamsFor(alg jwa.SignatureAlgorithm, key any, signing bool) (mldsa.Parameters, error) {
switch k := key.(type) {
case *mldsa.PrivateKey:
if k == nil {
return mldsa.Parameters{}, errNeedKeyType(alg, "crypto/mldsa", typeString(key))
}
if !signing {
return mldsa.Parameters{}, fmt.Errorf("algorithm %s requires an ML-DSA public key for verification", alg)
}
return k.PublicKey().Parameters(), nil
case *mldsa.PublicKey:
if k == nil {
return mldsa.Parameters{}, errNeedKeyType(alg, "crypto/mldsa", typeString(key))
}
if signing {
return mldsa.Parameters{}, fmt.Errorf("algorithm %s requires an ML-DSA private key for signing", alg)
}
return k.Parameters(), nil
default:
pub, ok := publicFromSigner(key)
if !ok {
return mldsa.Parameters{}, errNeedKeyType(alg, "crypto/mldsa", typeString(key))
}
mp, ok := pub.(*mldsa.PublicKey)
if !ok || mp == nil {
return mldsa.Parameters{}, errNeedKeyTypeFromSigner(alg, "crypto/mldsa", typeString(key), typeString(pub))
}
return mp.Parameters(), nil
}
}