C Collections

chttpclient · Section 3

chttpcli_set_tls

chttpcli_set_tls - set the TLS configuration of a client

C Collections (libccollections, -lccollections)

#include <ccollections/chttpclient.h>
 
ccol_retval chttpcli_set_tls(chttpcli cli,
                               const chttp_tls_config *tls);

This function copies the TLS configuration you pass into the client.
A tls of NULL brings the defaults back: Chain and hostname
verification, the system CA bundle and no client certificate.
 
The strings of chttp_tls_config (cert_path, key_path and
ca_bundle_path) are deep-copied into buffers that the client owns, so
the caller can free or reuse its own originals as soon as this call
returns.
 
This function does not check that cert_path, key_path and
ca_bundle_path can be read. The client checks each path later, at the
moment an HTTPS request needs it. A path that names no file at all, or
a directory or anything else that is not a file, is accepted here
without an error and then gives ccol_http_tls_cert_load_failed from
chttpcli_do or chttpcli_do_streaming once a request needs it.
 
The system CA bundle is OpenSSL's default CA file: The file that the
SSL_CERT_FILE environment variable names, or otherwise the file that
OpenSSL was built with (the variable is ignored in a set-user-ID or
set-group-ID process). The library reads it through a close-on-exec
descriptor and parses it once per process, the first time a client
needs it, and every later client shares the parsed certificates, so
changing the file or SSL_CERT_FILE after that point has no effect. A
file with a certificate whose key OpenSSL can't decode is read again
for each client instead, and a file with a block OpenSSL can't read is
read again too and counts as holding no certificate.
Only when that file is absent or holds no certificate does the client
fall back to OpenSSL's hashed directory (SSL_CERT_DIR, or the
directory OpenSSL was built with). OpenSSL then opens the files of
that directory itself during the handshakes that need them, with
descriptors that are not close-on-exec.
 
The client reads each of the three files whole, and rejects a file
larger than 16 MB with ccol_http_tls_cert_load_failed without reading
it into memory. A PEM file is far smaller than that (a certificate or
a key is a few kilobytes, a full system CA bundle a few hundred
kilobytes), so only a path that points at something other than a PEM
file can hit the limit.
 
ca_bundle_path must also yield at least one certificate. A readable
file can still parse to no certificate at all: An empty file, a key on
its own, or a file that isn't PEM. The client reports
ccol_http_tls_cert_load_failed for such a file, because it won't
accept a trust store that trusts no issuer.
 
ca_bundle_path may hold certificate revocation lists (CRLs) next to
its CA certificates. The client then enforces them against every
server's certificate chain, as the ssl_crl directive of nginx does:
each certificate of that chain below its trust anchor needs a CRL of
its issuer in the bundle. A request fails with
ccol_http_tls_cert_verification_failed for a revoked certificate, for
a certificate whose issuer has no CRL in the bundle, and for a CRL past
its next update time. A bundle with no CRL checks no revocation.
 
cert_path and key_path go together. If you set one of them and leave
the other NULL, the function returns ccol_invalid_args instead of
taking that as "no client certificate", because staying quiet there
would leave an mTLS deployment believing it presents a client
certificate when it never does.
 
A chttp_tls_config whose bytes are all zero is the SAFE
configuration: It verifies the server's certificate chain and matches
its hostname. Both verification fields are named for what you give up
by turning them ON, so a struct built with = {0}, or with a designated
initializer that names only some of its fields, can never end up
verifying less than the caller asked for.
 
insecure_skip_verify accepts any certificate from any peer and makes
insecure_skip_hostname_check irrelevant as well, because a hostname
matched against a certificate that nothing validated proves nothing.
For that reason you cannot ask for the hostname check without the
chain check.
 
insecure_skip_hostname_check keeps the chain verification and drops
only the hostname match. CHTTP_TLS_DEFAULT(3) has more on both.
 
The new configuration applies to every request made after this call
returns, including one that could reuse a keep-alive connection from
the pool. Both idle pools are keyed by origin ("scheme://host:port",
or "unix://<path>"), and that key says nothing about the TLS policy
under which a pooled connection did its handshake. So the client
retires, rather than reuses, every pooled HTTPS connection it made
under the configuration that this call replaces, and the next request
to that origin does a fresh handshake with the new settings. The
client goes on reusing pooled plain-HTTP connections, since no TLS
setting applies to them. A connection that another request has taken
out of the pool finishes under the configuration it started with; the
client retires only the connections that are idle.

cli   The client handle. It must not be CHTTPCLI_NULL.
tls   The TLS configuration to copy, or NULL to put the defaults
      back.

ccol_success          The client has the new configuration.
ccol_invalid_args     cli is CHTTPCLI_NULL, or a stale handle of
                      a client that has been destroyed, or you set
                      only one of tls->cert_path and tls->key_path,
                      or you set tls->insecure_skip_verify together
                      with a tls->ca_bundle_path. A bundle is there
                      to verify against, so that pair states two
                      incompatible policies, and the function
                      refuses it instead of picking one.
ccol_not_enough_memory
                      A memory allocation failed. The client's TLS
                      configuration then holds the defaults (chain
                      and hostname verification, the system CA
                      bundle and no client certificate); the
                      client never applies part of your
                      configuration. When memory is short even
                      for the defaults, the next request builds
                      them again, and a request that needs TLS
                      fails with ccol_not_enough_memory if that
                      fails too.

CHTTP_TLS_DEFAULT(3), chttpcli_create(3)