Skip to main content

simoom_tls/
lib.rs

1//! Sans-io TLS 1.3 server and client for simoom, with the QUIC interface (RFC 9001).
2//!
3//! Everything is std-only and written from scratch; the only `unsafe` code (hardware AES,
4//! carry-less multiply, SHA-256, SIMD ChaCha20 and the OS random source) lives in
5//! `simoom_sys::arch` behind safe wrappers. This crate forbids `unsafe`.
6//!
7//! # TLS over a byte stream
8//!
9//! A [`ServerConnection`] or [`ClientConnection`] never touches a socket or a clock. Feed it
10//! received bytes, process them, send what it produces, and exchange plaintext:
11//!
12//! ```
13//! # #[cfg(feature = "client")]
14//! # fn main() -> Result<(), simoom_tls::Error> {
15//! use simoom_tls::{certgen, x509::RootStore, CertifiedKey, ClientConfig, ClientConnection, ServerConfig, ServerConnection};
16//! use std::sync::Arc;
17//!
18//! let cert = certgen::self_signed("localhost");
19//! let server_cfg = Arc::new(ServerConfig::new(CertifiedKey::new(vec![cert.cert_der.clone()], cert.key.clone())?).with_alpn(&[b"h2"]));
20//! let mut roots = RootStore::empty();
21//! roots.add_der(&cert.cert_der).unwrap();
22//! let client_cfg = Arc::new(ClientConfig::new(roots).with_alpn(&[b"h2"]));
23//!
24//! let mut server = ServerConnection::new(server_cfg);
25//! let mut client = ClientConnection::new(client_cfg, "localhost")?;
26//! client.write_plaintext(b"hello")?; // sent as soon as the handshake completes
27//! while client.wants_write() || server.wants_write() {
28//!     let n = server.read_tls(client.tls_output());
29//!     client.consume_tls_output(n);
30//!     server.process_new_packets()?;
31//!     let n = client.read_tls(server.tls_output());
32//!     server.consume_tls_output(n);
33//!     client.process_new_packets()?;
34//! }
35//! let mut buf = [0u8; 16];
36//! let n = server.read_plaintext(&mut buf);
37//! assert_eq!(&buf[..n], b"hello");
38//! assert_eq!(server.alpn_protocol(), Some(&b"h2"[..]));
39//! # Ok(())
40//! # }
41//! # #[cfg(not(feature = "client"))]
42//! # fn main() {}
43//! ```
44//!
45//! # What is supported
46//!
47//! - TLS 1.3 only (RFC 8446): `TLS_AES_128_GCM_SHA256`, `TLS_AES_256_GCM_SHA384`,
48//!   `TLS_CHACHA20_POLY1305_SHA256`; X25519 and secp256r1 key exchange; HelloRetryRequest.
49//! - Certificates: ECDSA P-256 and P-384, Ed25519, RSA (PSS signatures in the handshake,
50//!   PKCS#1 v1.5 and PSS in certificates); chains from PEM or DER; keys in PKCS#8, SEC1 or
51//!   PKCS#1; SNI-based certificate selection; X.509 path validation with hostname checks.
52//! - ALPN, SNI, client certificates ([`ClientAuth`]), session tickets with rotating keys
53//!   ([`Ticketer`]) and PSK-DHE resumption, KeyUpdate, alerts, close_notify, the keying
54//!   material exporter, and [`Connection::export_traffic_secrets`] for kernel TLS.
55//! - 0-RTT is never offered or accepted; rejected early data is skipped as RFC 8446 requires.
56//! - [`quic::QuicConnection`]: handshake messages per encryption level, packet and header
57//!   protection keys, key updates, Retry integrity tags, QUIC v1 and v2.
58//! - [`certgen`]: self-signed and CA-issued certificates for tests.
59//!
60//! - With the `tls12` feature, TLS 1.2 (RFC 5246) next to 1.3 for both sides: the six ECDHE
61//!   AES-GCM and ChaCha20-Poly1305 suites, extended master secret required, tickets and session
62//!   IDs, renegotiation refused, downgrade protection ([`tls::tls12`]). QUIC stays TLS 1.3 only.
63//!
64//! # Cargo features
65//!
66//! All on by default: `client` (the client side; servers can turn it off), `rsa`, `p384`,
67//! `ed25519` (key and certificate types; `ed25519` also speeds up X25519 key generation) and
68//! `chacha20` (the ChaCha20-Poly1305 suite). `--no-default-features` is the smallest server:
69//! X25519, P-256 and AES-GCM.
70//!
71//! Off by default (and in simoom's server builds): `tls12`; `keystore` (PEM bundles, encrypted
72//! PKCS#8, [`keystore::KeyStore`], [`params::SslContextParameters`]), `pkcs12`, `jks`;
73//! `revocation` (CRLs, OCSP, stapling: [`revocation`]); `blocking` (a `Read + Write` stream
74//! adapter: [`blocking`]); `sha1` and `legacy` (the old digests and ciphers those need). See
75//! `docs/parity/tls.md` for how they map to Cameleer's `cameleer-tls`.
76//!
77//! # Hardware paths
78//!
79//! On x86-64 the public-key arithmetic uses MULX/ADX kernels (P-256 field and order, the
80//! X25519 field, RSA's Montgomery multiplication) and Poly1305 an AVX2 kernel, all in
81//! `simoom_sys::arch` with portable fallbacks chosen at run time.
82
83#![forbid(unsafe_code)]
84
85pub mod asn1;
86#[cfg(feature = "blocking")]
87pub mod blocking;
88pub mod certgen;
89pub mod crypto;
90pub mod keys;
91#[cfg(feature = "keystore")]
92pub mod params;
93pub mod pem;
94#[cfg(any(feature = "keystore", feature = "revocation"))]
95pub mod pki;
96pub mod quic;
97pub mod tls;
98pub mod util;
99pub mod x509;
100
101#[cfg(feature = "keystore")]
102pub use pki::keystore;
103#[cfg(feature = "revocation")]
104pub use pki::revocation;
105pub use tls::config::{CertifiedKey, ClientAuth, ServerConfig, SystemClock, Ticketer, TimeSource};
106#[cfg(feature = "client")]
107pub use tls::config::{ClientConfig, SessionCache};
108#[cfg(feature = "client")]
109pub use tls::conn::ClientConnection;
110pub use tls::conn::{Connection, DirectionalSecret, ExportedSecrets, IoState, ServerConnection};
111pub use tls::{alert, CipherSuite, Error, Level, NamedGroup};