d2i_pr.c 7.2 KB

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