SSL_CTX_ADD1_CHAIN_CERT(3SSL)
π SSL_CTX_ADD1_CHAIN_CERT π (3SSL)
okay bestie, welcome to the 3SSL corner of the OpenSSL manual β this page is literally all about extra chain certificate processing and honestly? she's a baddie. Let's get into it. β¨
π NAME π
Secure all the chain-certificate glow-up energy with: 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, and SSL_set_current_cert.
That's the whole cast. Main character energy across the board. π
β¨ 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);
no cap, that's a LOT of little functions and every single one of them is here to serve. π«Ά
π DESCRIPTION π
Okay so, buckle in, we're going one by one. Every one. π¦
SSL_CTX_set0_chain()andSSL_CTX_set1_chain()β these two set the certificate chain associated with the current certificate ofctxtosk. Boom, done, chain swapped.SSL_CTX_add0_chain_cert()andSSL_CTX_add1_chain_cert()β these append the single certificatex509to the chain associated with the current certificate ofctx. One cert at a time, we're building her up. β¨SSL_CTX_get0_chain_certs()β retrieves the chain associated with the current certificate ofctx. Give her to meee.SSL_CTX_clear_chain_certs()β clears any existing chain associated with the current certificate ofctx. lowkey how does she do it? she's implemented by callingSSL_CTX_set0_chain()withskset toNULL. iconic, honestly.SSL_CTX_build_cert_chain()β builds the certificate chain forctx. Normally this uses the chain store, or the verify store if the chain store is not set. If the function is successful, the built chain will replace any existing chain (not me evicting the old chain π).
The flags parameter can be set to:
SSL_BUILD_CHAIN_FLAG_UNTRUSTEDβ use existing chain certificates as untrusted CAsSSL_BUILD_CHAIN_FLAG_NO_ROOTβ omit the root CA from the built chainSSL_BUILD_CHAIN_FLAG_CHECKβ 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- and if flag
SSL_BUILD_CHAIN_FLAG_CLEAR_ERRORis 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). iykyk. π
Each of these functions operates on the current end entity (i.e. server or client) certificate. That'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(). you can't just pick a stranger, she has to already be in the building. π
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 similar β except they apply to the SSL structure ssl. Same vibes, different object. π
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β use the first valid certificateSSL_CERT_SET_NEXTβ 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. hot girl iteration. π₯
SSL_set_current_cert() also supports the option SSL_CERT_SET_SERVER. Here's the tea:
- If
sslis a server and has sent a certificate to a connected client, this option sets that certificate to the current certificate and returns1. - If the negotiated cipher suite is anonymous (and thus no certificate will be sent)
2is returned and the current certificate is unchanged. - If
sslis not a server, or a certificate has not been sent,0is returned and the current certificate is unchanged.
All these functions are implemented as macros. And this part MATTERS, bestie:
- Those containing a
1increment the reference count of the supplied certificate or chain, so it must be freed at some point after the operation. - Those containing a
0do not increment reference counts, and the supplied certificate or chain MUST NOT be freed after the operation.
β οΈ fr fr read that twice. The 0 ones are the "you don't own this, don't touch it" girlies. Freeing them anyway is a whole mess you do not want. π
πΈ NOTES πΈ
The chains associated with an SSL_CTX structure are copied to any SSL structures when SSL_new() is called. SSL structures will not be affected by any chains subsequently changed in the parent SSL_CTX. copies stay copies, no take-backs. π
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 girls, two chains, no drama. π
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. π so check her before you wreck her.
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. chef's kiss. π
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 as it is only performed once. Automatic chain building is performed on each new session. efficiency is the vibe. β¨
If any certificates are added using these functions, no certificates added using SSL_CTX_add_extra_chain_cert() will be used. pick a lane bestie, she's exclusive. π«Ά
β RETURN VALUES β
SSL_set_current_cert()withSSL_CERT_SET_SERVERreturns1for success,2if no server certificate is used because the cipher suite is anonymous, and0for failure.SSL_CTX_build_cert_chain()andSSL_build_cert_chain()return1for success and0for failure. If the flagSSL_BUILD_CHAIN_FLAG_IGNORE_ERRORis set and a verification error occurs, then2is returned.- All other functions return
1for success and0for failure. classic. simple. slay. π
π SEE ALSO π
go read those too, they're part of the lore. π
π¦ HISTORY π¦
These functions were added in OpenSSL 1.0.2. vintage but still serving. π
π COPYRIGHT π
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 babes!! π₯° this was 4.0.3, dated 2026-09-29, straight from the OpenSSL 3SSL collection β go forth and build those chains correctly, mwah ππ§ββοΈβ¨
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms