@@ -23,6 +23,8 @@ use p256::{ecdsa::SigningKey as P256SigningKey, pkcs8::DecodePrivateKey};
2323#[ cfg( feature = "rust_crypto" ) ]
2424use p384:: ecdsa:: SigningKey as P384SigningKey ;
2525#[ cfg( feature = "rust_crypto" ) ]
26+ use k256:: ecdsa:: SigningKey as K256SigningKey ;
27+ #[ cfg( feature = "rust_crypto" ) ]
2628use rsa:: { RsaPrivateKey , pkcs1:: DecodeRsaPrivateKey , traits:: PublicKeyParts } ;
2729#[ cfg( feature = "rust_crypto" ) ]
2830use sha2:: { Digest , Sha256 , Sha384 , Sha512 } ;
@@ -177,6 +179,9 @@ pub enum KeyAlgorithm {
177179 /// ECDSA using SHA-384
178180 ES384 ,
179181
182+ /// ECDSA using secp256k
183+ ES256K ,
184+
180185 /// RSASSA-PKCS1-v1_5 using SHA-256
181186 RS256 ,
182187 /// RSASSA-PKCS1-v1_5 using SHA-384
@@ -219,6 +224,7 @@ impl FromStr for KeyAlgorithm {
219224 "HS512" => Ok ( KeyAlgorithm :: HS512 ) ,
220225 "ES256" => Ok ( KeyAlgorithm :: ES256 ) ,
221226 "ES384" => Ok ( KeyAlgorithm :: ES384 ) ,
227+ "ES256K" => Ok ( KeyAlgorithm :: ES256K ) ,
222228 "RS256" => Ok ( KeyAlgorithm :: RS256 ) ,
223229 "RS384" => Ok ( KeyAlgorithm :: RS384 ) ,
224230 "PS256" => Ok ( KeyAlgorithm :: PS256 ) ,
@@ -319,6 +325,9 @@ pub enum EllipticCurve {
319325 /// P-521 curve -- unsupported by `ring`.
320326 #[ serde( rename = "P-521" ) ]
321327 P521 ,
328+ /// K-256 curve
329+ #[ serde( rename = "secp256k1" ) ]
330+ Secp256k1 ,
322331 /// Ed25519 curve
323332 #[ serde( rename = "Ed25519" ) ]
324333 Ed25519 ,
@@ -501,6 +510,18 @@ fn extract_ec_public_key_coordinates(
501510 _ => Err ( ErrorKind :: InvalidEcdsaKey . into ( ) ) ,
502511 }
503512 }
513+ Algorithm :: ES256K => {
514+ let signing_key = K256SigningKey :: from_pkcs8_der ( key_content)
515+ . map_err ( |_| ErrorKind :: InvalidEcdsaKey ) ?;
516+ let public_key = signing_key. verifying_key ( ) ;
517+ let encoded = public_key. to_encoded_point ( false ) ;
518+ match encoded. coordinates ( ) {
519+ k256:: elliptic_curve:: sec1:: Coordinates :: Uncompressed { x, y } => {
520+ Ok ( ( EllipticCurve :: Secp256k1 , x. to_vec ( ) , y. to_vec ( ) ) )
521+ }
522+ _ => Err ( ErrorKind :: InvalidEcdsaKey . into ( ) )
523+ }
524+ }
504525 Algorithm :: ES384 => {
505526 let signing_key = P384SigningKey :: from_pkcs8_der ( key_content)
506527 . map_err ( |_| ErrorKind :: InvalidEcdsaKey ) ?;
@@ -553,6 +574,7 @@ impl Jwk {
553574 Algorithm :: HS512 => KeyAlgorithm :: HS512 ,
554575 Algorithm :: ES256 => KeyAlgorithm :: ES256 ,
555576 Algorithm :: ES384 => KeyAlgorithm :: ES384 ,
577+ Algorithm :: ES256K => KeyAlgorithm :: ES256K ,
556578 Algorithm :: RS256 => KeyAlgorithm :: RS256 ,
557579 Algorithm :: RS384 => KeyAlgorithm :: RS384 ,
558580 Algorithm :: RS512 => KeyAlgorithm :: RS512 ,
@@ -600,7 +622,7 @@ impl Jwk {
600622 pub fn thumbprint ( & self , hash_function : ThumbprintHash ) -> String {
601623 let pre = match & self . algorithm {
602624 AlgorithmParameters :: EllipticCurve ( a) => match a. curve {
603- EllipticCurve :: P256 | EllipticCurve :: P384 | EllipticCurve :: P521 => {
625+ EllipticCurve :: P256 | EllipticCurve :: P384 | EllipticCurve :: P521 | EllipticCurve :: Secp256k1 => {
604626 format ! (
605627 r#"{{"crv":{},"kty":{},"x":"{}","y":"{}"}}"# ,
606628 serde_json:: to_string( & a. curve) . unwrap( ) ,
@@ -627,7 +649,7 @@ impl Jwk {
627649 )
628650 }
629651 AlgorithmParameters :: OctetKeyPair ( a) => match a. curve {
630- EllipticCurve :: P256 | EllipticCurve :: P384 | EllipticCurve :: P521 => {
652+ EllipticCurve :: P256 | EllipticCurve :: P384 | EllipticCurve :: P521 | EllipticCurve :: Secp256k1 => {
631653 panic ! ( "OctetKeyPair can't contain this curve type" )
632654 }
633655 EllipticCurve :: Ed25519 => {
0 commit comments