1use pki_types::PrivateKeyDer;
2pub(crate) use ring as ring_like;
3use webpki::ring as webpki_algs;
4
5use crate::Error;
6use crate::crypto::{CryptoProvider, KeyProvider, SecureRandom, SupportedKxGroup};
7use crate::enums::SignatureScheme;
8use crate::rand::GetRandomFailed;
9use crate::sign::SigningKey;
10use crate::suites::SupportedCipherSuite;
11use crate::sync::Arc;
12use crate::webpki::WebPkiSupportedAlgorithms;
13
14pub mod sign;
16
17pub(crate) mod hash;
18pub(crate) mod hmac;
19pub(crate) mod kx;
20pub(crate) mod quic;
21#[cfg(feature = "std")]
22pub(crate) mod ticketer;
23pub(crate) mod tls12;
24pub(crate) mod tls13;
25
26pub fn default_provider() -> CryptoProvider {
30 CryptoProvider {
31 cipher_suites: DEFAULT_CIPHER_SUITES.to_vec(),
32 kx_groups: DEFAULT_KX_GROUPS.to_vec(),
33 signature_verification_algorithms: SUPPORTED_SIG_ALGS,
34 secure_random: &Ring,
35 key_provider: &Ring,
36 }
37}
38
39#[derive(Debug)]
41struct Ring;
42
43impl SecureRandom for Ring {
44 fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
45 use ring_like::rand::SecureRandom;
46
47 ring_like::rand::SystemRandom::new()
48 .fill(buf)
49 .map_err(|_| GetRandomFailed)
50 }
51}
52
53impl KeyProvider for Ring {
54 fn load_private_key(
55 &self,
56 key_der: PrivateKeyDer<'static>,
57 ) -> Result<Arc<dyn SigningKey>, Error> {
58 sign::any_supported_type(&key_der)
59 }
60}
61
62pub static DEFAULT_CIPHER_SUITES: &[SupportedCipherSuite] = ALL_CIPHER_SUITES;
67
68pub static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = &[
70 tls13::TLS13_AES_256_GCM_SHA384,
72 tls13::TLS13_AES_128_GCM_SHA256,
73 tls13::TLS13_CHACHA20_POLY1305_SHA256,
74 tls12::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
76 tls12::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
77 tls12::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
78 tls12::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
79 tls12::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
80 tls12::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
81];
82
83pub mod cipher_suite {
85 pub use super::tls12::{
86 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
87 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
88 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
89 };
90 pub use super::tls13::{
91 TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
92 };
93}
94
95static SUPPORTED_SIG_ALGS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
98 all: &[
99 webpki_algs::ECDSA_P256_SHA256,
100 webpki_algs::ECDSA_P256_SHA384,
101 webpki_algs::ECDSA_P384_SHA256,
102 webpki_algs::ECDSA_P384_SHA384,
103 webpki_algs::ED25519,
104 webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
105 webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
106 webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
107 webpki_algs::RSA_PKCS1_2048_8192_SHA256,
108 webpki_algs::RSA_PKCS1_2048_8192_SHA384,
109 webpki_algs::RSA_PKCS1_2048_8192_SHA512,
110 webpki_algs::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
111 webpki_algs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
112 webpki_algs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
113 ],
114 mapping: &[
115 (
117 SignatureScheme::ECDSA_NISTP384_SHA384,
118 &[
119 webpki_algs::ECDSA_P384_SHA384,
120 webpki_algs::ECDSA_P256_SHA384,
121 ],
122 ),
123 (
124 SignatureScheme::ECDSA_NISTP256_SHA256,
125 &[
126 webpki_algs::ECDSA_P256_SHA256,
127 webpki_algs::ECDSA_P384_SHA256,
128 ],
129 ),
130 (SignatureScheme::ED25519, &[webpki_algs::ED25519]),
131 (
132 SignatureScheme::RSA_PSS_SHA512,
133 &[webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY],
134 ),
135 (
136 SignatureScheme::RSA_PSS_SHA384,
137 &[webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY],
138 ),
139 (
140 SignatureScheme::RSA_PSS_SHA256,
141 &[webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY],
142 ),
143 (
144 SignatureScheme::RSA_PKCS1_SHA512,
145 &[webpki_algs::RSA_PKCS1_2048_8192_SHA512],
146 ),
147 (
148 SignatureScheme::RSA_PKCS1_SHA384,
149 &[webpki_algs::RSA_PKCS1_2048_8192_SHA384],
150 ),
151 (
152 SignatureScheme::RSA_PKCS1_SHA256,
153 &[webpki_algs::RSA_PKCS1_2048_8192_SHA256],
154 ),
155 ],
156};
157
158pub mod kx_group {
163 pub use super::kx::{SECP256R1, SECP384R1, X25519};
164}
165
166pub static DEFAULT_KX_GROUPS: &[&dyn SupportedKxGroup] = ALL_KX_GROUPS;
168
169pub static ALL_KX_GROUPS: &[&dyn SupportedKxGroup] =
171 &[kx_group::X25519, kx_group::SECP256R1, kx_group::SECP384R1];
172
173#[cfg(feature = "std")]
174pub use ticketer::Ticketer;
175
176mod ring_shim {
178 use super::ring_like;
179 use crate::crypto::SharedSecret;
180
181 pub(super) fn agree_ephemeral(
182 priv_key: ring_like::agreement::EphemeralPrivateKey,
183 peer_key: &ring_like::agreement::UnparsedPublicKey<&[u8]>,
184 ) -> Result<SharedSecret, ()> {
185 ring_like::agreement::agree_ephemeral(priv_key, peer_key, |secret| {
186 SharedSecret::from(secret)
187 })
188 .map_err(|_| ())
189 }
190}
191
192pub(super) fn fips() -> bool {
193 false
194}