decode_epki2pki.c 5.9 KB

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