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

# libressl-PEM\_read\_bio\_PrivateKey(3)

PEM routines

## NAME

**PEM\_read\_bio\_PrivateKey**, **PEM\_read\_PrivateKey**, **PEM\_write\_bio\_PrivateKey**, **PEM\_write\_PrivateKey**, **PEM\_write\_bio\_PKCS8PrivateKey**, **PEM\_write\_PKCS8PrivateKey**, **PEM\_write\_bio\_PKCS8PrivateKey\_nid**, **PEM\_write\_PKCS8PrivateKey\_nid**, **PEM\_read\_bio\_PKCS8**, **PEM\_read\_PKCS8**, **PEM\_write\_bio\_PKCS8**, **PEM\_write\_PKCS8**, **PEM\_read\_bio\_PKCS8\_PRIV\_KEY\_INFO**, **PEM\_read\_PKCS8\_PRIV\_KEY\_INFO**, **PEM\_write\_bio\_PKCS8\_PRIV\_KEY\_INFO**, **PEM\_write\_PKCS8\_PRIV\_KEY\_INFO**, **PEM\_read\_bio\_PUBKEY**, **PEM\_read\_PUBKEY**, **PEM\_write\_bio\_PUBKEY**, **PEM\_write\_PUBKEY**, **PEM\_read\_bio\_RSAPrivateKey**, **PEM\_read\_RSAPrivateKey**, **PEM\_write\_bio\_RSAPrivateKey**, **PEM\_write\_RSAPrivateKey**, **PEM\_read\_bio\_RSAPublicKey**, **PEM\_read\_RSAPublicKey**, **PEM\_write\_bio\_RSAPublicKey**, **PEM\_write\_RSAPublicKey**, **PEM\_read\_bio\_RSA\_PUBKEY**, **PEM\_read\_RSA\_PUBKEY**, **PEM\_write\_bio\_RSA\_PUBKEY**, **PEM\_write\_RSA\_PUBKEY**, **PEM\_read\_bio\_DSAPrivateKey**, **PEM\_read\_DSAPrivateKey**, **PEM\_write\_bio\_DSAPrivateKey**, **PEM\_write\_DSAPrivateKey**, **PEM\_read\_bio\_DSA\_PUBKEY**, **PEM\_read\_DSA\_PUBKEY**, **PEM\_write\_bio\_DSA\_PUBKEY**, **PEM\_write\_DSA\_PUBKEY**, **PEM\_read\_bio\_DSAparams**, **PEM\_read\_DSAparams**, **PEM\_write\_bio\_DSAparams**, **PEM\_write\_DSAparams**, **PEM\_read\_bio\_DHparams**, **PEM\_read\_DHparams**, **PEM\_write\_bio\_DHparams**, **PEM\_write\_DHparams**, **PEM\_read\_bio\_ECPKParameters**, **PEM\_read\_ECPKParameters**, **PEM\_write\_bio\_ECPKParameters**, **PEM\_write\_ECPKParameters**, **PEM\_read\_bio\_ECPrivateKey**, **PEM\_read\_ECPrivateKey**, **PEM\_write\_bio\_ECPrivateKey**, **PEM\_write\_ECPrivateKey**, **PEM\_read\_bio\_EC\_PUBKEY**, **PEM\_read\_EC\_PUBKEY**, **PEM\_write\_bio\_EC\_PUBKEY**, **PEM\_write\_EC\_PUBKEY**, **PEM\_read\_bio\_X509**, **PEM\_read\_X509**, **PEM\_write\_bio\_X509**, **PEM\_write\_X509**, **PEM\_read\_bio\_X509\_AUX**, **PEM\_read\_X509\_AUX**, **PEM\_write\_bio\_X509\_AUX**, **PEM\_write\_X509\_AUX**, **PEM\_read\_bio\_X509\_REQ**, **PEM\_read\_X509\_REQ**, **PEM\_write\_bio\_X509\_REQ**, **PEM\_write\_X509\_REQ**, **PEM\_write\_bio\_X509\_REQ\_NEW**, **PEM\_write\_X509\_REQ\_NEW**, **PEM\_read\_bio\_X509\_CRL**, **PEM\_read\_X509\_CRL**, **PEM\_write\_bio\_X509\_CRL**, **PEM\_write\_X509\_CRL**, **PEM\_read\_bio\_PKCS7**, **PEM\_read\_PKCS7**, **PEM\_write\_bio\_PKCS7**, **PEM\_write\_PKCS7**, **PEM\_read\_CMS**, **PEM\_read\_bio\_CMS**, **PEM\_write\_CMS**, **PEM\_write\_bio\_CMS** – PEM routines

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

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

