use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct TlsConfig {
pub cert_path: String,
pub key_path: String,
}
impl TlsConfig {
pub fn new(cert_path: impl Into<String>, key_path: impl Into<String>) -> Self {
Self {
cert_path: cert_path.into(),
key_path: key_path.into(),
}
}
pub fn load_server_config(&self) -> Result<Arc<rustls::ServerConfig>, TlsError> {
use rustls_pemfile::{certs, private_key};
use std::io::BufReader;
let cert_file = std::fs::File::open(&self.cert_path)
.map_err(|e| TlsError::CertificateRead(self.cert_path.clone(), e))?;
let mut cert_reader = BufReader::new(cert_file);
let cert_chain: Vec<rustls::pki_types::CertificateDer> = certs(&mut cert_reader)
.collect::<Result<Vec<_>, _>>()
.map_err(TlsError::CertificateParse)?;
if cert_chain.is_empty() {
return Err(TlsError::NoCertificates);
}
let key_file = std::fs::File::open(&self.key_path)
.map_err(|e| TlsError::KeyRead(self.key_path.clone(), e))?;
let mut key_reader = BufReader::new(key_file);
let key = private_key(&mut key_reader)
.map_err(TlsError::KeyParse)?
.ok_or(TlsError::NoPrivateKey)?;
let config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(cert_chain, key)
.map_err(TlsError::InvalidConfig)?;
Ok(Arc::new(config))
}
}
#[derive(Debug, thiserror::Error)]
pub enum TlsError {
#[error("Failed to read certificate file '{0}': {1}")]
CertificateRead(String, std::io::Error),
#[error("Failed to parse certificates: {0}")]
CertificateParse(std::io::Error),
#[error("No certificates found in certificate file")]
NoCertificates,
#[error("Failed to read private key file '{0}': {1}")]
KeyRead(String, std::io::Error),
#[error("Failed to parse private key: {0}")]
KeyParse(std::io::Error),
#[error("No private key found in key file")]
NoPrivateKey,
#[error("Invalid TLS configuration: {0}")]
InvalidConfig(rustls::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tls_config_new() {
let tls = TlsConfig::new("/path/to/cert.pem", "/path/to/key.pem");
assert_eq!(tls.cert_path, "/path/to/cert.pem");
assert_eq!(tls.key_path, "/path/to/key.pem");
}
#[test]
fn test_tls_error_display() {
let err = TlsError::NoCertificates;
assert!(err.to_string().contains("No certificates"));
let err = TlsError::NoPrivateKey;
assert!(err.to_string().contains("No private key"));
}
}