@@ -3,9 +3,6 @@ use alloc::{boxed::Box, format, sync::Arc};
33use core:: fmt:: Debug ;
44use core:: marker:: PhantomData ;
55
6- // #[cfg(feature = "sec1")]
7- // use sec1::DecodeEcPrivateKey;
8-
96use crate :: sign:: rand:: GenericRandomizedSigner ;
107use rustls:: sign:: SigningKey ;
118use rustls:: { SignatureAlgorithm , SignatureScheme } ;
@@ -17,52 +14,57 @@ trait EcdsaKey: Sized {
1714 const SCHEME : SignatureScheme ;
1815}
1916
20- // #[cfg(all(feature = "pkcs8", not(feature = "sec1")))]
21- // trait DecodePrivateKey: ::pkcs8::DecodePrivateKey {}
17+ #[ cfg( all( feature = "pkcs8" , not( feature = "sec1" ) ) ) ]
18+ trait DecodePrivateKey : :: pkcs8:: DecodePrivateKey { }
2219
23- // #[cfg(all(feature = "sec1", not(feature = "pkcs8")))]
24- // trait DecodePrivateKey: ::sec1::DecodeEcPrivateKey {}
20+ #[ cfg( all( feature = "sec1" , not( feature = "pkcs8" ) ) ) ]
21+ trait DecodePrivateKey : :: sec1:: DecodeEcPrivateKey { }
2522
26- // #[cfg(all(feature = "pkcs8", feature = "sec1"))]
27- // trait DecodePrivateKey: ::pkcs8::DecodePrivateKey + ::sec1::DecodeEcPrivateKey {}
23+ #[ cfg( all( feature = "pkcs8" , feature = "sec1" ) ) ]
24+ trait DecodePrivateKey : :: pkcs8:: DecodePrivateKey + :: sec1:: DecodeEcPrivateKey { }
2825
2926#[ cfg( feature = "der" ) ]
30- impl < SK , SIG > TryFrom < & PrivateKeyDer < ' _ > > for EcdsaSigningKey < SK , SIG >
27+ impl < SecretKey , SigningKey , Signature > TryFrom < & PrivateKeyDer < ' _ > >
28+ for EcdsaSigningKey < SecretKey , SigningKey , Signature >
3129where
32- SK : EcdsaKey + :: pkcs8:: DecodePrivateKey + Send + Sync + ' static ,
33- SIG : Send + Sync + ' static ,
30+ SecretKey : Debug + DecodePrivateKey ,
31+ SigningKey : EcdsaKey + Send + Sync + ' static + From < SecretKey > ,
32+ Signature : Send + Sync + ' static ,
3433{
3534 type Error = rustls:: Error ;
3635
3736 fn try_from ( value : & PrivateKeyDer < ' _ > ) -> Result < Self , Self :: Error > {
3837 let pkey = match value {
3938 #[ cfg( feature = "pkcs8" ) ]
40- PrivateKeyDer :: Pkcs8 ( der) => SK :: from_pkcs8_der ( der. secret_pkcs8_der ( ) )
39+ PrivateKeyDer :: Pkcs8 ( der) => SecretKey :: from_pkcs8_der ( der. secret_pkcs8_der ( ) )
40+ . map_err ( |e| format ! ( "failed to decrypt private key: {e}" ) ) ,
41+ #[ cfg( feature = "sec1" ) ]
42+ PrivateKeyDer :: Sec1 ( sec1) => SecretKey :: from_sec1_der ( sec1. secret_sec1_der ( ) )
4143 . map_err ( |e| format ! ( "failed to decrypt private key: {e}" ) ) ,
42- // #[cfg(feature = "sec1")]
43- // PrivateKeyDer::Sec1(sec1) => SK::from_sec1_der(sec1.secret_sec1_der())
44- // .map_err(|e| format!("failed to decrypt private key: {e}")),
4544 PrivateKeyDer :: Pkcs1 ( _) => Err ( "ECDSA does not support PKCS#1 key" . into ( ) ) ,
4645 _ => Err ( "not supported" . into ( ) ) ,
4746 } ;
4847 pkey. map ( |kp| Self {
49- key : Arc :: new ( kp) ,
50- scheme : SK :: SCHEME ,
48+ key : Arc :: new ( kp. into ( ) ) ,
49+ scheme : SigningKey :: SCHEME ,
5150 _phantom : PhantomData ,
51+ _phantom_sk : PhantomData ,
5252 } )
5353 . map_err ( rustls:: Error :: General )
5454 }
5555}
5656
5757#[ derive( Debug ) ]
58- pub struct EcdsaSigningKey < SK , SIG > {
58+ pub struct EcdsaSigningKey < SecretKey , SK , SIG > {
5959 key : Arc < SK > ,
6060 scheme : SignatureScheme ,
6161 _phantom : PhantomData < SIG > ,
62+ _phantom_sk : PhantomData < SecretKey > ,
6263}
6364
64- impl < SK , SIG > SigningKey for EcdsaSigningKey < SK , SIG >
65+ impl < SecretKey , SK , SIG > SigningKey for EcdsaSigningKey < SecretKey , SK , SIG >
6566where
67+ SecretKey : Debug + Send + Sync ,
6668 SK : Send + Sync + ' static + Debug + ecdsa:: signature:: RandomizedSigner < SIG > ,
6769 SIG : Send + Sync + ' static + Debug + ecdsa:: signature:: SignatureEncoding ,
6870{
@@ -83,35 +85,39 @@ where
8385 }
8486}
8587
86- #[ cfg( feature = "ecdsa-p256" ) ]
87- pub type EcdsaSigningKeyP256 =
88- EcdsaSigningKey < :: p256:: ecdsa:: SigningKey , :: p256:: ecdsa:: DerSignature > ;
89-
90- #[ cfg( all( feature = "ecdsa-p256" , feature = "hash-sha256" ) ) ]
91- impl EcdsaKey for :: p256:: ecdsa:: SigningKey {
92- const SCHEME : SignatureScheme = SignatureScheme :: ECDSA_NISTP256_SHA256 ;
93- }
94-
95- // #[cfg(feature = "ecdsa-p384")]
96- // impl DecodePrivateKey for ::p384::ecdsa::SigningKey {}
88+ macro_rules! impl_ecdsa_curve {
89+ ( $curve: ident, $scheme: expr, $type_name: ident) => {
90+ pub type $type_name = EcdsaSigningKey <
91+ :: $curve:: SecretKey ,
92+ :: $curve:: ecdsa:: SigningKey ,
93+ :: $curve:: ecdsa:: DerSignature ,
94+ >;
9795
98- # [ cfg ( feature = " ecdsa-p384" ) ]
99- pub type EcdsaSigningKeyP384 =
100- EcdsaSigningKey < :: p384 :: ecdsa :: SigningKey , :: p384 :: ecdsa :: DerSignature > ;
96+ impl EcdsaKey for :: $curve :: ecdsa:: SigningKey {
97+ const SCHEME : SignatureScheme = $scheme ;
98+ }
10199
102- #[ cfg( feature = "ecdsa-p521" ) ]
103- impl EcdsaKey for :: p384:: ecdsa:: SigningKey {
104- const SCHEME : SignatureScheme = SignatureScheme :: ECDSA_NISTP384_SHA384 ;
100+ impl DecodePrivateKey for :: $curve:: SecretKey { }
101+ } ;
105102}
106103
107- // #[cfg(feature = "ecdsa-p521")]
108- // impl DecodePrivateKey for ::p521::ecdsa::SigningKey {}
109-
110- #[ cfg( feature = "ecdsa-p521" ) ]
111- pub type EcdsaSigningKeyP521 =
112- EcdsaSigningKey < :: p521:: ecdsa:: SigningKey , :: p521:: ecdsa:: DerSignature > ;
104+ #[ cfg( all( feature = "ecdsa-p256" , feature = "hash-sha256" ) ) ]
105+ impl_ecdsa_curve ! (
106+ p256,
107+ SignatureScheme :: ECDSA_NISTP256_SHA256 ,
108+ EcdsaSigningKeyP256
109+ ) ;
110+
111+ #[ cfg( all( feature = "ecdsa-p384" , feature = "hash-sha384" ) ) ]
112+ impl_ecdsa_curve ! (
113+ p384,
114+ SignatureScheme :: ECDSA_NISTP384_SHA384 ,
115+ EcdsaSigningKeyP384
116+ ) ;
113117
114118#[ cfg( all( feature = "ecdsa-p521" , feature = "hash-sha512" ) ) ]
115- impl EcdsaKey for :: p521:: ecdsa:: SigningKey {
116- const SCHEME : SignatureScheme = SignatureScheme :: ECDSA_NISTP521_SHA512 ;
117- }
119+ impl_ecdsa_curve ! (
120+ p521,
121+ SignatureScheme :: ECDSA_NISTP521_SHA512 ,
122+ EcdsaSigningKeyP521
123+ ) ;
0 commit comments