*EVP\_PKEY* *\**\

**PEM\_read\_bio\_PrivateKey**(*BIO \*bp*, *EVP\_PKEY \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*EVP\_PKEY* *\**\

**PEM\_read\_PrivateKey**(*FILE \*fp*, *EVP\_PKEY \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PrivateKey**(*BIO \*bp*, *EVP\_PKEY \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_PrivateKey**(*FILE \*fp*, *EVP\_PKEY \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PKCS8PrivateKey**(*BIO \*bp*, *EVP\_PKEY \*x*, *const EVP\_CIPHER \*enc*, *char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_PKCS8PrivateKey**(*FILE \*fp*, *EVP\_PKEY \*x*, *const EVP\_CIPHER \*enc*, *char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PKCS8PrivateKey\_nid**(*BIO \*bp*, *EVP\_PKEY \*x*, *int nid*, *char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_PKCS8PrivateKey\_nid**(*FILE \*fp*, *EVP\_PKEY \*x*, *int nid*, *char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*X509\_SIG* *\**\

**PEM\_read\_bio\_PKCS8**(*BIO \*bp*, *X509\_SIG \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*X509\_SIG* *\**\

**PEM\_read\_PKCS8**(*FILE \*fp*, *X509\_SIG \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PKCS8**(*BIO \*bp*, *X509\_SIG \*x*);

*int*\

**PEM\_write\_PKCS8**(*FILE \*fp*, *X509\_SIG \*x*);

*PKCS8\_PRIV\_KEY\_INFO* *\**\

**PEM\_read\_bio\_PKCS8\_PRIV\_KEY\_INFO**(*BIO \*bp*, *PKCS8\_PRIV\_KEY\_INFO \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*PKCS8\_PRIV\_KEY\_INFO* *\**\

**PEM\_read\_PKCS8\_PRIV\_KEY\_INFO**(*FILE \*fp*, *PKCS8\_PRIV\_KEY\_INFO \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PKCS8\_PRIV\_KEY\_INFO**(*BIO \*bp*, *PKCS8\_PRIV\_KEY\_INFO \*x*);

*int*\

**PEM\_write\_PKCS8\_PRIV\_KEY\_INFO**(*FILE \*fp*, *PKCS8\_PRIV\_KEY\_INFO \*x*);

*EVP\_PKEY* *\**\

**PEM\_read\_bio\_PUBKEY**(*BIO \*bp*, *EVP\_PKEY \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*EVP\_PKEY* *\**\

**PEM\_read\_PUBKEY**(*FILE \*fp*, *EVP\_PKEY \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PUBKEY**(*BIO \*bp*, *EVP\_PKEY \*x*);

*int*\

**PEM\_write\_PUBKEY**(*FILE \*fp*, *EVP\_PKEY \*x*);

*RSA* *\**\

**PEM\_read\_bio\_RSAPrivateKey**(*BIO \*bp*, *RSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*RSA* *\**\

**PEM\_read\_RSAPrivateKey**(*FILE \*fp*, *RSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_RSAPrivateKey**(*BIO \*bp*, *RSA \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_RSAPrivateKey**(*FILE \*fp*, *RSA \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*RSA* *\**\

**PEM\_read\_bio\_RSAPublicKey**(*BIO \*bp*, *RSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*RSA* *\**\

**PEM\_read\_RSAPublicKey**(*FILE \*fp*, *RSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_RSAPublicKey**(*BIO \*bp*, *RSA \*x*);

*int*\

**PEM\_write\_RSAPublicKey**(*FILE \*fp*, *RSA \*x*);

*RSA* *\**\

**PEM\_read\_bio\_RSA\_PUBKEY**(*BIO \*bp*, *RSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*RSA* *\**\

**PEM\_read\_RSA\_PUBKEY**(*FILE \*fp*, *RSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_RSA\_PUBKEY**(*BIO \*bp*, *RSA \*x*);

*int*\

**PEM\_write\_RSA\_PUBKEY**(*FILE \*fp*, *RSA \*x*);

*DSA* *\**\

**PEM\_read\_bio\_DSAPrivateKey**(*BIO \*bp*, *DSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*DSA* *\**\

**PEM\_read\_DSAPrivateKey**(*FILE \*fp*, *DSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_DSAPrivateKey**(*BIO \*bp*, *DSA \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_DSAPrivateKey**(*FILE \*fp*, *DSA \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*DSA* *\**\

**PEM\_read\_bio\_DSA\_PUBKEY**(*BIO \*bp*, *DSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*DSA* *\**\

**PEM\_read\_DSA\_PUBKEY**(*FILE \*fp*, *DSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_DSA\_PUBKEY**(*BIO \*bp*, *DSA \*x*);

*int*\

**PEM\_write\_DSA\_PUBKEY**(*FILE \*fp*, *DSA \*x*);

*DSA* *\**\

**PEM\_read\_bio\_DSAparams**(*BIO \*bp*, *DSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*DSA* *\**\

**PEM\_read\_DSAparams**(*FILE \*fp*, *DSA \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_DSAparams**(*BIO \*bp*, *DSA \*x*);

*int*\

**PEM\_write\_DSAparams**(*FILE \*fp*, *DSA \*x*);

*DH* *\**\

**PEM\_read\_bio\_DHparams**(*BIO \*bp*, *DH \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*DH* *\**\

**PEM\_read\_DHparams**(*FILE \*fp*, *DH \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_DHparams**(*BIO \*bp*, *DH \*x*);

*int*\

**PEM\_write\_DHparams**(*FILE \*fp*, *DH \*x*);

*EC\_GROUP* *\**\

**PEM\_read\_bio\_ECPKParameters**(*BIO \*bp*, *EC\_GROUP \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*EC\_GROUP* *\**\

**PEM\_read\_ECPKParameters**(*FILE \*fp*, *EC\_GROUP \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_ECPKParameters**(*BIO \*bp*, *const EC\_GROUP \*x*);

*int*\

**PEM\_write\_ECPKParameters**(*FILE \*fp*, *const EC\_GROUP \*x*);

*EC\_KEY* *\**\

**PEM\_read\_bio\_ECPrivateKey**(*BIO \*bp*, *EC\_KEY \*\*key*, *pem\_password\_cb \*cb*, *void \*u*);

*EC\_KEY* *\**\

**PEM\_read\_ECPrivateKey**(*FILE \*fp*, *EC\_KEY \*\*eckey*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_ECPrivateKey**(*BIO \*bp*, *EC\_KEY \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_ECPrivateKey**(*FILE \*fp*, *EC\_KEY \*x*, *const EVP\_CIPHER \*enc*, *unsigned char \*kstr*, *int klen*, *pem\_password\_cb \*cb*, *void \*u*);

*EC\_KEY* *\**\

**PEM\_read\_bio\_EC\_PUBKEY**(*BIO \*bp*, *EC\_KEY \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*EC\_KEY* *\**\

**PEM\_read\_EC\_PUBKEY**(*FILE \*fp*, *EC\_KEY \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_EC\_PUBKEY**(*BIO \*bp*, *EC\_KEY \*x*);

*int*\

**PEM\_write\_EC\_PUBKEY**(*FILE \*fp*, *EC\_KEY \*x*);

*X509* *\**\

**PEM\_read\_bio\_X509**(*BIO \*bp*, *X509 \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*X509* *\**\

**PEM\_read\_X509**(*FILE \*fp*, *X509 \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_X509**(*BIO \*bp*, *X509 \*x*);

*int*\

**PEM\_write\_X509**(*FILE \*fp*, *X509 \*x*);

*X509* *\**\

**PEM\_read\_bio\_X509\_AUX**(*BIO \*bp*, *X509 \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*X509* *\**\

**PEM\_read\_X509\_AUX**(*FILE \*fp*, *X509 \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_X509\_AUX**(*BIO \*bp*, *X509 \*x*);

*int*\

**PEM\_write\_X509\_AUX**(*FILE \*fp*, *X509 \*x*);

*X509\_REQ* *\**\

**PEM\_read\_bio\_X509\_REQ**(*BIO \*bp*, *X509\_REQ \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*X509\_REQ* *\**\

**PEM\_read\_X509\_REQ**(*FILE \*fp*, *X509\_REQ \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_X509\_REQ**(*BIO \*bp*, *X509\_REQ \*x*);

*int*\

**PEM\_write\_X509\_REQ**(*FILE \*fp*, *X509\_REQ \*x*);

*int*\

**PEM\_write\_bio\_X509\_REQ\_NEW**(*BIO \*bp*, *X509\_REQ \*x*);

*int*\

**PEM\_write\_X509\_REQ\_NEW**(*FILE \*fp*, *X509\_REQ \*x*);

*X509\_CRL* *\**\

**PEM\_read\_bio\_X509\_CRL**(*BIO \*bp*, *X509\_CRL \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*X509\_CRL* *\**\

**PEM\_read\_X509\_CRL**(*FILE \*fp*, *X509\_CRL \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_X509\_CRL**(*BIO \*bp*, *X509\_CRL \*x*);

*int*\

**PEM\_write\_X509\_CRL**(*FILE \*fp*, *X509\_CRL \*x*);

*PKCS7* *\**\

**PEM\_read\_bio\_PKCS7**(*BIO \*bp*, *PKCS7 \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*PKCS7* *\**\

**PEM\_read\_PKCS7**(*FILE \*fp*, *PKCS7 \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_bio\_PKCS7**(*BIO \*bp*, *PKCS7 \*x*);

*int*\

**PEM\_write\_PKCS7**(*FILE \*fp*, *PKCS7 \*x*);

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

*CMS\_ContentInfo* *\**\

**PEM\_read\_CMS**(*FILE \*fp*, *CMS\_ContentInfo \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*CMS\_ContentInfo* *\**\

**PEM\_read\_bio\_CMS**(*BIO \*bp*, *CMS\_ContentInfo \*\*x*, *pem\_password\_cb \*cb*, *void \*u*);

*int*\

**PEM\_write\_CMS**(*FILE \*fp*, *const CMS\_ContentInfo \*x*);

*int*\

**PEM\_write\_bio\_CMS**(*BIO \*bp*, *const CMS\_ContentInfo \*x*);

## DESCRIPTION

The PEM functions read or write structures in PEM format. In this sense PEM format is simply base64-encoded data surrounded by header lines; see PEM\_read(3) for more details.

For more details about the meaning of arguments see the PEM function arguments section.

Each operation has four functions associated with it. For brevity the term “*TYPE* functions” will be used to collectively refer to the **PEM\_read\_bio\_TYPE**(), **PEM\_read\_TYPE**(), **PEM\_write\_bio\_TYPE**(), and **PEM\_write\_TYPE**() functions. If no set of specific functions exists for a given type, PEM\_ASN1\_read(3) can be used instead.

The **PrivateKey** functions read or write a private key in PEM format using an *EVP\_PKEY* structure. The write routines use "traditional" private key format and can handle both RSA and DSA private keys. The read functions can additionally transparently handle PKCS#8 format encrypted and unencrypted keys too.

**PEM\_write\_bio\_PKCS8PrivateKey**() and **PEM\_write\_PKCS8PrivateKey**() write a private key in an *EVP\_PKEY* structure in PKCS#8 *EncryptedPrivateKeyInfo* format using PKCS#5 v2.0 password based encryption algorithms. The *enc* argument specifies the encryption algorithm to use: unlike all other PEM routines, the encryption is applied at the PKCS#8 level and not in the PEM headers. If *enc* is NULL, then no encryption is used and a PKCS#8 *PrivateKeyInfo* structure is used instead.

**PEM\_write\_bio\_PKCS8PrivateKey\_nid**() and **PEM\_write\_PKCS8PrivateKey\_nid**() also write out a private key as a PKCS#8 *EncryptedPrivateKeyInfo*. However they use PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm to use is specified in the *nid* parameter and should be the NID of the corresponding OBJECT IDENTIFIER.

The **PKCS8** functions process an encrypted private key using an *X509\_SIG* structure and the d2i\_X509\_SIG(3) function.

The **PKCS8\_PRIV\_KEY\_INFO** functions process a private key using a *PKCS8\_PRIV\_KEY\_INFO* structure.

The **PUBKEY** functions process a public key using an *EVP\_PKEY* structure. The public key is encoded as an ASN.1 *SubjectPublicKeyInfo* structure.

The **RSAPrivateKey** functions process an RSA private key using an *RSA* structure. They handle the same formats as the **PrivateKey** functions, but an error occurs if the private key is not RSA.

The **RSAPublicKey** functions process an RSA public key using an *RSA* structure. The public key is encoded using a PKCS#1 *RSAPublicKey* structure.

The **RSA\_PUBKEY** functions also process an RSA public key using an *RSA* structure. However the public key is encoded using an ASN.1 *SubjectPublicKeyInfo* structure and an error occurs if the public key is not RSA.

The **DSAPrivateKey** functions process a DSA private key using a *DSA* structure. They handle the same formats as the **PrivateKey** functions but an error occurs if the private key is not DSA.

The **DSA\_PUBKEY** functions process a DSA public key using a *DSA* structure. The public key is encoded using an ASN.1 *SubjectPublicKeyInfo* structure and an error occurs if the public key is not DSA.

The **DSAparams** functions process DSA parameters using a *DSA* structure. The parameters are encoded using a Dss-Parms structure as defined in RFC 2459.

The **DHparams** functions process DH parameters using a *DH* structure. The parameters are encoded using a PKCS#3 DHparameter structure.

The **ECPKParameters** functions process EC parameters using an *EC\_GROUP* structure and the d2i\_ECPKParameters(3) function.

The **ECPrivateKey** functions process an EC private key using an *EC\_KEY* structure.

The **EC\_PUBKEY** functions process an EC public key using an *EC\_KEY* structure.

The **X509** functions process an X509 certificate using an *X509* structure. They will also process a trusted X509 certificate but any trust settings are discarded.

The **X509\_AUX** functions process a trusted X509 certificate using an *X509* structure.

The **X509\_REQ** and **X509\_REQ\_NEW** functions process a PKCS#10 certificate request using an *X509\_REQ* structure. The **X509\_REQ** write functions use CERTIFICATE REQUEST in the header whereas the **X509\_REQ\_NEW** functions use NEW CERTIFICATE REQUEST (as required by some CAs). The **X509\_REQ** read functions will handle either form so there are no **X509\_REQ\_NEW** read functions.

The **X509\_CRL** functions process an X509 CRL using an *X509\_CRL* structure.

The **PKCS7** functions process a PKCS#7 *ContentInfo* using a *PKCS7* structure.

The **CMS** functions process a *CMS\_ContentInfo* structure.

The old **PrivateKey** write routines are retained for compatibility. New applications should write private keys using the **PEM\_write\_bio\_PKCS8PrivateKey**() or **PEM\_write\_PKCS8PrivateKey**() routines because they are more secure (they use an iteration count of 2048 whereas the traditional routines use a count of 1) unless compatibility with older versions of OpenSSL is important.

The **PrivateKey** read routines can be used in all applications because they handle all formats transparently.

### PEM function arguments

The PEM functions have many common arguments.

The *bp* parameter specifies the *BIO* to read from or write to.

The *fp* parameter specifies the *FILE* pointer to read from or write to.

The PEM read functions all take a pointer to pointer argument *x* and return a pointer of the same type. If *x* is NULL, then the parameter is ignored. If *x* is not NULL but \**x* is NULL, then the structure returned will be written to \**x*. If neither *x* nor \**x* are NULL, then an attempt is made to reuse the structure at \**x*, but see the BUGS and EXAMPLES sections. Irrespective of the value of *x*, a pointer to the structure is always returned, or NULL if an error occurred.

The PEM functions which write private keys take an *enc* parameter, which specifies the encryption algorithm to use. Encryption is done at the PEM level. If this parameter is set to NULL, then the private key is written in unencrypted form.

The optional arguments *u* and *cb* are a passphrase used for encrypting a PEM structure or a callback to obtain the passphrase; see pem\_password\_cb(3) for details.

For the PEM write routines, if the *kstr* parameter is not NULL, then *klen* bytes at *kstr* are used as the passphrase and *cb* is ignored.

### PEM encryption format

These old **PrivateKey** routines use a non-standard technique for encryption.

The private key (or other data) takes the following form:

```
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
...base64 encoded data...
-----END RSA PRIVATE KEY-----
```

The line beginning with “DEK-Info” contains two comma separated pieces of information: the encryption algorithm name as used by EVP\_get\_cipherbyname(3) and an 8-byte salt encoded as a set of hexadecimal digits.

After this is the base64-encoded encrypted data.

The encryption key is determined using EVP\_BytesToKey(3), using the salt and an iteration count of 1. The IV used is the value of the salt and \*not\* the IV returned by EVP\_BytesToKey(3).

## RETURN VALUES

The read routines return either a pointer to the structure read or NULL if an error occurred.

The write routines return 1 for success or 0 for failure.

## EXAMPLES

Although the PEM routines take several arguments, in almost all applications most of them are set to 0 or NULL.

Read a certificate in PEM format from a *BIO*:

```
X509 *x;
x = PEM_read_bio_X509(bp, NULL, 0, NULL);
if (x == NULL) {
	/* Error */
}
```

Alternative method:

```
X509 *x = NULL;
if (!PEM_read_bio_X509(bp, &x, 0, NULL)) {
	/* Error */
}
```

Write a certificate to a *BIO*:

```
if (!PEM_write_bio_X509(bp, x)) {
	/* Error */
}
```

Write an unencrypted private key to a *FILE*:

```
if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL)) {
	/* Error */
}
```

Write a private key (using traditional format) to a *BIO* using triple DES encryption; the pass phrase is prompted for:

```
if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(),
    NULL, 0, 0, NULL)) {
	/* Error */
}
```

Write a private key (using PKCS#8 format) to a *BIO* using triple DES encryption, using the pass phrase "hello":

```
if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
    NULL, 0, 0, "hello")) {
	/* Error */
}
```

Read a private key from a *BIO* using the pass phrase "hello":

```
key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello");
if (key == NULL) {
	/* Error */
}
```

Read a private key from a *BIO* using a pass phrase callback:

```
key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
if (key == NULL) {
	/* Error */
}
```

Skeleton pass phrase callback:

```
int
pass_cb(char *buf, int size, int rwflag, void *u)
{
	char	*tmp;
	size_t	 len;
	/* We'd probably do something else if 'rwflag' is 1 */
	printf("Enter pass phrase for \"%s\"\n", u);
	/*
	 * Instead of the following line, get the passphrase
	 * from the user in some way.
	 */
	tmp = "hello";
	if (tmp == NULL) /* An error occurred. */
		return -1;
	len = strlen(tmp);
	if (len == 0) /* Treat an empty passphrase as an error, too. */
		return -1;
	/* if too long, truncate */
	if (len > size)
		len = size;
	memcpy(buf, tmp, len);
	return len;
}
```

## SEE ALSO

BIO\_new(3), DSA\_new(3), PEM\_ASN1\_read(3), PEM\_bytes\_read\_bio(3), PEM\_read(3), PEM\_read\_SSL\_SESSION(3), PEM\_write\_bio\_CMS\_stream(3), PEM\_write\_bio\_PKCS7\_stream(3), PEM\_X509\_INFO\_read\_bio(3), RSA\_new(3), X509\_CRL\_new(3), X509\_REQ\_new(3), X509\_SIG\_new(3)

## HISTORY

**PEM\_read\_X509**() and **PEM\_write\_X509**() appeared in SSLeay 0.4 or earlier. **PEM\_read\_X509\_REQ**(), **PEM\_write\_X509\_REQ**(), **PEM\_read\_X509\_CRL**(), and **PEM\_write\_X509\_CRL**() first appeared in SSLeay 0.4.4. **PEM\_read\_RSAPrivateKey**(), **PEM\_write\_RSAPrivateKey**(), **PEM\_read\_DHparams**(), **PEM\_write\_DHparams**(), **PEM\_read\_PKCS7**(), and **PEM\_write\_PKCS7**() first appeared in SSLeay 0.5.1. **PEM\_read\_bio\_PrivateKey**(), **PEM\_read\_PrivateKey**(), **PEM\_read\_bio\_RSAPrivateKey**(), **PEM\_write\_bio\_RSAPrivateKey**(), **PEM\_read\_bio\_DSAPrivateKey**(), **PEM\_read\_DSAPrivateKey**(), **PEM\_write\_bio\_DSAPrivateKey**(), **PEM\_write\_DSAPrivateKey**(), **PEM\_read\_bio\_DHparams**(), **PEM\_write\_bio\_DHparams**(), **PEM\_read\_bio\_X509**(), **PEM\_write\_bio\_X509**(), **PEM\_read\_bio\_X509\_REQ**(), **PEM\_write\_bio\_X509\_REQ**(), **PEM\_read\_bio\_X509\_CRL**(), **PEM\_write\_bio\_X509\_CRL**(), **PEM\_read\_bio\_PKCS7**(), and **PEM\_write\_bio\_PKCS7**() first appeared in SSLeay 0.6.0. **PEM\_write\_bio\_PrivateKey**(), **PEM\_write\_PrivateKey**(), **PEM\_read\_bio\_DSAparams**(), **PEM\_read\_DSAparams**(), **PEM\_write\_bio\_DSAparams**(), and **PEM\_write\_DSAparams**() first appeared in SSLeay 0.8.0. **PEM\_read\_bio\_RSAPublicKey**(), **PEM\_read\_RSAPublicKey**(), **PEM\_write\_bio\_RSAPublicKey**(), and **PEM\_write\_RSAPublicKey**() first appeared in SSLeay 0.8.1. All these functions have been available since OpenBSD 2.4.

**PEM\_write\_bio\_PKCS8PrivateKey**(), **PEM\_write\_PKCS8PrivateKey**(), **PEM\_read\_bio\_PKCS8**(), **PEM\_read\_PKCS8**(), **PEM\_write\_bio\_PKCS8**(), **PEM\_write\_PKCS8**(), **PEM\_read\_bio\_PKCS8\_PRIV\_KEY\_INFO**(), **PEM\_read\_PKCS8\_PRIV\_KEY\_INFO**(), **PEM\_write\_bio\_PKCS8\_PRIV\_KEY\_INFO**(), **PEM\_write\_PKCS8\_PRIV\_KEY\_INFO**(),

**PEM\_write\_bio\_PKCS8PrivateKey\_nid**(), **PEM\_write\_PKCS8PrivateKey\_nid**(), **PEM\_read\_bio\_PUBKEY**(), **PEM\_read\_PUBKEY**(), **PEM\_write\_bio\_PUBKEY**(), **PEM\_write\_PUBKEY**(), **PEM\_read\_bio\_RSA\_PUBKEY**(), **PEM\_read\_RSA\_PUBKEY**(), **PEM\_write\_bio\_RSA\_PUBKEY**(), **PEM\_write\_RSA\_PUBKEY**(), **PEM\_read\_bio\_DSA\_PUBKEY**(), **PEM\_read\_DSA\_PUBKEY**(), **PEM\_write\_bio\_DSA\_PUBKEY**(), **PEM\_write\_DSA\_PUBKEY**(), **PEM\_write\_bio\_X509\_REQ\_NEW**(), **PEM\_write\_X509\_REQ\_NEW**(), **PEM\_read\_bio\_X509\_AUX**(), **PEM\_read\_X509\_AUX**(), **PEM\_write\_bio\_X509\_AUX**(), and **PEM\_write\_X509\_AUX**() first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7.

**PEM\_read\_bio\_ECPKParameters**(), **PEM\_read\_ECPKParameters**(), **PEM\_write\_bio\_ECPKParameters**(), **PEM\_write\_ECPKParameters**(), **PEM\_read\_bio\_ECPrivateKey**(), **PEM\_read\_ECPrivateKey**(), **PEM\_write\_bio\_ECPrivateKey**(), **PEM\_write\_ECPrivateKey**(), **PEM\_read\_bio\_EC\_PUBKEY**(), **PEM\_read\_EC\_PUBKEY**(), **PEM\_write\_bio\_EC\_PUBKEY**(), and **PEM\_write\_EC\_PUBKEY**() first appeared in OpenSSL 0.9.8 and have been available since OpenBSD 4.5.

**PEM\_read\_CMS**(), **PEM\_read\_bio\_CMS**(), **PEM\_write\_CMS**(), and **PEM\_write\_bio\_CMS**() first appeared in OpenSSL 0.9.8h and have been available since OpenBSD 6.7.

## CAVEATS

A frequent cause of problems is attempting to use the PEM routines like this:

```
X509 *x;
PEM_read_bio_X509(bp, &x, 0, NULL);
```

This is a bug because an attempt will be made to reuse the data at *x*, which is an uninitialised pointer.

These functions make no assumption regarding the pass phrase received from the password callback. It will simply be treated as a byte sequence.

## BUGS

The PEM read routines in some versions of OpenSSL will not correctly reuse an existing structure. Therefore

```
PEM_read_bio_X509(bp, &x, 0, NULL);
```

where *x* already contains a valid certificate may not work, whereas

```
X509_free(x);
x = PEM_read_bio_X509(bp, NULL, 0, NULL);
```

is guaranteed to work.
