d2i_pr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/bn.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/decoder.h>
  17. #include <openssl/engine.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/asn1.h>
  20. #include "crypto/asn1.h"
  21. #include "crypto/evp.h"
  22. #include "crypto/x509.h"
  23. #include "internal/asn1.h"
  24. #include "internal/sizes.h"
  25. static EVP_PKEY *
  26. d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
  27. long length, OSSL_LIB_CTX *libctx, const char *propq)
  28. {
  29. OSSL_DECODER_CTX *dctx = NULL;
  30. size_t len = length;
  31. EVP_PKEY *pkey = NULL, *bak_a = NULL;
  32. EVP_PKEY **ppkey = &pkey;
  33. const char *key_name = NULL;
  34. char keytypebuf[OSSL_MAX_NAME_SIZE];
  35. int ret;
  36. const unsigned char *p = *pp;
  37. const char *structure;
  38. PKCS8_PRIV_KEY_INFO *p8info;
  39. const ASN1_OBJECT *algoid;
  40. if (keytype != EVP_PKEY_NONE) {
  41. key_name = evp_pkey_type2name(keytype);
  42. if (key_name == NULL)
  43. return NULL;
  44. }
  45. /* This is just a probe. It might fail, so we ignore errors */
  46. ERR_set_mark();
  47. p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len);
  48. ERR_pop_to_mark();
  49. if (p8info != NULL) {
  50. int64_t v;
  51. /* ascertain version is 0 or 1 as per RFC5958 */
  52. if (!ASN1_INTEGER_get_int64(&v, p8info->version)
  53. || (v != 0 && v != 1)) {
  54. *pp = p;
  55. ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_PARSE_ERROR);
  56. PKCS8_PRIV_KEY_INFO_free(p8info);
  57. return NULL;
  58. }
  59. if (key_name == NULL
  60. && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info)
  61. && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0))
  62. key_name = keytypebuf;
  63. structure = "PrivateKeyInfo";
  64. PKCS8_PRIV_KEY_INFO_free(p8info);
  65. } else {
  66. structure = "type-specific";
  67. }
  68. *pp = p;
  69. if (a != NULL && (bak_a = *a) != NULL)
  70. ppkey = a;
  71. dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,
  72. EVP_PKEY_KEYPAIR, libctx, propq);
  73. if (a != NULL)
  74. *a = bak_a;
  75. if (dctx == NULL)
  76. goto err;
  77. ret = OSSL_DECODER_from_data(dctx, pp, &len);
  78. OSSL_DECODER_CTX_free(dctx);
  79. if (ret
  80. && *ppkey != NULL
  81. && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
  82. if (a != NULL)
  83. *a = *ppkey;
  84. return *ppkey;
  85. }
  86. err:
  87. if (ppkey != a)
  88. EVP_PKEY_free(*ppkey);
  89. return NULL;
  90. }
  91. EVP_PKEY *
  92. ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp,
  93. long length, OSSL_LIB_CTX *libctx, const char *propq)
  94. {
  95. EVP_PKEY *ret;
  96. const unsigned char *p = *pp;
  97. if (a == NULL || *a == NULL) {
  98. if ((ret = EVP_PKEY_new()) == NULL) {
  99. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  100. return NULL;
  101. }
  102. } else {
  103. ret = *a;
  104. #ifndef OPENSSL_NO_ENGINE
  105. ENGINE_finish(ret->engine);
  106. ret->engine = NULL;
  107. #endif
  108. }
  109. if (!EVP_PKEY_set_type(ret, keytype)) {
  110. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
  111. goto err;
  112. }
  113. ERR_set_mark();
  114. if (!ret->ameth->old_priv_decode ||
  115. !ret->ameth->old_priv_decode(ret, &p, length)) {
  116. if (ret->ameth->priv_decode != NULL
  117. || ret->ameth->priv_decode_ex != NULL) {
  118. EVP_PKEY *tmp;
  119. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  120. p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
  121. if (p8 == NULL) {
  122. ERR_clear_last_mark();
  123. goto err;
  124. }
  125. tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
  126. PKCS8_PRIV_KEY_INFO_free(p8);
  127. if (tmp == NULL) {
  128. ERR_clear_last_mark();
  129. goto err;
  130. }
  131. EVP_PKEY_free(ret);
  132. ret = tmp;
  133. ERR_pop_to_mark();
  134. if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
  135. goto err;
  136. } else {
  137. ERR_clear_last_mark();
  138. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  139. goto err;
  140. }
  141. } else {
  142. ERR_clear_last_mark();
  143. }
  144. *pp = p;
  145. if (a != NULL)
  146. *a = ret;
  147. return ret;
  148. err:
  149. if (a == NULL || *a != ret)
  150. EVP_PKEY_free(ret);
  151. return NULL;
  152. }
  153. EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp,
  154. long length, OSSL_LIB_CTX *libctx,
  155. const char *propq)
  156. {
  157. EVP_PKEY *ret;
  158. ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
  159. /* try the legacy path if the decoder failed */
  160. if (ret == NULL)
  161. ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
  162. return ret;
  163. }
  164. EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
  165. long length)
  166. {
  167. return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
  168. }
  169. static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
  170. const unsigned char **pp,
  171. long length,
  172. OSSL_LIB_CTX *libctx,
  173. const char *propq)
  174. {
  175. STACK_OF(ASN1_TYPE) *inkey;
  176. const unsigned char *p;
  177. int keytype;
  178. p = *pp;
  179. /*
  180. * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
  181. * analyzing it we can determine the passed structure: this assumes the
  182. * input is surrounded by an ASN1 SEQUENCE.
  183. */
  184. inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
  185. p = *pp;
  186. /*
  187. * Since we only need to discern "traditional format" RSA and DSA keys we
  188. * can just count the elements.
  189. */
  190. if (sk_ASN1_TYPE_num(inkey) == 6) {
  191. keytype = EVP_PKEY_DSA;
  192. } else if (sk_ASN1_TYPE_num(inkey) == 4) {
  193. keytype = EVP_PKEY_EC;
  194. } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
  195. * traditional format */
  196. PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
  197. EVP_PKEY *ret;
  198. sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  199. if (p8 == NULL) {
  200. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  201. return NULL;
  202. }
  203. ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
  204. PKCS8_PRIV_KEY_INFO_free(p8);
  205. if (ret == NULL)
  206. return NULL;
  207. *pp = p;
  208. if (a != NULL) {
  209. *a = ret;
  210. }
  211. return ret;
  212. } else {
  213. keytype = EVP_PKEY_RSA;
  214. }
  215. sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  216. return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
  217. }
  218. /*
  219. * This works like d2i_PrivateKey() except it passes the keytype as
  220. * EVP_PKEY_NONE, which then figures out the type during decoding.
  221. */
  222. EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
  223. long length, OSSL_LIB_CTX *libctx,
  224. const char *propq)
  225. {
  226. EVP_PKEY *ret;
  227. ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
  228. /* try the legacy path if the decoder failed */
  229. if (ret == NULL)
  230. ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
  231. return ret;
  232. }
  233. EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
  234. long length)
  235. {
  236. return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
  237. }