1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =begin comment 4 1.1 christos 5 1.1 christos Any deprecated keypair/params d2i or i2d functions are collected on this page. 6 1.1 christos 7 1.1 christos =end comment 8 1.1 christos 9 1.1 christos =head1 NAME 10 1.1 christos 11 1.1 christos d2i_DSAPrivateKey, 12 1.1 christos d2i_DSAPrivateKey_bio, 13 1.1 christos d2i_DSAPrivateKey_fp, 14 1.1 christos d2i_DSAPublicKey, 15 1.1 christos d2i_DSA_PUBKEY, 16 1.1 christos d2i_DSA_PUBKEY_bio, 17 1.1 christos d2i_DSA_PUBKEY_fp, 18 1.1 christos d2i_DSAparams, 19 1.1 christos d2i_RSAPrivateKey, 20 1.1 christos d2i_RSAPrivateKey_bio, 21 1.1 christos d2i_RSAPrivateKey_fp, 22 1.1 christos d2i_RSAPublicKey, 23 1.1 christos d2i_RSAPublicKey_bio, 24 1.1 christos d2i_RSAPublicKey_fp, 25 1.1 christos d2i_RSA_PUBKEY, 26 1.1 christos d2i_RSA_PUBKEY_bio, 27 1.1 christos d2i_RSA_PUBKEY_fp, 28 1.1 christos d2i_DHparams, 29 1.1 christos d2i_DHparams_bio, 30 1.1 christos d2i_DHparams_fp, 31 1.1 christos d2i_ECParameters, 32 1.1 christos d2i_ECPrivateKey, 33 1.1 christos d2i_ECPrivateKey_bio, 34 1.1 christos d2i_ECPrivateKey_fp, 35 1.1 christos d2i_EC_PUBKEY, 36 1.1 christos d2i_EC_PUBKEY_bio, 37 1.1 christos d2i_EC_PUBKEY_fp, 38 1.1 christos i2d_RSAPrivateKey, 39 1.1 christos i2d_RSAPrivateKey_bio, 40 1.1 christos i2d_RSAPrivateKey_fp, 41 1.1 christos i2d_RSAPublicKey, 42 1.1 christos i2d_RSAPublicKey_bio, 43 1.1 christos i2d_RSAPublicKey_fp, 44 1.1 christos i2d_RSA_PUBKEY, 45 1.1 christos i2d_RSA_PUBKEY_bio, 46 1.1 christos i2d_RSA_PUBKEY_fp, 47 1.1 christos i2d_DHparams, 48 1.1 christos i2d_DHparams_bio, 49 1.1 christos i2d_DHparams_fp, 50 1.1 christos i2d_DSAPrivateKey, 51 1.1 christos i2d_DSAPrivateKey_bio, 52 1.1 christos i2d_DSAPrivateKey_fp, 53 1.1 christos i2d_DSAPublicKey, 54 1.1 christos i2d_DSA_PUBKEY, 55 1.1 christos i2d_DSA_PUBKEY_bio, 56 1.1 christos i2d_DSA_PUBKEY_fp, 57 1.1 christos i2d_DSAparams, 58 1.1 christos i2d_ECParameters, 59 1.1 christos i2d_ECPrivateKey, 60 1.1 christos i2d_ECPrivateKey_bio, 61 1.1 christos i2d_ECPrivateKey_fp, 62 1.1 christos i2d_EC_PUBKEY, 63 1.1 christos i2d_EC_PUBKEY_bio, 64 1.1 christos i2d_EC_PUBKEY_fp 65 1.1 christos - DEPRECATED 66 1.1 christos 67 1.1 christos =head1 SYNOPSIS 68 1.1 christos 69 1.1 christos =for openssl generic 70 1.1 christos 71 1.1 christos The following functions have been deprecated since OpenSSL 3.0, and can be 72 1.1 christos hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value, 73 1.1 christos see L<openssl_user_macros(7)>: 74 1.1 christos 75 1.1 christos TYPE *d2i_TYPEPrivateKey(TYPE **a, const unsigned char **ppin, long length); 76 1.1 christos TYPE *d2i_TYPEPrivateKey_bio(BIO *bp, TYPE **a); 77 1.1 christos TYPE *d2i_TYPEPrivateKey_fp(FILE *fp, TYPE **a); 78 1.1 christos TYPE *d2i_TYPEPublicKey(TYPE **a, const unsigned char **ppin, long length); 79 1.1 christos TYPE *d2i_TYPEPublicKey_bio(BIO *bp, TYPE **a); 80 1.1 christos TYPE *d2i_TYPEPublicKey_fp(FILE *fp, TYPE **a); 81 1.1 christos TYPE *d2i_TYPEparams(TYPE **a, const unsigned char **ppin, long length); 82 1.1 christos TYPE *d2i_TYPEparams_bio(BIO *bp, TYPE **a); 83 1.1 christos TYPE *d2i_TYPEparams_fp(FILE *fp, TYPE **a); 84 1.1 christos TYPE *d2i_TYPE_PUBKEY(TYPE **a, const unsigned char **ppin, long length); 85 1.1 christos TYPE *d2i_TYPE_PUBKEY_bio(BIO *bp, TYPE **a); 86 1.1 christos TYPE *d2i_TYPE_PUBKEY_fp(FILE *fp, TYPE **a); 87 1.1 christos 88 1.1 christos int i2d_TYPEPrivateKey(const TYPE *a, unsigned char **ppout); 89 1.1 christos int i2d_TYPEPrivateKey(TYPE *a, unsigned char **ppout); 90 1.1 christos int i2d_TYPEPrivateKey_fp(FILE *fp, const TYPE *a); 91 1.1 christos int i2d_TYPEPrivateKey_fp(FILE *fp, TYPE *a); 92 1.1 christos int i2d_TYPEPrivateKey_bio(BIO *bp, const TYPE *a); 93 1.1 christos int i2d_TYPEPrivateKey_bio(BIO *bp, TYPE *a); 94 1.1 christos int i2d_TYPEPublicKey(const TYPE *a, unsigned char **ppout); 95 1.1 christos int i2d_TYPEPublicKey(TYPE *a, unsigned char **ppout); 96 1.1 christos int i2d_TYPEPublicKey_fp(FILE *fp, const TYPE *a); 97 1.1 christos int i2d_TYPEPublicKey_fp(FILE *fp, TYPE *a); 98 1.1 christos int i2d_TYPEPublicKey_bio(BIO *bp, const TYPE *a); 99 1.1 christos int i2d_TYPEPublicKey_bio(BIO *bp, TYPE *a); 100 1.1 christos int i2d_TYPEparams(const TYPE *a, unsigned char **ppout); 101 1.1 christos int i2d_TYPEparams(TYPE *a, unsigned char **ppout); 102 1.1 christos int i2d_TYPEparams_fp(FILE *fp, const TYPE *a); 103 1.1 christos int i2d_TYPEparams_fp(FILE *fp, TYPE *a); 104 1.1 christos int i2d_TYPEparams_bio(BIO *bp, const TYPE *a); 105 1.1 christos int i2d_TYPEparams_bio(BIO *bp, TYPE *a); 106 1.1 christos int i2d_TYPE_PUBKEY(const TYPE *a, unsigned char **ppout); 107 1.1 christos int i2d_TYPE_PUBKEY(TYPE *a, unsigned char **ppout); 108 1.1 christos int i2d_TYPE_PUBKEY_fp(FILE *fp, const TYPE *a); 109 1.1 christos int i2d_TYPE_PUBKEY_fp(FILE *fp, TYPE *a); 110 1.1 christos int i2d_TYPE_PUBKEY_bio(BIO *bp, const TYPE *a); 111 1.1 christos int i2d_TYPE_PUBKEY_bio(BIO *bp, TYPE *a); 112 1.1 christos 113 1.1 christos =head1 DESCRIPTION 114 1.1 christos 115 1.1 christos All functions described here are deprecated. Please use L<OSSL_DECODER(3)> 116 1.1 christos instead of the B<d2i> functions and L<OSSL_ENCODER(3)> instead of the B<i2d> 117 1.1 christos functions. See L</Migration> below. 118 1.1 christos 119 1.1 christos In the description here, B<I<TYPE>> is used a placeholder for any of the 120 1.1 christos OpenSSL datatypes, such as B<RSA>. 121 1.1 christos The function parameters I<ppin> and I<ppout> are generally either both named 122 1.1 christos I<pp> in the headers, or I<in> and I<out>. 123 1.1 christos 124 1.1 christos All the functions here behave the way that's described in L<d2i_X509(3)>. 125 1.1 christos 126 1.1 christos Please note that not all functions in the synopsis are available for all key 127 1.1 christos types. For example, there are no d2i_RSAparams() or i2d_RSAparams(), 128 1.1 christos because the PKCS#1 B<RSA> structure doesn't include any key parameters. 129 1.1 christos 130 1.1 christos B<d2i_I<TYPE>PrivateKey>() and derivates thereof decode DER encoded 131 1.1 christos B<I<TYPE>> private key data organized in a type specific structure. 132 1.1 christos 133 1.1 christos B<d2i_I<TYPE>PublicKey>() and derivates thereof decode DER encoded 134 1.1 christos B<I<TYPE>> public key data organized in a type specific structure. 135 1.1 christos 136 1.1 christos B<d2i_I<TYPE>params>() and derivates thereof decode DER encoded B<I<TYPE>> 137 1.1 christos key parameters organized in a type specific structure. 138 1.1 christos 139 1.1 christos B<d2i_I<TYPE>_PUBKEY>() and derivates thereof decode DER encoded B<I<TYPE>> 140 1.1 christos public key data organized in a B<SubjectPublicKeyInfo> structure. 141 1.1 christos 142 1.1 christos B<i2d_I<TYPE>PrivateKey>() and derivates thereof encode the private key 143 1.1 christos B<I<TYPE>> data into a type specific DER encoded structure. 144 1.1 christos 145 1.1 christos B<i2d_I<TYPE>PublicKey>() and derivates thereof encode the public key 146 1.1 christos B<I<TYPE>> data into a type specific DER encoded structure. 147 1.1 christos 148 1.1 christos B<i2d_I<TYPE>params>() and derivates thereof encode the B<I<TYPE>> key 149 1.1 christos parameters data into a type specific DER encoded structure. 150 1.1 christos 151 1.1 christos B<i2d_I<TYPE>_PUBKEY>() and derivates thereof encode the public key 152 1.1 christos B<I<TYPE>> data into a DER encoded B<SubjectPublicKeyInfo> structure. 153 1.1 christos 154 1.1 christos For example, d2i_RSAPrivateKey() and d2i_RSAPublicKey() expects the 155 1.1 christos structure defined by PKCS#1. 156 1.1 christos Similarly, i2d_RSAPrivateKey() and i2d_RSAPublicKey() produce DER encoded 157 1.1 christos string organized according to PKCS#1. 158 1.1 christos 159 1.1 christos =head2 Migration 160 1.1 christos 161 1.1 christos Migration from the diverse B<I<TYPE>>s requires using corresponding new 162 1.1 christos OpenSSL types. For all B<I<TYPE>>s described here, the corresponding new 163 1.1 christos type is B<EVP_PKEY>. The rest of this section assumes that this has been 164 1.1 christos done, exactly how to do that is described elsewhere. 165 1.1 christos 166 1.1 christos There are two migration paths: 167 1.1 christos 168 1.1 christos =over 4 169 1.1 christos 170 1.1 christos =item * 171 1.1 christos 172 1.1 christos Replace 173 1.1 christos b<d2i_I<TYPE>PrivateKey()> with L<d2i_PrivateKey(3)>, 174 1.1 christos b<d2i_I<TYPE>PublicKey()> with L<d2i_PublicKey(3)>, 175 1.1 christos b<d2i_I<TYPE>params()> with L<d2i_KeyParams(3)>, 176 1.1 christos b<d2i_I<TYPE>_PUBKEY()> with L<d2i_PUBKEY(3)>, 177 1.1 christos b<i2d_I<TYPE>PrivateKey()> with L<i2d_PrivateKey(3)>, 178 1.1 christos b<i2d_I<TYPE>PublicKey()> with L<i2d_PublicKey(3)>, 179 1.1 christos b<i2d_I<TYPE>params()> with L<i2d_KeyParams(3)>, 180 1.1 christos b<i2d_I<TYPE>_PUBKEY()> with L<i2d_PUBKEY(3)>. 181 1.1 christos A caveat is that L<i2d_PrivateKey(3)> may output a DER encoded PKCS#8 182 1.1 christos outermost structure instead of the type specific structure, and that 183 1.1 christos L<d2i_PrivateKey(3)> recognises and unpacks a PKCS#8 structures. 184 1.1 christos 185 1.1 christos =item * 186 1.1 christos 187 1.1 christos Use L<OSSL_DECODER(3)> and L<OSSL_ENCODER(3)>. How to migrate is described 188 1.1 christos below. All those descriptions assume that the key to be encoded is in the 189 1.1 christos variable I<pkey>. 190 1.1 christos 191 1.1 christos =back 192 1.1 christos 193 1.1 christos =head3 Migrating B<i2d> functions to B<OSSL_ENCODER> 194 1.1 christos 195 1.1 christos The exact L<OSSL_ENCODER(3)> output is driven by arguments rather than by 196 1.1 christos function names. The sample code to get DER encoded output in a type 197 1.1 christos specific structure is uniform, the only things that vary are the selection 198 1.1 christos of what part of the B<EVP_PKEY> should be output, and the structure. The 199 1.1 christos B<i2d> functions names can therefore be translated into two variables, 200 1.1 christos I<selection> and I<structure> as follows: 201 1.1 christos 202 1.1 christos =over 4 203 1.1 christos 204 1.1 christos =item B<i2d_I<TYPE>PrivateKey>() translates into: 205 1.1 christos 206 1.1.1.2 christos int selection = EVP_PKEY_KEYPAIR; 207 1.1 christos const char *structure = "type-specific"; 208 1.1 christos 209 1.1 christos =item B<i2d_I<TYPE>PublicKey>() translates into: 210 1.1 christos 211 1.1 christos int selection = EVP_PKEY_PUBLIC_KEY; 212 1.1 christos const char *structure = "type-specific"; 213 1.1 christos 214 1.1 christos =item B<i2d_I<TYPE>params>() translates into: 215 1.1 christos 216 1.1 christos int selection = EVP_PKEY_PARAMETERS; 217 1.1 christos const char *structure = "type-specific"; 218 1.1 christos 219 1.1 christos =item B<i2d_I<TYPE>_PUBKEY>() translates into: 220 1.1 christos 221 1.1 christos int selection = EVP_PKEY_PUBLIC_KEY; 222 1.1 christos const char *structure = "SubjectPublicKeyInfo"; 223 1.1 christos 224 1.1 christos =back 225 1.1 christos 226 1.1 christos The following sample code does the rest of the work: 227 1.1 christos 228 1.1 christos unsigned char *p = buffer; /* |buffer| is supplied by the caller */ 229 1.1 christos size_t len = buffer_size; /* assumed be the size of |buffer| */ 230 1.1 christos OSSL_ENCODER_CTX *ctx = 231 1.1 christos OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", structure, 232 1.1 christos NULL, NULL); 233 1.1 christos if (ctx == NULL) { 234 1.1 christos /* fatal error handling */ 235 1.1 christos } 236 1.1 christos if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { 237 1.1 christos OSSL_ENCODER_CTX_free(ctx); 238 1.1 christos /* non-fatal error handling */ 239 1.1 christos } 240 1.1 christos if (!OSSL_ENCODER_to_data(ctx, &p, &len)) { 241 1.1 christos OSSL_ENCODER_CTX_free(ctx); 242 1.1 christos /* error handling */ 243 1.1 christos } 244 1.1 christos OSSL_ENCODER_CTX_free(ctx); 245 1.1 christos 246 1.1 christos =for comment TODO: a similar section on OSSL_DECODER is to be added 247 1.1 christos 248 1.1 christos =head1 NOTES 249 1.1 christos 250 1.1 christos The letters B<i> and B<d> in B<i2d_I<TYPE>>() stand for 251 1.1 christos "internal" (that is, an internal C structure) and "DER" respectively. 252 1.1 christos So B<i2d_I<TYPE>>() converts from internal to DER. 253 1.1 christos 254 1.1 christos The functions can also understand B<BER> forms. 255 1.1 christos 256 1.1 christos The actual TYPE structure passed to B<i2d_I<TYPE>>() must be a valid 257 1.1 christos populated B<I<TYPE>> structure -- it B<cannot> simply be fed with an 258 1.1 christos empty structure such as that returned by TYPE_new(). 259 1.1 christos 260 1.1 christos The encoded data is in binary form and may contain embedded zeros. 261 1.1 christos Therefore, any FILE pointers or BIOs should be opened in binary mode. 262 1.1 christos Functions such as strlen() will B<not> return the correct length 263 1.1 christos of the encoded structure. 264 1.1 christos 265 1.1 christos The ways that I<*ppin> and I<*ppout> are incremented after the operation 266 1.1 christos can trap the unwary. See the B<WARNINGS> section in L<d2i_X509(3)> for some 267 1.1 christos common errors. 268 1.1 christos The reason for this-auto increment behaviour is to reflect a typical 269 1.1 christos usage of ASN1 functions: after one structure is encoded or decoded 270 1.1 christos another will be processed after it. 271 1.1 christos 272 1.1 christos The following points about the data types might be useful: 273 1.1 christos 274 1.1 christos =over 4 275 1.1 christos 276 1.1 christos =item B<DSA_PUBKEY> 277 1.1 christos 278 1.1 christos Represents a DSA public key using a B<SubjectPublicKeyInfo> structure. 279 1.1 christos 280 1.1 christos =item B<DSAPublicKey>, B<DSAPrivateKey> 281 1.1 christos 282 1.1 christos Use a non-standard OpenSSL format and should be avoided; use B<DSA_PUBKEY>, 283 1.1 christos L<PEM_write_PrivateKey(3)>, or similar instead. 284 1.1 christos 285 1.1 christos =back 286 1.1 christos 287 1.1 christos =head1 RETURN VALUES 288 1.1 christos 289 1.1 christos B<d2i_I<TYPE>>(), B<d2i_I<TYPE>_bio>() and B<d2i_I<TYPE>_fp>() return a valid 290 1.1 christos B<I<TYPE>> structure or NULL if an error occurs. If the "reuse" capability has 291 1.1 christos been used with a valid structure being passed in via I<a>, then the object is 292 1.1 christos freed in the event of error and I<*a> is set to NULL. 293 1.1 christos 294 1.1 christos B<i2d_I<TYPE>>() returns the number of bytes successfully encoded or a negative 295 1.1 christos value if an error occurs. 296 1.1 christos 297 1.1 christos B<i2d_I<TYPE>_bio>() and B<i2d_I<TYPE>_fp>() return 1 for success and 0 if an 298 1.1 christos error occurs. 299 1.1 christos 300 1.1 christos =head1 SEE ALSO 301 1.1 christos 302 1.1 christos L<OSSL_ENCODER(3)>, L<OSSL_DECODER(3)>, 303 1.1 christos L<d2i_PrivateKey(3)>, L<d2i_PublicKey(3)>, L<d2i_KeyParams(3)>, 304 1.1 christos L<d2i_PUBKEY(3)>, 305 1.1 christos L<i2d_PrivateKey(3)>, L<i2d_PublicKey(3)>, L<i2d_KeyParams(3)>, 306 1.1 christos L<i2d_PUBKEY(3)> 307 1.1 christos 308 1.1 christos =head1 COPYRIGHT 309 1.1 christos 310 1.1.1.2 christos Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. 311 1.1 christos 312 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 313 1.1 christos this file except in compliance with the License. You can obtain a copy 314 1.1 christos in the file LICENSE in the source distribution or at 315 1.1 christos L<https://www.openssl.org/source/license.html>. 316 1.1 christos 317 1.1 christos =cut 318