1use alloc::vec::Vec;
2
3pub(crate) use aws_lc_rs as ring_like;
8use pki_types::PrivateKeyDer;
9use webpki::aws_lc_rs as webpki_algs;
10
11use crate::crypto::{CryptoProvider, KeyProvider, SecureRandom, SupportedKxGroup};
12use crate::enums::SignatureScheme;
13use crate::rand::GetRandomFailed;
14use crate::sign::SigningKey;
15use crate::suites::SupportedCipherSuite;
16use crate::sync::Arc;
17use crate::webpki::WebPkiSupportedAlgorithms;
18use crate::{Error, OtherError};
19
20pub mod hpke;
22pub(crate) mod pq;
24pub mod sign;
26
27#[path = "../ring/hash.rs"]
28pub(crate) mod hash;
29#[path = "../ring/hmac.rs"]
30pub(crate) mod hmac;
31#[path = "../ring/kx.rs"]
32pub(crate) mod kx;
33#[path = "../ring/quic.rs"]
34pub(crate) mod quic;
35#[cfg(feature = "std")]
36pub(crate) mod ticketer;
37#[cfg(feature = "tls12")]
38pub(crate) mod tls12;
39pub(crate) mod tls13;
40
41pub fn default_provider() -> CryptoProvider {
43 CryptoProvider {
44 cipher_suites: DEFAULT_CIPHER_SUITES.to_vec(),
45 kx_groups: default_kx_groups(),
46 signature_verification_algorithms: SUPPORTED_SIG_ALGS,
47 secure_random: &AwsLcRs,
48 key_provider: &AwsLcRs,
49 }
50}
51
52fn default_kx_groups() -> Vec<&'static dyn SupportedKxGroup> {
53 #[cfg(feature = "fips")]
54 {
55 DEFAULT_KX_GROUPS
56 .iter()
57 .filter(|cs| cs.fips())
58 .copied()
59 .collect()
60 }
61 #[cfg(not(feature = "fips"))]
62 {
63 DEFAULT_KX_GROUPS.to_vec()
64 }
65}
66
67#[derive(Debug)]
68struct AwsLcRs;
69
70impl SecureRandom for AwsLcRs {
71 fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
72 use ring_like::rand::SecureRandom;
73
74 ring_like::rand::SystemRandom::new()
75 .fill(buf)
76 .map_err(|_| GetRandomFailed)
77 }
78
79 fn fips(&self) -> bool {
80 fips()
81 }
82}
83
84impl KeyProvider for AwsLcRs {
85 fn load_private_key(
86 &self,
87 key_der: PrivateKeyDer<'static>,
88 ) -> Result<Arc<dyn SigningKey>, Error> {
89 sign::any_supported_type(&key_der)
90 }
91
92 fn fips(&self) -> bool {
93 fips()
94 }
95}
96
97pub static DEFAULT_CIPHER_SUITES: &[SupportedCipherSuite] = &[
102 tls13::TLS13_AES_256_GCM_SHA384,
104 tls13::TLS13_AES_128_GCM_SHA256,
105 #[cfg(not(feature = "fips"))]
106 tls13::TLS13_CHACHA20_POLY1305_SHA256,
107 #[cfg(feature = "tls12")]
109 tls12::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
110 #[cfg(feature = "tls12")]
111 tls12::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
112 #[cfg(all(feature = "tls12", not(feature = "fips")))]
113 tls12::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
114 #[cfg(feature = "tls12")]
115 tls12::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
116 #[cfg(feature = "tls12")]
117 tls12::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
118 #[cfg(all(feature = "tls12", not(feature = "fips")))]
119 tls12::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
120];
121
122pub static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = &[
124 tls13::TLS13_AES_256_GCM_SHA384,
126 tls13::TLS13_AES_128_GCM_SHA256,
127 tls13::TLS13_CHACHA20_POLY1305_SHA256,
128 #[cfg(feature = "tls12")]
130 tls12::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
131 #[cfg(feature = "tls12")]
132 tls12::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
133 #[cfg(feature = "tls12")]
134 tls12::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
135 #[cfg(feature = "tls12")]
136 tls12::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
137 #[cfg(feature = "tls12")]
138 tls12::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
139 #[cfg(feature = "tls12")]
140 tls12::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
141];
142
143pub mod cipher_suite {
145 #[cfg(feature = "tls12")]
146 pub use super::tls12::{
147 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
148 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
149 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
150 };
151 pub use super::tls13::{
152 TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
153 };
154}
155
156static SUPPORTED_SIG_ALGS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
159 all: &[
160 webpki_algs::ECDSA_P256_SHA256,
161 webpki_algs::ECDSA_P256_SHA384,
162 webpki_algs::ECDSA_P384_SHA256,
163 webpki_algs::ECDSA_P384_SHA384,
164 webpki_algs::ECDSA_P521_SHA256,
165 webpki_algs::ECDSA_P521_SHA384,
166 webpki_algs::ECDSA_P521_SHA512,
167 webpki_algs::ED25519,
168 webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
169 webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
170 webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
171 webpki_algs::RSA_PKCS1_2048_8192_SHA256,
172 webpki_algs::RSA_PKCS1_2048_8192_SHA384,
173 webpki_algs::RSA_PKCS1_2048_8192_SHA512,
174 webpki_algs::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
175 webpki_algs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
176 webpki_algs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
177 ],
178 mapping: &[
179 (
181 SignatureScheme::ECDSA_NISTP384_SHA384,
182 &[
183 webpki_algs::ECDSA_P384_SHA384,
184 webpki_algs::ECDSA_P256_SHA384,
185 webpki_algs::ECDSA_P521_SHA384,
186 ],
187 ),
188 (
189 SignatureScheme::ECDSA_NISTP256_SHA256,
190 &[
191 webpki_algs::ECDSA_P256_SHA256,
192 webpki_algs::ECDSA_P384_SHA256,
193 webpki_algs::ECDSA_P521_SHA256,
194 ],
195 ),
196 (
197 SignatureScheme::ECDSA_NISTP521_SHA512,
198 &[
199 webpki_algs::ECDSA_P521_SHA512,
200 webpki_algs::ECDSA_P384_SHA512,
201 webpki_algs::ECDSA_P256_SHA512,
202 ],
203 ),
204 (SignatureScheme::ED25519, &[webpki_algs::ED25519]),
205 (
206 SignatureScheme::RSA_PSS_SHA512,
207 &[webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY],
208 ),
209 (
210 SignatureScheme::RSA_PSS_SHA384,
211 &[webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY],
212 ),
213 (
214 SignatureScheme::RSA_PSS_SHA256,
215 &[webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY],
216 ),
217 (
218 SignatureScheme::RSA_PKCS1_SHA512,
219 &[webpki_algs::RSA_PKCS1_2048_8192_SHA512],
220 ),
221 (
222 SignatureScheme::RSA_PKCS1_SHA384,
223 &[webpki_algs::RSA_PKCS1_2048_8192_SHA384],
224 ),
225 (
226 SignatureScheme::RSA_PKCS1_SHA256,
227 &[webpki_algs::RSA_PKCS1_2048_8192_SHA256],
228 ),
229 ],
230};
231
232pub mod kx_group {
237 pub use super::kx::{SECP256R1, SECP384R1, X25519};
238 pub use super::pq::{MLKEM768, SECP256R1MLKEM768, X25519MLKEM768};
239}
240
241pub static DEFAULT_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
246 #[cfg(feature = "prefer-post-quantum")]
247 kx_group::X25519MLKEM768,
248 kx_group::X25519,
249 kx_group::SECP256R1,
250 kx_group::SECP384R1,
251 #[cfg(not(feature = "prefer-post-quantum"))]
252 kx_group::X25519MLKEM768,
253];
254
255pub static ALL_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
257 #[cfg(feature = "prefer-post-quantum")]
258 kx_group::X25519MLKEM768,
259 #[cfg(feature = "prefer-post-quantum")]
260 kx_group::SECP256R1MLKEM768,
261 kx_group::X25519,
262 kx_group::SECP256R1,
263 kx_group::SECP384R1,
264 #[cfg(not(feature = "prefer-post-quantum"))]
265 kx_group::X25519MLKEM768,
266 #[cfg(not(feature = "prefer-post-quantum"))]
267 kx_group::SECP256R1MLKEM768,
268 kx_group::MLKEM768,
269];
270
271#[cfg(feature = "std")]
272pub use ticketer::Ticketer;
273
274mod ring_shim {
276 use super::ring_like;
277 use crate::crypto::SharedSecret;
278
279 pub(super) fn agree_ephemeral(
280 priv_key: ring_like::agreement::EphemeralPrivateKey,
281 peer_key: &ring_like::agreement::UnparsedPublicKey<&[u8]>,
282 ) -> Result<SharedSecret, ()> {
283 ring_like::agreement::agree_ephemeral(priv_key, peer_key, (), |secret| {
284 Ok(SharedSecret::from(secret))
285 })
286 }
287}
288
289pub(super) fn fips() -> bool {
291 aws_lc_rs::try_fips_mode().is_ok()
292}
293
294pub(super) fn unspecified_err(_e: aws_lc_rs::error::Unspecified) -> Error {
295 #[cfg(feature = "std")]
296 {
297 Error::Other(OtherError(Arc::new(_e)))
298 }
299 #[cfg(not(feature = "std"))]
300 {
301 Error::Other(OtherError())
302 }
303}
304
305#[cfg(test)]
306mod tests {
307 #[cfg(feature = "fips")]
308 #[test]
309 fn default_suites_are_fips() {
310 assert!(
311 super::DEFAULT_CIPHER_SUITES
312 .iter()
313 .all(|scs| scs.fips())
314 );
315 }
316
317 #[cfg(not(feature = "fips"))]
318 #[test]
319 fn default_suites() {
320 assert_eq!(super::DEFAULT_CIPHER_SUITES, super::ALL_CIPHER_SUITES);
321 }
322}