D2I_PKCS8PRIVATEKEY_BIO(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

🌸 NAME 🌸

okay bestie, welcome to D2I_PKCS8PRIVATEKEY_BIO(3) from the Library Functions Manual β€” yes, the name is a whole tongue-twister, but it's iconic πŸ’…. This page is all about:

  • d2i_PKCS8PrivateKey_bio
  • d2i_PKCS8PrivateKey_fp
  • i2d_PKCS8PrivateKey_bio
  • i2d_PKCS8PrivateKey_fp
  • i2d_PKCS8PrivateKey_nid_bio
  • i2d_PKCS8PrivateKey_nid_fp

…and they're the PKCS#8 format private key functions. Slay.


✨ SYNOPSIS ✨

library β€œlibcrypto” #include <openssl/evp.h>

EVP_PKEY *
d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
    void *u);

EVP_PKEY *
d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
    void *u);

int
i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
    char *kstr, int klen, pem_password_cb *cb, void *u);

int
i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
    char *kstr, int klen, pem_password_cb *cb, void *u);

int
i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid, char *kstr,
    int klen, pem_password_cb *cb, void *u);

int
i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid, char *kstr,
    int klen, pem_password_cb *cb, void *u);

πŸ’– DESCRIPTION πŸ’–

So the PKCS#8 functions encode and decode private keys in PKCS#8 format, and they use both PKCS#5 v1.5 and PKCS#5 v2.0 password based encryption algorithms. Very demure, very mindful πŸŽ€.

Other than the use of DER as opposed to PEM, these functions are identical to the corresponding functions described in PEM_read_PrivateKey(3). Like, same energy, different format bestie ✨.

These functions are currently the only way to store encrypted private keys using DER format. That's the whole main character moment right there πŸ‘‘.

Currently all the functions use BIO or FILE pointers. There are no functions which work directly on memory, though this can be readily worked around by converting the buffers to memory BIOs; see BIO_s_mem(3) for details. No cap, that's the workaround 🌈.


πŸŽ€ RETURN VALUES πŸŽ€

  • d2i_PKCS8PrivateKey_bio() and d2i_PKCS8PrivateKey_fp() return a EVP_PKEY object or NULL if an error occurs. Bestie either gets the key or gets NULL, no in-between πŸ’€.
  • i2d_PKCS8PrivateKey_bio(), i2d_PKCS8PrivateKey_fp(), i2d_PKCS8PrivateKey_nid_bio(), and i2d_PKCS8PrivateKey_nid_fp() return 1 on success or 0 on error. Clean, simple, periodt πŸ’….

πŸ”₯ SEE ALSO πŸ”₯


πŸ¦‹ HISTORY πŸ¦‹

These functions first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7. Little legends from way back, fr fr 🫢.


πŸ’€ CAVEATS πŸ’€

Do not confuse these functions with i2d_PKCS8PrivateKeyInfo_bio(3) and i2d_PKCS8PrivateKeyInfo_fp(3), which write out private keys in unencrypted DER format. That's a different vibe entirely, and you do not want to mix them up bestie 😭.

kisses and sparkles, this cute little page is from Debian, dated June 8, 2025, and its title is D2I_PKCS8PRIVATEKEY_BIO(3) πŸ“πŸ’œ

πŸ“¦ where this page comes from