@@ -7,10 +7,10 @@ use std::{fmt, str::FromStr};
77
88use serde:: { Deserialize , Deserializer , Serialize , Serializer , de} ;
99
10- use crate :: crypto:: CryptoProvider ;
10+ use crate :: crypto:: { CryptoProvider , ec_pub_components_from_public_key } ;
1111use crate :: errors:: { self , Error , ErrorKind , new_error} ;
1212use crate :: serialization:: b64_encode;
13- use crate :: { Algorithm , AlgorithmFamily , EncodingKey } ;
13+ use crate :: { Algorithm , AlgorithmFamily , DecodingKey , EncodingKey , decoding :: DecodingKeyKind } ;
1414
1515/// The intended usage of the public `KeyType`. This enum is serialized `untagged`
1616#[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
@@ -490,8 +490,8 @@ impl Jwk {
490490 } ) ,
491491 AlgorithmFamily :: Rsa => {
492492 let ( n, e) = ( CryptoProvider :: get_default ( )
493- . jwk_utils
494- . extract_rsa_public_key_components ) (
493+ . key_utils
494+ . rsa_pub_components_from_private_key ) (
495495 key. inner ( )
496496 ) ?;
497497 AlgorithmParameters :: RSA ( RSAKeyParameters {
@@ -502,8 +502,8 @@ impl Jwk {
502502 }
503503 AlgorithmFamily :: Ec => {
504504 let ( curve, x, y) = ( CryptoProvider :: get_default ( )
505- . jwk_utils
506- . extract_ec_public_key_coordinates ) (
505+ . key_utils
506+ . ec_pub_components_from_private_key ) (
507507 key. inner ( ) , alg
508508 ) ?;
509509 AlgorithmParameters :: EllipticCurve ( EllipticCurveKeyParameters {
@@ -520,6 +520,66 @@ impl Jwk {
520520 } )
521521 }
522522
523+ /// Create a `JWK` from a `DecodingKey`.
524+ ///
525+ /// Edwards curve based keys are not supported.
526+ pub fn from_decoding_key (
527+ key : & DecodingKey ,
528+ alg : Option < Algorithm > ,
529+ ) -> crate :: errors:: Result < Self > {
530+ Ok ( Self {
531+ common : CommonParameters { key_algorithm : alg. map ( |a| a. into ( ) ) , ..Default :: default ( ) } ,
532+ algorithm : match key. family ( ) {
533+ crate :: algorithms:: AlgorithmFamily :: Hmac => {
534+ let secret = match & key. kind ( ) {
535+ DecodingKeyKind :: SecretOrDer ( secret) => secret,
536+ _ => return Err ( ErrorKind :: InvalidKeyFormat . into ( ) ) ,
537+ } ;
538+
539+ AlgorithmParameters :: OctetKey ( OctetKeyParameters {
540+ key_type : OctetKeyType :: Octet ,
541+ value : b64_encode ( secret) ,
542+ } )
543+ }
544+ crate :: algorithms:: AlgorithmFamily :: Rsa => {
545+ let ( n, e) = match & key. kind ( ) {
546+ DecodingKeyKind :: RsaModulusExponent { n, e } => {
547+ ( b64_encode ( n) , b64_encode ( e) )
548+ }
549+ DecodingKeyKind :: SecretOrDer ( der) => {
550+ let ( n, e) = ( CryptoProvider :: get_default ( )
551+ . key_utils
552+ . rsa_pub_components_from_public_key ) (
553+ der
554+ ) ?;
555+ ( b64_encode ( n) , b64_encode ( e) )
556+ }
557+ } ;
558+
559+ AlgorithmParameters :: RSA ( RSAKeyParameters { key_type : RSAKeyType :: RSA , n, e } )
560+ }
561+ crate :: algorithms:: AlgorithmFamily :: Ec => {
562+ let ( curve, x, y) = match & key. kind ( ) {
563+ DecodingKeyKind :: SecretOrDer ( pub_bytes) => {
564+ ec_pub_components_from_public_key ( pub_bytes) ?
565+ }
566+ _ => return Err ( ErrorKind :: InvalidKeyFormat . into ( ) ) ,
567+ } ;
568+
569+ AlgorithmParameters :: EllipticCurve ( EllipticCurveKeyParameters {
570+ key_type : EllipticCurveKeyType :: EC ,
571+ curve,
572+ x : b64_encode ( x) ,
573+ y : b64_encode ( y) ,
574+ } )
575+ }
576+ crate :: algorithms:: AlgorithmFamily :: Ed => {
577+ unimplemented ! ( "Edwards curves are not supported" ) ;
578+ }
579+ } ,
580+ } )
581+ }
582+
523583 /// Compute the thumbprint of the JWK.
524584 ///
525585 /// Per [RFC-7638](https://datatracker.ietf.org/doc/html/rfc7638)
@@ -567,7 +627,7 @@ impl Jwk {
567627 } ,
568628 } ;
569629
570- Ok ( b64_encode ( ( CryptoProvider :: get_default ( ) . jwk_utils . compute_digest ) (
630+ Ok ( b64_encode ( ( CryptoProvider :: get_default ( ) . key_utils . compute_digest ) (
571631 pre. as_bytes ( ) ,
572632 hash_function,
573633 ) ?) )
@@ -602,6 +662,7 @@ mod tests {
602662 ThumbprintHash ,
603663 } ;
604664 use crate :: serialization:: b64_encode;
665+ use crate :: { DecodingKey , EncodingKey } ;
605666
606667 #[ test]
607668 #[ wasm_bindgen_test]
@@ -695,4 +756,32 @@ mod tests {
695756 . is_err_and( |e| * e. kind( ) == ErrorKind :: UnsupportedAlgorithm )
696757 ) ;
697758 }
759+
760+ #[ test]
761+ #[ cfg( feature = "use_pem" ) ]
762+ fn check_jwk_from_decoding_key_rsa ( ) {
763+ let enc_key =
764+ EncodingKey :: from_rsa_pem ( include_bytes ! ( "../tests/rsa/private_rsa_key_pkcs8.pem" ) )
765+ . unwrap ( ) ;
766+ let dec_key =
767+ DecodingKey :: from_rsa_pem ( include_bytes ! ( "../tests/rsa/public_rsa_key_pkcs8.pem" ) )
768+ . unwrap ( ) ;
769+ let expected_jwk = Jwk :: from_encoding_key ( & enc_key, Algorithm :: RS256 ) . unwrap ( ) ;
770+ let jwk = Jwk :: from_decoding_key ( & dec_key, Some ( Algorithm :: RS256 ) ) . unwrap ( ) ;
771+ assert_eq ! ( jwk, expected_jwk) ;
772+ }
773+
774+ #[ test]
775+ #[ cfg( feature = "use_pem" ) ]
776+ fn check_jwk_from_decoding_key_ec ( ) {
777+ let enc_key =
778+ EncodingKey :: from_ec_pem ( include_bytes ! ( "../tests/ecdsa/private_ecdsa_key.pem" ) )
779+ . unwrap ( ) ;
780+ let dec_key =
781+ DecodingKey :: from_ec_pem ( include_bytes ! ( "../tests/ecdsa/public_ecdsa_key.pem" ) )
782+ . unwrap ( ) ;
783+ let expected_jwk = Jwk :: from_encoding_key ( & enc_key, Algorithm :: ES256 ) . unwrap ( ) ;
784+ let jwk = Jwk :: from_decoding_key ( & dec_key, Some ( Algorithm :: ES256 ) ) . unwrap ( ) ;
785+ assert_eq ! ( jwk, expected_jwk) ;
786+ }
698787}
0 commit comments