1010//! [`CryptoProvider`]: crate::crypto::CryptoProvider
1111
1212use crate :: algorithms:: Algorithm ;
13- use crate :: errors:: Result ;
13+ use crate :: errors:: { self , ErrorKind , Result } ;
1414use crate :: jwk:: { EllipticCurve , ThumbprintHash } ;
1515use crate :: { DecodingKey , EncodingKey } ;
1616
@@ -86,7 +86,7 @@ pub struct CryptoProvider {
8686 /// A function that produces a [`JwtVerifier`] for a given [`Algorithm`]
8787 pub verifier_factory : fn ( & Algorithm , & DecodingKey ) -> Result < Box < dyn JwtVerifier > > ,
8888 /// Struct with utility functions for JWK processing.
89- pub jwk_utils : JwkUtils ,
89+ pub key_utils : KeyUtils ,
9090}
9191
9292impl CryptoProvider {
@@ -123,7 +123,7 @@ See the documentation of the CryptoProvider type for more information.
123123 static INSTANCE : CryptoProvider = CryptoProvider {
124124 signer_factory : |_, _| panic ! ( "{}" , NOT_INSTALLED_ERROR ) ,
125125 verifier_factory : |_, _| panic ! ( "{}" , NOT_INSTALLED_ERROR ) ,
126- jwk_utils : JwkUtils :: new_unimplemented ( ) ,
126+ key_utils : KeyUtils :: new_unimplemented ( ) ,
127127 } ;
128128
129129 & INSTANCE
@@ -132,22 +132,29 @@ See the documentation of the CryptoProvider type for more information.
132132}
133133
134134/// Holds utility functions required for JWK processing.
135- /// Use the [`JwkUtils ::new_unimplemented`] function to initialize all values to dummies.
135+ /// Use the [`KeyUtils ::new_unimplemented`] function to initialize all values to dummies.
136136#[ derive( Clone , Debug ) ]
137- pub struct JwkUtils {
137+ pub struct KeyUtils {
138138 /// Given a DER encoded private key, extract the RSA public key components (n, e)
139139 #[ allow( clippy:: type_complexity) ]
140- pub extract_rsa_public_key_components : fn ( & [ u8 ] ) -> Result < ( Vec < u8 > , Vec < u8 > ) > ,
140+ pub rsa_pub_components_from_private_key : fn ( & [ u8 ] ) -> Result < ( Vec < u8 > , Vec < u8 > ) > ,
141+ /// Given a DER encoded public key, extract the RSA public key components (n, e)
142+ #[ allow( clippy:: type_complexity) ]
143+ pub rsa_pub_components_from_public_key : fn ( & [ u8 ] ) -> Result < ( Vec < u8 > , Vec < u8 > ) > ,
141144 /// Given a DER encoded private key and an algorithm, extract the associated curve
142145 /// and the EC public key components (x, y)
143146 #[ allow( clippy:: type_complexity) ]
144- pub extract_ec_public_key_coordinates :
147+ pub ec_pub_components_from_private_key :
145148 fn ( & [ u8 ] , Algorithm ) -> Result < ( EllipticCurve , Vec < u8 > , Vec < u8 > ) > ,
149+ /// Given bitstring from DER encoded private key, extract the associated curve
150+ /// and the EC public key components (x, y)
151+ #[ allow( clippy:: type_complexity) ]
152+ pub ec_pub_components_from_public_key : fn ( & [ u8 ] ) -> Result < ( EllipticCurve , Vec < u8 > , Vec < u8 > ) > ,
146153 /// Given some data and a name of a hash function, compute hash_function(data)
147154 pub compute_digest : fn ( & [ u8 ] , ThumbprintHash ) -> Result < Vec < u8 > > ,
148155}
149156
150- impl JwkUtils {
157+ impl KeyUtils {
151158 /// Initialises all values to dummies.
152159 /// Will lead to a panic when JWKs are required, so only use it if you don't want to support JWKs.
153160 pub const fn new_unimplemented ( ) -> Self {
@@ -157,17 +164,41 @@ Call CryptoProvider::install_default() before this point to select a provider ma
157164See the documentation of the CryptoProvider type for more information.
158165"### ;
159166 Self {
160- extract_rsa_public_key_components : |_| {
167+ rsa_pub_components_from_private_key : |_| {
161168 panic ! ( "{}" , NOT_INSTALLED_OR_UNIMPLEMENTED_ERROR )
162169 } ,
163- extract_ec_public_key_coordinates : |_, _| {
170+ rsa_pub_components_from_public_key : |_| {
171+ panic ! ( "{}" , NOT_INSTALLED_OR_UNIMPLEMENTED_ERROR )
172+ } ,
173+ ec_pub_components_from_private_key : |_, _| {
174+ panic ! ( "{}" , NOT_INSTALLED_OR_UNIMPLEMENTED_ERROR )
175+ } ,
176+ ec_pub_components_from_public_key : |_| {
164177 panic ! ( "{}" , NOT_INSTALLED_OR_UNIMPLEMENTED_ERROR )
165178 } ,
166179 compute_digest : |_, _| panic ! ( "{}" , NOT_INSTALLED_OR_UNIMPLEMENTED_ERROR ) ,
167180 }
168181 }
169182}
170183
184+ #[ allow( unused) ]
185+ fn ec_components_from_public_key (
186+ pub_bytes : & [ u8 ] ,
187+ ) -> errors:: Result < ( EllipticCurve , Vec < u8 > , Vec < u8 > ) > {
188+ let ( curve, pub_elem_bytes) = match pub_bytes. len ( ) {
189+ 65 => ( EllipticCurve :: P256 , 32 ) ,
190+ 97 => ( EllipticCurve :: P384 , 48 ) ,
191+ _ => return Err ( ErrorKind :: InvalidEcdsaKey . into ( ) ) ,
192+ } ;
193+
194+ if pub_bytes[ 0 ] != 4 {
195+ return Err ( ErrorKind :: InvalidEcdsaKey . into ( ) ) ;
196+ }
197+
198+ let ( x, y) = pub_bytes[ 1 ..] . split_at ( pub_elem_bytes) ;
199+ Ok ( ( curve, x. to_vec ( ) , y. to_vec ( ) ) )
200+ }
201+
171202mod static_default {
172203 use std:: sync:: OnceLock ;
173204
0 commit comments