SSL_CTX_SET_VERIFY(3)
๐ SSL_CTX_SET_VERIFY(3) ๐
okay so bestie, welcome to this page from the Library Functions Manual โ it's all about setting peer certificate verification parameters. Trust issues, but make them secure. ๐
๐ธ NAME ๐ธ
SSL_CTX_set_verify, SSL_set_verify, SSL_CTX_set_verify_depth, SSL_set_verify_depth โ set peer certificate verification parameters
โจ SYNOPSIS โจ
#include <openssl/ssl.h>
void
SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
int (*verify_callback)(int, X509_STORE_CTX *));
void
SSL_set_verify(SSL *s, int mode,
int (*verify_callback)(int, X509_STORE_CTX *));
void
SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth);
void
SSL_set_verify_depth(SSL *s, int depth);
int
verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx);
๐ฆ DESCRIPTION ๐ฆ
SSL_CTX_set_verify() sets the verification flags for ctx to mode and specifies the verify_callback function to be used. If no callback function shall be specified, the NULL pointer can be used for verify_callback.
SSL_set_verify() sets the verification flags for ssl to mode and specifies the verify_callback function to be used. If no callback function shall be specified, the NULL pointer can be used for verify_callback. In this case the last verify_callback set specifically for this ssl remains. If no special callback was set before, the default callback for the underlying ctx is used, that was valid at the time ssl was created with SSL_new(3). Within the callback function, SSL_get_ex_data_X509_STORE_CTX_idx(3) can be called to get the data index of the current SSL object that is doing the verification.
SSL_CTX_set_verify_depth() sets the maximum depth for the certificate chain verification that shall be allowed for ctx. (See the โBUGSโ section.)
SSL_set_verify_depth() sets the maximum depth for the certificate chain verification that shall be allowed for ssl. (See the โBUGSโ section.)
The verification of certificates can be controlled by a set of bitwise ORed mode flags:
๐ฉ the mode flags ๐ฉ
SSL_VERIFY_NONEโ server mode: the server will not send a client certificate request to the client, so the client will not send a certificate.
Client mode: if not using an anonymous cipher (by default disabled), the server will send a certificate which will be checked. The result of the certificate verification process can be checked after the TLS/SSL handshake using the SSL_get_verify_result(3) function. The handshake will be continued regardless of the verification result.
SSL_VERIFY_PEERโ server mode: the server sends a client certificate request to the client. The certificate returned (if any) is checked. If the verification process fails, the TLS/SSL handshake is immediately terminated with an alert message containing the reason for the verification failure. The behaviour can be controlled by the additionalSSL_VERIFY_FAIL_IF_NO_PEER_CERTandSSL_VERIFY_CLIENT_ONCEflags.
Client mode: the server certificate is verified. If the verification process fails, the TLS/SSL handshake is immediately terminated with an alert message containing the reason for the verification failure. If no server certificate is sent, because an anonymous cipher is used, SSL_VERIFY_PEER is ignored.
SSL_VERIFY_FAIL_IF_NO_PEER_CERTโ server mode: if the client did not return a certificate, the TLS/SSL handshake is immediately terminated with a โhandshake failureโ alert. This flag must be used together withSSL_VERIFY_PEER.
Client mode: ignored.
SSL_VERIFY_CLIENT_ONCEโ server mode: only request a client certificate on the initial TLS/SSL handshake. Do not ask for a client certificate again in case of a renegotiation. This flag must be used together withSSL_VERIFY_PEER.
Client mode: ignored.
Exactly one of the mode flags SSL_VERIFY_NONE and SSL_VERIFY_PEER must be set at any time.
The actual verification procedure is performed either using the built-in verification procedure or using another application provided verification function set with SSL_CTX_set_cert_verify_callback(3). The following descriptions apply in the case of the built-in procedure. An application provided procedure also has access to the verify depth information and the verify_callback() function, but the way this information is used may be different.
SSL_CTX_set_verify_depth() and SSL_set_verify_depth() set the limit up to which depth certificates in a chain are used during the verification procedure. If the certificate chain is longer than allowed, the certificates above the limit are ignored. Error messages are generated as if these certificates would not be present, most likely a X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY will be issued. The depth count is โlevel 0: peer certificateโ, โlevel 1: CA certificateโ, โlevel 2: higher level CA certificateโ, and so on. Setting the maximum depth to 2 allows the levels 0, 1, and 2. The default depth limit is 100, allowing for the peer certificate and an additional 100 CA certificates.
The verify_callback function is used to control the behaviour when the SSL_VERIFY_PEER flag is set. It must be supplied by the application and receives two arguments: preverify_ok indicates whether the verification of the certificate in question was passed (preverify_ok=1) or not (preverify_ok=0). x509_ctx is a pointer to the complete context used for the certificate chain verification.
The certificate chain is checked starting with the deepest nesting level (the root CA certificate) and worked upward to the peer's certificate. At each level signatures and issuer attributes are checked. Whenever a verification error is found, the error number is stored in x509_ctx and verify_callback is called with preverify_ok equal to 0. By applying X509_CTX_store_*() functions verify_callback can locate the certificate in question and perform additional steps (see โEXAMPLESโ). If no error is found for a certificate, verify_callback is called with preverify_ok equal to 1 before advancing to the next level.
The return value of verify_callback controls the strategy of the further verification process. If verify_callback returns 0, the verification process is immediately stopped with โverification failedโ state. If SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and the TLS/SSL handshake is terminated. If verify_callback returns 1, the verification process is continued. If verify_callback always returns 1, the TLS/SSL handshake will not be terminated with respect to verification failures and the connection will be established. The calling process can however retrieve the error code of the last verification error using SSL_get_verify_result(3) or by maintaining its own error storage managed by verify_callback.
If no verify_callback is specified, the default callback will be used. Its return value is identical to preverify_ok, so that any verification failure will lead to a termination of the TLS/SSL handshake with an alert message, if SSL_VERIFY_PEER is set.
โจ EXAMPLES โจ
Okay bestie, this is the part where it gets so good, because they actually show you a full verify_callback ๐ญ๐. The block below is a code sequence that realizes an example verify_callback function that will always continue the TLS/SSL handshake regardless of verification failure, if wished. The callback realizes a verification depth limit with more informational output.
The vibe: all verification errors are printed, and information about the certificate chain is printed on request. The example is realized for a server that does allow but not require client certificates ๐.
And the technique it's using? It makes use of the ex_data technique to store application data into/retrieve application data from the SSL structure (see SSL_get_ex_new_index(3), SSL_get_ex_data_X509_STORE_CTX_idx(3)) ๐ซถ.
...
typedef struct {
int verbose_mode;
int verify_depth;
int always_continue;
} mydata_t;
int mydata_index;
...
static int
verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
{
char buf[256];
X509 *err_cert;
int err, depth;
SSL *ssl;
mydata_t *mydata;
err_cert = X509_STORE_CTX_get_current_cert(ctx);
err = X509_STORE_CTX_get_error(ctx);
depth = X509_STORE_CTX_get_error_depth(ctx);
/*
* Retrieve the pointer to the SSL of the connection currently
* treated * and the application specific data stored into the
* SSL object.
*/
ssl = X509_STORE_CTX_get_ex_data(ctx,
SSL_get_ex_data_X509_STORE_CTX_idx());
mydata = SSL_get_ex_data(ssl, mydata_index);
X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
/*
* Catch a too long certificate chain. The depth limit set using
* SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
* that whenever the "depth>verify_depth" condition is met, we
* have violated the limit and want to log this error condition.
* We must do it here, because the CHAIN_TOO_LONG error would not
* be found explicitly; only errors introduced by cutting off the
* additional certificates would be logged.
*/
if (depth > mydata->verify_depth) {
preverify_ok = 0;
err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
X509_STORE_CTX_set_error(ctx, err);
}
if (!preverify_ok) {
printf("verify error:num=%d:%s:depth=%d:%s\n", err,
X509_verify_cert_error_string(err), depth, buf);
} else if (mydata->verbose_mode) {
printf("depth=%d:%s\n", depth, buf);
}
/*
* At this point, err contains the last verification error.
* We can use it for something special
*/
if (!preverify_ok && (err ==
X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) {
X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert),
buf, 256);
printf("issuer= %s\n", buf);
}
if (mydata->always_continue)
return 1;
else
return preverify_ok;
}
...
mydata_t mydata;
...
mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
...
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
verify_callback);
/*
* Let the verify_callback catch the verify_depth error so that we get
* an appropriate error in the logfile.
*/
SSL_CTX_set_verify_depth(verify_depth + 1);
/*
* Set up the SSL specific data into "mydata" and store it into the SSL
* structure.
*/
mydata.verify_depth = verify_depth; ...
SSL_set_ex_data(ssl, mydata_index, &mydata);
...
SSL_accept(ssl); /* check of success left out for clarity */
if (peer = SSL_get_peer_certificate(ssl)) {
if (SSL_get_verify_result(ssl) == X509_V_OK) {
/* The client sent a certificate which verified OK */
}
}
Iconic, right? The way it catches X509_V_ERR_CERT_CHAIN_TOO_LONG by setting the depth to "limit+1" so the depth>verify_depth condition actually fires โ that's main character energy fr fr ๐
๐ฅ. And yeah, that trailing if (peer = SSL_get_peer_certificate(ssl)) is exactly how they wrote it, we don't fix, we just appreciate ๐.
๐ธ SEE ALSO ๐ธ
Go read these next, bestie, they're all part of the same little universe ๐:
ssl(3)SSL_CTX_get_verify_mode(3)SSL_CTX_load_verify_locations(3)SSL_CTX_set_cert_verify_callback(3)SSL_get_ex_data_X509_STORE_CTX_idx(3)SSL_get_ex_new_index(3)SSL_get_peer_certificate(3)SSL_get_verify_result(3)SSL_new(3)SSL_set1_host(3)
๐ฆ HISTORY ๐ฆ
Story time, and ngl it's giving "this code is older than your mom" ๐โจ:
SSL_set_verify()appeared in SSLeay 0.4 or earlier.SSL_CTX_set_verify()first appeared in SSLeay 0.6.4.- Both functions have been available since OpenBSD 2.4.
SSL_CTX_set_verify_depth()andSSL_set_verify_depth()first appeared in OpenSSL 0.9.3 and have been available since OpenBSD 2.6.
So like... these functions have been thriving since before most of us could even read. Respect ๐ญ๐ซถ.
๐ BUGS ๐
Bestie this section is the "read before you get got" section. Nothing here is a fun fact, it is a real heads up ๐ฅ.
- In client mode, it is not checked whether the
SSL_VERIFY_PEERflag is set, but whetherSSL_VERIFY_NONEis not set. This can lead to unexpected behaviour, if theSSL_VERIFY_PEERandSSL_VERIFY_NONEare not used as required (exactly one must be set at any time). So like... exactly one must be set at any time, okay? Not both, not neither. Periodt ๐ . - The certificate verification depth set with
SSL[_CTX]_verify_depth()stops the verification at a certain depth. The error message produced will be that of an incomplete certificate chain and notX509_V_ERR_CERT_CHAIN_TOO_LONGas may be expected. Which is lowkey a jumpscare ๐, so don't be surprised when the error you see isn't the error you thought you'd get.
That's the whole page, bestie! ๐ This lovely little write-up ships from Debian, dated June 12, 2021, and it lives under the name SSL_CTX_SET_VERIFY(3) โ go forth and verify those certificates responsibly ๐โจ๐ฆ๐โญ
๐ฆ where this page comes from
- opensuse-leap 16.0: libressl-devel-doc 4.1.0-bp160.1.1 ยท OpenSSL
- Copyright (c) 2000, 2001, 2002, 2003, 2014 The OpenSSL Project
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms