SSL_CTX_ADD1_CHAIN_CERT(3ossl)

πŸ’– SSL_CTX_ADD1_CHAIN_CERT πŸ’–

okay bestie, gather round, because this is the 3ossl manual page for all the extra chain certificate processing girlies ✨. It's a whole squad of functions, not just one, so buckle up πŸŽ€


✨ NAME ✨

The whole crew, no cap:

  • SSL_CTX_set0_chain, SSL_CTX_set1_chain
  • SSL_CTX_add0_chain_cert, SSL_CTX_add1_chain_cert
  • SSL_CTX_get0_chain_certs, SSL_CTX_clear_chain_certs
  • SSL_set0_chain, SSL_set1_chain
  • SSL_add0_chain_cert, SSL_add1_chain_cert
  • SSL_get0_chain_certs, SSL_clear_chain_certs
  • SSL_CTX_build_cert_chain, SSL_build_cert_chain
  • SSL_CTX_select_current_cert, SSL_select_current_cert
  • SSL_CTX_set_current_cert, SSL_set_current_cert

...all serving extra chain certificate processing πŸ’…


🌈 SYNOPSIS 🌈

#include <openssl/ssl.h>

int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *sk);
int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *sk);
int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509);
int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509);
int SSL_CTX_get0_chain_certs(SSL_CTX *ctx, STACK_OF(X509) **sk);
int SSL_CTX_clear_chain_certs(SSL_CTX *ctx);

int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *sk);
int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *sk);
int SSL_add0_chain_cert(SSL *ssl, X509 *x509);
int SSL_add1_chain_cert(SSL *ssl, X509 *x509);
int SSL_get0_chain_certs(SSL *ssl, STACK_OF(X509) **sk);
int SSL_clear_chain_certs(SSL *ssl);

int SSL_CTX_build_cert_chain(SSL_CTX *ctx, flags);
int SSL_build_cert_chain(SSL *ssl, flags);

int SSL_CTX_select_current_cert(SSL_CTX *ctx, X509 *x509);
int SSL_select_current_cert(SSL *ssl, X509 *x509);
int SSL_CTX_set_current_cert(SSL_CTX *ctx, long op);
int SSL_set_current_cert(SSL *ssl, long op);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so first up, SSL_CTX_set0_chain() and SSL_CTX_set1_chain() β€” these set the certificate chain that's chained to the current certificate of ctx, and they set it to sk. iconic behavior fr fr πŸ’…

Then SSL_CTX_add0_chain_cert() and SSL_CTX_add1_chain_cert() β€” these append the single certificate x509 to the chain associated with the current certificate of ctx. just tacking it on the end, main character energy πŸ‘‘

SSL_CTX_get0_chain_certs()? it retrieves the chain associated with the current certificate of ctx. going and getting it. slay.

SSL_CTX_clear_chain_certs() clears any existing chain associated with the current certificate of ctx. and bestie, here's the tea: (this is implemented by calling SSL_CTX_set0_chain() with sk set to NULL). so it's lowkey just a fancy wrapper, iykyk πŸ™ˆ

Now, SSL_CTX_build_cert_chain() builds the certificate chain for ctx. Normally it uses the chain store, or the verify store if the chain store isn't set. If the function is successful, the built chain will replace any existing chain β€” out with the old, in with the new ✨.

The flags parameter can be set to:

  • SSL_BUILD_CHAIN_FLAG_UNTRUSTED β€” to use existing chain certificates as untrusted CAs
  • SSL_BUILD_CHAIN_FLAG_NO_ROOT β€” to omit the root CA from the built chain
  • SSL_BUILD_CHAIN_FLAG_CHECK β€” to use all existing chain certificates only to build the chain (effectively sanity checking and rearranging them if necessary)
  • SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR β€” ignores any errors during verification: if the flag SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR is also set, verification errors are cleared from the error queue

Details of the chain building process are described in "Certification Path Building" in openssl-verification-options(1). go read that if you want the deep lore πŸ“–


Every single one of these functions operates on the current end entity (i.e. server or client) certificate. And what even is that? It's the last certificate loaded or selected on the corresponding ctx structure. periodt.

SSL_CTX_select_current_cert() selects x509 as the current end entity certificate, but only if x509 has already been loaded into ctx using a function such as SSL_CTX_use_certificate(). like, it has to already be in the group chat, you can't just add a stranger πŸ’€

SSL_set0_chain(), SSL_set1_chain(), SSL_add0_chain_cert(), SSL_add1_chain_cert(), SSL_get0_chain_certs(), SSL_clear_chain_certs(), SSL_build_cert_chain(), SSL_select_current_cert() and SSL_set_current_cert() are all similar β€” except they apply to the SSL structure ssl. same vibe, different target 🎯

SSL_CTX_set_current_cert() changes the current certificate to a value based on the op argument. Currently op can be:

  • SSL_CERT_SET_FIRST β€” to use the first valid certificate
  • SSL_CERT_SET_NEXT β€” to set the next valid certificate after the current certificate

These two operations can be used to iterate over all certificates in an SSL_CTX structure. cute little loop, love that for us πŸ”

SSL_set_current_cert() also supports the option SSL_CERT_SET_SERVER. Here's the rundown:

  • If ssl is a server and has sent a certificate to a connected client, this option sets that certificate to the current certificate and returns 1.
  • If the negotiated cipher suite is anonymous (and thus no certificate will be sent), 2 is returned and the current certificate is unchanged.
  • If ssl is not a server, or a certificate has not been sent, 0 is returned and the current certificate is unchanged.
bestie listen UP: all these functions are implemented as macros. Those containing a 1 increment the reference count of the supplied certificate or chain, so it must be freed at some point after the operation. Those containing a 0 do not increment reference counts, and the supplied certificate or chain MUST NOT be freed after the operation. do not mess this up, i am begging πŸ™

🌸 NOTES 🌸

The chains associated with an SSL_CTX structure are copied to any SSL structures when SSL_new() is called. And SSL structures will not be affected by any chains subsequently changed in the parent SSL_CTX. so like, the copy is a copy, it's not still tethered to the original πŸ¦‹

One chain can be set for each key type supported by a server. So, for example, an RSA and a DSA certificate can (and often will) have different chains. two besties, two different chain aesthetics πŸ’…

The functions SSL_CTX_build_cert_chain() and SSL_build_cert_chain() can be used to check application configuration and to ensure any necessary subordinate CAs are sent in the correct order. Misconfigured applications sending incorrect certificate chains often cause problems with peers. like, embarrassing for them 😭

For example, an application can add any set of certificates using SSL_CTX_use_certificate_chain_file() then call SSL_CTX_build_cert_chain() with the option SSL_BUILD_CHAIN_FLAG_CHECK to check and reorder them.

Applications can issue non fatal warnings when checking chains by setting the flag SSL_BUILD_CHAIN_FLAG_IGNORE_ERRORS and checking the return value.

Calling SSL_CTX_build_cert_chain() or SSL_build_cert_chain() is more efficient than the automatic chain building, because it's only performed once. Automatic chain building is performed on each new session. efficiency is the moment, no cap ⚑

If any certificates are added using these functions, no certificates added using SSL_CTX_add_extra_chain_cert() will be used. them's the rules 🚫


πŸŽ€ RETURN VALUES πŸŽ€

SSL_set_current_cert() with SSL_CERT_SET_SERVER returns:

  • 1 for success
  • 2 if no server certificate is used because the cipher suite is anonymous
  • 0 for failure

SSL_CTX_build_cert_chain() and SSL_build_cert_chain() return 1 for success and 0 for failure. If the flag SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR and a verification error occurs, then 2 is returned.

All other functions return 1 for success and 0 for failure. simple as that πŸ’–


πŸ‘‘ SEE ALSO πŸ‘‘


⭐ HISTORY ⭐

These functions were added in OpenSSL 1.0.2. old money, honestly 🫢


Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the whole page, bestie πŸ’… this was OpenSSL 3.5.9, dated 2026-09-29, filed under SSL_CTX_ADD1_CHAIN_CERT(3ossl) β€” go build some gorgeous chains, you absolute icon πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from