d2i_pr.c 7.5 KB

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