X509_NEW(3ossl)
π X509_NEW β bestie edition π
okay so this is X509_NEW(3ossl), a page from the OpenSSL manual, and it's all about the allocation + deallocation glow-up for X509 certificates. sit with me, we're gonna get through this together π«Ά
β¨ NAME β¨
X509_new, X509_new_ex, X509_free, X509_up_ref, X509_chain_up_ref, OSSL_STACK_OF_X509_free β the X509 certificate ASN1 allocation and deallocation functions. yes bestie, this is the birth-and-death page for certificate objects π
πΈ SYNOPSIS πΈ
#include <openssl/x509.h>
X509 *X509_new(void);
X509 *X509_new_ex(OSSL_LIB_CTX *libctx, const char *propq);
void X509_free(X509 *a);
int X509_up_ref(X509 *a);
STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *x);
void OSSL_STACK_OF_X509_free(STACK_OF(X509) *certs);
π¦ DESCRIPTION π¦
so the X509 ASN1 allocation routines allocate and free an X509 structure, which is literally the representation of an X509 certificate. that's the whole vibe π
X509_new_ex() allocates and initializes an X509 structure with a library context of libctx, a property query of propq, and a reference count of 1. and the way that matters is: lots of X509 functions β like X509_check_purpose(), and X509_verify() β use this library context to pick which providers supply the fetched algorithms (SHA1 is used internally, iykyk). this freshly created X509 object can then be used when you're loading binary data with d2i_X509(). slay β¨
X509_new() is the same energy as X509_new_ex() except it sets the library context and property query to NULL. which means the default (NULL) library context gets used for any X509 operations that need algorithm fetches. no cap.
X509_free() decrements the reference count of the X509 structure a and frees it up if that reference count is zero. and if the argument is NULL? bestie, nothing happens. we simply do not engage π
X509_up_ref() increments the reference count of a. that's it, that's the function π
X509_chain_up_ref() increases the reference count of all the certificates in chain x and returns a copy of the stack β or an empty stack if a is NULL.
OSSL_STACK_OF_X509_free() deallocates the given list of pointers to certificates, after calling X509_free() on all its elements. and if the argument is NULL, nothing is done. iconic behavior tbh.
π NOTES π
X509_up_ref() is useful if a certificate structure is being used by several different operations, each of which will free it up after use β this avoids the need to duplicate the entire certificate structure. it's giving sharing-is-caring, but make it memory-safe π§
X509_chain_up_ref() doesn't just up the reference count of each certificate. it also returns a copy of the stack, using sk_X509_dup(). but it serves a similar purpose: the returned chain persists after the original has been freed. obsessed.
β RETURN VALUES β
- if the allocation fails,
X509_new()returnsNULLand sets an error code that you can grab withERR_get_error(3). otherwise it returns a pointer to the newly allocated structure. it's giving all-or-nothing π X509_up_ref()returns1for success and0for failure.X509_chain_up_ref()returns a copy of the stack, orNULLif an error occurred.OSSL_STACK_OF_X509_free()has no return value. it'svoid, bestie, it simply does not report back π
π SEE ALSO π
d2i_X509(3)ERR_get_error(3)X509_CRL_get0_by_serial(3)X509_get0_signature(3)X509_get_ext_d2i(3)X509_get_extension_flags(3)X509_get_pubkey(3)X509_get_subject_name(3)X509_get_version(3)X509_NAME_add_entry_by_txt(3)X509_NAME_ENTRY_get_object(3)X509_NAME_get_index_by_NID(3)X509_NAME_print_ex(3)X509_sign(3)X509V3_get_d2i(3)X509_verify_cert(3)
π HISTORY π
X509_new_ex()was added in OpenSSL 3.0.OSSL_STACK_OF_X509_free()was added in OpenSSL 3.2.
π COPYRIGHT π
Copyright 2002-2024 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>.
okay that's the whole page, no notes, no skips, every flag and every caveat is right there π
and that's a wrap bestie!! this page is OpenSSL 3.5.9, dated 2026-09-29, and it lives at X509_NEW(3ossl). go forth and manage your reference counts responsibly, mwah πβ¨π
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms