D2I_PRIVATEKEY(3)
okay so this is D2I_PRIVATEKEY(3), a Library Functions Manual page, and itβs literally the algorithm-independent interface to decode and encode private and public keys. Bestie, if youβre here, youβre probably doing EVP_PKEY stuff β and yes, this page serves. π
π NAME π
The whole squad: d2i_PrivateKey, d2i_AutoPrivateKey, d2i_PrivateKey_bio, d2i_PrivateKey_fp, i2d_PrivateKey, i2d_PrivateKey_bio, i2d_PrivateKey_fp, i2d_PKCS8PrivateKeyInfo_bio, i2d_PKCS8PrivateKeyInfo_fp, d2i_PublicKey, i2d_PublicKey β all here to decode and encode EVP_PKEY objects. Periodt. β¨
β¨ SYNOPSIS β¨
Okay, itβs giving libcrypto energy, and you need the header. Copy these signatures exactly, no cap:
library βlibcryptoβ #include <openssl/evp.h>
EVP_PKEY *
d2i_PrivateKey(int type, EVP_PKEY **val_out,
const unsigned char **der_in, long length);
EVP_PKEY *
d2i_AutoPrivateKey(EVP_PKEY **val_out, const unsigned char **der_in,
long length);
EVP_PKEY *
d2i_PrivateKey_bio(BIO *in_bio, EVP_PKEY **val_out);
EVP_PKEY *
d2i_PrivateKey_fp(FILE *in_fp, EVP_PKEY **val_out);
int
i2d_PrivateKey(EVP_PKEY *val_in, unsigned char **der_out);
int
i2d_PrivateKey_bio(BIO *out_bio, EVP_PKEY *val_in);
int
i2d_PrivateKey_fp(FILE *out_fp, EVP_PKEY *val_in);
int
i2d_PKCS8PrivateKeyInfo_bio(BIO *out_bio, EVP_PKEY *val_in);
int
i2d_PKCS8PrivateKeyInfo_fp(FILE *out_fp, EVP_PKEY *val_in);
EVP_PKEY *
d2i_PublicKey(int type, EVP_PKEY **val_out,
const unsigned char **der_in, long length);
int
i2d_PublicKey(EVP_PKEY *val_in, unsigned char **der_out);
πΈ DESCRIPTION πΈ
These are the algorithm-independent interfaces to decode and encode private and public keys. For the full semantics, examples, caveats, and bugs, go read ASN1_item_d2i(3) β iykyk.
d2i_PrivateKey()β decodes a private key using algorithmtype. It attempts to use any algorithm specific format or the PKCS#8 unencrypted PrivateKeyInfo format defined in RFC 5208 section 5. Thetypeparameter should be a public key algorithm constant such asEVP_PKEY_RSA. An error occurs if the decoded key does not matchtype. π«Άd2i_AutoPrivateKey()β similar tod2i_PrivateKey()except that it attempts to automatically detect the algorithm. Main character energy, honestly.d2i_PrivateKey_bio()andd2i_PrivateKey_fp()β similar tod2i_PrivateKey()except that they read from aBIOorFILEpointer.i2d_PrivateKey()β encodesval_in. It uses an algorithm specific format or, if none is defined for that key type, the PKCS#8 unencrypted PrivateKeyInfo format.i2d_PrivateKey_bio()andi2d_PrivateKey_fp()β similar toi2d_PrivateKey()except that they write to aBIOorFILEpointer and use a different convention for their return values.i2d_PKCS8PrivateKeyInfo_bio()andi2d_PKCS8PrivateKeyInfo_fp()β encodeval_inin PKCS#8 unencrypted PrivateKeyInfo format. They are similar toi2d_PrivateKey()except that they donβt use any algorithm-specific formats and that they write to aBIOorFILEpointer rather than to a buffer.
β οΈ bestie, 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_bio(3) instead. Thatβs the move. π₯
If *val_out is not NULL when calling d2i_PrivateKey() or d2i_AutoPrivateKey() (i.e. an existing structure is being reused) and the key format is PKCS#8, then *val_out will be freed and replaced on a successful call. Not me warning you about pointer reuse β but yes, thatβs the vibe. π
d2i_PublicKey() calls d2i_DSAPublicKey(3), o2i_ECPublicKey(3), or d2i_RSAPublicKey(3) depending on type and stores the result in the returned EVP_PKEY object.
i2d_PublicKey() calls i2d_DSAPublicKey(3), i2o_ECPublicKey(3), or i2d_RSAPublicKey(3) depending on the algorithm used by val_in. Slay. π
π RETURN VALUES π
d2i_PrivateKey(),d2i_AutoPrivateKey(),d2i_PrivateKey_bio(),d2i_PrivateKey_fp(), andd2i_PublicKey()return a validEVP_PKEYstructure orNULLif an error occurs.i2d_PrivateKey()andi2d_PublicKey()return the number of bytes successfully encoded or a negative value if an error occurs.i2d_PrivateKey_bio(),i2d_PrivateKey_fp(),i2d_PKCS8PrivateKeyInfo_bio(), andi2d_PKCS8PrivateKeyInfo_fp()return1for success or0if an error occurs.- For all functions, the error code can be obtained by calling
ERR_get_error(3). Go off. π₯
π¦ SEE ALSO π¦
The bestie cross-reference list, no skips:
d2i_PKCS8_PRIV_KEY_INFO(3)d2i_PKCS8PrivateKey_bio(3)EVP_PKEY_new(3)EVP_PKEY_type(3)PEM_write_PrivateKey(3)PKCS8_PRIV_KEY_INFO_new(3)
π STANDARDS π
RFC 5208: Public-Key Cryptography Standards (PKCS) #8: Private-Key Information Syntax Specification β iconic, honestly. π
π HISTORY π
d2i_PrivateKey(),i2d_PrivateKey(),d2i_PublicKey(), andi2d_PublicKey()first appeared in SSLeay 0.6.0 and have been available since OpenBSD 2.4.d2i_AutoPrivateKey(),d2i_PrivateKey_bio(),d2i_PrivateKey_fp(),i2d_PrivateKey_bio(),i2d_PrivateKey_fp(),i2d_PKCS8PrivateKeyInfo_bio(), andi2d_PKCS8PrivateKeyInfo_fp()first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7.
okay thatβs the page, bestie. Source/version vibe: Debian. Date: June 8, 2025. Manual: D2I_PRIVATEKEY(3). Mwah πβ¨
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2016, 2021 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2016 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms