D2I_RSAPRIVATEKEY(3SSL)
π D2I_RSAPRIVATEKEY π
okay bestie, welcome to D2I_RSAPRIVATEKEY(3SSL), an OpenSSL man page π and listen. this whole page is basically a museum exhibit, because every single function in it is DEPRECATED. but it's still lowkey iconic and you deserve to know exactly what it says, so let's go πΈ
β¨ NAME β¨
so here's the full squad. these are all the decoder (d2i_) and encoder (i2d_) functions this page covers, and yes, that's a lot of names, we're listing every single one π€:
d2i_DSAPrivateKey, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey_fp, d2i_DSAPublicKey, d2i_DSA_PUBKEY, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY_fp, d2i_DSAparams, d2i_RSAPrivateKey, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey_fp, d2i_RSAPublicKey, d2i_RSAPublicKey_bio, d2i_RSAPublicKey_fp, d2i_RSA_PUBKEY, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY_fp, d2i_DHparams, d2i_DHparams_bio, d2i_DHparams_fp, d2i_ECParameters, d2i_ECPrivateKey, d2i_ECPrivateKey_bio, d2i_ECPrivateKey_fp, d2i_EC_PUBKEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY_fp, i2d_RSAPrivateKey, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey_fp, i2d_RSAPublicKey, i2d_RSAPublicKey_bio, i2d_RSAPublicKey_fp, i2d_RSA_PUBKEY, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY_fp, i2d_DHparams, i2d_DHparams_bio, i2d_DHparams_fp, i2d_DSAPrivateKey, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey_fp, i2d_DSAPublicKey, i2d_DSA_PUBKEY, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY_fp, i2d_DSAparams, i2d_ECParameters, i2d_ECPrivateKey, i2d_ECPrivateKey_bio, i2d_ECPrivateKey_fp, i2d_EC_PUBKEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY_fp β DEPRECATED πͺ¦
π¦ SYNOPSIS π¦
heads up girlie: all of these have been deprecated since OpenSSL 3.0, and you can hide them entirely by definingOPENSSL_API_COMPATwith a suitable version value. seeopenssl_user_macros(7)for the deets π
TYPE *d2i_TYPEPrivateKey(TYPE **a, const unsigned char **ppin, long length);
TYPE *d2i_TYPEPrivateKey_bio(BIO *bp, TYPE **a);
TYPE *d2i_TYPEPrivateKey_fp(FILE *fp, TYPE **a);
TYPE *d2i_TYPEPublicKey(TYPE **a, const unsigned char **ppin, long length);
TYPE *d2i_TYPEPublicKey_bio(BIO *bp, TYPE **a);
TYPE *d2i_TYPEPublicKey_fp(FILE *fp, TYPE **a);
TYPE *d2i_TYPEparams(TYPE **a, const unsigned char **ppin, long length);
TYPE *d2i_TYPEparams_bio(BIO *bp, TYPE **a);
TYPE *d2i_TYPEparams_fp(FILE *fp, TYPE **a);
TYPE *d2i_TYPE_PUBKEY(TYPE **a, const unsigned char **ppin, long length);
TYPE *d2i_TYPE_PUBKEY_bio(BIO *bp, TYPE **a);
TYPE *d2i_TYPE_PUBKEY_fp(FILE *fp, TYPE **a);
int i2d_TYPEPrivateKey(const TYPE *a, unsigned char **ppout);
int i2d_TYPEPrivateKey(TYPE *a, unsigned char **ppout);
int i2d_TYPEPrivateKey_fp(FILE *fp, const TYPE *a);
int i2d_TYPEPrivateKey_fp(FILE *fp, TYPE *a);
int i2d_TYPEPrivateKey_bio(BIO *bp, const TYPE *a);
int i2d_TYPEPrivateKey_bio(BIO *bp, TYPE *a);
int i2d_TYPEPublicKey(const TYPE *a, unsigned char **ppout);
int i2d_TYPEPublicKey(TYPE *a, unsigned char **ppout);
int i2d_TYPEPublicKey_fp(FILE *fp, const TYPE *a);
int i2d_TYPEPublicKey_fp(FILE *fp, TYPE *a);
int i2d_TYPEPublicKey_bio(BIO *bp, const TYPE *a);
int i2d_TYPEPublicKey_bio(BIO *bp, TYPE *a);
int i2d_TYPEparams(const TYPE *a, unsigned char **ppout);
int i2d_TYPEparams(TYPE *a, unsigned char **ppout);
int i2d_TYPEparams_fp(FILE *fp, const TYPE *a);
int i2d_TYPEparams_fp(FILE *fp, TYPE *a);
int i2d_TYPEparams_bio(BIO *bp, const TYPE *a);
int i2d_TYPEparams_bio(BIO *bp, TYPE *a);
int i2d_TYPE_PUBKEY(const TYPE *a, unsigned char **ppout);
int i2d_TYPE_PUBKEY(TYPE *a, unsigned char **ppout);
int i2d_TYPE_PUBKEY_fp(FILE *fp, const TYPE *a);
int i2d_TYPE_PUBKEY_fp(FILE *fp, TYPE *a);
int i2d_TYPE_PUBKEY_bio(BIO *bp, const TYPE *a);
int i2d_TYPE_PUBKEY_bio(BIO *bp, TYPE *a);
π DESCRIPTION π
okay so, first thing first, the elephant in the room. All the functions described here are deprecated. like, all of them. every last one π« the glow-up replacement is OSSL_DECODER(3) instead of the d2i functions, and OSSL_ENCODER(3) instead of the i2d functions. there's a whole "Migration" section below, don't worry bestie, we'll get there π
so in this page, TYPE is just a placeholder for any of the OpenSSL datatypes, like RSA for example. and the parameters ppin and ppout are generally either both named pp in the headers, or in and out. naming lore, iconic β¨
and the tea on behavior: all these functions behave the way that's described in d2i_X509(3). periodt.
β οΈ also important: not all functions in the synopsis are available for all key types!! for example, there's nod2i_RSAparams()ori2d_RSAparams(), because the PKCS#1 RSA structure doesn't include any key parameters. so yes, it's giving "not every item on the menu is available at every location" π
now the actual rundown:
d2i_TYPEPrivateKey()and its derivates β decode DER encodedTYPEprivate key data organized in a type specific structure πd2i_TYPEPublicKey()and its derivates β decode DER encodedTYPEpublic key data organized in a type specific structure πd2i_TYPEparams()and its derivates β decode DER encodedTYPEkey parameters organized in a type specific structure πd2i_TYPE_PUBKEY()and its derivates β decode DER encodedTYPEpublic key data organized in aSubjectPublicKeyInfostructure π¦i2d_TYPEPrivateKey()and its derivates β encode the private keyTYPEdata into a type specific DER encoded structure π₯i2d_TYPEPublicKey()and its derivates β encode the public keyTYPEdata into a type specific DER encoded structure βi2d_TYPEparams()and its derivates β encode theTYPEkey parameters data into a type specific DER encoded structure πi2d_TYPE_PUBKEY()and its derivates β encode the public keyTYPEdata into a DER encodedSubjectPublicKeyInfostructure π
for example, d2i_RSAPrivateKey() and d2i_RSAPublicKey() expects the structure defined by PKCS#1. similarly, i2d_RSAPrivateKey() and i2d_RSAPublicKey() produce DER encoded string organized according to PKCS#1. so cute that they match π«Ά
π Migration π
migrating away from the diverse TYPEs requires using corresponding new OpenSSL types. for all the TYPEs described here, the corresponding new type is EVP_PKEY. and the rest of this section assumes you've already done that switch β exactly how to do it is described elsewhere, not our problem today bestie π
there are two migration paths:
- replace
d2i_TYPEPrivateKey()withd2i_PrivateKey(3),d2i_TYPEPublicKey()withd2i_PublicKey(3),d2i_TYPEparams()withd2i_KeyParams(3),d2i_TYPE_PUBKEY()withd2i_PUBKEY(3),i2d_TYPEPrivateKey()withi2d_PrivateKey(3),i2d_TYPEPublicKey()withi2d_PublicKey(3),i2d_TYPEparams()withi2d_KeyParams(3), andi2d_TYPE_PUBKEY()withi2d_PUBKEY(3).
β οΈ caveat time:i2d_PrivateKey(3)may output a DER encoded PKCS#8 outermost structure instead of the type specific structure, andd2i_PrivateKey(3)recognises and unpacks a PKCS#8 structures. so, like, not identical behavior, noted π
- use
OSSL_DECODER(3)andOSSL_ENCODER(3). how to migrate is described below. and all those descriptions assume that the key to be encoded is in the variablepkeyπΈ
π§ Migrating i2d functions to OSSL_ENCODER π§
the exact OSSL_ENCODER(3) output is driven by arguments rather than by function names. the sample code to get DER encoded output in a type specific structure is uniform β the only things that vary are the selection of what part of the EVP_PKEY should be output, and the structure. so the i2d function names can therefore be translated into two variables, selection and structure, as follows:
i2d_TYPEPrivateKey() translates into:
int selection = EVP_PKEY_KEYPAIR;
const char *structure = "type-specific";
i2d_TYPEPublicKey() translates into:
int selection = EVP_PKEY_PUBLIC_KEY;
const char *structure = "type-specific";
i2d_TYPEparams() translates into:
int selection = EVP_PKEY_PARAMETERS;
const char *structure = "type-specific";
i2d_TYPE_PUBKEY() translates into:
int selection = EVP_PKEY_PUBLIC_KEY;
const char *structure = "SubjectPublicKeyInfo";
and the following sample code does the rest of the work β¨:
unsigned char *p = buffer; /* |buffer| is supplied by the caller */
size_t len = buffer_size; /* assumed be the size of |buffer| */
OSSL_ENCODER_CTX *ctx =
OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", structure,
NULL, NULL);
if (ctx == NULL) {
/* fatal error handling */
}
if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) {
OSSL_ENCODER_CTX_free(ctx);
/* non-fatal error handling */
}
if (!OSSL_ENCODER_to_data(ctx, &p, &len)) {
OSSL_ENCODER_CTX_free(ctx);
/* error handling */
}
OSSL_ENCODER_CTX_free(ctx);
π NOTES π
the letters i and d in i2d_TYPE() stand for "internal" (that is, an internal C structure) and "DER" respectively. so i2d_TYPE() converts from internal to DER. name etymology, we love to see it π
and the functions can also understand BER forms. flexible queens π
β οΈ the actualTYPEstructure passed toi2d_TYPE()must be a valid populatedTYPEstructure β it cannot simply be fed with an empty structure such as that returned byTYPE_new(). no empty plates at this table girlie, it will NOT work π«
the encoded data is in binary form and may contain embedded zeros. therefore, any FILE pointers or BIOs should be opened in binary mode. and functions such as strlen() will not return the correct length of the encoded structure. so don't even try it, bestie, strlen() is gonna lie to you π
the ways that *ppin and *ppout are incremented after the operation can trap the unwary. see the WARNINGS section in d2i_X509(3) for some common errors. the reason for this auto-increment behaviour is to reflect a typical usage of ASN1 functions: after one structure is encoded or decoded another will be processed after it. so it's a feature not a bug, it's just sneaky about it π
the following points about the data types might be useful:
DSA_PUBKEYβ represents a DSA public key using aSubjectPublicKeyInfostructure. clean, standard, we stan β¨DSAPublicKey,DSAPrivateKeyβ use a non-standard OpenSSL format and should be avoided; useDSA_PUBKEY,PEM_write_PrivateKey(3), or similar instead. avoid avoid avoid, she's non-standard and messy π
π₯ RETURN VALUES π₯
d2i_TYPE(),d2i_TYPE_bio()andd2i_TYPE_fp()return a validTYPEstructure, orNULLif an error occurs. and if the "reuse" capability has been used with a valid structure being passed in viaa, then the object is freed in the event of error and*ais set toNULL. okay that one's important, write it down πi2d_TYPE()returns the number of bytes successfully encoded, or a negative value if an error occurs.i2d_TYPE_bio()andi2d_TYPE_fp()return1for success and0if an error occurs.
π SEE ALSO π
OSSL_ENCODER(3), OSSL_DECODER(3), d2i_PrivateKey(3), d2i_PublicKey(3), d2i_KeyParams(3), d2i_PUBKEY(3), i2d_PrivateKey(3), i2d_PublicKey(3), i2d_KeyParams(3), i2d_PUBKEY(3)
π COPYRIGHT π
Copyright 2020-2025 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, you ate that π
ok so that was version 4.0.3, dated 2026-09-29, straight from the OpenSSL manual, and it's a D2I_RSAPRIVATEKEY(3SSL) moment. go migrate your code, drink some water, and remember: deprecated doesn't mean forgotten π«Άβ¨π
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms