decode_pvk2key.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2020-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. /*
  10. * low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/core_object.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/params.h>
  20. #include <openssl/err.h>
  21. #include <openssl/pem.h> /* For public PVK functions */
  22. #include <openssl/x509.h>
  23. #include "internal/passphrase.h"
  24. #include "crypto/pem.h" /* For internal PVK and "blob" headers */
  25. #include "crypto/rsa.h"
  26. #include "prov/bio.h"
  27. #include "prov/implementations.h"
  28. #include "endecoder_local.h"
  29. struct pvk2key_ctx_st; /* Forward declaration */
  30. typedef int check_key_fn(void *, struct pvk2key_ctx_st *ctx);
  31. typedef void adjust_key_fn(void *, struct pvk2key_ctx_st *ctx);
  32. typedef void *b2i_PVK_of_bio_pw_fn(BIO *in, pem_password_cb *cb, void *u,
  33. OSSL_LIB_CTX *libctx, const char *propq);
  34. typedef void free_key_fn(void *);
  35. struct keytype_desc_st {
  36. int type; /* EVP key type */
  37. const char *name; /* Keytype */
  38. const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
  39. b2i_PVK_of_bio_pw_fn *read_private_key;
  40. adjust_key_fn *adjust_key;
  41. free_key_fn *free_key;
  42. };
  43. static OSSL_FUNC_decoder_freectx_fn pvk2key_freectx;
  44. static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
  45. static OSSL_FUNC_decoder_export_object_fn pvk2key_export_object;
  46. /*
  47. * Context used for DER to key decoding.
  48. */
  49. struct pvk2key_ctx_st {
  50. PROV_CTX *provctx;
  51. const struct keytype_desc_st *desc;
  52. /* The selection that is passed to der2key_decode() */
  53. int selection;
  54. };
  55. static struct pvk2key_ctx_st *
  56. pvk2key_newctx(void *provctx, const struct keytype_desc_st *desc)
  57. {
  58. struct pvk2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  59. if (ctx != NULL) {
  60. ctx->provctx = provctx;
  61. ctx->desc = desc;
  62. }
  63. return ctx;
  64. }
  65. static void pvk2key_freectx(void *vctx)
  66. {
  67. struct pvk2key_ctx_st *ctx = vctx;
  68. OPENSSL_free(ctx);
  69. }
  70. static int pvk2key_does_selection(void *provctx, int selection)
  71. {
  72. if (selection == 0)
  73. return 1;
  74. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  75. return 1;
  76. return 0;
  77. }
  78. static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  79. OSSL_CALLBACK *data_cb, void *data_cbarg,
  80. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  81. {
  82. struct pvk2key_ctx_st *ctx = vctx;
  83. BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
  84. void *key = NULL;
  85. int ok = 0;
  86. if (in == NULL)
  87. return 0;
  88. ctx->selection = selection;
  89. if ((selection == 0
  90. || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  91. && ctx->desc->read_private_key != NULL) {
  92. struct ossl_passphrase_data_st pwdata;
  93. int err, lib, reason;
  94. memset(&pwdata, 0, sizeof(pwdata));
  95. if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
  96. goto end;
  97. key = ctx->desc->read_private_key(in, ossl_pw_pvk_password, &pwdata,
  98. PROV_LIBCTX_OF(ctx->provctx), NULL);
  99. /*
  100. * Because the PVK API doesn't have a separate decrypt call, we need
  101. * to check the error queue for certain well known errors that are
  102. * considered fatal and which we pass through, while the rest gets
  103. * thrown away.
  104. */
  105. err = ERR_peek_last_error();
  106. lib = ERR_GET_LIB(err);
  107. reason = ERR_GET_REASON(err);
  108. if (lib == ERR_LIB_PEM
  109. && (reason == PEM_R_BAD_PASSWORD_READ
  110. || reason == PEM_R_BAD_DECRYPT)) {
  111. ERR_clear_last_mark();
  112. goto end;
  113. }
  114. if (selection != 0 && key == NULL)
  115. goto next;
  116. }
  117. if (key != NULL && ctx->desc->adjust_key != NULL)
  118. ctx->desc->adjust_key(key, ctx);
  119. next:
  120. /*
  121. * Indicated that we successfully decoded something, or not at all.
  122. * Ending up "empty handed" is not an error.
  123. */
  124. ok = 1;
  125. /*
  126. * We free resources here so it's not held up during the callback, because
  127. * we know the process is recursive and the allocated chunks of memory
  128. * add up.
  129. */
  130. BIO_free(in);
  131. in = NULL;
  132. if (key != NULL) {
  133. OSSL_PARAM params[4];
  134. int object_type = OSSL_OBJECT_PKEY;
  135. params[0] =
  136. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  137. params[1] =
  138. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  139. (char *)ctx->desc->name, 0);
  140. /* The address of the key becomes the octet string */
  141. params[2] =
  142. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  143. &key, sizeof(key));
  144. params[3] = OSSL_PARAM_construct_end();
  145. ok = data_cb(params, data_cbarg);
  146. }
  147. end:
  148. BIO_free(in);
  149. ctx->desc->free_key(key);
  150. return ok;
  151. }
  152. static int pvk2key_export_object(void *vctx,
  153. const void *reference, size_t reference_sz,
  154. OSSL_CALLBACK *export_cb, void *export_cbarg)
  155. {
  156. struct pvk2key_ctx_st *ctx = vctx;
  157. OSSL_FUNC_keymgmt_export_fn *export =
  158. ossl_prov_get_keymgmt_export(ctx->desc->fns);
  159. void *keydata;
  160. if (reference_sz == sizeof(keydata) && export != NULL) {
  161. int selection = ctx->selection;
  162. if (selection == 0)
  163. selection = OSSL_KEYMGMT_SELECT_ALL;
  164. /* The contents of the reference is the address to our object */
  165. keydata = *(void **)reference;
  166. return export(keydata, selection, export_cb, export_cbarg);
  167. }
  168. return 0;
  169. }
  170. /* ---------------------------------------------------------------------- */
  171. #define dsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex
  172. #define dsa_adjust NULL
  173. #define dsa_free (void (*)(void *))DSA_free
  174. /* ---------------------------------------------------------------------- */
  175. #define rsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex
  176. static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx)
  177. {
  178. ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  179. }
  180. #define rsa_free (void (*)(void *))RSA_free
  181. /* ---------------------------------------------------------------------- */
  182. #define IMPLEMENT_MS(KEYTYPE, keytype) \
  183. static const struct keytype_desc_st \
  184. pvk2##keytype##_desc = { \
  185. EVP_PKEY_##KEYTYPE, #KEYTYPE, \
  186. ossl_##keytype##_keymgmt_functions, \
  187. keytype##_private_key_bio, \
  188. keytype##_adjust, \
  189. keytype##_free \
  190. }; \
  191. static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx; \
  192. static void *pvk2##keytype##_newctx(void *provctx) \
  193. { \
  194. return pvk2key_newctx(provctx, &pvk2##keytype##_desc); \
  195. } \
  196. const OSSL_DISPATCH \
  197. ossl_##pvk_to_##keytype##_decoder_functions[] = { \
  198. { OSSL_FUNC_DECODER_NEWCTX, \
  199. (void (*)(void))pvk2##keytype##_newctx }, \
  200. { OSSL_FUNC_DECODER_FREECTX, \
  201. (void (*)(void))pvk2key_freectx }, \
  202. { OSSL_FUNC_DECODER_DOES_SELECTION, \
  203. (void (*)(void))pvk2key_does_selection }, \
  204. { OSSL_FUNC_DECODER_DECODE, \
  205. (void (*)(void))pvk2key_decode }, \
  206. { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
  207. (void (*)(void))pvk2key_export_object }, \
  208. { 0, NULL } \
  209. }
  210. #ifndef OPENSSL_NO_DSA
  211. IMPLEMENT_MS(DSA, dsa);
  212. #endif
  213. IMPLEMENT_MS(RSA, rsa);