Transport Layer Security in pure OCaml
README.md

ocaml-tls #

Warning. This is a work-in-progress experimental fork of ocaml-tls. Do not use it in production; use ocaml-tls instead.

Transport Layer Security (TLS) protects most traffic on the Internet: HTTPS (RFC 2818), mail, VPNs and wireless networks all rely on it to stop eavesdropping, tampering and forgery, and to authenticate one or both endpoints with X.509 certificates. The widely deployed implementations are written in C, where a parsing bug in the handshake becomes a memory-safety bug. This library implements TLS 1.2 and 1.3 in OCaml; TLS 1.0 and 1.1 are absent (RFC 8996).

TLS negotiates its algorithms: the two endpoints agree on the protocol version, the key exchange, the symmetric cipher and the hash. Every key exchange is ephemeral; the library ships no 3DES, static RSA key transport or export suite, no MD5 or SHA-1 signature scheme (RFC 9155), and no DH group or RSA key under 2048 bits (RFC 9325 section 4). It requires secure renegotiation (RFC 5746) and supports the extended master secret (RFC 7627). Secure renegotiation binds a renegotiated handshake to the Finished messages of the one before it, which stops an attacker from splicing its own prefix onto a victim's connection (CVE-2009-3555). The extended master secret derives the master secret from a hash of the whole handshake, so two connections with different peers cannot end with the same keys (the triple-handshake attack). The design and its evaluation are described in the USENIX Security 2015 paper by David Kaloper-Meršinjak, Hannes Mehnert, Anil Madhavapeddy and Peter Sewell.

Installation #

Install with opam:

$ opam install nox-tls

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-tls

To build from a checkout instead:

$ opam install --deps-only -t .
$ dune build --profile=release

Usage #

The project ships two opam packages:

  • nox-tls (library Tls) is the protocol core. It performs no I/O: Tls.Engine takes the current state and the bytes read, and returns the next state, the bytes to send and the data for the application. Its one piece of shared state is the randomness it draws from the process-wide default generator of nox-crypto-rng, which the application installs before the first handshake. It builds with no scheduler, so it composes with other protocols such as SMTP's STARTTLS and runs inside MirageOS unikernels.
  • nox-tls-eio (libraries Tls_eio and X509_eio) wraps an Eio flow into a TLS flow with Tls_eio.client_of_flow and Tls_eio.server_of_flow, and loads certificates, private keys and trust anchors from Eio paths.

Tls.Config.client and Tls.Config.server build the configuration: the authenticator, the protocol version range, the cipher suites, the certificates and the session ticket cache. The authenticator is the function that decides whether the peer's certificate chain is acceptable, by validating it against trust anchors or comparing a pinned fingerprint. A session ticket is an encrypted blob the server gives the client so that a later connection resumes without a full handshake (RFC 8446 section 4.6.1).

A server staples an OCSP response (RFC 6960) to its certificate for a client that asks with status_request (RFC 6066 section 8; in TLS 1.3, RFC 8446 section 4.4.2.1), so the client learns the certificate's revocation status without reaching the responder itself. The response is the ocsp field of the Tls.Config.certchain it is about, X509_eio.ocsp_of_der reads one from a file, and a client asks with ~ocsp_stapling:true and finds the bytes in peer_ocsp_response of its epoch. The library does not verify the response: its signature, freshness and the status it gives are the caller's to check, as with Go's crypto/tls, rustls and BoringSSL.

Tls.Quic drives the TLS 1.3 handshake for QUIC (RFC 9001). QUIC (RFC 9000) is a transport protocol over UDP, used by HTTP/3, that provides reliable multiplexed streams and takes its keys from a TLS 1.3 handshake whose messages it carries in its own CRYPTO frames rather than in TLS records.

Credits #

This is a fork of mirleft/ocaml-tls by David Kaloper-Meršinjak, Hannes Mehnert and its contributors, under the BSD 2-clause licence. The lwt, miou, MirageOS and async adapters of upstream are not carried here; the Eio adapter is. The QUIC hooks are ported from anmonteiro/ocaml-quic.