|
1 | | -#[cfg(feature = "alloc")] |
2 | | -use alloc::{sync::Arc, vec::Vec}; |
3 | | -use core::marker::PhantomData; |
4 | | - |
5 | | -use pki_types::PrivateKeyDer; |
6 | | -use rustls::sign::{Signer, SigningKey}; |
7 | | -use rustls::{Error, SignatureScheme}; |
8 | | -use signature::SignatureEncoding; |
9 | | - |
10 | | -#[derive(Debug)] |
11 | | -pub struct GenericSigner<S, T> |
12 | | -where |
13 | | - S: SignatureEncoding, |
14 | | - T: signature::Signer<S>, |
15 | | -{ |
16 | | - _marker: PhantomData<S>, |
17 | | - key: Arc<T>, |
18 | | - scheme: SignatureScheme, |
19 | | -} |
20 | | - |
21 | | -impl<S, T> Signer for GenericSigner<S, T> |
22 | | -where |
23 | | - S: SignatureEncoding + Send + Sync + core::fmt::Debug, |
24 | | - T: signature::Signer<S> + Send + Sync + core::fmt::Debug, |
25 | | -{ |
26 | | - fn sign(&self, message: &[u8]) -> Result<Vec<u8>, Error> { |
27 | | - self.key |
28 | | - .try_sign(message) |
29 | | - .map_err(|_| rustls::Error::General("signing failed".into())) |
30 | | - .map(|sig: S| sig.to_vec()) |
31 | | - } |
32 | | - |
33 | | - fn scheme(&self) -> SignatureScheme { |
34 | | - self.scheme |
35 | | - } |
36 | | -} |
37 | | - |
38 | | -/// Extract any supported key from the given DER input. |
39 | | -/// |
40 | | -/// # Errors |
41 | | -/// |
42 | | -/// Returns an error if the key couldn't be decoded. |
43 | | -#[allow(unused_variables)] |
44 | | -pub fn any_supported_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
45 | | - #[cfg(feature = "sign-rsa")] |
46 | | - if let Ok(key) = rsa::RsaSigningKey::try_from(der) { |
47 | | - return Ok(Arc::new(key) as _); |
48 | | - } |
49 | | - |
50 | | - #[cfg(feature = "sign-ecdsa-nist")] |
51 | | - if let Ok(key) = any_ecdsa_type(der) { |
52 | | - return Ok(key); |
53 | | - } |
54 | | - |
55 | | - #[cfg(feature = "sign-eddsa")] |
56 | | - if let Ok(key) = any_eddsa_type(der) { |
57 | | - return Ok(key); |
58 | | - } |
59 | | - |
60 | | - Err(rustls::Error::General("not supported".into())) |
61 | | -} |
62 | | - |
63 | | -/// Extract any supported ECDSA key from the given DER input. |
64 | | -/// |
65 | | -/// # Errors |
66 | | -/// |
67 | | -/// Returns an error if the key couldn't be decoded. |
68 | | -#[allow(unused_variables)] |
69 | | -#[cfg(feature = "sign-ecdsa-nist")] |
70 | | -pub fn any_ecdsa_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
71 | | - #[cfg(all(feature = "der", feature = "ecdsa-p256"))] |
72 | | - if let Ok(key) = ecdsa::nist::EcdsaSigningKeyP256::try_from(der) { |
73 | | - return Ok(Arc::new(key) as _); |
74 | | - } |
75 | | - #[cfg(all(feature = "der", feature = "ecdsa-p384"))] |
76 | | - if let Ok(key) = ecdsa::nist::EcdsaSigningKeyP384::try_from(der) { |
77 | | - return Ok(Arc::new(key) as _); |
78 | | - } |
79 | | - |
80 | | - Err(rustls::Error::General("not supported".into())) |
81 | | -} |
82 | | - |
83 | | -/// Extract any supported EDDSA key from the given DER input. |
84 | | -/// |
85 | | -/// # Errors |
86 | | -/// |
87 | | -/// Returns an error if the key couldn't be decoded. |
88 | | -#[allow(unused_variables)] |
89 | | -#[cfg(feature = "sign-eddsa")] |
90 | | -pub fn any_eddsa_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
91 | | - // TODO: Add support for Ed448 |
92 | | - #[cfg(all(feature = "der", feature = "eddsa-ed25519"))] |
93 | | - if let Ok(key) = eddsa::ed25519::Ed25519SigningKey::try_from(der) { |
94 | | - return Ok(Arc::new(key) as _); |
95 | | - } |
96 | | - |
97 | | - Err(rustls::Error::General("not supported".into())) |
98 | | -} |
99 | | - |
100 | | -#[cfg(feature = "ecdsa")] |
101 | | -pub mod ecdsa; |
102 | | -#[cfg(feature = "eddsa")] |
103 | | -pub mod eddsa; |
104 | | -#[cfg(feature = "rsa")] |
105 | | -pub mod rsa; |
106 | | - |
107 | | -#[cfg(feature = "rand")] |
108 | | -pub mod rand; |
| 1 | +#[cfg(feature = "alloc")] |
| 2 | +use alloc::{sync::Arc, vec::Vec}; |
| 3 | +use core::marker::PhantomData; |
| 4 | + |
| 5 | +use pki_types::PrivateKeyDer; |
| 6 | +use rustls::sign::{Signer, SigningKey}; |
| 7 | +use rustls::{Error, SignatureScheme}; |
| 8 | +use signature::SignatureEncoding; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct GenericSigner<S, T> |
| 12 | +where |
| 13 | + S: SignatureEncoding, |
| 14 | + T: signature::Signer<S>, |
| 15 | +{ |
| 16 | + _marker: PhantomData<S>, |
| 17 | + key: Arc<T>, |
| 18 | + scheme: SignatureScheme, |
| 19 | +} |
| 20 | + |
| 21 | +impl<S, T> Signer for GenericSigner<S, T> |
| 22 | +where |
| 23 | + S: SignatureEncoding + Send + Sync + core::fmt::Debug, |
| 24 | + T: signature::Signer<S> + Send + Sync + core::fmt::Debug, |
| 25 | +{ |
| 26 | + fn sign(&self, message: &[u8]) -> Result<Vec<u8>, Error> { |
| 27 | + self.key |
| 28 | + .try_sign(message) |
| 29 | + .map_err(|_| rustls::Error::General("signing failed".into())) |
| 30 | + .map(|sig: S| sig.to_vec()) |
| 31 | + } |
| 32 | + |
| 33 | + fn scheme(&self) -> SignatureScheme { |
| 34 | + self.scheme |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Extract any supported key from the given DER input. |
| 39 | +/// |
| 40 | +/// # Errors |
| 41 | +/// |
| 42 | +/// Returns an error if the key couldn't be decoded. |
| 43 | +#[allow(unused_variables)] |
| 44 | +pub fn any_supported_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
| 45 | + #[cfg(feature = "sign-rsa")] |
| 46 | + if let Ok(key) = rsa::RsaSigningKey::try_from(der) { |
| 47 | + return Ok(Arc::new(key) as _); |
| 48 | + } |
| 49 | + |
| 50 | + #[cfg(feature = "sign-ecdsa-nist")] |
| 51 | + if let Ok(key) = any_ecdsa_type(der) { |
| 52 | + return Ok(key); |
| 53 | + } |
| 54 | + |
| 55 | + #[cfg(feature = "sign-eddsa")] |
| 56 | + if let Ok(key) = any_eddsa_type(der) { |
| 57 | + return Ok(key); |
| 58 | + } |
| 59 | + |
| 60 | + Err(rustls::Error::General("not supported".into())) |
| 61 | +} |
| 62 | + |
| 63 | +/// Extract any supported ECDSA key from the given DER input. |
| 64 | +/// |
| 65 | +/// # Errors |
| 66 | +/// |
| 67 | +/// Returns an error if the key couldn't be decoded. |
| 68 | +#[allow(unused_variables)] |
| 69 | +#[cfg(feature = "sign-ecdsa-nist")] |
| 70 | +pub fn any_ecdsa_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
| 71 | + #[cfg(all(feature = "der", feature = "ecdsa-p256"))] |
| 72 | + if let Ok(key) = ecdsa::nist::EcdsaSigningKeyP256::try_from(der) { |
| 73 | + return Ok(Arc::new(key) as _); |
| 74 | + } |
| 75 | + #[cfg(all(feature = "der", feature = "ecdsa-p384"))] |
| 76 | + if let Ok(key) = ecdsa::nist::EcdsaSigningKeyP384::try_from(der) { |
| 77 | + return Ok(Arc::new(key) as _); |
| 78 | + } |
| 79 | + |
| 80 | + Err(rustls::Error::General("not supported".into())) |
| 81 | +} |
| 82 | + |
| 83 | +/// Extract any supported EDDSA key from the given DER input. |
| 84 | +/// |
| 85 | +/// # Errors |
| 86 | +/// |
| 87 | +/// Returns an error if the key couldn't be decoded. |
| 88 | +#[allow(unused_variables)] |
| 89 | +#[cfg(feature = "sign-eddsa")] |
| 90 | +pub fn any_eddsa_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
| 91 | + // TODO: Add support for Ed448 |
| 92 | + #[cfg(all(feature = "der", feature = "eddsa-ed25519"))] |
| 93 | + if let Ok(key) = eddsa::ed25519::Ed25519SigningKey::try_from(der) { |
| 94 | + return Ok(Arc::new(key) as _); |
| 95 | + } |
| 96 | + |
| 97 | + Err(rustls::Error::General("not supported".into())) |
| 98 | +} |
| 99 | + |
| 100 | +#[cfg(feature = "ecdsa")] |
| 101 | +pub mod ecdsa; |
| 102 | +#[cfg(feature = "eddsa")] |
| 103 | +pub mod eddsa; |
| 104 | +#[cfg(feature = "rsa")] |
| 105 | +pub mod rsa; |
| 106 | + |
| 107 | +#[cfg(feature = "rand")] |
| 108 | +pub mod rand; |
0 commit comments