CFSSL

repository·master·Indexed 27 days ago

https://github.com/cloudflare/cfssl

Cloudflare's PKI/TLS toolkit for managing Public Key Infrastructure and Transport Layer Security. It includes a command-line interface for signing certificates, generating keys, and building bundles, as well as an API server for certificate management. CFSSL supports database-backed functionality for certificate revocation and OCSP responses using sqlite3, postgres, and mysql drivers.

Tokens
12.6K
Snippets
29
Records
85
Agent score
89%

What's inside cfssl

  1. Use the whitelist package for IP access control

    master

    The whitelist package provides mechanisms to permit or deny access based on IP addresses. The core interface is the ACL type, which features a Permitted(net.IP) bool method to check if an address is whitelisted.

    There are two primary specialized types:

    • HostACL: Manages individual IP addresses using Add(net.IP) and Remove(net.IP).
    • NetACL: Manages network ranges using Add(*net.IPNet) and Remove(*net.IPNet).

    Available implementations:

    • Basic: A host-based whitelister using a map[string]bool and sync.Mutex.
    • BasicNet: A network-based whitelister. Note that BasicNet requires exact network matches for Add and Remove; it does not detect subsets or supersets (e.g., removing a subnet will not remove a larger network that contains it).
    • HostStub and NetStub: Stand-in implementations that always permit access and log warnings.
  2. Enable database functionality in CFSSL

    master

    Providing a database configuration enables advanced certificate management features.

    Enhanced Command Behavior:

    • sign and gencert: Automatically add a signed certificate to the certdb.
    • serve: Enables database-backed functionality for the sign and revoke endpoints.

    Required Database Commands:

    • revoke: Marks certificates as revoked in the database with an optional reason.
    • ocsprefresh: Refreshes the table of cached OCSP responses.
    • ocspdump: Outputs cached OCSP responses in a concatenated base64-encoded format.
  3. Run the maserver

    master

    The maserver is a mutually authenticated server. It requires a server.json configuration file in its directory. You can specify a custom configuration file using the -f flag. Use the -a flag to specify the address and port to listen on.

    cd maserver/
    go run server.go -a 127.0.0.1:9876
  4. Install CFSSL

    master

    You can install CFSSL by building from source or using prebuilt binaries.

    Building from source: Requires Go 1.20+. Note that on some Linux distributions (like RHEL-based ones), you should install Go manually from the official website rather than using the distribution's repository to ensure all necessary algorithms are available.

    Using Go install: This command installs all utility programs including cfssl, cfssljson, and mkbundle.

    Prebuilt binaries: Available on the GitHub releases page.

    # Build from source
    git clone git@github.com:cloudflare/cfssl.git
    cd cfssl
    make
    make install
    
    # Or install via go
    go install github.com/cloudflare/cfssl/cmd/...@latest
  5. Manage certdb migrations using goose

    master

    CFSSL uses goose for database migrations. You can use the goose CLI to apply or revert migrations for MySQL, PostgreSQL, or SQLite backends.

    Note: This assumes the target databases are already created and access control is configured.

    # Install goose
    goose -path certdb/mysql up
    
    # MySQL migrations
    goose -path certdb/mysql up
    goose -path certdb/mysql down
    
    # PostgreSQL migrations
    goose -path certdb/pg up
    goose -path certdb/pg down
    
    # SQLite migrations
    goose -path certdb/sqlite up
    goose -path certdb/sqlite down
  6. Run Transport Examples with Authentication

    master
    To use the authenticated versions of the examples, use the provided _auth.json configuration files for the CA, server, and client. This requires passing the specific auth configuration files via the -config flag for CFSSL and the -f flag for the Go applications.
  7. Compile changes to static files using esc

    master

    If you are developing the CFSSL server and need to update the embedded static files, use the esc tool to compile the static directory into a Go file. This allows the server to serve updated static assets without manual re-embedding.

    go install github.com/mjibson/esc
    
    # Compile changes to static files 
    esc -pkg serve -prefix cli/serve/static cli/serve/static > cli/serve/static.go
  8. Set up the Transport Package Example environment

    master
    To run the maserver and maclient examples, you first need a running CFSSL instance acting as a Certificate Authority (CA). Use the genca.sh script to generate the necessary CA keys and certificates, then start the CFSSL server using the provided config.json.
  9. Configure CFSSL database connection

    master

    Several CFSSL commands accept a -db-config flag. This flag requires a path to a JSON file containing a dictionary that specifies the database driver and the data source connection string.

    Supported drivers include sqlite3, postgres, and mysql.

  10. Configure Mutual TLS (mTLS) for the CFSSL server

    master
    To enable mutual TLS, provide a TLS certificate and key using -tls-cert and -tls-key. Additionally, provide a CA file via -mutual-tls-ca to verify client certificates. You can optionally restrict access to specific clients by providing a regex for the client's Common Name (CN) using -mutual-tls-cn.