decode_epki2pki.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2020-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. #include <openssl/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/core_object.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/err.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/pkcs12.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/proverr.h>
  19. #include "internal/asn1.h"
  20. #include "internal/sizes.h"
  21. #include "prov/bio.h"
  22. #include "prov/decoders.h"
  23. #include "prov/implementations.h"
  24. #include "endecoder_local.h"
  25. static OSSL_FUNC_decoder_newctx_fn epki2pki_newctx;
  26. static OSSL_FUNC_decoder_freectx_fn epki2pki_freectx;
  27. static OSSL_FUNC_decoder_decode_fn epki2pki_decode;
  28. static OSSL_FUNC_decoder_settable_ctx_params_fn epki2pki_settable_ctx_params;
  29. static OSSL_FUNC_decoder_set_ctx_params_fn epki2pki_set_ctx_params;
  30. /*
  31. * Context used for EncryptedPrivateKeyInfo to PrivateKeyInfo decoding.
  32. */
  33. struct epki2pki_ctx_st {
  34. PROV_CTX *provctx;
  35. char propq[OSSL_MAX_PROPQUERY_SIZE];
  36. };
  37. static void *epki2pki_newctx(void *provctx)
  38. {
  39. struct epki2pki_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  40. if (ctx != NULL)
  41. ctx->provctx = provctx;
  42. return ctx;
  43. }
  44. static void epki2pki_freectx(void *vctx)
  45. {
  46. struct epki2pki_ctx_st *ctx = vctx;
  47. OPENSSL_free(ctx);
  48. }
  49. static const OSSL_PARAM *epki2pki_settable_ctx_params(ossl_unused void *provctx)
  50. {
  51. static const OSSL_PARAM settables[] = {
  52. OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
  53. OSSL_PARAM_END
  54. };
  55. return settables;
  56. }
  57. static int epki2pki_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  58. {
  59. struct epki2pki_ctx_st *ctx = vctx;
  60. const OSSL_PARAM *p;
  61. char *str = ctx->propq;
  62. p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES);
  63. if (p != NULL && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq)))
  64. return 0;
  65. return 1;
  66. }
  67. /*
  68. * The selection parameter in epki2pki_decode() is not used by this function
  69. * because it's not relevant just to decode EncryptedPrivateKeyInfo to
  70. * PrivateKeyInfo.
  71. */
  72. static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  73. OSSL_CALLBACK *data_cb, void *data_cbarg,
  74. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  75. {
  76. struct epki2pki_ctx_st *ctx = vctx;
  77. BUF_MEM *mem = NULL;
  78. unsigned char *der = NULL;
  79. long der_len = 0;
  80. BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
  81. int ok = 0;
  82. if (in == NULL)
  83. return 0;
  84. ok = (asn1_d2i_read_bio(in, &mem) >= 0);
  85. BIO_free(in);
  86. /* We return "empty handed". This is not an error. */
  87. if (!ok)
  88. return 1;
  89. der = (unsigned char *)mem->data;
  90. der_len = (long)mem->length;
  91. OPENSSL_free(mem);
  92. ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb, data_cbarg,
  93. pw_cb, pw_cbarg, PROV_LIBCTX_OF(ctx->provctx),
  94. ctx->propq);
  95. OPENSSL_free(der);
  96. return ok;
  97. }
  98. int ossl_epki2pki_der_decode(unsigned char *der, long der_len, int selection,
  99. OSSL_CALLBACK *data_cb, void *data_cbarg,
  100. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg,
  101. OSSL_LIB_CTX *libctx, const char *propq)
  102. {
  103. const unsigned char *pder = der;
  104. unsigned char *new_der = NULL;
  105. X509_SIG *p8 = NULL;
  106. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  107. const X509_ALGOR *alg = NULL;
  108. int ok = 1; /* Assume good */
  109. ERR_set_mark();
  110. if ((p8 = d2i_X509_SIG(NULL, &pder, der_len)) != NULL) {
  111. char pbuf[1024];
  112. size_t plen = 0;
  113. ERR_clear_last_mark();
  114. if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg)) {
  115. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE);
  116. ok = 0;
  117. } else {
  118. const ASN1_OCTET_STRING *oct;
  119. int new_der_len = 0;
  120. X509_SIG_get0(p8, &alg, &oct);
  121. if (!PKCS12_pbe_crypt_ex(alg, pbuf, plen,
  122. oct->data, oct->length,
  123. &new_der, &new_der_len, 0,
  124. libctx, propq)) {
  125. ok = 0;
  126. } else {
  127. der = new_der;
  128. der_len = new_der_len;
  129. }
  130. alg = NULL;
  131. }
  132. X509_SIG_free(p8);
  133. } else {
  134. ERR_pop_to_mark();
  135. }
  136. ERR_set_mark();
  137. pder = der;
  138. p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pder, der_len);
  139. ERR_pop_to_mark();
  140. if (p8inf != NULL && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)) {
  141. /*
  142. * We have something and recognised it as PrivateKeyInfo, so let's
  143. * pass all the applicable data to the callback.
  144. */
  145. char keytype[OSSL_MAX_NAME_SIZE];
  146. OSSL_PARAM params[6], *p = params;
  147. int objtype = OSSL_OBJECT_PKEY;
  148. OBJ_obj2txt(keytype, sizeof(keytype), alg->algorithm, 0);
  149. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  150. keytype, 0);
  151. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_INPUT_TYPE,
  152. "DER", 0);
  153. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
  154. "PrivateKeyInfo", 0);
  155. *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
  156. der, der_len);
  157. *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
  158. *p = OSSL_PARAM_construct_end();
  159. ok = data_cb(params, data_cbarg);
  160. }
  161. PKCS8_PRIV_KEY_INFO_free(p8inf);
  162. OPENSSL_free(new_der);
  163. return ok;
  164. }
  165. const OSSL_DISPATCH ossl_EncryptedPrivateKeyInfo_der_to_der_decoder_functions[] = {
  166. { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))epki2pki_newctx },
  167. { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))epki2pki_freectx },
  168. { OSSL_FUNC_DECODER_DECODE, (void (*)(void))epki2pki_decode },
  169. { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,
  170. (void (*)(void))epki2pki_settable_ctx_params },
  171. { OSSL_FUNC_DECODER_SET_CTX_PARAMS,
  172. (void (*)(void))epki2pki_set_ctx_params },
  173. OSSL_DISPATCH_END
  174. };