11use :: rsa:: { RsaPrivateKey , pkcs1:: DecodeRsaPrivateKey , traits:: PublicKeyParts } ;
2- use p256:: { ecdsa:: SigningKey as P256SigningKey , pkcs8 :: DecodePrivateKey } ;
2+ use p256:: ecdsa:: SigningKey as P256SigningKey ;
33use p384:: ecdsa:: SigningKey as P384SigningKey ;
4+ use p521:: ecdsa:: SigningKey as P521SigningKey ;
5+ use pkcs8:: DecodePrivateKey ;
46use sha2:: { Digest , Sha256 , Sha384 , Sha512 } ;
57
68use crate :: {
@@ -51,6 +53,46 @@ fn extract_ec_public_key_coordinates(
5153 _ => Err ( ErrorKind :: InvalidEcdsaKey . into ( ) ) ,
5254 }
5355 }
56+ Algorithm :: ES512 => {
57+ // Use pkcs8 to parse the PKCS8 wrapper
58+ let private_key_info = pkcs8:: PrivateKeyInfo :: try_from ( key_content)
59+ . map_err ( |_| ErrorKind :: InvalidEcdsaKey ) ?;
60+
61+ // The private_key field contains the DER-encoded ECPrivateKey
62+ let ec_private_key_der = private_key_info. private_key ;
63+
64+ // Use simple_asn1 to parse the ECPrivateKey structure
65+ use simple_asn1:: ASN1Block ;
66+ let asn1_blocks = simple_asn1:: from_der ( ec_private_key_der)
67+ . map_err ( |_| ErrorKind :: InvalidEcdsaKey ) ?;
68+
69+ // Find the OCTET STRING containing the 66-byte private key
70+ for block in asn1_blocks {
71+ if let ASN1Block :: Sequence ( _, entries) = block {
72+ if entries. len ( ) >= 2 {
73+ if let ASN1Block :: OctetString ( _, key_bytes) = & entries[ 1 ] {
74+ if key_bytes. len ( ) == 66 {
75+ let mut field_bytes = p521:: FieldBytes :: default ( ) ;
76+ field_bytes. copy_from_slice ( key_bytes) ;
77+ let signing_key = P521SigningKey :: from_bytes ( & field_bytes)
78+ . map_err ( |_| ErrorKind :: InvalidEcdsaKey ) ?;
79+ let public_key = p521:: ecdsa:: VerifyingKey :: from ( & signing_key) ;
80+ let encoded = public_key. to_encoded_point ( false ) ;
81+ return match encoded. coordinates ( ) {
82+ p521:: elliptic_curve:: sec1:: Coordinates :: Uncompressed {
83+ x,
84+ y,
85+ } => Ok ( ( EllipticCurve :: P521 , x. to_vec ( ) , y. to_vec ( ) ) ) ,
86+ _ => Err ( ErrorKind :: InvalidEcdsaKey . into ( ) ) ,
87+ } ;
88+ }
89+ }
90+ }
91+ }
92+ }
93+
94+ Err ( ErrorKind :: InvalidEcdsaKey . into ( ) )
95+ }
5496 _ => Err ( ErrorKind :: InvalidEcdsaKey . into ( ) ) ,
5597 }
5698}
@@ -70,6 +112,7 @@ fn new_signer(algorithm: &Algorithm, key: &EncodingKey) -> Result<Box<dyn JwtSig
70112 Algorithm :: HS512 => Box :: new ( hmac:: Hs512Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
71113 Algorithm :: ES256 => Box :: new ( ecdsa:: Es256Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
72114 Algorithm :: ES384 => Box :: new ( ecdsa:: Es384Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
115+ Algorithm :: ES512 => Box :: new ( ecdsa:: Es512Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
73116 Algorithm :: RS256 => Box :: new ( rsa:: Rsa256Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
74117 Algorithm :: RS384 => Box :: new ( rsa:: Rsa384Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
75118 Algorithm :: RS512 => Box :: new ( rsa:: Rsa512Signer :: new ( key) ?) as Box < dyn JwtSigner > ,
@@ -92,6 +135,7 @@ fn new_verifier(
92135 Algorithm :: HS512 => Box :: new ( hmac:: Hs512Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
93136 Algorithm :: ES256 => Box :: new ( ecdsa:: Es256Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
94137 Algorithm :: ES384 => Box :: new ( ecdsa:: Es384Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
138+ Algorithm :: ES512 => Box :: new ( ecdsa:: Es512Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
95139 Algorithm :: RS256 => Box :: new ( rsa:: Rsa256Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
96140 Algorithm :: RS384 => Box :: new ( rsa:: Rsa384Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
97141 Algorithm :: RS512 => Box :: new ( rsa:: Rsa512Verifier :: new ( key) ?) as Box < dyn JwtVerifier > ,
0 commit comments