@@ -3,9 +3,6 @@ use alloc::{boxed::Box, format, sync::Arc};
3
3
use core:: fmt:: Debug ;
4
4
use core:: marker:: PhantomData ;
5
5
6
- // #[cfg(feature = "sec1")]
7
- // use sec1::DecodeEcPrivateKey;
8
-
9
6
use crate :: sign:: rand:: GenericRandomizedSigner ;
10
7
use rustls:: sign:: SigningKey ;
11
8
use rustls:: { SignatureAlgorithm , SignatureScheme } ;
@@ -17,52 +14,57 @@ trait EcdsaKey: Sized {
17
14
const SCHEME : SignatureScheme ;
18
15
}
19
16
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 { }
22
19
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 { }
25
22
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 { }
28
25
29
26
#[ 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 >
31
29
where
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 ,
34
33
{
35
34
type Error = rustls:: Error ;
36
35
37
36
fn try_from ( value : & PrivateKeyDer < ' _ > ) -> Result < Self , Self :: Error > {
38
37
let pkey = match value {
39
38
#[ 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 ( ) )
41
43
. 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}")),
45
44
PrivateKeyDer :: Pkcs1 ( _) => Err ( "ECDSA does not support PKCS#1 key" . into ( ) ) ,
46
45
_ => Err ( "not supported" . into ( ) ) ,
47
46
} ;
48
47
pkey. map ( |kp| Self {
49
- key : Arc :: new ( kp) ,
50
- scheme : SK :: SCHEME ,
48
+ key : Arc :: new ( kp. into ( ) ) ,
49
+ scheme : SigningKey :: SCHEME ,
51
50
_phantom : PhantomData ,
51
+ _phantom_sk : PhantomData ,
52
52
} )
53
53
. map_err ( rustls:: Error :: General )
54
54
}
55
55
}
56
56
57
57
#[ derive( Debug ) ]
58
- pub struct EcdsaSigningKey < SK , SIG > {
58
+ pub struct EcdsaSigningKey < SecretKey , SK , SIG > {
59
59
key : Arc < SK > ,
60
60
scheme : SignatureScheme ,
61
61
_phantom : PhantomData < SIG > ,
62
+ _phantom_sk : PhantomData < SecretKey > ,
62
63
}
63
64
64
- impl < SK , SIG > SigningKey for EcdsaSigningKey < SK , SIG >
65
+ impl < SecretKey , SK , SIG > SigningKey for EcdsaSigningKey < SecretKey , SK , SIG >
65
66
where
67
+ SecretKey : Debug + Send + Sync ,
66
68
SK : Send + Sync + ' static + Debug + ecdsa:: signature:: RandomizedSigner < SIG > ,
67
69
SIG : Send + Sync + ' static + Debug + ecdsa:: signature:: SignatureEncoding ,
68
70
{
@@ -83,35 +85,39 @@ where
83
85
}
84
86
}
85
87
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
+ >;
97
95
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
+ }
101
99
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
+ } ;
105
102
}
106
103
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
+ ) ;
113
117
114
118
#[ 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