use async_trait::async_trait;
use pingora::listeners::TlsAccept;
use pingora_openssl::{pkey::PKey, ssl::NameType, x509::X509};
use tracing::debug;
#[derive(Debug)]
pub struct CertStore {}
impl CertStore {
pub fn new() -> Box<Self> {
Box::new(CertStore {})
}
}
#[async_trait]
impl TlsAccept for CertStore {
async fn certificate_callback(&self, ssl: &mut pingora::tls::ssl::SslRef) {
use pingora::tls::ext;
let host_name = ssl.servername(NameType::HOST_NAME);
if host_name.is_none() {
return;
}
if let Some(host_name) = host_name {
if host_name == "localhost" {
let cert_bytes = std::fs::read("./fixtures/localhost.crt").unwrap();
let cert = X509::from_pem(&cert_bytes).unwrap();
let key_bytes = std::fs::read("./fixtures/localhost.key").unwrap();
let key = PKey::private_key_from_pem(&key_bytes).unwrap();
ext::ssl_use_certificate(ssl, &cert).unwrap();
ext::ssl_use_private_key(ssl, &key).unwrap();
return;
}
}
let cert_file = format!("./data/certificates/{}.crt", host_name.unwrap());
let key_file = format!("./data/certificates/{}.key", host_name.unwrap());
debug!("host {}", cert_file);
let cert_exists = std::fs::metadata(&cert_file);
let key_exists = std::fs::metadata(&key_file);
if cert_exists.is_err() || key_exists.is_err() {
debug!("nothing there");
return;
}
let cert_bytes = std::fs::read(cert_file).unwrap();
let cert = X509::from_pem(&cert_bytes).unwrap();
let key_bytes = std::fs::read(key_file).unwrap();
let key = PKey::private_key_from_pem(&key_bytes).unwrap();
ext::ssl_use_certificate(ssl, &cert).unwrap();
ext::ssl_use_private_key(ssl, &key).unwrap();
}
}