Categorygithub.com/gwatts/rootcerts
modulepackage
0.0.0-20250301185136-8180986b1a75
Repository: https://github.com/gwatts/rootcerts.git
Documentation: pkg.go.dev

# README

Root CA Certificates for Go

GoDoc Build Status

This project converts root certificate authority certificates provided by the Mozilla project into a .go package that can be statically compiled into a Go project.

Motivation

Many Go programs need to access remote SSL/TLS services (eg. over https). The crypto/tls package validates remote certificates by loading public CA certificates from the filesystem as provided by the operating system (eg. in /etc/ssl).

This works well, unless the program is running in any environment where these certificates are not installed, are not accessible, or are out of date. This commonly occurs when running Go programs in a minimal docker container or the binary is distributed as-is to end users who may or may not be running the program on a machine with up to date certificates installed/accessible.

Compiling the root certificates into the program provides guaranteed stable access to them without external dependencies.

Alternatives

Instead of compiling the certificates into the binary, one could:

  • Install packages at the OS level and mount /etc/ssl as a volume for the container
  • Add a certificates layer to the image when defining the Dockerfile

Compiling the certificates may, however, be simpler to deploy in many cases and result in a predictable outcome whether the binary is executed in a container or on a host.

Usage

You may either use the certificates provided at the root level of this project, which are periodically refreshed, or use the gencerts tool to create a rootcerts.go file that may be copied into your project.

Calling the UpdateDefaultTransport method will make the certificates available to the default http transport, which is sufficient for many projects.

Using gencerts

The gencerts tool reads a certdata.txt file, either from the local filesystem, or directly from the Mozilla Mercurial site (though note, it uses https by default so does itself require local ca certificates!)

Note also that the format of certdata.txt changes occasionally, which may break the gencerts tool. Relying on -download for a production build process may thus be a bad idea!

gencerts -download -package mypackage -target rootcerts.go

gencerts will generate a rootcerts.go and also a rootcerts_16.go if there are any certificate with a negative serial number. Only Go version 1.6 and later supports such certificates, so rootcerts_16.go uses a build flag to ensure compatibility with older versions of Go.

Other Notes

gencerts only outputs certificates that the certdata.txt file has labeled as a trusted delegator (ie. certificates that are suitable for use a certificate authority).

Certificates may be marked as trusted for servers, email or code signing.

Useful Resources

Some of the information I came across while writing this tool:

Similar Projects

# Packages

Package certparse parses root CA certificates from a Mozilla NSS certdata.txt io.Reader.
Command gencerts converts root CA certificates from the Mozilla NSS project to a .go file.

# Functions

Certs returns all trusted certificates extracted from certdata.txt.
CertsByTrust returns only those certificates that match all bits of the specified TrustLevel.
ServerCertPool returns a pool containing all root CA certificates that are trusted for issuing server certificates.
UpdateDefaultTransport updates the configuration for http.DefaultTransport to use the root CA certificates defined here when used as an HTTP client.

# Constants

Trusted for issuing code signing certificates.
Trusted for issuing email certificates.
Trusted for issuing server certificates.

# Structs

A Cert defines a single unparsed certificate.

# Type aliases

TrustLevel defines for which purposes the certificate is trusted to issue certificates (ie.