Skip to content

Commit

Permalink
feat: accept self-signed nauta.cu certificates
Browse files Browse the repository at this point in the history
This will allow users to enable strict TLS checks.
  • Loading branch information
link2xt committed Sep 4, 2024
1 parent 418dfbf commit 365afd9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Binary file added assets/certificates/imap.nauta.cu.der
Binary file not shown.
Binary file added assets/certificates/smtp.nauta.cu.der
Binary file not shown.
29 changes: 25 additions & 4 deletions src/net/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ static LETSENCRYPT_ROOT: Lazy<Certificate> = Lazy::new(|| {
.unwrap()
});

pub fn build_tls(strict_tls: bool, alpns: &[&str]) -> TlsConnector {
static IMAP_NAUTA_CU: Lazy<Certificate> = Lazy::new(|| {
Certificate::from_der(include_bytes!(
"../../assets/certificates/imap.nauta.cu.der"
))
.unwrap()
});

static SMTP_NAUTA_CU: Lazy<Certificate> = Lazy::new(|| {
Certificate::from_der(include_bytes!(
"../../assets/certificates/smtp.nauta.cu.der"
))
.unwrap()
});

fn build_tls(strict_tls: bool, hostname: &str, alpns: &[&str]) -> TlsConnector {
let tls_builder = TlsConnector::new()
.min_protocol_version(Some(Protocol::Tlsv12))
.request_alpns(alpns)
.add_root_certificate(LETSENCRYPT_ROOT.clone());

// Add self-signed certificates for known hostnames.
let tls_builder = match hostname {
"imap.nauta.cu" => tls_builder.add_root_certificate(IMAP_NAUTA_CU.clone()),
"smtp.nauta.cu" => tls_builder.add_root_certificate(SMTP_NAUTA_CU.clone()),
_ => tls_builder,
};

if strict_tls {
tls_builder
} else {
Expand All @@ -35,7 +56,7 @@ pub async fn wrap_tls<T: AsyncRead + AsyncWrite + Unpin>(
alpn: &[&str],
stream: T,
) -> Result<TlsStream<T>> {
let tls = build_tls(strict_tls, alpn);
let tls = build_tls(strict_tls, hostname, alpn);
let tls_stream = tls.connect(hostname, stream).await?;
Ok(tls_stream)
}
Expand All @@ -48,7 +69,7 @@ mod tests {
fn test_build_tls() {
// we are using some additional root certificates.
// make sure, they do not break construction of TlsConnector
let _ = build_tls(true, &[]);
let _ = build_tls(false, &[]);
let _ = build_tls(true, "example.org", &[]);
let _ = build_tls(false, "example.org", &[]);
}
}

0 comments on commit 365afd9

Please sign in to comment.