.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "X509_VERIFY_CERT 3ssl" .TH X509_VERIFY_CERT 3ssl 2026-08-25 3.6.4 OpenSSL .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME X509_verify_cert, X509_STORE_CTX_verify, X509_build_chain \- build and verify X509 certificate chain .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& #include \& \& int X509_verify_cert(X509_STORE_CTX *ctx); \& int X509_STORE_CTX_verify(X509_STORE_CTX *ctx); \& STACK_OF(X509) *X509_build_chain(X509 *target, STACK_OF(X509) *certs, \& X509_STORE *store, int with_self_signed, \& OSSL_LIB_CTX *libctx, const char *propq); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" .SS "X509_verify_cert and X509_STORE_CTX_verify" .IX Subsection "X509_verify_cert and X509_STORE_CTX_verify" \&\fBX509_verify_cert()\fR attempts to build and validate a certificate chain for the target certificate set in \fIctx\fR. The verification context, of type \&\fBX509_STORE_CTX\fR, must first be constructed with \fBX509_STORE_CTX_new\fR\|(3) and initialised with \fBX509_STORE_CTX_init\fR\|(3). It carries the target certificate, the trust store, an optional stack of untrusted certificates that may assist chain construction, verification parameters such as flags and a verification purpose, an optional verification callback, and, after a call, the verification outcome. .PP A \fBX509_STORE_CTX\fR can be used for only one verification. Calling \&\fBX509_verify_cert()\fR a second time on the same context without reinitialising it fails with a negative return value, and \fBX509_STORE_CTX_get_error\fR\|(3) subsequently returns \fBX509_V_ERR_INVALID_CALL\fR. .PP When the target is a certificate, the function performs the following steps in order. The first step that fails aborts verification, except where a verification callback explicitly waives the error (see "THE VERIFICATION CALLBACK" below): .IP 1. 4 \&\fBChain construction.\fR Starting from the target certificate, the verification machinery seeks an issuer for the certificate currently at the top of the chain, drawing candidates from \fIctx\fR\*(Aqs untrusted stack and from the trust store. By default the search is untrusted\-first: the untrusted stack is examined before the trust store. Setting \fBX509_V_FLAG_TRUSTED_FIRST\fR on \&\fIctx\fR\*(Aqs verification parameters reverses this. When an untrusted\-first search fails to reach a trust anchor and \fBX509_V_FLAG_NO_ALT_CHAINS\fR is not set, the search is retried with progressively shorter untrusted prefixes in an attempt to find an alternative trusted path. The chain length is bounded by the configured depth limit (see \fBX509_VERIFY_PARAM_set_depth\fR\|(3)); exceeding it yields \fBX509_V_ERR_CERT_CHAIN_TOO_LONG\fR. If more than one chain is possible, only one is taken. .Sp Failure to build a chain to a trust anchor yields an error such as \&\fBX509_V_ERR_UNABLE_TO_GET_ISSUER_CERT\fR, \&\fBX509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY\fR, \&\fBX509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN\fR, \&\fBX509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT\fR, or \&\fBX509_V_ERR_CERT_CHAIN_TOO_LONG\fR. .IP 2. 4 \&\fBCertificate extension validation\fR per RFC 5280, including basic constraints, key usage and the verification purpose set via \&\fBX509_VERIFY_PARAM_set_purpose\fR\|(3). .IP 3. 4 \&\fBSecurity level checks\fR against the configured authentication level, covering issuer key sizes (\fBX509_V_ERR_CA_KEY_TOO_SMALL\fR) and signature algorithm strength (\fBX509_V_ERR_CA_MD_TOO_WEAK\fR). The leaf key is checked separately before chain construction begins (\fBX509_V_ERR_EE_KEY_TOO_SMALL\fR). .IP 4. 4 \&\fBIdentity checks\fR against any hostnames, email addresses or IP addresses configured on the verification parameters (\fBX509_VERIFY_PARAM_set1_host\fR\|(3) and related). .IP 5. 4 \&\fBRevocation checks\fR via CRLs and, when configured, OCSP. The set of checks performed is controlled by flags such as \fBX509_V_FLAG_CRL_CHECK\fR and \&\fBX509_V_FLAG_CRL_CHECK_ALL\fR. .IP 6. 4 \&\fBSignature and validity period checks\fR on every certificate in the chain, walking from the trust anchor down to the target. The signature on the chain\*(Aqs terminating certificate is not verified: trust is taken from its presence in the trust store rather than from its signature. This applies both to a conventional self\-signed trust anchor and, when \&\fBX509_V_FLAG_PARTIAL_CHAIN\fR is in effect, to a non\-self\-signed intermediate promoted to anchor status. \fBX509_V_FLAG_CHECK_SS_SIGNATURE\fR requests that the self\-signature on a self\-signed terminator be verified; it has no effect when the terminator is a non\-self\-signed certificate. Validity periods are checked on every certificate, including the terminator. .IP 7. 4 \&\fBName constraint validation\fR per RFC 5280 section 4.2.1.10; see \&\fBNAME_CONSTRAINTS_check\fR\|(3) for details of the matching primitive and the general\-name types it covers. .IP 8. 4 \&\fBRFC 3779 path validation\fR of AS\-number and IP\-address delegation extensions, performed by default unless OpenSSL was built with \&\fBno\-rfc3779\fR. .IP 9. 4 \&\fBCertificate policy validation\fR, performed only when \&\fBX509_V_FLAG_POLICY_CHECK\fR is set. .PP Several \fBX509_V_FLAG_*\fR values modify the behaviour of these checks; representative ones are named at the relevant step above, but the list there is not exhaustive. The complete set of verification flags, the effect each one has, and the functions used to query and modify them are documented in \&\fBX509_VERIFY_PARAM_set_flags\fR\|(3). .PP Applications rarely call \fBX509_verify_cert()\fR directly. It is invoked internally by OpenSSL during S/MIME and CMS verification and during the TLS handshake. .PP \&\fBX509_STORE_CTX_verify()\fR behaves identically to \fBX509_verify_cert()\fR except for the selection of the target certificate: if no target has been set on \fIctx\fR (see \fBX509_STORE_CTX_set_cert\fR\|(3)) and the untrusted stack is nonempty, the first certificate in the untrusted stack is adopted as the target before verification begins. If a target was set explicitly, \fBX509_STORE_CTX_verify()\fR uses it and does not consult the untrusted stack for this purpose. .SS "Raw public key targets" .IX Subsection "Raw public key targets" When the verification target is a raw public key rather than a certificate (set via \fBX509_STORE_CTX_init_rpk\fR\|(3)), both functions validate the raw public key instead of a certificate chain. The set of possible checks is significantly reduced: there are no extensions, names, CRLs or signatures to verify. The raw public key can be authenticated only via DANE TLSA records, either locally synthesised or obtained by the application from DNS. Raw public key DANE TLSA records may be added via \fBSSL_add_expected_rpk\fR\|(3) or \fBSSL_dane_tlsa_add\fR\|(3). .SS X509_build_chain .IX Subsection "X509_build_chain" \&\fBX509_build_chain()\fR builds a certificate chain starting from \fItarget\fR, using the same chain\-construction algorithm as \fBX509_verify_cert()\fR (see "X509_verify_cert and X509_STORE_CTX_verify" above). It internally uses a \&\fBX509_STORE_CTX\fR structure associated with the library context \fIlibctx\fR and property query string \fIpropq\fR, both of which may be NULL. The role of \&\fIcerts\fR depends on \fIstore\fR: .IP \(bu 4 If \fIstore\fR is non\-NULL, \fIcerts\fR is treated as an optional list of \&\fBuntrusted\fR intermediate certificates that may help complete the chain, and the chain must reach a trust anchor contained in \fIstore\fR. If no chain to a trust anchor can be built, the function fails and returns NULL. .IP \(bu 4 If \fIstore\fR is NULL, \fIcerts\fR is installed as the \fBtrusted\fR stack for the internal context (see \fBX509_STORE_CTX_set0_trusted_stack\fR\|(3)). In this mode the function builds the chain as far as it can but does not require it to reach an anchor: if chain construction fails partway, the partial chain built so far is still returned. .PP Because the internal \fBX509_STORE_CTX\fR is allocated and freed inside \&\fBX509_build_chain()\fR, search\-policy flags such as \fBX509_V_FLAG_TRUSTED_FIRST\fR and \fBX509_V_FLAG_NO_ALT_CHAINS\fR and the configured depth limit always take their default values, and the specific \fBX509_V_ERR_*\fR code that caused chain construction to fail is not reported back: the function signals only success or failure through its return value (and, when \fIstore\fR is NULL, may also return a partial chain). .PP On success the returned stack starts with a newly up\-referenced \fItarget\fR followed by the issuer certificates that were found. A self\-signed certificate at the top of the chain is included in the returned stack when either \&\fIwith_self_signed\fR is 1, or the chain consists solely of \fItarget\fR (for example because \fItarget\fR itself is self\-signed or no further issuer could be found). When the chain has more than one element and \fIwith_self_signed\fR is 0, the self\-signed top is omitted from the result. .PP The caller is responsible for freeing the returned stack. .SH "THE VERIFICATION CALLBACK" .IX Header "THE VERIFICATION CALLBACK" Each \fBX509_STORE_CTX\fR carries a \fIverification callback\fR with the signature .PP .Vb 1 \& int (*verify_cb)(int ok, X509_STORE_CTX *ctx); .Ve .PP This callback is invoked by \fBX509_verify_cert()\fR and \fBX509_STORE_CTX_verify()\fR at multiple points during verification, both to \fBreport errors\fR and to \&\fBnotify of progress\fR. It is installed by \fBX509_STORE_CTX_set_verify_cb\fR\|(3) on the context, or it is inherited at \fBX509_STORE_CTX_init\fR\|(3) time from the \fBX509_STORE\fR (see \fBX509_STORE_set_verify_cb_func\fR\|(3)). If neither has been set, a default callback is used which simply returns its \fIok\fR argument unchanged, causing every error to abort verification. .SS "When the callback is called" .IX Subsection "When the callback is called" There are two distinct invocation patterns: .IP "\fBError notification\fR (\fIok\fR = 0)" 4 .IX Item "Error notification (ok = 0)" The callback is called with \fIok\fR set to 0 each time a check fails. Before the call, the verification machinery records the \fBX509_V_ERR_*\fR code describing the failure on \fIctx\fR, and for certificate\-level errors also records the depth at which the error was detected and the certificate in question. The callback inspects these via \fBX509_STORE_CTX_get_error\fR\|(3), \&\fBX509_STORE_CTX_get_error_depth\fR\|(3), and \fBX509_STORE_CTX_get_current_cert\fR\|(3). CRL\- and OCSP\-related errors update only the error code; the depth and current certificate retain their values from a preceding context. .IP "\fBPer\-certificate success notification\fR (\fIok\fR = 1)" 4 .IX Item "Per-certificate success notification (ok = 1)" During the signature\-and\-validity pass, after each certificate in the chain has been checked successfully, the callback is called with \fIok\fR set to 1. The current certificate, current issuer, and error depth (queryable respectively via \fBX509_STORE_CTX_get_current_cert\fR\|(3), \&\fBX509_STORE_CTX_get0_current_issuer\fR\|(3), and \&\fBX509_STORE_CTX_get_error_depth\fR\|(3)) describe the certificate that has just been accepted. The callback may use this to log progress, but \fBmust\fR return a nonzero value, otherwise verification is aborted. .PP The callback\*(Aqs return value controls subsequent verification: .IP \(bu 4 A nonzero return value causes verification to \fBcontinue\fR. For an error notification this constitutes \fBwaiving\fR the error. .IP \(bu 4 A zero return value causes verification to \fBabort\fR immediately. The function returns 0 to its caller in this case, regardless of whether the callback was invoked with \fIok\fR = 0 or \fIok\fR = 1. .SS "Sticky errors" .IX Subsection "Sticky errors" When the callback waives an error by returning nonzero, the underlying check is treated as passed for control\-flow purposes, but the error code recorded on \&\fIctx\fR is \fBnot\fR reset to \fBX509_V_OK\fR. A subsequent successful return from \&\fBX509_verify_cert()\fR therefore does \fBnot\fR imply that \&\fBX509_STORE_CTX_get_error\fR\|(3) will return \fBX509_V_OK\fR: it may still hold the last error code that was waived. This is intentional. Only the callback itself is permitted to overwrite the error code, via \&\fBX509_STORE_CTX_set_error\fR\|(3), and only at its own risk. .SS Dangers .IX Subsection "Dangers" A verification callback that returns nonzero on an error notification has, by definition, suppressed an authentication check that OpenSSL considered necessary. Callers should treat installing a callback that waives errors as a deliberate weakening of the security guarantees of \fBX509_verify_cert()\fR, to be done only for specific, well\-understood error codes. The following pitfalls are common: .IP \(bu 4 \&\fBBlanket waivers\fR. A callback that returns 1 unconditionally turns \&\fBX509_verify_cert()\fR into "accept anything" and is almost always wrong outside of diagnostics. Inspect the error code via \fBX509_STORE_CTX_get_error\fR\|(3) and waive only the specific codes you intend to. .IP \(bu 4 \&\fBFailing a success notification\fR. Because the callback is also called with \&\fIok\fR = 1, a callback that mistakenly returns 0 in that case causes verification to fail even though every check passed. The caller cannot distinguish this from a genuine failure based on the return value alone. .IP \(bu 4 \&\fBClearing the error code\fR. The sticky\-error rule exists so that a waived error remains visible to the caller after verification returns. A callback that calls \fBX509_STORE_CTX_set_error\fR\|(3) with \fBX509_V_OK\fR hides this information and can also mask a later error if the callback is invoked again before verification completes. .IP \(bu 4 \&\fBMutating the verification context\fR. The \fBX509_STORE_CTX\fR is live during the callback: the verification routines are actively reading its chain, parameters, and other state. Calling context\-mutating functions from within the callback \-\- for example, replacing the verified chain via \&\fBX509_STORE_CTX_set0_verified_chain\fR\|(3), swapping the trust store or untrusted stack, or changing verification flags, depth, purpose, or target \&\-\- can corrupt the in\-progress verification, produce inconsistent behaviour between later steps of the pipeline, or, in the case of the verified chain, cause use\-after\-free. The only mutators reasonable from within a callback are the error\-related setters (\fBX509_STORE_CTX_set_error\fR\|(3), \&\fBX509_STORE_CTX_set_error_depth\fR\|(3), \fBX509_STORE_CTX_set_current_cert\fR\|(3)), and even those should be used sparingly (see "Sticky errors"). .IP \(bu 4 \&\fBHeavy work in the callback\fR. The callback is on the hot path of every certificate check; expensive work performed there will slow every TLS handshake or S/MIME verification that uses the surrounding context. .IP \(bu 4 \&\fBTrusting the depth alone\fR. The error depth, from \&\fBX509_STORE_CTX_get_error_depth\fR\|(3), records where an error was detected during chain processing, not the position of the certificate in the final chain. Always consult \fBX509_STORE_CTX_get_current_cert\fR\|(3) in addition to the depth when deciding whether to waive. .IP \(bu 4 \&\fBFragility with respect to check order\fR. The set of errors the callback observes, and the order in which it observes them, depends on the internal order in which verification checks are performed. When a certificate has more than one problem, only the first check to detect a problem causes the callback to be invoked for that certificate; later checks are not reached unless the callback waives the earlier error. This ordering is an implementation detail and is not part of the stable API: a refactor that reorders internal checks without altering the binary success/failure contract of \fBX509_verify_cert()\fR may still change which \fBX509_V_ERR_*\fR code the callback sees, or whether a given code is reported at all. Callbacks that branch on a specific error code being reported, or that assume earlier checks have already filtered out certain conditions, can therefore change behaviour silently across OpenSSL releases. Write callbacks defensively: re\-fetch the error code and the current certificate via \fBX509_STORE_CTX_get_error\fR\|(3) and \fBX509_STORE_CTX_get_current_cert\fR\|(3) afresh on each invocation, and treat "this error code never appears" as an assumption that may be invalidated. More fundamentally, because the set and order of error notifications is not a stable contract, the callback cannot be relied upon to observe any particular condition or sequence of conditions; that makes it an unsound mechanism for enforcing or modifying security policy. Use of the verification callback to alter verification outcomes \-\- to waive errors, to inject conditional acceptance, or to gate behaviour on a specific \&\fBX509_V_ERR_*\fR code being reported \-\- is therefore discouraged in production code. Reserve the callback for diagnostic and logging purposes, where future changes in which errors appear, or in what order, are not security\-relevant. .PP The default callback waives nothing and is the safe choice; it is the right behaviour for almost all production uses. .SH "RETURN VALUES" .IX Header "RETURN VALUES" \&\fBX509_verify_cert()\fR and \fBX509_STORE_CTX_verify()\fR return: .IP \fB1\fR 4 .IX Item "1" if a complete chain has been built and every check either succeeded or was waived by the verification callback. Note that the latter case does not guarantee that \fBX509_STORE_CTX_get_error\fR\|(3) returns \fBX509_V_OK\fR; see "Sticky errors". The return value is the authoritative success/failure signal: callers do not need to additionally check that \&\fBX509_STORE_CTX_get_error\fR\|(3) returns \fBX509_V_OK\fR to consider verification successful. They may consult it to learn whether, and which, errors were waived by the verification callback. .IP \fB0\fR 4 .IX Item "0" if verification was rejected. This occurs when a check failed and the callback did not waive the error, when a trust decision actively rejected the chain, or when the verification callback returned 0 from a success notification (see "THE VERIFICATION CALLBACK"). When a certificate would have failed more than one check, the specific \fBX509_V_ERR_*\fR code returned by \&\fBX509_STORE_CTX_get_error\fR\|(3) reflects whichever check fired first; this ordering is an implementation detail and is not stable across releases. Callers must therefore treat the return value as the authoritative success/failure signal, and treat the specific error code as diagnostic information that may shift over time. .IP "A \fBnegative\fR value" 4 .IX Item "A negative value" on a hard error that prevented verification from running to completion. The documented cases are: \fIctx\fR is NULL; \fIctx\fR has no target certificate set; \&\fIctx\fR has already been used for a previous verification; memory allocation failed; the trust store lookup function returned an error; or an internal invariant was violated. In these cases \fBX509_STORE_CTX_get_error\fR\|(3) returns an appropriate \fBX509_V_ERR_*\fR value (\fBX509_V_ERR_INVALID_CALL\fR, \&\fBX509_V_ERR_OUT_OF_MEM\fR, \fBX509_V_ERR_STORE_LOOKUP\fR or \&\fBX509_V_ERR_UNSPECIFIED\fR). .PP In all failure modes, additional information can be obtained from \&\fBX509_STORE_CTX_get_error\fR\|(3) and the related accessors. Applications must treat any return value <= 0 as verification not having succeeded. .PP \&\fBX509_build_chain()\fR returns NULL on error. Otherwise it returns a newly allocated stack of certificates that the caller must free; the stack may represent only a partial chain when \fIstore\fR is NULL. .SH BUGS .IX Header "BUGS" Several aspects of chain construction depart from the recommendations of RFC 4158 (Certification Path Building) and from strict RFC 5280 path validation. Callers should be aware of the following: .PP The chain search is not optimised in the manner described by RFC 4158 sections 3.1 to 3.5. Candidate issuers are not scored against the set of heuristics RFC 4158 recommends; at each step the first viable candidate is committed to, with the only preference being for a candidate whose validity period covers the current time. There is no tree\-traversal backtracking: when an initial chain does not reach a trust anchor, the search is retried with progressively shorter untrusted prefixes (unless \&\fBX509_V_FLAG_NO_ALT_CHAINS\fR is set), but different candidate issuers at intermediate positions of the same chain are not tried. In simple hierarchical PKIs this is rarely an issue. In cross\-certified or bridged PKI environments \fBX509_verify_cert()\fR may fail to find a valid certification path even when one demonstrably exists in the available certificate set. .PP Issuer key usage is not enforced during chain construction. RFC 5280 section 6.1.4(n) and RFC 4158 section 3.5.3 call for verifying that an issuer candidate\*(Aqs keyUsage extension permits certificate signing (\fBkeyCertSign\fR) before that candidate is selected. OpenSSL defers this check to the later extension\-validation pass: if two candidate issuers exist for a certificate, and the one lacking \fBkeyCertSign\fR happens to be selected first, verification fails on the extension check rather than backing off and trying the other candidate. The misuse is ultimately caught, but a chain that would have validated through the alternative issuer is not built. .PP \&\fBX509_V_FLAG_PARTIAL_CHAIN\fR relaxes the trust\-anchor requirement from the one defined by RFC 5280 section 6.1.1(d). With the flag set, any certificate in the trust store is acceptable as the terminator of the chain, even if it is not a self\-signed root. This is an intentional and now\-common deviation that supports modern practices such as pinning trust to a specific intermediate, or shortening chains by treating a sufficiently\-trusted intermediate as the trust point and eliding the root above it. Callers should nevertheless be aware that the chain returned in this mode does not necessarily terminate at an RFC 5280\-style trust anchor. .PP \&\fBNAME_CONSTRAINTS_check\fR\|(3) caps each per\-pair check at 2**20 comparisons, but the chain orchestrator issues \fBN\fR(\fBN\fR\-1)/2 such checks for an \fBN\fR\-certificate chain. An adversary submitting a maximally constructed chain can therefore force up to approximately 6.3 million name\-constraint comparisons in a four\-certificate chain (one leaf and three name\-constrained signers), or approximately 5.2 billion at the default chain\-depth limit of 100. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBNAME_CONSTRAINTS_check\fR\|(3), \&\fBSSL_add_expected_rpk\fR\|(3), \&\fBSSL_CTX_dane_enable\fR\|(3), \&\fBSSL_dane_tlsa_add\fR\|(3), \&\fBX509_STORE_CTX_new\fR\|(3), \&\fBX509_STORE_CTX_init\fR\|(3), \&\fBX509_STORE_CTX_init_rpk\fR\|(3), \&\fBX509_STORE_CTX_get_error\fR\|(3), \&\fBX509_STORE_CTX_set_verify_cb\fR\|(3), \&\fBX509_STORE_set_verify_cb_func\fR\|(3), \&\fBX509_VERIFY_PARAM_set_flags\fR\|(3) .SH HISTORY .IX Header "HISTORY" \&\fBX509_build_chain()\fR and \fBX509_STORE_CTX_verify()\fR were added in OpenSSL 3.0. .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright 2009\-2026 The OpenSSL Project Authors. All Rights Reserved. .PP 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 .