$ show aws/s2n-tls
s2n-tls is a C99 implementation of the TLS/SSL protocols designed by AWS for simplicity, small size, and security. It supports TLS 1.2 and 1.3, including features like 0-RTT early data, session tickets, post-quantum KEMs (ML-KEM, ML-DSA), and QUIC transport integration. The library provides a public C API (api/s2n.h) and multiple libcrypto backends (OpenSSL, AWS-LC, BoringSSL, LibreSSL). Extensive formal verification (CBMC, SAW, SideTrail) and fuzz testing ensure memory safety and constant-time execution. The architecture is highly modular with a stuffer buffer abstraction, blob/array utilities, a table-driven TLS extension framework, and a robust error-handling macro system. Rust bindings are first-class, offering safe wrappers (s2n-tls), async Tokio integration (s2n-tls-tokio), an HTTP client connector for Hyper (s2n-tls-hyper), and a metrics subscriber for handshake analysis. The library is used by AWS services like S3 and KMS, and includes interoperability tests against OpenSSL, GnuTLS, Java, and rustls. It is actively maintained with a sophisticated CI pipeline covering unit, integration, fuzz, formal proof, and performance regression tests across multiple platforms and libcrypto versions.
The LLM identified 5 files worth highlighting for reuse:
The s2n_stuffer provides a growable, read-write buffer with cursor management, base64/hex encoding, network-order integer I/O, reservations, and vector-sized writes. It is the core serialization primitive used throughout the TLS handshake and record layer. The implementation has extensive CBMC formal proofs for memory safety and overflow prevention, making it an example of a high-assurance buffer utility that could be extracted for any protocol processing library.
Defines the `s2n_result` type and accompanying `POSIX_GUARD`/`RESULT_GUARD`/`DEFER_CLEANUP` macros that provide consistent error propagation, resource cleanup, and compiler warnings for unhandled errors. The pattern forces explicit handling of failures, avoids goto-based cleanup, and integrates with constant-time blinding. This error-handling pattern is idiomatic C for safety-critical systems and could be reused in any C project requiring robust error management.
Implements a generic TLS extension framework where each extension defines a struct with `send`, `recv`, `should_send`, `if_missing` function pointers. The framework handles version validation, response matching, and automatic wiring. This table-driven pattern decouples extension logic from handshake code, making it easy to add new extensions (e.g., ALPN, SNI, key_share) without modifying core flow. The pattern is directly reusable for any protocol that supports optional extensions.
Implements a Rust builder pattern for constructing `s2n-tls` connections with type-safe configuration (e.g., `Config::builder().with_security_policy(...).build()`). It provides a `ModifiedBuilder` pattern that allows chaining modifications before finalizing. This abstraction simplifies the C configuration API and prevents misuse of unsafe code. The builder pattern is cleanly separated and could be reused as a template for wrapping complex C APIs in Rust.
Implements `AggregatedMetricsSubscriber` that collects TLS handshake events (cipher suite, group, version, alerts) and aggregates them into a compact `MetricRecord` that can be serialized (EMF or serde). The subscriber pattern decouples TLS event sources from telemetry sinks, and the aggregation logic is generic enough to be applied to any event stream that needs bucketed counting. The `TelemetrySink` trait enables pluggable exporters.