d2i_RSAPrivateKey.pod 10 KB

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