Skip to main content

base_endpoint/tls/
rustls.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4//! Rustls client/server providers over Base scheduler stream sockets. Enable the `rustls` feature, supply an `Arc<ClientConfig>` or `Arc<ServerConfig>`, and compose the provider with the generic TLS endpoint adapters.
5//!
6//! Connections use explicit scheduler capabilities for sequential application I/O. `write` buffers accepted plaintext; `flush`, `read`, or `shutdown` drives pending TLS output. No runtime or background task is created. `RustlsConnection::with_duplex` provides independent plaintext directions and supervises both network lanes without spawning a task.
7
8mod rustls_acceptor;
9mod rustls_connection;
10mod rustls_connector;
11
12pub use rustls_acceptor::RustlsAcceptor;
13pub use rustls_connection::{
14    EncryptedSocketStreamReader, EncryptedSocketStreamWriter, RustlsConnection,
15};
16pub use rustls_connector::RustlsConnector;
17
18use base_scheduler::{
19    Cancellation, OperationResult, SocketClose, SocketStreamReading, SocketStreamWriting,
20};
21use std::io;
22
23fn tls_error(error: ::rustls::Error) -> io::Error {
24    io::Error::new(io::ErrorKind::InvalidData, error)
25}
26
27async fn establish<S>(
28    scheduler: &S,
29    socket: S::Socket,
30    state: OperationResult<::rustls::Connection>,
31    cancellation: &Cancellation,
32) -> OperationResult<RustlsConnection<S::Socket>>
33where
34    S: SocketStreamReading + SocketStreamWriting + SocketClose + ?Sized,
35    S::Socket: 'static,
36{
37    let state = match state {
38        Ok(state) => state,
39        Err(error) => {
40            let _ = scheduler.socket_close(socket).await;
41            return Err(error);
42        }
43    };
44    let mut connection = RustlsConnection::new(socket, state);
45    match connection.handshake(scheduler, cancellation).await {
46        Ok(()) => Ok(connection),
47        Err(error) => {
48            let _ = connection.abort(scheduler).await;
49            Err(error)
50        }
51    }
52}