X509_STORE_CTX_NEW(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

X509_STORE_CTX_NEW(3) β€” Library Functions Manual, but make it cute πŸ’–

Okay so this is the Library Functions Manual page for the whole X509_STORE_CTX initialisation squad, and honestly? It's giving β€œsetup era” energy ✨. We're talking the functions that get an X509_STORE_CTX object ready for X509_verify_cert(3) to do its main-character thing later.


🌸 NAME 🌸

X509_STORE_CTX_new, X509_STORE_CTX_init, X509_STORE_CTX_cleanup, X509_STORE_CTX_free, X509_STORE_CTX_get0_store, X509_STORE_CTX_set0_trusted_stack, X509_STORE_CTX_trusted_stack, X509_STORE_CTX_set_cert, X509_STORE_CTX_get0_cert, X509_STORE_CTX_set_chain, X509_STORE_CTX_set0_untrusted, X509_STORE_CTX_get0_untrusted, X509_STORE_CTX_set0_crls β€” literally all about X509_STORE_CTX initialisation, bestie πŸ’…

✨ SYNOPSIS ✨

library β€œlibcrypto” #include <openssl/x509_vfy.h>

X509_STORE_CTX *
X509_STORE_CTX_new(void);

int
X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x,
    STACK_OF(X509) *untrusted);

void
X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);

void
X509_STORE_CTX_free(X509_STORE_CTX *ctx);

X509_STORE *
X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx);

void
X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
    STACK_OF(X509) *trusted);

void
X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx,
    STACK_OF(X509) *trusted);

void
X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x);

X509 *
X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx);

void
X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx,
    STACK_OF(X509) *untrusted);

void
X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx,
    STACK_OF(X509) *untrusted);

STACK_OF(X509) *
X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx);

void
X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *crls);

πŸ’… DESCRIPTION πŸ’…

So these functions set up an X509_STORE_CTX object for subsequent use by X509_verify_cert(3). Think of it like packing your bag before the certification verification road trip πŸŽ’βœ¨.

X509_STORE_CTX_new() allocates an empty X509_STORE_CTX object that does not yet contain the subobjects required for normal operation. It's the blank canvas era, no cap.

X509_STORE_CTX_init() needs to be called on each new ctx before any of the other functions become useful. It prepares ctx for one single verification operation using X509_verify_cert(3). The trusted certificate store to be used, the end entity certificate x to be verified, and a set of additional untrusted certificates, to be used for building the chain, can be supplied β€” or any or all of them can be set to NULL.

🫢 Important bestie detail: the three pointers passed in are stored internally, but the three objects pointed to are not copied, their reference count is not incremented, and the caller remains responsible for managing their storage and for not freeing them before X509_STORE_CTX_free() is called on ctx. So like… don't free the vibes early 😭.

If a store is provided, the verification parameters contained in it are copied using X509_VERIFY_PARAM_inherit(3).

X509_STORE_CTX_cleanup() internally cleans up ctx, returning it to an empty state similar to the one after X509_STORE_CTX_new(). It can then be reused with a new call to X509_STORE_CTX_init(). Reuse era! β™»οΈβœ¨

X509_STORE_CTX_free() calls X509_STORE_CTX_cleanup() and frees the storage pointed to by ctx. If ctx is a NULL pointer, no action occurs. Very chill of it, honestly.

X509_STORE_CTX_get0_store() returns the internal pointer to the trusted certificate store that was set with X509_STORE_CTX_init().

X509_STORE_CTX_set0_trusted_stack() sets the set of trusted certificates used by ctx. This is an alternative way of specifying trusted certificates instead of using the store. And X509_STORE_CTX_trusted_stack() is a deprecated alias for X509_STORE_CTX_set0_trusted_stack() β€” so like, it exists, but new code should not be making it a main character πŸ’€.

X509_STORE_CTX_set_cert() sets the certificate to be verified in ctx to x, overriding the certificate that was set with X509_STORE_CTX_init(). Again, the certificate is not copied and its reference count is not incremented. Same energy as before: pointers stay pointers, friends stay friends 🫢.

X509_STORE_CTX_get0_cert() retrieves the internal pointer to the certificate being verified by ctx, i.e. the last one set using either X509_STORE_CTX_init() or X509_STORE_CTX_set_cert().

X509_STORE_CTX_set_chain() and X509_STORE_CTX_set0_untrusted() are identical and set the additional, untrusted certificates used by ctx, overriding the set of additional, untrusted certificates that was set with X509_STORE_CTX_init(). Again, the set and the certificates contained in it are not copied and their reference counts are not incremented. Twin behaviour, zero copying, we love to see it ✨.

X509_STORE_CTX_get0_untrusted() retrieves the internal pointer to the set of additional, untrusted certificates associated with ctx, i.e. the last one set using either X509_STORE_CTX_init(), X509_STORE_CTX_set_chain(), or X509_STORE_CTX_set0_untrusted().

X509_STORE_CTX_set0_crls() sets a set of crls to use during certificate verification. These CRLs will only be used if CRL verification is enabled in the associated X509_VERIFY_PARAM structure. This might be used where additional "useful" CRLs are supplied as part of a protocol, for example in a PKCS#7 structure.

Legacy applications might implicitly use an X509_STORE_CTX like this:

X509_STORE_CTX ctx;
X509_STORE_CTX_init(&ctx, store, cert, chain);
🚨 This is not recommended in new applications, bestie. It's giving old-school stack vibes. New apps should instead do:
X509_STORE_CTX *ctx;
ctx = X509_STORE_CTX_new();
if (ctx == NULL)
        /* Bad error */
X509_STORE_CTX_init(ctx, store, cert, chain);

🫢 RETURN VALUES 🫢

  • X509_STORE_CTX_new() β€” returns a newly allocated context or NULL if an error occurred.
  • X509_STORE_CTX_init() β€” returns 1 for success or 0 if an error occurred.
  • X509_STORE_CTX_get0_store() β€” returns the internal pointer to the trusted certificate store or NULL if none was set.
  • X509_STORE_CTX_get0_cert() β€” returns the internal pointer to the certificate to be verified or NULL if no such certificate was set.
  • X509_STORE_CTX_get0_untrusted() β€” returns the internal pointer to the set of additional, untrusted certificates or NULL if no set of additional certificates was provided.

πŸ”₯ SEE ALSO πŸ”₯

πŸŽ€ HISTORY πŸŽ€

  • X509_STORE_CTX_init(), X509_STORE_CTX_cleanup(), X509_STORE_CTX_set_cert(), and X509_STORE_CTX_set_chain() first appeared in SSLeay 0.8.0 and have been available since OpenBSD 2.4.
  • X509_STORE_CTX_new() and X509_STORE_CTX_free() first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7.
  • X509_STORE_CTX_trusted_stack() first appeared in OpenSSL 0.9.6 and has been available since OpenBSD 2.9.
  • X509_STORE_CTX_get0_store() first appeared in OpenSSL 1.0.2.
  • X509_STORE_CTX_set0_trusted_stack(), X509_STORE_CTX_get0_cert(), X509_STORE_CTX_set0_untrusted(), and X509_STORE_CTX_get0_untrusted() first appeared in OpenSSL 1.1.0. These functions have been available since OpenBSD 6.3.

And that's the tea, bestie πŸ“βœ¨ β€” this whole page came from the Debian build dated June 8, 2025, filed under X509_STORE_CTX_NEW(3). Slay, read, reuse, and don't free your pointers too early πŸ’…πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from