PEM_READ(3ossl)

πŸ’– PEM_READ β€” the PEM encoding routines, bestie edition πŸ’–

okay so this is the PEM_read(3ossl) page from the OpenSSL manual, and we're gonna get you fluent in it fr fr. no gatekeeping here 🫢


✨ NAME ✨

PEM_read, PEM_read_bio, PEM_do_header, PEM_get_EVP_CIPHER_INFO, PEM_write, PEM_write_bio, PEM_ASN1_write, PEM_ASN1_write_bio, PEM_ASN1_write_bio_ctx β€” the PEM encoding routines πŸ’…

like, all of them. the whole little family. iconic behavior honestly.


🌸 SYNOPSIS 🌸

#include <openssl/pem.h>

int PEM_read(FILE *fp, char **name, char **header,
             unsigned char **data, long *len);
int PEM_read_bio(BIO *bp, char **name, char **header,
                 unsigned char **data, long *len);

int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo);
int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len,
                  pem_password_cb *cb, void *u);

int PEM_write(FILE *fp, const char *name, const char *header,
              const unsigned char *data, long len);
int PEM_write_bio(BIO *bp, const char *name, const char *header,
                  const unsigned char *data, long len);
int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
                   const void *x, const EVP_CIPHER *enc,
                   const unsigned char *kstr, int klen,
                   pem_password_cb *callback, void *u);
int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
                       const void *x, const EVP_CIPHER *enc,
                       const unsigned char *kstr, int klen,
                       pem_password_cb *callback, void *u);
int PEM_ASN1_write_bio_ctx(OSSL_i2d_of_void_ctx *i2d, void *vctx,
                           const char *name, BIO *bp, const void *x,
                           const EVP_CIPHER *enc, const unsigned char *kstr,
                           int klen, pem_password_cb *callback, void *u);

no cap, that's the whole squad up there πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so these functions read and write PEM-encoded objects, using the PEM type name, any extra header info, and the raw data of length len. that's the vibe.

PEM is the term for the binary content encoding that got first defined in IETF RFC 1421. the content is literally just a series of base64-encoded lines, surrounded by begin/end markers, each one on its own line. example time, bestie:

-----BEGIN PRIVATE KEY-----
MIICdg....
... bhTQ==
-----END PRIVATE KEY-----

optional header line(s) can show up after the begin line, and whether they exist depends on the type of object being written or read. it's giving "depends on the situation" energy ✨

PEM_write() writes to the file fp, while PEM_write_bio() writes to the BIO bp. the name is the name to use in the marker, header is the header value or NULL, and data and len specify the data and its length.

the final data buffer is typically an ASN.1 object which can be decoded with the d2i function appropriate to the type name β€” go look at d2i_X509(3) for examples πŸ‘‘

PEM_read() reads from the file fp, while PEM_read_bio() reads from the BIO bp. both of them skip any non-PEM data that precedes the start of the next PEM object (yes, they just walk right past it, main character energy). when an object is successfully retrieved, the type name from the "----BEGIN <type>-----" comes back via the name argument, any encapsulation headers come back in header, and the base64-decoded content plus its length come back via data and len respectively. the name, header and data pointers are allocated via OPENSSL_malloc() and you β€” the caller β€” should free them via OPENSSL_free() when they're no longer needed. don't ghost them, free them πŸ’€

PEM_get_EVP_CIPHER_INFO() can be used to figure out whether the data returned by PEM_read() or PEM_read_bio() is encrypted, and to retrieve the associated cipher and IV. the caller passes a pointer to a structure of type EVP_CIPHER_INFO via the cinfo argument, plus the header returned via PEM_read() or PEM_read_bio(). if the call succeeds, 1 is returned and the cipher and IV are stored at the address pointed to by cinfo. when the header is malformed, or not supported, or when the cipher is unknown, or when some internal error happens, 0 is returned. this function is deprecated β€” see NOTES below, babe.

PEM_do_header() can then be used to decrypt the data if the header indicates encryption. the cinfo argument is a pointer to the structure initialized by the previous call to PEM_get_EVP_CIPHER_INFO(). the data and len arguments are those returned by the previous call to PEM_read() or PEM_read_bio(). the cb and u arguments make it possible to override the default password prompt function, as described in PEM_read_PrivateKey(3). on successful completion, the data is decrypted in place, and len is updated to indicate the plaintext length. this function is also deprecated β€” again, see NOTES.

if the data is a priori known to not be encrypted, then neither PEM_do_header() nor PEM_get_EVP_CIPHER_INFO() need be called. skip the whole step, periodt. πŸŽ€


⭐ RETURN VALUES ⭐

  • PEM_read() and PEM_read_bio() return 1 on success and 0 on failure β€” and the failure case includes when no more PEM objects remain in the input file. to distinguish end of file from more serious errors, the caller must peek at the error stack and check for PEM_R_NO_START_LINE, which indicates that no more PEM objects were found. see ERR_peek_last_error(3), ERR_GET_REASON(3).
  • PEM_get_EVP_CIPHER_INFO() and PEM_do_header() return 1 on success, and 0 on failure.
😭 heads up bestie: the data is likely meaningless if these functions fail. not a cute look. check your return values.

πŸ”₯ NOTES πŸ”₯

okay so PEM_get_EVP_CIPHER_INFO() and PEM_do_header() are deprecated. and the reason is: the underlying PEM encryption format is obsolete and should be avoided. it uses an encryption format with an OpenSSL-specific key-derivation function, which employs MD5 with an iteration count of 1! yeah. one. πŸ’€ instead, private keys should be stored in PKCS#8 form, with a strong PKCS#5 v2.0 PBE. see PEM_write_PrivateKey(3) and d2i_PKCS8PrivateKey_bio(3).

PEM_do_header() makes no assumption regarding the pass phrase received from the password callback. it will simply be treated as a byte sequence. that's it, that's the whole deal.

PEM_write() and PEM_write_bio() return the number of encoded bytes (not counting the PEM header and end marker) written on success, or 0 on failure. count carefully, bestie, the markers don't count πŸ’…

PEM_ASN1_write_bio() and PEM_ASN1_write_bio_ctx() return 1 on success and 0 on failure. the latter function passes an additional application-provided context value to the i2d function that serialises the input ASN.1 object. so extra context, extra power 🫢


🌈 SEE ALSO 🌈


πŸ‘‘ HISTORY πŸ‘‘

the PEM_ASN1_write_bio_ctx() function was added in OpenSSL 3.5. new-ish but already slaying ✨


Copyright 1998-2025 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, bestie, you ate it πŸ“ this was OpenSSL 3.5.9, dated 2026-09-29, and the manual you just read is PEM_READ(3ossl). go forth and encode responsibly πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from