---
name: "libressl-PEM_ASN1_read"
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-PEM_ASN1_read"
---

# libressl-PEM\_ASN1\_read(3)

PEM and DER decode an arbitrary ASN.1 value

## NAME

**d2i\_of\_void**, **PEM\_ASN1\_read**, **PEM\_ASN1\_read\_bio** – PEM and DER decode an arbitrary ASN.1 value

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

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

*typedef* *void* *\**\

**d2i\_of\_void**(*void \*\*val\_out*, *const unsigned char \*\*der\_in*, *long length*);

*void* *\**\

**PEM\_ASN1\_read**(*d2i\_of\_void \*d2i*, *const char \*name*, *FILE \*in\_fp*, *void \*\*val\_out*, *pem\_password\_cb \*cb*, *void \*u*);

*void* *\**\

**PEM\_ASN1\_read\_bio**(*d2i\_of\_void \*d2i*, *const char \*name*, *BIO \*in\_bp*, *void \*\*val\_out*, *pem\_password\_cb \*cb*, *void \*u*);

## DESCRIPTION

These functions read one object from *in\_fp* or *in\_bp* and perform both PEM and DER decoding. They are needed when more specific decoding functions like those documented in PEM\_read\_bio\_PrivateKey(3) and PEM\_read\_SSL\_SESSION(3) are inadequate for the type *name*.

For PEM decoding, PEM\_bytes\_read\_bio(3) is called internally. Consequently, the first object of type *name* is returned and preceding objects of other types are discarded. If necessary, data is decrypted, using *cb* and/or *u* if they are not NULL, as described in the pem\_password\_cb(3) manual page.

For subsequent DER decoding, pass a *d2i* callback function that is adequate for the type *name*, typically returning a pointer of a type more specific than *void* *\**. For example, d2i\_ASN1\_TYPE(3) can always be used and its manual page describes the required behaviour of the callback function to be passed. Normally, passing a more specific function is more useful; candidate functions can be found with ‘man -k Nm~^d2i\_’.

For the *name* argument, the PEM\_STRING\_\* string constants defined in ⟨openssl/pem.h⟩ can be used.

The *val\_out* argument is useless and its many dangers are described in detail in the d2i\_ASN1\_TYPE(3) manual page. To reduce the risk of bugs, always passing NULL is recommended.

## RETURN VALUES

These functions return a pointer to the decoded object or NULL if an error occurs. They fail if PEM\_bytes\_read\_bio(3) fails, for example because of invalid syntax in the input, an unknown encryption, or an invalid passphrase entered by the user. They also fail if *d2i* returns NULL, for example due to DER decoding errors.

**PEM\_ASN1\_read**() may also fail if memory is exhausted.

## EXAMPLES

Typical usage of **PEM\_ASN1\_read**() is demonstrated by the implementation of the more specific function to PEM and DER decode an X.509 certificate:

```
X509 *
PEM_read_X509(FILE *fp, X509 **val_out, pem_password_cb *cb, void *u)
{
	return PEM_ASN1_read((d2i_of_void *)d2i_X509, PEM_STRING_X509,
	    fp, (void **)val_out, cb, u);
}
```

## ERRORS

Diagnostics that can be retrieved with ERR\_get\_error(3), ERR\_GET\_REASON(3), and ERR\_reason\_error\_string(3) include:

- ERR\_R\_BUF\_LIB "BUF lib"

  **PEM\_ASN1\_read**() failed to set up a temporary BIO, for example because memory was exhausted.

- ERR\_R\_ASN1\_LIB "ASN1 lib"

  *d2i* returned NULL, for example due to a DER syntax error.

Additional types of errors can result from PEM\_bytes\_read\_bio(3).

## SEE ALSO

BIO\_new(3), d2i\_ASN1\_TYPE(3), PEM\_bytes\_read\_bio(3), PEM\_read(3), PEM\_read\_bio\_PrivateKey(3), PEM\_read\_SSL\_SESSION(3), PEM\_X509\_INFO\_read\_bio(3)

## HISTORY

These functions first appeared in SSLeay 0.5.1 and have been available since OpenBSD 2.4.
