|
| 1 | +//! The rustls crypto provider for the pricing refresh. |
| 2 | +//! |
| 3 | +//! rustls' own `ring` provider also carries everything a TLS server or a client |
| 4 | +//! presenting a certificate needs, and its key loader keeps ring's RSA, ECDSA, |
| 5 | +//! and Ed25519 signing code in the binary. The refresh only ever verifies a |
| 6 | +//! server, so this provider is assembled from the same ring primitives without |
| 7 | +//! a way to load private keys. |
| 8 | +
|
| 9 | +use std::sync::Arc; |
| 10 | + |
| 11 | +use rustls::{ |
| 12 | + SignatureScheme, |
| 13 | + crypto::{ |
| 14 | + CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom, WebPkiSupportedAlgorithms, ring, |
| 15 | + }, |
| 16 | + pki_types::PrivateKeyDer, |
| 17 | + sign::SigningKey, |
| 18 | +}; |
| 19 | +use webpki::ring as algs; |
| 20 | + |
| 21 | +/// Builds the provider the pricing client hands to rustls. |
| 22 | +/// |
| 23 | +/// Only TLS 1.3 suites are offered: both pricing hosts negotiate TLS 1.3, and |
| 24 | +/// the TLS 1.2 suites would keep their key schedule and AEAD glue in the binary. |
| 25 | +/// TLS-intercepting middleboxes already failed, because the client trusts no |
| 26 | +/// root beyond the embedded web PKI ones; an `https://` proxy with a publicly |
| 27 | +/// trusted certificate now needs TLS 1.3 as well. |
| 28 | +/// |
| 29 | +/// X25519 is the only key exchange group, which both hosts pick on their own; |
| 30 | +/// the NIST curves stay for certificate signatures, not for key agreement. |
| 31 | +pub(super) fn provider() -> CryptoProvider { |
| 32 | + CryptoProvider { |
| 33 | + cipher_suites: vec![ |
| 34 | + ring::cipher_suite::TLS13_AES_256_GCM_SHA384, |
| 35 | + ring::cipher_suite::TLS13_AES_128_GCM_SHA256, |
| 36 | + ring::cipher_suite::TLS13_CHACHA20_POLY1305_SHA256, |
| 37 | + ], |
| 38 | + kx_groups: vec![ring::kx_group::X25519], |
| 39 | + signature_verification_algorithms: SIGNATURE_ALGORITHMS, |
| 40 | + secure_random: &VerifyOnly, |
| 41 | + key_provider: &VerifyOnly, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// The signature algorithms rustls' ring provider verifies, in its order, minus |
| 46 | +/// Ed25519: the CA/Browser Forum Baseline Requirements allow only RSA and ECDSA |
| 47 | +/// keys, so no publicly trusted chain or TLS 1.3 server signature can use it. |
| 48 | +static SIGNATURE_ALGORITHMS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms { |
| 49 | + all: &[ |
| 50 | + algs::ECDSA_P256_SHA256, |
| 51 | + algs::ECDSA_P256_SHA384, |
| 52 | + algs::ECDSA_P384_SHA256, |
| 53 | + algs::ECDSA_P384_SHA384, |
| 54 | + algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY, |
| 55 | + algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY, |
| 56 | + algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY, |
| 57 | + algs::RSA_PKCS1_2048_8192_SHA256, |
| 58 | + algs::RSA_PKCS1_2048_8192_SHA384, |
| 59 | + algs::RSA_PKCS1_2048_8192_SHA512, |
| 60 | + algs::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS, |
| 61 | + algs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS, |
| 62 | + algs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS, |
| 63 | + ], |
| 64 | + mapping: &[ |
| 65 | + // TLS 1.3 fixes the curve per scheme; TLS 1.2 does not, hence two entries. |
| 66 | + ( |
| 67 | + SignatureScheme::ECDSA_NISTP384_SHA384, |
| 68 | + &[algs::ECDSA_P384_SHA384, algs::ECDSA_P256_SHA384], |
| 69 | + ), |
| 70 | + ( |
| 71 | + SignatureScheme::ECDSA_NISTP256_SHA256, |
| 72 | + &[algs::ECDSA_P256_SHA256, algs::ECDSA_P384_SHA256], |
| 73 | + ), |
| 74 | + ( |
| 75 | + SignatureScheme::RSA_PSS_SHA512, |
| 76 | + &[algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY], |
| 77 | + ), |
| 78 | + ( |
| 79 | + SignatureScheme::RSA_PSS_SHA384, |
| 80 | + &[algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY], |
| 81 | + ), |
| 82 | + ( |
| 83 | + SignatureScheme::RSA_PSS_SHA256, |
| 84 | + &[algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY], |
| 85 | + ), |
| 86 | + ( |
| 87 | + SignatureScheme::RSA_PKCS1_SHA512, |
| 88 | + &[algs::RSA_PKCS1_2048_8192_SHA512], |
| 89 | + ), |
| 90 | + ( |
| 91 | + SignatureScheme::RSA_PKCS1_SHA384, |
| 92 | + &[algs::RSA_PKCS1_2048_8192_SHA384], |
| 93 | + ), |
| 94 | + ( |
| 95 | + SignatureScheme::RSA_PKCS1_SHA256, |
| 96 | + &[algs::RSA_PKCS1_2048_8192_SHA256], |
| 97 | + ), |
| 98 | + ], |
| 99 | +}; |
| 100 | + |
| 101 | +/// Supplies ring's system randomness and refuses to load private keys. |
| 102 | +#[derive(Debug)] |
| 103 | +struct VerifyOnly; |
| 104 | + |
| 105 | +impl SecureRandom for VerifyOnly { |
| 106 | + fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> { |
| 107 | + use ::ring::rand::SecureRandom as _; |
| 108 | + |
| 109 | + ::ring::rand::SystemRandom::new() |
| 110 | + .fill(buf) |
| 111 | + .map_err(|_| GetRandomFailed) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl KeyProvider for VerifyOnly { |
| 116 | + fn load_private_key( |
| 117 | + &self, |
| 118 | + _key_der: PrivateKeyDer<'static>, |
| 119 | + ) -> Result<Arc<dyn SigningKey>, rustls::Error> { |
| 120 | + Err(rustls::Error::General( |
| 121 | + "the pricing client does not present certificates".to_string(), |
| 122 | + )) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod tests { |
| 128 | + use std::sync::Arc; |
| 129 | + |
| 130 | + use rustls::{ClientConfig, pki_types::PrivateKeyDer}; |
| 131 | + |
| 132 | + use super::provider; |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn provider_builds_a_client_config_but_loads_no_keys() { |
| 136 | + let provider = provider(); |
| 137 | + assert!( |
| 138 | + provider |
| 139 | + .key_provider |
| 140 | + .load_private_key(PrivateKeyDer::Pkcs8(Vec::new().into())) |
| 141 | + .is_err() |
| 142 | + ); |
| 143 | + assert!( |
| 144 | + ClientConfig::builder_with_provider(Arc::new(provider)) |
| 145 | + .with_safe_default_protocol_versions() |
| 146 | + .is_ok(), |
| 147 | + "rustls must accept the provider" |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn provider_cannot_negotiate_tls_1_2() { |
| 153 | + assert!( |
| 154 | + ClientConfig::builder_with_provider(Arc::new(provider())) |
| 155 | + .with_protocol_versions(&[&rustls::version::TLS12]) |
| 156 | + .is_err() |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
0 commit comments