@@ -91,7 +91,10 @@ pub(crate) mod tls12;
91
91
pub ( crate ) mod tls13;
92
92
93
93
use mbedtls:: rng:: Random ;
94
- use rustls:: { SignatureScheme , SupportedCipherSuite , WebPkiSupportedAlgorithms } ;
94
+ use rustls:: {
95
+ crypto:: { CryptoProvider , KeyProvider , SecureRandom , WebPkiSupportedAlgorithms } ,
96
+ SignatureScheme , SupportedCipherSuite ,
97
+ } ;
95
98
96
99
/// RNG supported by *mbedtls*
97
100
pub mod rng {
@@ -115,43 +118,43 @@ pub mod rng {
115
118
}
116
119
}
117
120
118
- /// A `CryptoProvider` backed by the [*mbedtls*] crate.
121
+ /// returns a `CryptoProvider` backed by the [*mbedtls*] crate.
119
122
///
120
123
/// [*mbedtls*]: https://github.com/fortanix/rust-mbedtls
121
- pub static MBEDTLS : & ' static dyn rustls:: crypto:: CryptoProvider = & Mbedtls ;
124
+ pub fn mbedtls_crypto_provider ( ) -> CryptoProvider {
125
+ CryptoProvider {
126
+ cipher_suites : ALL_CIPHER_SUITES . to_vec ( ) ,
127
+ kx_groups : ALL_KX_GROUPS . to_vec ( ) ,
128
+ signature_verification_algorithms : SUPPORTED_SIG_ALGS ,
129
+ secure_random : & MbedtlsSecureRandom ,
130
+ key_provider : & MbedtlsKeyProvider ,
131
+ }
132
+ }
122
133
123
- /// Crypto provider based on the [*mbedtls*] crate.
124
- ///
125
- /// [*mbedtls*]: https://github.com/fortanix/rust-mbedtls
126
134
#[ derive( Debug ) ]
127
- struct Mbedtls ;
135
+ /// Implements `SecureRandom` using `mbedtls`
136
+ pub struct MbedtlsSecureRandom ;
128
137
129
- impl rustls :: crypto :: CryptoProvider for Mbedtls {
130
- fn fill_random ( & self , bytes : & mut [ u8 ] ) -> Result < ( ) , rustls:: crypto:: GetRandomFailed > {
138
+ impl SecureRandom for MbedtlsSecureRandom {
139
+ fn fill ( & self , buf : & mut [ u8 ] ) -> Result < ( ) , rustls:: crypto:: GetRandomFailed > {
131
140
rng:: rng_new ( )
132
141
. ok_or ( rustls:: crypto:: GetRandomFailed ) ?
133
- . random ( bytes )
142
+ . random ( buf )
134
143
. map_err ( |_| rustls:: crypto:: GetRandomFailed )
135
144
}
145
+ }
136
146
137
- fn default_cipher_suites ( & self ) -> & ' static [ SupportedCipherSuite ] {
138
- ALL_CIPHER_SUITES
139
- }
140
-
141
- fn default_kx_groups ( & self ) -> & ' static [ & ' static dyn rustls:: crypto:: SupportedKxGroup ] {
142
- ALL_KX_GROUPS
143
- }
147
+ #[ derive( Debug ) ]
148
+ /// Implements `KeyProvider` using `mbedtls`
149
+ pub struct MbedtlsKeyProvider ;
144
150
151
+ impl KeyProvider for MbedtlsKeyProvider {
145
152
fn load_private_key (
146
153
& self ,
147
- key_der : pki_types :: PrivateKeyDer < ' static > ,
154
+ key_der : webpki :: types :: PrivateKeyDer < ' static > ,
148
155
) -> Result < alloc:: sync:: Arc < dyn rustls:: sign:: SigningKey > , rustls:: Error > {
149
156
Ok ( alloc:: sync:: Arc :: new ( sign:: MbedTlsPkSigningKey :: new ( & key_der) ?) )
150
157
}
151
-
152
- fn signature_verification_algorithms ( & self ) -> WebPkiSupportedAlgorithms {
153
- SUPPORTED_SIG_ALGS
154
- }
155
158
}
156
159
157
160
/// The cipher suite configuration that an application should use by default.
0 commit comments