|
1
|
use crate::{generate_certificate::generate_certificate, handle_session::handle_session}; |
|
2
|
use native_tls::{Identity, TlsAcceptor}; |
|
3
|
use tokio::net::TcpStream; |
|
4
|
use tokio_native_tls::TlsAcceptor as TokioTlsAcceptor; |
|
5
|
|
|
6
|
pub async fn handle_starttls(stream: &mut TcpStream) -> anyhow::Result<()> { |
|
7
|
// ideally the certificate should only be loaded from here and not generated each time |
|
8
|
let (pem_certificate, pem_private_key) = generate_certificate()?; |
|
9
|
let identity = Identity::from_pkcs8(pem_certificate.as_bytes(), pem_private_key.as_bytes())?; |
|
10
|
let tls_acceptor = TlsAcceptor::builder(identity).build()?; |
|
11
|
let tls_acceptor = TokioTlsAcceptor::from(tls_acceptor); |
|
12
|
|
|
13
|
match tls_acceptor.accept(stream).await { |
|
14
|
Ok(tls_stream) => { |
|
15
|
// we can now handle the normal SMTP session |
|
16
|
handle_session(tls_stream).await?; |
|
17
|
} |
|
18
|
Err(e) => { |
|
19
|
tracing::error!("Error establishing SMTP TLS connection: {:?}", e); |
|
20
|
} |
|
21
|
}; |
|
22
|
Ok(()) |
|
23
|
} |