7. SSL

     void *cs_get_ssl(COMSTACK cs);
    

Returns the SSL handle, SSL * for comstack. If comstack is not of type SSL, then NULL is returned.

     int cs_set_ssl_ctx(COMSTACK cs, void *ctx);
    

Sets SSL context for comstack. The parameter is expected to be of type SSL_CTX *. This function should be called just after comstack has been created (before connect, bind, etc). This function returns 1 for success; 0 for failure.

     int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname);
    

Sets the endpoint certificate and private key as a PEM file, or as certificate-file,key-file for separate files. This configures the server identity when listening, or the client identity when connecting; it does not configure trusted server certificates. This function returns 1 for success; 0 for failure.

     int cs_set_ssl_cafile(COMSTACK cs, const char *fname);
    

Selects a PEM CA bundle and enables certificate and hostname verification for client TLS connections. Call before connecting. The filename is copied; the file is loaded during connect. The bundle replaces system trust. An unreadable, invalid, or empty bundle fails the connection with CSERRORSSL. A NULL or empty filename restores system trust without disabling verification. Returns 1 on success, or 0 for a non-TLS endpoint or an existing TLS session.

     int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len);
    

This function returns the peer certificate. If successful, *buf and *len holds X509 buffer and length respectively. Buffer should be freed with xfree. This function returns 1 for success; 0 for failure.

7.1. Configuring and testing TLS trust

Certificate verification is currently opt-in. Use --check-cert with yaz-client or yaz-url to verify against system trust, or --cacert file to enable verification against a private PEM CA bundle. The explicit bundle replaces system trust. For ZOOM, set cafile; for the URL API, call yaz_url_set_cafile. These settings allow applications to configure verified connections before a future default change.

Obtain CA certificates through a trusted channel. Include only public certificates in the client bundle, never a CA or server private key. The server certificate must name the host used to connect in its Subject Alternative Name (SAN); trusting a CA does not disable hostname checking. For several trusted CAs, concatenate their PEM certificates into one bundle.

The following OpenSSL commands create a private test CA and a server certificate for localhost. Run them in a new directory. The unencrypted private keys are intended for this local test; restrict their permissions and keep the CA key off production servers.

umask 077
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -days 365 \
  -keyout ca.key -out ca.pem -subj "/CN=YAZ test CA" \
  -addext "basicConstraints=critical,CA:TRUE" \
  -addext "keyUsage=critical,keyCertSign,cRLSign"
openssl req -new -newkey rsa:2048 -nodes \
  -keyout server.key -out server.csr -subj "/CN=localhost"
cat > server.ext <<'EOF'
basicConstraints=critical,CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
extendedKeyUsage=serverAuth
subjectAltName=DNS:localhost
EOF
openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key \
  -CAcreateserial -out server.pem -days 365 -sha256 -extfile server.ext
openssl verify -CAfile ca.pem -purpose sslserver \
  -verify_hostname localhost server.pem

Start a local HTTPS test endpoint in one terminal, and fetch it in another:

openssl s_server -accept 8443 -cert server.pem -key server.key -www
yaz-url --cacert ca.pem https://localhost:8443/

For Z39.50 over TLS, start yaz-ztest with the same certificate and key, then connect with yaz-client:

yaz-ztest -C server.pem,server.key ssl:@:9999
yaz-client --cacert ca.pem ssl:localhost:9999

To inspect and verify a running TLS endpoint independently, use openssl s_client. The same command works for a Z39.50 TLS endpoint by changing the port. Connecting as 127.0.0.1 with YAZ should fail hostname verification with this certificate, because its SAN contains only localhost.

openssl s_client -connect localhost:8443 -servername localhost \
  -CAfile ca.pem -verify_hostname localhost -verify_return_error
openssl x509 -in server.pem -noout -subject -issuer -dates \
  -ext subjectAltName

A deliberately trusted self-signed certificate can also be supplied in the CA bundle; the hostname must still match. For deployments with multiple servers, a private CA permits server certificates to be renewed without replacing the trusted CA bundle on every client. See the OpenSSL command documentation for further certificate options.