- OCaml 96%
- C 3%
- Raku <1%
ca-certs #
Detect root CA certificates from the operating system.
Warning. This is an experimental fork of ca-certs. Do not use it in production; use ca-certs instead.
Overview #
A TLS client authenticates a server against a set of root certificates,
the trust anchors, and every operating system keeps them somewhere
different: a PEM bundle under /etc on Linux and the BSDs, the
Security.framework trust settings on macOS, the certificate store on
Windows. (PEM is the text form of a certificate: its DER bytes in base64
between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines,
RFC 7468; a bundle is a file of such blocks one after the other.)
nox-ca-certs finds the store of the machine it runs on and
returns its certificates, ready for
ocaml-tls or any other X.509
validation.
A store that cannot be read is returned as a value of Ca_certs_error.t
rather than raised. The error names the store (a path, or the platform
store's own name) and what the system said, so a caller can print advice
of its own without parsing a message.
This repository also ships nox-ca-certs-nss, a fork of
ca-certs-nss, for targets with no
operating system to ask. It is described below.
Installation #
Install with opam:
$ opam install nox-ca-certs
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-ca-certs
Usage #
This builds a TLS client configuration from the operating system's trust store:
let () =
match Ca_certs.authenticator () with
| Ok authenticator ->
(match Tls.Config.client ~authenticator () with
| Ok tls_config -> ignore tls_config (* use for TLS connections *)
| Error (`Msg msg) ->
Format.eprintf "TLS config failed: %s\n" msg)
| Error e ->
Format.eprintf "CA detection failed: %a\n" Ca_certs_error.pp e
API #
Ca_certs.authenticator ?crls ?allowed_hashes ()detects the root CAs and builds anX509.Authenticator.tthat validates against the current system time. It can be passed directly toTls.Config.client.Ca_certs.trust_anchors ()is the PEM-encoded root certificates of the operating system's trust store, as one string.Ca_certs_error.tsays why a store could not be read:Unreadable(the store is there and the system refused it),AbsentorUndecodable.
Three environment variables change where the certificates come from:
SSL_CERT_FILEoverrides the path of the system trust anchors on Unix.NIX_SSL_CERT_FILEis read whenSSL_CERT_FILEis not set, as on NixOS.OCAML_EXTRA_CA_CERTSnames a PEM file whose certificates are added to the system's.
The platform is the one the library was compiled for, and finding the store
runs no program. A program that ships its own CA bundle, in an image that
holds nothing but itself, installs the bundle at
/etc/ssl/certs/ca-certificates.crt or names it in SSL_CERT_FILE.
Supported platforms #
- Linux, the BSDs and every other Unix: the library reads the first PEM
bundle that exists among
/etc/ssl/certs/ca-certificates.crt(Debian and its derivatives),/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem(Fedora, RHEL),/etc/ssl/ca-bundle.pem(openSUSE),/etc/ssl/cert.pem(OpenBSD, Alpine, FreeBSD) and/usr/local/share/certs/ca-root-nss.crt(theca_root_nsspackage of FreeBSD and DragonFly). - macOS: it reads the system's anchors from the Security.framework and
the trust settings of the user and admin domains, and leaves out
certificates the user or administrator distrusts. A user or admin domain
the process cannot reach (a sandbox that denies it the per-user
trustdagent) is left out with a warning; system anchors it cannot read are an error. - Windows: it reads the Windows certificate store through the system APIs.
Embedded NSS anchors #
A unikernel is one program linked with only the operating-system libraries it
needs into a single image that runs directly on a hypervisor, with no
general-purpose kernel, no processes and no file system under it. Xen is such a
hypervisor, and Solo5 is a minimal interface (a handful of calls for the
console, the clock, the network and block devices) through which the image talks
to its host. A freestanding unikernel on Solo5 or Xen therefore has no trust
store and no file system, and nox-ca-certs does not cross-build to those
targets. nox-ca-certs-nss embeds the Mozilla NSS trust anchors at build time
instead, so it needs no unix and no file system. Its authenticator
takes the validation clock as a ~time argument, because a freestanding target
has no operating system clock to read.
NSS keeps its roots in certdata.txt, with one trust record per root and per
purpose. The attribute CKA_TRUST_SERVER_AUTH is the purpose "authenticate a
TLS server", and its value CKT_NSS_TRUSTED_DELEGATOR means that Mozilla trusts
the root to issue certificates for that purpose. The embedded anchors are
exactly the roots with that value. A root that NSS keeps only for S/MIME or code
signing is left out, which is the selection curl's mk-ca-bundle.pl and
FreeBSD's MAca-bundle.pl make as well. They come from NSS 3.128 (changeset
ab9a6ee2a83c6a8ae2d498fc97eb320f69653ba8, the same list as NSS 3.126).
To move to a newer NSS release, copy that release's
lib/ckfw/builtins/certdata.txt from
https://hg.mozilla.org/projects/nss/tags over nss-gen/certdata.txt,
set nss_release in nss-gen/extract_from_certdata.ml to match, then
regenerate and format:
$ REGEN=1 dune build @lib/nss/certdata
$ dune build @fmt --auto-promote
The first command promotes the regenerated table over
lib/nss/trust_anchor.ml and the second formats it. Review the diff of
the anchors, and update the counts that the nox-ca-certs-nss tests
check.
Licence #
ISC. See LICENSE.md.