@@ -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