pub use rustls;
pub use rustls_pki_types as pki_types;
use std::sync::Arc;
pub fn crypto_provider() -> rustls::crypto::CryptoProvider {
oxitls_rustcrypto_provider::provider()
}
pub fn client_config(alpn: &[&[u8]]) -> Result<rustls::ClientConfig, rustls::Error> {
client_config_with_roots(alpn, &[])
}
pub fn client_config_with_roots(
alpn: &[&[u8]],
extra_roots: &[pki_types::CertificateDer<'static>],
) -> Result<rustls::ClientConfig, rustls::Error> {
let mut roots = rustls::RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
for cert in extra_roots {
roots.add(cert.clone())?;
}
let mut config = rustls::ClientConfig::builder_with_provider(Arc::new(crypto_provider()))
.with_safe_default_protocol_versions()?
.with_root_certificates(roots)
.with_no_client_auth();
config.alpn_protocols = alpn.iter().map(|p| p.to_vec()).collect();
Ok(config)
}
pub fn server_config(
certs: Vec<pki_types::CertificateDer<'static>>,
key: pki_types::PrivateKeyDer<'static>,
alpn: &[&[u8]],
) -> Result<rustls::ServerConfig, rustls::Error> {
let mut config = rustls::ServerConfig::builder_with_provider(Arc::new(crypto_provider()))
.with_safe_default_protocol_versions()?
.with_no_client_auth()
.with_single_cert(certs, key)?;
config.alpn_protocols = alpn.iter().map(|p| p.to_vec()).collect();
Ok(config)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn client_config_offers_the_alpn_it_was_given() {
let c = client_config(&[crate::ALPN]).expect("config");
assert_eq!(c.alpn_protocols, vec![b"ntske/1".to_vec()]);
}
#[test]
fn provider_supplies_tls13_suites() {
let p = crypto_provider();
assert!(
p.cipher_suites
.iter()
.any(|s| s.version().version == rustls::ProtocolVersion::TLSv1_3),
"provider has no TLS 1.3 suites"
);
}
}