D2I_PRIVATEKEY(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… d2i_PRIVATEKEY β€” the OpenSSL manual, but make it make sense

okay so this is d2i_PRIVATEKEY(3ossl), bestie. it's the OpenSSL page for all the decode-and-encode girlies out there β€” reading and saving EVP_PKEY structures. like, taking keys from bytes and putting keys back into bytes. it's giving ✨serialization✨ and honestly? iconic.

here's the whole cast, no one gets left out:

  • d2i_PrivateKey_ex
  • d2i_PrivateKey
  • d2i_PublicKey
  • d2i_KeyParams
  • d2i_AutoPrivateKey_ex
  • d2i_AutoPrivateKey
  • i2d_PrivateKey
  • i2d_PublicKey
  • i2d_KeyParams
  • i2d_KeyParams_bio
  • d2i_PrivateKey_ex_bio
  • d2i_PrivateKey_bio
  • d2i_PrivateKey_ex_fp
  • d2i_PrivateKey_fp
  • d2i_KeyParams_bio
  • i2d_PrivateKey_bio
  • i2d_PrivateKey_fp

collectively: decode and encode functions for reading and saving EVP_PKEY structures 🫢


✨ SYNOPSIS ✨

so first you gotta bring in the header, obviously:

#include <openssl/evp.h>

EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp,
                            long length, OSSL_LIB_CTX *libctx,
                            const char *propq);
EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
                         long length);
EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
                        long length);
EVP_PKEY *d2i_KeyParams(int type, EVP_PKEY **a, const unsigned char **pp,
                        long length);
EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
                                long length, OSSL_LIB_CTX *libctx,
                                const char *propq);
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
                             long length);

int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp);
int i2d_PublicKey(const EVP_PKEY *a, unsigned char **pp);
int i2d_KeyParams(const EVP_PKEY *a, unsigned char **pp);
int i2d_KeyParams_bio(BIO *bp, const EVP_PKEY *pkey);
EVP_PKEY *d2i_KeyParams_bio(int type, EVP_PKEY **a, BIO *in);

and then for the BIO/FILE crew you gotta swap in the other header:

#include <openssl/x509.h>

EVP_PKEY *d2i_PrivateKey_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
                                const char *propq);
EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
EVP_PKEY *d2i_PrivateKey_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
                               const char *propq);
EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);

int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey);
int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey);

two includes, two vibes. slay πŸ’…


🌸 DESCRIPTION 🌸

okay sit down, this is the whole lore.

d2i_PrivateKey_ex() decodes a private key using algorithm type. it tries to use, like, any key-specific format OR the PKCS#8 unencrypted PrivateKeyInfo format β€” whichever fits. the type param should be a public key algorithm constant, like EVP_PKEY_RSA (you know the one). if the decoded key doesn't match type? error. respectfully, mismatch = nope.

some private key decoding implementations might use cryptographic algorithms β€” for example, to automatically derive the public key if it wasn't explicitly included in the encoding. in that case, the library context libctx and property query string propq you supplied get used. and if it's successful and the a parameter isn't NULL, the function assigns the returned EVP_PKEY structure pointer to *a, overwriting whatever was there before. fair warning: overwritten, not merged. gone. πŸ’€

d2i_PrivateKey() does the same thing as d2i_PrivateKey_ex() except it uses the default library context and property query string (no custom ones, no thoughts, just defaults).

d2i_PublicKey() does the same β€” but for public keys.

d2i_KeyParams() does the same β€” but for key parameters.

d2i_PrivateKey_ex_bio() and d2i_PrivateKey_bio() are similar to d2i_PrivateKey_ex() and d2i_PrivateKey() respectively, except they decode the data read from the given BIO. πŸ“₯

d2i_PrivateKey_ex_fp() and d2i_PrivateKey_fp() are the same thing again, except they read the data from the given FILE.

d2i_AutoPrivateKey_ex() and d2i_AutoPrivateKey() are similar to d2i_PrivateKey_ex() and d2i_PrivateKey() respectively, except they attempt to automatically detect the private key format. main character energy: they figure it out themselves πŸ”₯

now the encoding side, bestie:

i2d_PrivateKey() encodes a. it uses a key specific format or, if none is defined for that key type, the PKCS#8 unencrypted PrivateKeyInfo format.

i2d_PublicKey() does the same for public keys.

i2d_KeyParams() does the same for key parameters.

these functions are similar to the d2i_X509() functions β€” go peek at d2i_X509(3) if you want the cousin page. πŸŽ€

i2d_PrivateKey_bio() and i2d_PrivateKey_fp() do the same thing except they encode to a BIO or FILE respectively. and again, these work similarly to the functions described in d2i_X509(3).


πŸ¦‹ NOTES πŸ¦‹

every single one of the functions that operates on data in memory updates the data pointer *pp after a successful operation. just like all the other d2i and i2d functions. see d2i_X509(3). the pointer moves, that's the whole bit.

all these functions use DER format and unencrypted keys. so if your app wants to encrypt or decrypt private keys, you need other functions β€” like d2i_PKCS8PrivateKey() instead. this page is not that page, iykyk.

and here's a very specific little caveat πŸ‘‘: to decode a key with type EVP_PKEY_EC, d2i_PublicKey() requires *a to be a non-NULL EVP_PKEY structure assigned an EC_KEY structure referencing the proper EC_GROUP. not optional. not a suggestion. fr fr.


⭐ RETURN VALUES ⭐

group one β€” the decoders. these return a valid EVP_PKEY structure, or NULL if an error occurs. the error code can be obtained by calling ERR_get_error(3):

  • d2i_PrivateKey_ex()
  • d2i_PrivateKey()
  • d2i_AutoPrivateKey_ex()
  • d2i_AutoPrivateKey()
  • d2i_PrivateKey_ex_bio()
  • d2i_PrivateKey_bio()
  • d2i_PrivateKey_ex_fp()
  • d2i_PrivateKey_fp()
  • d2i_PublicKey()
  • d2i_KeyParams()
  • d2i_KeyParams_bio()

group two β€” i2d_PrivateKey(), i2d_PublicKey() and i2d_KeyParams() return the number of bytes successfully encoded, or a negative value if an error occurs. error code? ERR_get_error(3). same as above.

group three β€” i2d_PrivateKey_bio(), i2d_PrivateKey_fp() and i2d_KeyParams_bio() return 1 if successfully encoded, or zero if an error occurs. one or zero. that's it. that's the return value. βœ…


🌈 SEE ALSO 🌈


πŸŽ€ HISTORY πŸŽ€

the glow-up era: d2i_PrivateKey_ex(), d2i_PrivateKey_ex_bio(), d2i_PrivateKey_ex_fp(), and d2i_AutoPrivateKey_ex() were added in OpenSSL 3.0. before that? they simply did not exist. we don't talk about it πŸ’œ


Copyright 2017-2021 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 page!! 🧚 this was OpenSSL 3.5.0, dated 2026-09-25, and the whole thing lives under D2I_PRIVATEKEY(3ossl). go forth and encode responsibly, bestie πŸ’–πŸŒΈ

πŸ“¦ where this page comes from