D2I_PRIVATEKEY(3ossl)
π D2I_PRIVATEKEY π
okay so bestie, this is page D2I_PRIVATEKEY(3ossl) from the OpenSSL manual, section 3ossl β it's the whole girlboss squad of decode-and-encode functions for reading and saving EVP_PKEY structures. like, keys go in, keys come out, DER all day. let's get into it π¦
β¨ NAME β¨
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 β decode and encode functions for reading and saving EVP_PKEY structures π
πΈ SYNOPSIS πΈ
#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);
#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);
π DESCRIPTION π
d2i_PrivateKey_ex() decodes a private key using algorithm type. the way it works is it attempts to use any key-specific format, or the PKCS#8 unencrypted PrivateKeyInfo format. the type parameter should be a public key algorithm constant, like EVP_PKEY_RSA. π«Ά if the decoded key doesn't match type, that's an error, no cap. some private key decoding implementations may use cryptographic algorithms (for example, to automatically derive the public key if it isn't explicitly included in the encoding). in that case, the supplied library context libctx and property query string propq are what get used. if it succeeds and the a parameter isn't NULL, the function assigns the returned EVP_PKEY structure pointer to *a, overwriting whatever was there before. π (yes, overwriting. it does not care about your old value.)
d2i_PrivateKey() does the exact same thing as d2i_PrivateKey_ex() except it uses the default library context and property query string. d2i_PublicKey() does the same thing but for public keys. d2i_KeyParams() does the same thing but for key parameters. trio of icons fr fr β¨
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. and d2i_PrivateKey_ex_fp() and d2i_PrivateKey_fp() are the same thing 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, honestly β they just figure it out themselves π
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; see d2i_X509(3). i2d_PrivateKey_bio() and i2d_PrivateKey_fp() do the same thing except they encode to a BIO or FILE respectively. again, these work similarly to the functions described in d2i_X509(3). π₯
π§ NOTES π§
- all the functions that operate on data in memory update the data pointer
*ppafter a successful operation, just like the otherd2iandi2dfunctions; seed2i_X509(3). β - all these functions use DER format and unencrypted keys. applications wishing to encrypt or decrypt private keys should use other functions such as
d2i_PKCS8PrivateKey()instead. π - to decode a key with type
EVP_PKEY_EC,d2i_PublicKey()requires*ato be a non-NULLEVP_PKEYstructure assigned anEC_KEYstructure referencing the properEC_GROUP. π
bestie, note the unencrypted thing. these functions are NOT your encryption girlies. if you want encryption/decryption of private keys, go use d2i_PKCS8PrivateKey(). periodt. π
π RETURN VALUES π
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() and d2i_KeyParams_bio() return a valid EVP_PKEY structure, or NULL if an error occurs. π the error code can be obtained by calling ERR_get_error(3).
i2d_PrivateKey(), i2d_PublicKey() and i2d_KeyParams() return the number of bytes successfully encoded, or a negative value if an error occurs. error code? same deal, ERR_get_error(3). π
i2d_PrivateKey_bio(), i2d_PrivateKey_fp() and i2d_KeyParams_bio() return 1 if successfully encoded, or zero if an error occurs. πΈ
π¦ SEE ALSO π¦
π« HISTORY π«
d2i_PrivateKey_ex(), d2i_PrivateKey_ex_bio(), d2i_PrivateKey_ex_fp(), and d2i_AutoPrivateKey_ex() were added in OpenSSL 3.0. so if you're on 1.1.1 and wondering where they went... they weren't born yet, bestie πβ¨
π COPYRIGHT π
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>.
and that's the whole page, bestie!! πβ¨ this was OpenSSL 3.5.9, dated 2026-09-29, page D2I_PRIVATEKEY(3ossl). go forth and decode responsibly, mwah π«Άπ
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms