mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
feat: accept self-signed nauta.cu certificates
This will allow users to enable strict TLS checks.
This commit is contained in:
BIN
assets/certificates/imap.nauta.cu.der
Normal file
BIN
assets/certificates/imap.nauta.cu.der
Normal file
Binary file not shown.
BIN
assets/certificates/smtp.nauta.cu.der
Normal file
BIN
assets/certificates/smtp.nauta.cu.der
Normal file
Binary file not shown.
@@ -14,12 +14,33 @@ static LETSENCRYPT_ROOT: Lazy<Certificate> = Lazy::new(|| {
|
|||||||
.unwrap()
|
.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()
|
let tls_builder = TlsConnector::new()
|
||||||
.min_protocol_version(Some(Protocol::Tlsv12))
|
.min_protocol_version(Some(Protocol::Tlsv12))
|
||||||
.request_alpns(alpns)
|
.request_alpns(alpns)
|
||||||
.add_root_certificate(LETSENCRYPT_ROOT.clone());
|
.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 {
|
if strict_tls {
|
||||||
tls_builder
|
tls_builder
|
||||||
} else {
|
} else {
|
||||||
@@ -35,7 +56,7 @@ pub async fn wrap_tls<T: AsyncRead + AsyncWrite + Unpin>(
|
|||||||
alpn: &[&str],
|
alpn: &[&str],
|
||||||
stream: T,
|
stream: T,
|
||||||
) -> Result<TlsStream<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?;
|
let tls_stream = tls.connect(hostname, stream).await?;
|
||||||
Ok(tls_stream)
|
Ok(tls_stream)
|
||||||
}
|
}
|
||||||
@@ -48,7 +69,7 @@ mod tests {
|
|||||||
fn test_build_tls() {
|
fn test_build_tls() {
|
||||||
// we are using some additional root certificates.
|
// we are using some additional root certificates.
|
||||||
// make sure, they do not break construction of TlsConnector
|
// make sure, they do not break construction of TlsConnector
|
||||||
let _ = build_tls(true, &[]);
|
let _ = build_tls(true, "example.org", &[]);
|
||||||
let _ = build_tls(false, &[]);
|
let _ = build_tls(false, "example.org", &[]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user