---
name: "libressl-STACK_OF"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-STACK_OF"
---

# libressl-STACK\_OF(3)

variable-sized arrays of pointers, called OpenSSL stacks

## NAME

**STACK\_OF** – variable-sized arrays of pointers, called OpenSSL stacks

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/safestack.h\>**

**STACK\_OF**(*type*);

## DESCRIPTION

The ⟨openssl/safestack.h⟩ header provides 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)](/man/1.md) for any of the functions operating on stacks cannot yield any result.

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).

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).

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 questions, 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.

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\_INTEGER*, *ASN1\_OBJECT*, *ASN1\_UTF8STRING*, *CMS\_RecipientInfo*, *CMS\_SignerInfo*, *CONF\_VALUE*, *GENERAL\_NAMES*, *GENERAL\_SUBTREE*, *OPENSSL\_STRING* (which is just *char* *\**), *PKCS12\_SAFEBAG*, *PKCS7*, *PKCS7\_RECIP\_INFO*, *PKCS7\_SIGNER\_INFO*, *POLICYQUALINFO*, *SRTP\_PROTECTION\_PROFILE*, *SSL\_CIPHER*, *SSL\_COMP*, *X509*, *X509\_ALGOR*, *X509\_ATTRIBUTE*, *X509\_CRL*, *X509\_EXTENSION*, *X509\_INFO*, *X509\_NAME*, *X509\_OBJECT*, *X509\_POLICY\_NODE*, *X509\_REVOKED*.

Additionally, some public API functions use the following types which are declared with **typedef**:

| | |
| --- | --- |
| *STACK\_OF(ACCESS\_DESCRIPTION)* | *AUTHORITY\_INFO\_ACCESS* |
| *STACK\_OF(ASN1\_OBJECT)* | *EXTENDED\_KEY\_USAGE* |
| *STACK\_OF(ASN1\_TYPE)* | *ASN1\_SEQUENCE\_ANY* |
| *STACK\_OF(DIST\_POINT)* | *CRL\_DIST\_POINTS* |
| *STACK\_OF(GENERAL\_NAME)* | *GENERAL\_NAMES* |
| *STACK\_OF(IPAddressFamily)* | *IPAddrBlocks* |
| *STACK\_OF(POLICY\_MAPPING)* | *POLICY\_MAPPINGS* |
| *STACK\_OF(POLICYINFO)* | *CERTIFICATEPOLICIES* |
| *STACK\_OF(X509\_ALGOR)* | *X509\_ALGORS* |
| *STACK\_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.

## 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

[crypto(3)](/crypto/3.md), 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.
