decode_pvk2key.c 9.8 KB

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