C Collections

chttp · Section 3

CHTTP_TLS_DEFAULT

CHTTP_TLS_DEFAULT - the initializer for the default TLS configuration

C Collections (libccollections, -lccollections)

#include <ccollections/chttp.h>
 
CHTTP_TLS_DEFAULT

This macro expands to an initializer for a chttp_tls_config with:
  insecure_skip_verify         = false (verify the certificate chain)
  insecure_skip_hostname_check = false (match the hostname)
  ca_bundle_path               = NULL  (use the system CA bundle)
  cert_path                    = NULL  (no client certificate)
  key_path                     = NULL  (no client key)
  client_cert_optional         = false (a server with ca_bundle_path
                                        requires a client certificate)

Every field of the default is zero, so this macro and a zero-initialized struct name the same settings. A chttp_tls_config built with = {0}, or with a designated initializer that names only some of its fields, verifies the server's chain and matches its hostname on a client. On a server, the same struct with a ca_bundle_path requires every client to present a certificate that verifies against that bundle. Writing less than you meant to never gives you a weaker setting: You have to assign a field to weaken a check, and the name of that field says what you give up.

insecure_skip_verify accepts any certificate from any peer, so an attacker who can intercept the connection can read and rewrite every byte of it. It also makes insecure_skip_hostname_check irrelevant, because a hostname matched against a certificate that nothing validated proves nothing. You cannot ask for the hostname check without the chain check; the configuration has no way to express that combination.

insecure_skip_hostname_check is the narrower relaxation. It keeps the full chain verification and drops only the match of the hostname against the subject of the certificate. Use it when you reach a host by an address or an internal name that its certificate does not carry, and you want the certificate to chain to a CA that you pinned with ca_bundle_path.

client_cert_optional is a server setting, and a client ignores it. It turns the requirement of a client certificate into a request, so the server refuses a certificate that does not verify but also serves a client that presents none. A handler can then call chttpsvr_req_peer_cert_verified(3) to find out which kind of client it serves.

cert_path, key_path and ca_bundle_path must each name a regular file of at most 16 MB. A path that names a directory, a FIFO or a device is refused at once as a load failure, before a byte of it is read.

A ca_bundle_path holds one or more CA certificates in PEM form and may hold certificate revocation lists (CRLs) beside them. A file that holds only CRLs names no CA and is refused. When the bundle holds a CRL, revocation is checked for the whole chain that the peer presents, as the ssl_crl directive of nginx does: Every certificate of that chain below its trust anchor needs a CRL of its issuer in the bundle. The handshake fails for a revoked certificate, for a certificate whose issuer has no CRL in the bundle, and for a CRL past its next update time, because a chain that cannot be checked is not accepted. For a chain of a root and an intermediate CA, an operator who enables revocation puts the root's CRL (which covers the intermediate) and the intermediate's CRL (which covers the leaf) into the bundle, and renews both before they expire. A bundle with no CRL checks no revocation. The library reads the bundle when it applies the configuration, so a renewed CRL takes effect at the next chttpsvr_start(3) of a server, or at the next chttpcli_set_tls(3) of a client.

/* The defaults: verify the chain and the hostname. */
chttp_tls_config tls = CHTTP_TLS_DEFAULT;
chttpcli_set_tls(cli, &tls);
 
/* The same defaults, written as a zero-initialized struct. */
chttp_tls_config same = {0};
chttpcli_set_tls(cli, &same);
 
/* Pin a private CA and skip only the hostname match. */
chttp_tls_config pinned = {
    .ca_bundle_path = "/etc/myapp/ca-chain.pem",
    .insecure_skip_hostname_check = true,
};
chttpcli_set_tls(cli, &pinned);
 
/* Accept any certificate. Development and throwaway self-signed
   servers only; this gives an interceptor full control of the
   connection. */
chttp_tls_config insecure = {.insecure_skip_verify = true};
chttpcli_set_tls(cli, &insecure);

chttpcli_set_tls(3), chttpsvr_start(3), chttpsvr_req_peer_cert_verified(3)