1use crate::config::{app_dir, config_error_exit, error_exit};
2use rustls::{Certificate, PrivateKey, ServerConfig};
3use rustls_pemfile::{certs, pkcs8_private_keys};
4use std::{fs::File, io::BufReader};
5
6pub fn load_rustls_config(tls_cert: &str, tls_key: &str) -> rustls::ServerConfig {
22 let config = ServerConfig::builder()
24 .with_safe_defaults()
25 .with_no_client_auth();
26
27 let cert_file = &mut BufReader::new(File::open(app_dir(tls_cert)).unwrap_or_else(error_exit));
29 let key_file = &mut BufReader::new(File::open(app_dir(tls_key)).unwrap_or_else(error_exit));
30
31 let cert_chain = certs(cert_file)
33 .unwrap_or_else(error_exit)
34 .into_iter()
35 .map(Certificate)
36 .collect();
37 let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)
38 .unwrap_or_else(error_exit)
39 .into_iter()
40 .map(PrivateKey)
41 .collect();
42
43 if keys.is_empty() {
45 config_error_exit("Could not locate PKCS 8 private keys.");
46 }
47
48 config
49 .with_single_cert(cert_chain, keys.remove(0))
50 .unwrap_or_else(error_exit)
51}