STACK_OF(3)
okay bestie, welcome to STACK_OF(3) from the Library Functions Manual β the libcrypto page about variable-sized arrays of pointers, called OpenSSL stacks. Yes, the name is giving stack, but the plot twist is that it's really a pointer-array situation. π
π NAME π
STACK_OF β variable-sized arrays of pointers, called OpenSSL stacks. That's the whole name, no cap. π
β¨ SYNOPSIS β¨
the synopsis is literally:
library βlibcryptoβ #include <openssl/safestack.h>
STACK_OF(type);
π¦ DESCRIPTION π¦
so the <openssl/safestack.h> header gives you a fragile, unusually complicated system of macro-generated wrappers around the functions described in the OPENSSL_sk_new(3) manual page. It is intended to implement superficially type-safe variable-sized arrays of pointers, somewhat misleadingly called βstacksβ by OpenSSL. Due to the excessive number of API functions, it is impossible to properly document this system. In particular, calling man(1) for any of the functions operating on stacks cannot yield any result. like, at all. π
Unfortunately, application programs can hardly avoid using the concept because several important OpenSSL APIs rely on it; see the βSEE ALSOβ section for examples. Even though both pages are more complicated than any manual page ought to be, using the concept safely requires a complete understanding of all the details in both this manual page and in OPENSSL_sk_new(3). iykyk. π«Ά
The STACK_OF() macro takes a type name as its argument, typically the name of a type that has been defined as an alias for a specific struct type using a typedef declaration. It expands to an incomplete struct type which is intended to represent a βstackβ of objects of the given type. That type does not actually exist, so it is not possible to define, for example, an automatic variable STACK_OF(X509) my_certificates; it is only possible to define pointers to stacks, for example STACK_OF(X509) *my_certificates. The only way such pointers can ever be used is by wrapper functions casting them to the type _STACK * described in OPENSSL_sk_new(3). that's the whole lore. π
For a considerable number of types, OpenSSL provides one wrapper function for each function described in OPENSSL_sk_new(3). The names of these wrapper functions are usually constructed by inserting the name of the type and an underscore after the sk_ prefix of the function name. Usually, where the real functions take void * arguments, the wrappers take pointers to the type in question, and where the real functions take _STACK * arguments, the wrappers take pointers to STACK_OF(type). The same applies to return values. Various exceptions to all this exist, but the above applies to all the types listed below. slay for consistency, honestly. β¨
Using the above may make sense for the following types because public API functions exist that take stacks of these types as arguments or return them:
ASN1_INTEGERASN1_OBJECTASN1_UTF8STRINGCMS_RecipientInfoCMS_SignerInfoCONF_VALUEGENERAL_NAMESGENERAL_SUBTREEOPENSSL_STRING(which is justchar *)PKCS12_SAFEBAGPKCS7PKCS7_RECIP_INFOPKCS7_SIGNER_INFOPOLICYQUALINFOSRTP_PROTECTION_PROFILESSL_CIPHERSSL_COMPX509X509_ALGORX509_ATTRIBUTEX509_CRLX509_EXTENSIONX509_INFOX509_NAMEX509_OBJECTX509_POLICY_NODEX509_REVOKED
Additionally, some public API functions use the following types which are declared with typedef:
STACK_OF(ACCESS_DESCRIPTION)βAUTHORITY_INFO_ACCESSSTACK_OF(ASN1_OBJECT)βEXTENDED_KEY_USAGESTACK_OF(ASN1_TYPE)βASN1_SEQUENCE_ANYSTACK_OF(DIST_POINT)βCRL_DIST_POINTSSTACK_OF(GENERAL_NAME)βGENERAL_NAMESSTACK_OF(IPAddressFamily)βIPAddrBlocksSTACK_OF(POLICY_MAPPING)βPOLICY_MAPPINGSSTACK_OF(POLICYINFO)βCERTIFICATEPOLICIESSTACK_OF(X509_ALGOR)βX509_ALGORSSTACK_OF(X509_EXTENSION)βX509_EXTENSIONS
Even though the OpenSSL headers declare wrapper functions for many more types and even though the OpenSSL documentation says that users can declare their own stack types, using STACK_OF() with any type not listed here is strongly discouraged. For other types, there may be subtle, undocumented differences in syntax and semantics, and attempting to declare custom stack types is very error prone; using plain C arrays of pointers to the desired type is much simpler and less dangerous.
π¨ bestie warning: custom stack types are not the vibe. subtle, undocumented differences in syntax and semantics can absolutely jump out, and it is very error prone. plain C arrays of pointers to the desired type are much simpler and less dangerous. periodt. π
πΈ EXAMPLES πΈ
The following program creates a certificate object, puts two pointers to it on a stack, and uses X509_free(3) to clean up properly:
#include <err.h>
#include <stdio.h>
#include <openssl/x509.h>
int
main(void)
{
STACK_OF(X509) *stack;
X509 *x;
if ((stack = sk_X509_new_null()) == NULL)
err(1, NULL);
if ((x = X509_new()) == NULL)
err(1, NULL);
if (sk_X509_push(stack, x) == 0)
err(1, NULL);
if (X509_up_ref(x) == 0)
errx(1, "X509_up_ref failed");
if (sk_X509_push(stack, x) == 0)
err(1, NULL);
printf("%d pointers: %p, %p\n", sk_X509_num(stack),
sk_X509_value(stack, 0), sk_X509_value(stack, 1));
sk_X509_pop_free(stack, X509_free);
return 0;
}
The output looks similar to:
2 pointers: 0x4693ff24c00, 0x4693ff24c00
π§ SEE ALSO π§
go check these besties out:
crypto(3)OCSP_request_sign(3)OPENSSL_sk_new(3)PKCS12_parse(3)PKCS7_encrypt(3)SSL_CTX_set_client_CA_list(3)SSL_get_ciphers(3)SSL_get_peer_cert_chain(3)SSL_load_client_CA_file(3)X509_CRL_get_REVOKED(3)X509_STORE_CTX_get0_chain(3)
π HISTORY π
The STACK_OF() macro first appeared in OpenSSL 0.9.3 and has been available since OpenBSD 2.6. iconic behavior, honestly. π
π This page came from Debian, dated June 8, 2025, and yes bestie, it's STACK_OF(3) β mwah, periodt. π
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2018 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms