Skip to content

Commit 96a9cf7

Browse files
yaronfcursoragent
andcommitted
fix: require private keys for JWS signers and public keys for verifiers
Enforce asymmetric key role at NewJWSSigner/NewJWSVerifier construction so mismatches fail early instead of at Sign/Verify time. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b566d8a commit 96a9cf7

3 files changed

Lines changed: 102 additions & 31 deletions

File tree

‎crypto.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func NewJWSSigner(alg jwa.SignatureAlgorithm, key interface{}, config *SignConfi
137137
if alg == jwa.NoSignature() {
138138
return nil, fmt.Errorf("the NONE signing algorithm is expressly disallowed")
139139
}
140-
if err := validateJWSKeyAlg(alg, key); err != nil {
140+
if err := validateJWSKeyAlg(alg, key, true); err != nil {
141141
return nil, err
142142
}
143143
if config == nil {
@@ -339,7 +339,7 @@ func NewJWSVerifier(alg jwa.SignatureAlgorithm, key interface{}, config *VerifyC
339339
if alg == jwa.NoSignature() {
340340
return nil, fmt.Errorf("the NONE signing algorithm is expressly disallowed")
341341
}
342-
if err := validateJWSKeyAlg(alg, key); err != nil {
342+
if err := validateJWSKeyAlg(alg, key, false); err != nil {
343343
return nil, err
344344
}
345345
if config == nil {

‎crypto_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,12 @@ func TestNewJWSSigner(t *testing.T) {
273273
{name: "empty hmac key", alg: jwa.HS256(), key: []byte{}, wantErr: true},
274274
{name: "key alg mismatch", alg: jwa.HS256(), key: priv, wantErr: true},
275275
{name: "rsa match", alg: jwa.RS256(), key: priv},
276+
{name: "rsa public key", alg: jwa.RS256(), key: &priv.PublicKey, wantErr: true},
276277
{name: "ecdsa curve match", alg: jwa.ES256(), key: p256},
278+
{name: "ecdsa public key", alg: jwa.ES256(), key: &p256.PublicKey, wantErr: true},
277279
{name: "ecdsa curve mismatch", alg: jwa.ES384(), key: p256, wantErr: true},
278280
{name: "mldsa params match", alg: jwa.MLDSA44(), key: mldsa44},
281+
{name: "mldsa public key", alg: jwa.MLDSA44(), key: mldsa44.Public().(*mldsa.PublicKey), wantErr: true},
279282
{name: "mldsa params mismatch", alg: jwa.MLDSA65(), key: mldsa44, wantErr: true},
280283
{name: "mldsa wrong key type", alg: jwa.MLDSA44(), key: p256, wantErr: true},
281284
}
@@ -378,6 +381,17 @@ func TestNewJWSVerifier(t *testing.T) {
378381
want: nil,
379382
wantErr: true,
380383
},
384+
{
385+
name: "rsa private key",
386+
args: args{
387+
alg: jwa.RS256(),
388+
key: priv,
389+
config: NewVerifyConfig(),
390+
fields: *NewFields(),
391+
},
392+
want: nil,
393+
wantErr: true,
394+
},
381395
{
382396
name: "ecdsa curve match",
383397
args: args{
@@ -405,6 +419,17 @@ func TestNewJWSVerifier(t *testing.T) {
405419
want: nil,
406420
wantErr: true,
407421
},
422+
{
423+
name: "ecdsa private key",
424+
args: args{
425+
alg: jwa.ES256(),
426+
key: p256,
427+
config: NewVerifyConfig(),
428+
fields: *NewFields(),
429+
},
430+
want: nil,
431+
wantErr: true,
432+
},
408433
{
409434
name: "mldsa params match",
410435
args: args{
@@ -432,6 +457,17 @@ func TestNewJWSVerifier(t *testing.T) {
432457
want: nil,
433458
wantErr: true,
434459
},
460+
{
461+
name: "mldsa private key",
462+
args: args{
463+
alg: jwa.MLDSA44(),
464+
key: mldsa44,
465+
config: NewVerifyConfig(),
466+
fields: *NewFields(),
467+
},
468+
want: nil,
469+
wantErr: true,
470+
},
435471
}
436472
for _, tt := range tests {
437473
t.Run(tt.name, func(t *testing.T) {

‎jwskey.go‎

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ import (
1313

1414
// validateJWSKeyAlg checks that key is an appropriate Go type for alg, without using
1515
// jws.AlgorithmsForKey (deprecated; not a compatibility API; over-broad for ECDSA/ML-DSA).
16-
// Only raw stdlib key types are accepted; crypto.Signer / JWK wrappers are rejected here
17-
// so callers convert to concrete keys first.
18-
func validateJWSKeyAlg(alg jwa.SignatureAlgorithm, key interface{}) error {
16+
// When signing is true, asymmetric keys must be private; when false, they must be public.
17+
// HMAC keys are symmetric and accepted for either role. Only raw stdlib key types are
18+
// accepted; crypto.Signer / JWK wrappers are rejected here so callers convert first.
19+
func validateJWSKeyAlg(alg jwa.SignatureAlgorithm, key interface{}, signing bool) error {
1920
switch alg {
2021
case jwa.HS256(), jwa.HS384(), jwa.HS512():
2122
return validateHMACKey(alg, key)
2223
case jwa.RS256(), jwa.RS384(), jwa.RS512(), jwa.PS256(), jwa.PS384(), jwa.PS512():
23-
return validateRSAKey(alg, key)
24+
return validateRSAKey(alg, key, signing)
2425
case jwa.ES256(), jwa.ES384(), jwa.ES512():
25-
return validateECDSAKey(alg, key)
26+
return validateECDSAKey(alg, key, signing)
2627
case jwa.EdDSA(), jwa.EdDSAEd25519():
27-
return validateEd25519Key(alg, key)
28+
return validateEd25519Key(alg, key, signing)
2829
case jwa.MLDSA44(), jwa.MLDSA65(), jwa.MLDSA87():
29-
return validateMLDSAKey(alg, key)
30+
return validateMLDSAKey(alg, key, signing)
3031
default:
3132
return fmt.Errorf("unsupported JWS algorithm %s", alg)
3233
}
@@ -55,20 +56,30 @@ func validateHMACKey(alg jwa.SignatureAlgorithm, key interface{}) error {
5556
return nil
5657
}
5758

58-
func validateRSAKey(alg jwa.SignatureAlgorithm, key interface{}) error {
59+
func validateRSAKey(alg jwa.SignatureAlgorithm, key interface{}, signing bool) error {
5960
switch k := key.(type) {
6061
case *rsa.PrivateKey:
6162
if k == nil {
6263
return fmt.Errorf("algorithm %s: nil RSA private key", alg)
6364
}
65+
if !signing {
66+
return fmt.Errorf("algorithm %s requires an RSA public key for verification", alg)
67+
}
6468
case rsa.PrivateKey:
65-
// ok
69+
if !signing {
70+
return fmt.Errorf("algorithm %s requires an RSA public key for verification", alg)
71+
}
6672
case *rsa.PublicKey:
6773
if k == nil {
6874
return fmt.Errorf("algorithm %s: nil RSA public key", alg)
6975
}
76+
if signing {
77+
return fmt.Errorf("algorithm %s requires an RSA private key for signing", alg)
78+
}
7079
case rsa.PublicKey:
71-
// ok
80+
if signing {
81+
return fmt.Errorf("algorithm %s requires an RSA private key for signing", alg)
82+
}
7283
default:
7384
return fmt.Errorf("algorithm %s requires an RSA key, got %T", alg, key)
7485
}
@@ -77,11 +88,17 @@ func validateRSAKey(alg jwa.SignatureAlgorithm, key interface{}) error {
7788

7889
// validateECDSAKey enforces RFC 7518 §3.4: ES256/ES384/ES512 bind to P-256/P-384/P-521.
7990
// jwx's SignerFor path does not enforce this.
80-
func validateECDSAKey(alg jwa.SignatureAlgorithm, key interface{}) error {
81-
curve, ok := ecdsaCurveOf(key)
91+
func validateECDSAKey(alg jwa.SignatureAlgorithm, key interface{}, signing bool) error {
92+
curve, isPrivate, ok := ecdsaKeyOf(key)
8293
if !ok {
8394
return fmt.Errorf("algorithm %s requires an ECDSA key, got %T", alg, key)
8495
}
96+
if signing && !isPrivate {
97+
return fmt.Errorf("algorithm %s requires an ECDSA private key for signing", alg)
98+
}
99+
if !signing && isPrivate {
100+
return fmt.Errorf("algorithm %s requires an ECDSA public key for verification", alg)
101+
}
85102
var want elliptic.Curve
86103
switch alg {
87104
case jwa.ES256():
@@ -99,45 +116,57 @@ func validateECDSAKey(alg jwa.SignatureAlgorithm, key interface{}) error {
99116
return nil
100117
}
101118

102-
func ecdsaCurveOf(key interface{}) (elliptic.Curve, bool) {
119+
func ecdsaKeyOf(key interface{}) (curve elliptic.Curve, isPrivate, ok bool) {
103120
switch k := key.(type) {
104121
case *ecdsa.PrivateKey:
105122
if k == nil {
106-
return nil, false
123+
return nil, false, false
107124
}
108-
return k.Curve, true
125+
return k.Curve, true, true
109126
case ecdsa.PrivateKey:
110-
return k.Curve, true
127+
return k.Curve, true, true
111128
case *ecdsa.PublicKey:
112129
if k == nil {
113-
return nil, false
130+
return nil, false, false
114131
}
115-
return k.Curve, true
132+
return k.Curve, false, true
116133
case ecdsa.PublicKey:
117-
return k.Curve, true
134+
return k.Curve, false, true
118135
default:
119-
return nil, false
136+
return nil, false, false
120137
}
121138
}
122139

123-
func validateEd25519Key(alg jwa.SignatureAlgorithm, key interface{}) error {
140+
func validateEd25519Key(alg jwa.SignatureAlgorithm, key interface{}, signing bool) error {
124141
switch k := key.(type) {
125142
case ed25519.PrivateKey:
126143
if len(k) != ed25519.PrivateKeySize {
127144
return fmt.Errorf("algorithm %s: Ed25519 private key must be %d bytes, got %d", alg, ed25519.PrivateKeySize, len(k))
128145
}
146+
if !signing {
147+
return fmt.Errorf("algorithm %s requires an Ed25519 public key for verification", alg)
148+
}
129149
case ed25519.PublicKey:
130150
if len(k) != ed25519.PublicKeySize {
131151
return fmt.Errorf("algorithm %s: Ed25519 public key must be %d bytes, got %d", alg, ed25519.PublicKeySize, len(k))
132152
}
153+
if signing {
154+
return fmt.Errorf("algorithm %s requires an Ed25519 private key for signing", alg)
155+
}
133156
case *ed25519.PrivateKey:
134157
if k == nil || len(*k) != ed25519.PrivateKeySize {
135158
return fmt.Errorf("algorithm %s: invalid Ed25519 private key", alg)
136159
}
160+
if !signing {
161+
return fmt.Errorf("algorithm %s requires an Ed25519 public key for verification", alg)
162+
}
137163
case *ed25519.PublicKey:
138164
if k == nil || len(*k) != ed25519.PublicKeySize {
139165
return fmt.Errorf("algorithm %s: invalid Ed25519 public key", alg)
140166
}
167+
if signing {
168+
return fmt.Errorf("algorithm %s requires an Ed25519 private key for signing", alg)
169+
}
141170
default:
142171
return fmt.Errorf("algorithm %s requires an Ed25519 key, got %T", alg, key)
143172
}
@@ -146,11 +175,17 @@ func validateEd25519Key(alg jwa.SignatureAlgorithm, key interface{}) error {
146175

147176
// validateMLDSAKey enforces that an ML-DSA JWS algorithm matches the key's parameter set.
148177
// jwx also rejects mismatches at Sign/Verify; this fails earlier at NewJWS* construction.
149-
func validateMLDSAKey(alg jwa.SignatureAlgorithm, key interface{}) error {
150-
got, ok := mldsaParamsOf(key)
178+
func validateMLDSAKey(alg jwa.SignatureAlgorithm, key interface{}, signing bool) error {
179+
got, isPrivate, ok := mldsaKeyOf(key)
151180
if !ok {
152181
return fmt.Errorf("algorithm %s requires a crypto/mldsa key, got %T", alg, key)
153182
}
183+
if signing && !isPrivate {
184+
return fmt.Errorf("algorithm %s requires an ML-DSA private key for signing", alg)
185+
}
186+
if !signing && isPrivate {
187+
return fmt.Errorf("algorithm %s requires an ML-DSA public key for verification", alg)
188+
}
154189
var want mldsa.Parameters
155190
switch alg {
156191
case jwa.MLDSA44():
@@ -168,19 +203,19 @@ func validateMLDSAKey(alg jwa.SignatureAlgorithm, key interface{}) error {
168203
return nil
169204
}
170205

171-
func mldsaParamsOf(key interface{}) (mldsa.Parameters, bool) {
206+
func mldsaKeyOf(key interface{}) (params mldsa.Parameters, isPrivate, ok bool) {
172207
switch k := key.(type) {
173208
case *mldsa.PrivateKey:
174209
if k == nil {
175-
return mldsa.Parameters{}, false
210+
return mldsa.Parameters{}, false, false
176211
}
177-
return k.PublicKey().Parameters(), true
212+
return k.PublicKey().Parameters(), true, true
178213
case *mldsa.PublicKey:
179214
if k == nil {
180-
return mldsa.Parameters{}, false
215+
return mldsa.Parameters{}, false, false
181216
}
182-
return k.Parameters(), true
217+
return k.Parameters(), false, true
183218
default:
184-
return mldsa.Parameters{}, false
219+
return mldsa.Parameters{}, false, false
185220
}
186221
}

0 commit comments

Comments
 (0)