PEM_BYTES_READ_BIO(3ossl)
πΈ PEM_BYTES_READ_BIO(3ossl) β read a PEM-encoded data structure from a BIO πΈ
Okay so first things first, bestie: this is from the 3ossl section of the OpenSSL manual. Today we're talking about PEM_bytes_read_bio and PEM_bytes_read_bio_secmem β literally the duo you need when you want to read PEM-encoded stuff from a BIO. No cap. π
β¨ SYNOPSIS β¨
#include <openssl/pem.h>
int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
const char *name, BIO *bp, pem_password_cb *cb,
void *u);
int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
const char *name, BIO *bp, pem_password_cb *cb,
void *u);
π DESCRIPTION π
Okay so here's the tea. PEM_bytes_read_bio() reads PEM-formatted data β and yes, we're talking IETF RFC 1421 and IETF RFC 7468, no cap β from the BIO bp for the data type you give it in name. That's stuff like RSA PRIVATE KEY, CERTIFICATE, etc.
If multiple PEM-encoded data structures are hanging out in the same stream, PEM_bytes_read_bio() will be like "not my type" and skip the non-matching data types, continuing to read. But non-PEM data present in the stream? That may cause an error, so watch out. π
The PEM header may indicate that the following data is encrypted. If so, the data will be decrypted, and it'll wait on user input to supply a passphrase if needed. The password callback cb and rock u are used to obtain the decryption passphrase, if applicable. So basically you're covered. π«Ά
Some data types have compatibility aliases β like a file containing X509 CERTIFICATE matching a request for the deprecated type CERTIFICATE. The actual type indicated by the file is returned in *pnm if pnm is non-NULL. The caller must free the storage pointed to by *pnm. Don't forget, periodt. π
The returned data is the DER-encoded form of the requested type, in *pdata with length *plen. The caller must free the storage pointed to by *pdata. Again: free it. π€
PEM_bytes_read_bio_secmem() is similar to PEM_bytes_read_bio(), but it uses memory from the secure heap for its temporary buffers and the storage returned in *pdata and *pnm. Accordingly, the caller must use OPENSSL_secure_free() to free that storage. Not free, not OPENSSL_free β OPENSSL_secure_free(). Iykyk. π₯
π NOTES π
So PEM_bytes_read_bio_secmem() only enforces that the secure heap is used for storage allocated within the PEM processing stack. The BIO stack from which input is read may also use temporary buffers, and those are not necessarily allocated from the secure heap. So in cases where it is desirable to ensure that the contents of the PEM file only appear in memory from the secure heap, care is needed in generating the BIO passed as bp. In particular, the use of BIO_s_file() indicates the use of the operating system stdio functionality, which includes buffering as a feature β and BIO_s_fd() is likely to be more appropriate in such cases. So like, choose your BIO wisely, bestie. π§
Also these functions make no assumption regarding the pass phrase received from the password callback. It will simply be treated as a byte sequence. No vibes, just bytes. π
π¦ RETURN VALUES π¦
PEM_bytes_read_bio() and PEM_bytes_read_bio_secmem() return 1 for success or 0 for failure. Simple. Iconic. No notes. π
π SEE ALSO π
PEM_read_bio_ex(3), passphrase-encoding(7)
β HISTORY β
PEM_bytes_read_bio_secmem() was introduced in OpenSSL 1.1.1. So if you're on something older, it's giving vintage. π
π COPYRIGHT π
Copyright 2017-2018 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>.
And that's the whole thing, bestie! This page is from OpenSSL version 3.5.0, dated 2026-09-25, and the manual name is PEM_BYTES_READ_BIO(3ossl). Slay! π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms