decoder_pkey.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. #include <openssl/core_names.h>
  10. #include <openssl/core_object.h>
  11. #include <openssl/provider.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/ui.h>
  14. #include <openssl/decoder.h>
  15. #include <openssl/safestack.h>
  16. #include <openssl/trace.h>
  17. #include "crypto/evp.h"
  18. #include "crypto/decoder.h"
  19. #include "crypto/evp/evp_local.h"
  20. #include "crypto/lhash.h"
  21. #include "encoder_local.h"
  22. #include "internal/namemap.h"
  23. #include "internal/sizes.h"
  24. int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx,
  25. const unsigned char *kstr,
  26. size_t klen)
  27. {
  28. return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
  29. }
  30. int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx,
  31. const UI_METHOD *ui_method,
  32. void *ui_data)
  33. {
  34. return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
  35. }
  36. int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx,
  37. pem_password_cb *cb, void *cbarg)
  38. {
  39. return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
  40. }
  41. int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx,
  42. OSSL_PASSPHRASE_CALLBACK *cb,
  43. void *cbarg)
  44. {
  45. return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
  46. }
  47. /*
  48. * Support for OSSL_DECODER_CTX_new_for_pkey:
  49. * The construct data, and collecting keymgmt information for it
  50. */
  51. DEFINE_STACK_OF(EVP_KEYMGMT)
  52. struct decoder_pkey_data_st {
  53. OSSL_LIB_CTX *libctx;
  54. char *propq;
  55. int selection;
  56. STACK_OF(EVP_KEYMGMT) *keymgmts;
  57. char *object_type; /* recorded object data type, may be NULL */
  58. void **object; /* Where the result should end up */
  59. };
  60. static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst,
  61. const OSSL_PARAM *params,
  62. void *construct_data)
  63. {
  64. struct decoder_pkey_data_st *data = construct_data;
  65. OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  66. void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
  67. const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder);
  68. EVP_KEYMGMT *keymgmt = NULL;
  69. const OSSL_PROVIDER *keymgmt_prov = NULL;
  70. int i, end;
  71. /*
  72. * |object_ref| points to a provider reference to an object, its exact
  73. * contents entirely opaque to us, but may be passed to any provider
  74. * function that expects this (such as OSSL_FUNC_keymgmt_load().
  75. *
  76. * This pointer is considered volatile, i.e. whatever it points at
  77. * is assumed to be freed as soon as this function returns.
  78. */
  79. void *object_ref = NULL;
  80. size_t object_ref_sz = 0;
  81. const OSSL_PARAM *p;
  82. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
  83. if (p != NULL) {
  84. char *object_type = NULL;
  85. if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0))
  86. return 0;
  87. OPENSSL_free(data->object_type);
  88. data->object_type = object_type;
  89. }
  90. /*
  91. * For stuff that should end up in an EVP_PKEY, we only accept an object
  92. * reference for the moment. This enforces that the key data itself
  93. * remains with the provider.
  94. */
  95. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
  96. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  97. return 0;
  98. object_ref = p->data;
  99. object_ref_sz = p->data_size;
  100. /*
  101. * First, we try to find a keymgmt that comes from the same provider as
  102. * the decoder that passed the params.
  103. */
  104. end = sk_EVP_KEYMGMT_num(data->keymgmts);
  105. for (i = 0; i < end; i++) {
  106. keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i);
  107. keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
  108. if (keymgmt_prov == decoder_prov
  109. && evp_keymgmt_has_load(keymgmt)
  110. && EVP_KEYMGMT_is_a(keymgmt, data->object_type))
  111. break;
  112. }
  113. if (i < end) {
  114. /* To allow it to be freed further down */
  115. if (!EVP_KEYMGMT_up_ref(keymgmt))
  116. return 0;
  117. } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx,
  118. data->object_type,
  119. data->propq)) != NULL) {
  120. keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
  121. }
  122. if (keymgmt != NULL) {
  123. EVP_PKEY *pkey = NULL;
  124. void *keydata = NULL;
  125. /*
  126. * If the EVP_KEYMGMT and the OSSL_DECODER are from the
  127. * same provider, we assume that the KEYMGMT has a key loading
  128. * function that can handle the provider reference we hold.
  129. *
  130. * Otherwise, we export from the decoder and import the
  131. * result in the keymgmt.
  132. */
  133. if (keymgmt_prov == decoder_prov) {
  134. keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz);
  135. } else {
  136. struct evp_keymgmt_util_try_import_data_st import_data;
  137. import_data.keymgmt = keymgmt;
  138. import_data.keydata = NULL;
  139. if (data->selection == 0)
  140. /* import/export functions do not tolerate 0 selection */
  141. import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
  142. else
  143. import_data.selection = data->selection;
  144. /*
  145. * No need to check for errors here, the value of
  146. * |import_data.keydata| is as much an indicator.
  147. */
  148. (void)decoder->export_object(decoderctx,
  149. object_ref, object_ref_sz,
  150. &evp_keymgmt_util_try_import,
  151. &import_data);
  152. keydata = import_data.keydata;
  153. import_data.keydata = NULL;
  154. }
  155. if (keydata != NULL
  156. && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL)
  157. evp_keymgmt_freedata(keymgmt, keydata);
  158. *data->object = pkey;
  159. /*
  160. * evp_keymgmt_util_make_pkey() increments the reference count when
  161. * assigning the EVP_PKEY, so we can free the keymgmt here.
  162. */
  163. EVP_KEYMGMT_free(keymgmt);
  164. }
  165. /*
  166. * We successfully looked through, |*ctx->object| determines if we
  167. * actually found something.
  168. */
  169. return (*data->object != NULL);
  170. }
  171. static void decoder_clean_pkey_construct_arg(void *construct_data)
  172. {
  173. struct decoder_pkey_data_st *data = construct_data;
  174. if (data != NULL) {
  175. sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free);
  176. OPENSSL_free(data->propq);
  177. OPENSSL_free(data->object_type);
  178. OPENSSL_free(data);
  179. }
  180. }
  181. struct collect_data_st {
  182. OSSL_LIB_CTX *libctx;
  183. OSSL_DECODER_CTX *ctx;
  184. const char *keytype; /* the keytype requested, if any */
  185. int keytype_id; /* if keytype_resolved is set, keymgmt name_id; else 0 */
  186. int sm2_id; /* if keytype_resolved is set and EC, SM2 name_id; else 0 */
  187. int total; /* number of matching results */
  188. char error_occurred;
  189. char keytype_resolved;
  190. STACK_OF(EVP_KEYMGMT) *keymgmts;
  191. };
  192. static void collect_decoder_keymgmt(EVP_KEYMGMT *keymgmt, OSSL_DECODER *decoder,
  193. void *provctx, struct collect_data_st *data)
  194. {
  195. void *decoderctx = NULL;
  196. OSSL_DECODER_INSTANCE *di = NULL;
  197. /*
  198. * We already checked the EVP_KEYMGMT is applicable in check_keymgmt so we
  199. * don't check it again here.
  200. */
  201. if (keymgmt->name_id != decoder->base.id)
  202. /* Mismatch is not an error, continue. */
  203. return;
  204. if ((decoderctx = decoder->newctx(provctx)) == NULL) {
  205. data->error_occurred = 1;
  206. return;
  207. }
  208. if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
  209. decoder->freectx(decoderctx);
  210. data->error_occurred = 1;
  211. return;
  212. }
  213. OSSL_TRACE_BEGIN(DECODER) {
  214. BIO_printf(trc_out,
  215. "(ctx %p) Checking out decoder %p:\n"
  216. " %s with %s\n",
  217. (void *)data->ctx, (void *)decoder,
  218. OSSL_DECODER_get0_name(decoder),
  219. OSSL_DECODER_get0_properties(decoder));
  220. } OSSL_TRACE_END(DECODER);
  221. if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
  222. ossl_decoder_instance_free(di);
  223. data->error_occurred = 1;
  224. return;
  225. }
  226. ++data->total;
  227. }
  228. static void collect_decoder(OSSL_DECODER *decoder, void *arg)
  229. {
  230. struct collect_data_st *data = arg;
  231. STACK_OF(EVP_KEYMGMT) *keymgmts = data->keymgmts;
  232. int i, end_i;
  233. EVP_KEYMGMT *keymgmt;
  234. const OSSL_PROVIDER *prov;
  235. void *provctx;
  236. if (data->error_occurred)
  237. return;
  238. prov = OSSL_DECODER_get0_provider(decoder);
  239. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  240. /*
  241. * Either the caller didn't give us a selection, or if they did, the decoder
  242. * must tell us if it supports that selection to be accepted. If the decoder
  243. * doesn't have |does_selection|, it's seen as taking anything.
  244. */
  245. if (decoder->does_selection != NULL
  246. && !decoder->does_selection(provctx, data->ctx->selection))
  247. return;
  248. OSSL_TRACE_BEGIN(DECODER) {
  249. BIO_printf(trc_out,
  250. "(ctx %p) Checking out decoder %p:\n"
  251. " %s with %s\n",
  252. (void *)data->ctx, (void *)decoder,
  253. OSSL_DECODER_get0_name(decoder),
  254. OSSL_DECODER_get0_properties(decoder));
  255. } OSSL_TRACE_END(DECODER);
  256. end_i = sk_EVP_KEYMGMT_num(keymgmts);
  257. for (i = 0; i < end_i; ++i) {
  258. keymgmt = sk_EVP_KEYMGMT_value(keymgmts, i);
  259. collect_decoder_keymgmt(keymgmt, decoder, provctx, data);
  260. if (data->error_occurred)
  261. return;
  262. }
  263. }
  264. /*
  265. * Is this EVP_KEYMGMT applicable given the key type given in the call to
  266. * ossl_decoder_ctx_setup_for_pkey (if any)?
  267. */
  268. static int check_keymgmt(EVP_KEYMGMT *keymgmt, struct collect_data_st *data)
  269. {
  270. /* If no keytype was specified, everything matches. */
  271. if (data->keytype == NULL)
  272. return 1;
  273. if (!data->keytype_resolved) {
  274. /* We haven't cached the IDs from the keytype string yet. */
  275. OSSL_NAMEMAP *namemap = ossl_namemap_stored(data->libctx);
  276. data->keytype_id = ossl_namemap_name2num(namemap, data->keytype);
  277. /*
  278. * If keytype is a value ambiguously used for both EC and SM2,
  279. * collect the ID for SM2 as well.
  280. */
  281. if (data->keytype_id != 0
  282. && (strcmp(data->keytype, "id-ecPublicKey") == 0
  283. || strcmp(data->keytype, "1.2.840.10045.2.1") == 0))
  284. data->sm2_id = ossl_namemap_name2num(namemap, "SM2");
  285. /*
  286. * If keytype_id is zero the name was not found, but we still
  287. * set keytype_resolved to avoid trying all this again.
  288. */
  289. data->keytype_resolved = 1;
  290. }
  291. /* Specified keytype could not be resolved, so nothing matches. */
  292. if (data->keytype_id == 0)
  293. return 0;
  294. /* Does not match the keytype specified, so skip. */
  295. if (keymgmt->name_id != data->keytype_id
  296. && keymgmt->name_id != data->sm2_id)
  297. return 0;
  298. return 1;
  299. }
  300. static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
  301. {
  302. struct collect_data_st *data = arg;
  303. if (!check_keymgmt(keymgmt, data))
  304. return;
  305. /*
  306. * We have to ref EVP_KEYMGMT here because in the success case,
  307. * data->keymgmts is referenced by the constructor we register in the
  308. * OSSL_DECODER_CTX. The registered cleanup function
  309. * (decoder_clean_pkey_construct_arg) unrefs every element of the stack and
  310. * frees it.
  311. */
  312. if (!EVP_KEYMGMT_up_ref(keymgmt))
  313. return;
  314. if (sk_EVP_KEYMGMT_push(data->keymgmts, keymgmt) <= 0) {
  315. EVP_KEYMGMT_free(keymgmt);
  316. data->error_occurred = 1;
  317. }
  318. }
  319. /*
  320. * This function does the actual binding of decoders to the OSSL_DECODER_CTX. It
  321. * searches for decoders matching 'keytype', which is a string like "RSA", "DH",
  322. * etc. If 'keytype' is NULL, decoders for all keytypes are bound.
  323. */
  324. static int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx,
  325. const char *keytype,
  326. OSSL_LIB_CTX *libctx,
  327. const char *propquery)
  328. {
  329. int ok = 0;
  330. struct decoder_pkey_data_st *process_data = NULL;
  331. struct collect_data_st collect_data = { NULL };
  332. STACK_OF(EVP_KEYMGMT) *keymgmts = NULL;
  333. OSSL_TRACE_BEGIN(DECODER) {
  334. const char *input_type = ctx->start_input_type;
  335. const char *input_structure = ctx->input_structure;
  336. BIO_printf(trc_out,
  337. "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n",
  338. (void *)ctx,
  339. keytype != NULL ? keytype : "",
  340. keytype != NULL ? " keys" : "keys of any type",
  341. input_type != NULL ? " from " : "",
  342. input_type != NULL ? input_type : "",
  343. input_structure != NULL ? " with " : "",
  344. input_structure != NULL ? input_structure : "");
  345. } OSSL_TRACE_END(DECODER);
  346. /* Allocate data. */
  347. if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL)
  348. goto err;
  349. if ((propquery != NULL
  350. && (process_data->propq = OPENSSL_strdup(propquery)) == NULL))
  351. goto err;
  352. /* Allocate our list of EVP_KEYMGMTs. */
  353. keymgmts = sk_EVP_KEYMGMT_new_null();
  354. if (keymgmts == NULL) {
  355. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  356. goto err;
  357. }
  358. process_data->object = NULL;
  359. process_data->libctx = libctx;
  360. process_data->selection = ctx->selection;
  361. process_data->keymgmts = keymgmts;
  362. /*
  363. * Enumerate all keymgmts into a stack.
  364. *
  365. * We could nest EVP_KEYMGMT_do_all_provided inside
  366. * OSSL_DECODER_do_all_provided or vice versa but these functions become
  367. * bottlenecks if called repeatedly, which is why we collect the
  368. * EVP_KEYMGMTs into a stack here and call both functions only once.
  369. *
  370. * We resolve the keytype string to a name ID so we don't have to resolve it
  371. * multiple times, avoiding repeated calls to EVP_KEYMGMT_is_a, which is a
  372. * performance bottleneck. However, we do this lazily on the first call to
  373. * collect_keymgmt made by EVP_KEYMGMT_do_all_provided, rather than do it
  374. * upfront, as this ensures that the names for all loaded providers have
  375. * been registered by the time we try to resolve the keytype string.
  376. */
  377. collect_data.ctx = ctx;
  378. collect_data.libctx = libctx;
  379. collect_data.keymgmts = keymgmts;
  380. collect_data.keytype = keytype;
  381. EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt, &collect_data);
  382. if (collect_data.error_occurred)
  383. goto err;
  384. /* Enumerate all matching decoders. */
  385. OSSL_DECODER_do_all_provided(libctx, collect_decoder, &collect_data);
  386. if (collect_data.error_occurred)
  387. goto err;
  388. OSSL_TRACE_BEGIN(DECODER) {
  389. BIO_printf(trc_out,
  390. "(ctx %p) Got %d decoders producing keys\n",
  391. (void *)ctx, collect_data.total);
  392. } OSSL_TRACE_END(DECODER);
  393. /*
  394. * Finish initializing the decoder context. If one or more decoders matched
  395. * above then the number of decoders attached to the OSSL_DECODER_CTX will
  396. * be nonzero. Else nothing was found and we do nothing.
  397. */
  398. if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
  399. if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey)
  400. || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data)
  401. || !OSSL_DECODER_CTX_set_cleanup(ctx,
  402. decoder_clean_pkey_construct_arg))
  403. goto err;
  404. process_data = NULL; /* Avoid it being freed */
  405. }
  406. ok = 1;
  407. err:
  408. decoder_clean_pkey_construct_arg(process_data);
  409. return ok;
  410. }
  411. /* Only const here because deep_copy requires it */
  412. static EVP_KEYMGMT *keymgmt_dup(const EVP_KEYMGMT *keymgmt)
  413. {
  414. if (!EVP_KEYMGMT_up_ref((EVP_KEYMGMT *)keymgmt))
  415. return NULL;
  416. return (EVP_KEYMGMT *)keymgmt;
  417. }
  418. /*
  419. * Duplicates a template OSSL_DECODER_CTX that has been setup for an EVP_PKEY
  420. * operation and sets up the duplicate for a new operation.
  421. * It does not duplicate the pwdata on the assumption that this does not form
  422. * part of the template. That is set up later.
  423. */
  424. static OSSL_DECODER_CTX *
  425. ossl_decoder_ctx_for_pkey_dup(OSSL_DECODER_CTX *src,
  426. EVP_PKEY **pkey,
  427. const char *input_type,
  428. const char *input_structure)
  429. {
  430. OSSL_DECODER_CTX *dest;
  431. struct decoder_pkey_data_st *process_data_src, *process_data_dest = NULL;
  432. if (src == NULL)
  433. return NULL;
  434. if ((dest = OSSL_DECODER_CTX_new()) == NULL) {
  435. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  436. return NULL;
  437. }
  438. if (!OSSL_DECODER_CTX_set_input_type(dest, input_type)
  439. || !OSSL_DECODER_CTX_set_input_structure(dest, input_structure)) {
  440. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  441. goto err;
  442. }
  443. dest->selection = src->selection;
  444. if (src->decoder_insts != NULL) {
  445. dest->decoder_insts
  446. = sk_OSSL_DECODER_INSTANCE_deep_copy(src->decoder_insts,
  447. ossl_decoder_instance_dup,
  448. ossl_decoder_instance_free);
  449. if (dest->decoder_insts == NULL) {
  450. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  451. goto err;
  452. }
  453. }
  454. if (!OSSL_DECODER_CTX_set_construct(dest,
  455. OSSL_DECODER_CTX_get_construct(src))) {
  456. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  457. goto err;
  458. }
  459. process_data_src = OSSL_DECODER_CTX_get_construct_data(src);
  460. if (process_data_src != NULL) {
  461. process_data_dest = OPENSSL_zalloc(sizeof(*process_data_dest));
  462. if (process_data_dest == NULL) {
  463. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  464. goto err;
  465. }
  466. if (process_data_src->propq != NULL) {
  467. process_data_dest->propq = OPENSSL_strdup(process_data_src->propq);
  468. if (process_data_dest->propq == NULL) {
  469. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  470. goto err;
  471. }
  472. }
  473. if (process_data_src->keymgmts != NULL) {
  474. process_data_dest->keymgmts
  475. = sk_EVP_KEYMGMT_deep_copy(process_data_src->keymgmts,
  476. keymgmt_dup,
  477. EVP_KEYMGMT_free);
  478. if (process_data_dest->keymgmts == NULL) {
  479. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_EVP_LIB);
  480. goto err;
  481. }
  482. }
  483. process_data_dest->object = (void **)pkey;
  484. process_data_dest->libctx = process_data_src->libctx;
  485. process_data_dest->selection = process_data_src->selection;
  486. if (!OSSL_DECODER_CTX_set_construct_data(dest, process_data_dest)) {
  487. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  488. goto err;
  489. }
  490. process_data_dest = NULL;
  491. }
  492. if (!OSSL_DECODER_CTX_set_cleanup(dest,
  493. OSSL_DECODER_CTX_get_cleanup(src))) {
  494. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  495. goto err;
  496. }
  497. return dest;
  498. err:
  499. if (process_data_dest != NULL) {
  500. OPENSSL_free(process_data_dest->propq);
  501. sk_EVP_KEYMGMT_pop_free(process_data_dest->keymgmts, EVP_KEYMGMT_free);
  502. OPENSSL_free(process_data_dest);
  503. }
  504. OSSL_DECODER_CTX_free(dest);
  505. return NULL;
  506. }
  507. typedef struct {
  508. char *input_type;
  509. char *input_structure;
  510. char *keytype;
  511. int selection;
  512. char *propquery;
  513. OSSL_DECODER_CTX *template;
  514. } DECODER_CACHE_ENTRY;
  515. DEFINE_LHASH_OF_EX(DECODER_CACHE_ENTRY);
  516. typedef struct {
  517. CRYPTO_RWLOCK *lock;
  518. LHASH_OF(DECODER_CACHE_ENTRY) *hashtable;
  519. } DECODER_CACHE;
  520. static void decoder_cache_entry_free(DECODER_CACHE_ENTRY *entry)
  521. {
  522. if (entry == NULL)
  523. return;
  524. OPENSSL_free(entry->input_type);
  525. OPENSSL_free(entry->input_structure);
  526. OPENSSL_free(entry->keytype);
  527. OPENSSL_free(entry->propquery);
  528. OSSL_DECODER_CTX_free(entry->template);
  529. OPENSSL_free(entry);
  530. }
  531. static unsigned long decoder_cache_entry_hash(const DECODER_CACHE_ENTRY *cache)
  532. {
  533. unsigned long hash = 17;
  534. hash = (hash * 23)
  535. + (cache->propquery == NULL
  536. ? 0 : ossl_lh_strcasehash(cache->propquery));
  537. hash = (hash * 23)
  538. + (cache->input_structure == NULL
  539. ? 0 : ossl_lh_strcasehash(cache->input_structure));
  540. hash = (hash * 23)
  541. + (cache->input_type == NULL
  542. ? 0 : ossl_lh_strcasehash(cache->input_type));
  543. hash = (hash * 23)
  544. + (cache->keytype == NULL
  545. ? 0 : ossl_lh_strcasehash(cache->keytype));
  546. hash ^= cache->selection;
  547. return hash;
  548. }
  549. static ossl_inline int nullstrcmp(const char *a, const char *b, int casecmp)
  550. {
  551. if (a == NULL || b == NULL) {
  552. if (a == NULL) {
  553. if (b == NULL)
  554. return 0;
  555. else
  556. return 1;
  557. } else {
  558. return -1;
  559. }
  560. } else {
  561. if (casecmp)
  562. return OPENSSL_strcasecmp(a, b);
  563. else
  564. return strcmp(a, b);
  565. }
  566. }
  567. static int decoder_cache_entry_cmp(const DECODER_CACHE_ENTRY *a,
  568. const DECODER_CACHE_ENTRY *b)
  569. {
  570. int cmp;
  571. if (a->selection != b->selection)
  572. return (a->selection < b->selection) ? -1 : 1;
  573. cmp = nullstrcmp(a->keytype, b->keytype, 1);
  574. if (cmp != 0)
  575. return cmp;
  576. cmp = nullstrcmp(a->input_type, b->input_type, 1);
  577. if (cmp != 0)
  578. return cmp;
  579. cmp = nullstrcmp(a->input_structure, b->input_structure, 1);
  580. if (cmp != 0)
  581. return cmp;
  582. cmp = nullstrcmp(a->propquery, b->propquery, 0);
  583. return cmp;
  584. }
  585. void *ossl_decoder_cache_new(OSSL_LIB_CTX *ctx)
  586. {
  587. DECODER_CACHE *cache = OPENSSL_malloc(sizeof(*cache));
  588. if (cache == NULL)
  589. return NULL;
  590. cache->lock = CRYPTO_THREAD_lock_new();
  591. if (cache->lock == NULL) {
  592. OPENSSL_free(cache);
  593. return NULL;
  594. }
  595. cache->hashtable = lh_DECODER_CACHE_ENTRY_new(decoder_cache_entry_hash,
  596. decoder_cache_entry_cmp);
  597. if (cache->hashtable == NULL) {
  598. CRYPTO_THREAD_lock_free(cache->lock);
  599. OPENSSL_free(cache);
  600. return NULL;
  601. }
  602. return cache;
  603. }
  604. void ossl_decoder_cache_free(void *vcache)
  605. {
  606. DECODER_CACHE *cache = (DECODER_CACHE *)vcache;
  607. lh_DECODER_CACHE_ENTRY_doall(cache->hashtable, decoder_cache_entry_free);
  608. lh_DECODER_CACHE_ENTRY_free(cache->hashtable);
  609. CRYPTO_THREAD_lock_free(cache->lock);
  610. OPENSSL_free(cache);
  611. }
  612. /*
  613. * Called whenever a provider gets activated/deactivated. In that case the
  614. * decoders that are available might change so we flush our cache.
  615. */
  616. int ossl_decoder_cache_flush(OSSL_LIB_CTX *libctx)
  617. {
  618. DECODER_CACHE *cache
  619. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_CACHE_INDEX);
  620. if (cache == NULL)
  621. return 0;
  622. if (!CRYPTO_THREAD_write_lock(cache->lock)) {
  623. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  624. return 0;
  625. }
  626. lh_DECODER_CACHE_ENTRY_doall(cache->hashtable, decoder_cache_entry_free);
  627. lh_DECODER_CACHE_ENTRY_flush(cache->hashtable);
  628. CRYPTO_THREAD_unlock(cache->lock);
  629. return 1;
  630. }
  631. OSSL_DECODER_CTX *
  632. OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey,
  633. const char *input_type,
  634. const char *input_structure,
  635. const char *keytype, int selection,
  636. OSSL_LIB_CTX *libctx, const char *propquery)
  637. {
  638. OSSL_DECODER_CTX *ctx = NULL;
  639. OSSL_PARAM decoder_params[] = {
  640. OSSL_PARAM_END,
  641. OSSL_PARAM_END
  642. };
  643. DECODER_CACHE *cache
  644. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_CACHE_INDEX);
  645. DECODER_CACHE_ENTRY cacheent, *res, *newcache = NULL;
  646. if (cache == NULL) {
  647. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  648. return NULL;
  649. }
  650. if (propquery != NULL)
  651. decoder_params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DECODER_PARAM_PROPERTIES,
  652. (char *)propquery, 0);
  653. /* It is safe to cast away the const here */
  654. cacheent.input_type = (char *)input_type;
  655. cacheent.input_structure = (char *)input_structure;
  656. cacheent.keytype = (char *)keytype;
  657. cacheent.selection = selection;
  658. cacheent.propquery = (char *)propquery;
  659. if (!CRYPTO_THREAD_read_lock(cache->lock)) {
  660. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  661. return NULL;
  662. }
  663. /* First see if we have a template OSSL_DECODER_CTX */
  664. res = lh_DECODER_CACHE_ENTRY_retrieve(cache->hashtable, &cacheent);
  665. if (res == NULL) {
  666. /*
  667. * There is no template so we will have to construct one. This will be
  668. * time consuming so release the lock and we will later upgrade it to a
  669. * write lock.
  670. */
  671. CRYPTO_THREAD_unlock(cache->lock);
  672. if ((ctx = OSSL_DECODER_CTX_new()) == NULL) {
  673. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  674. return NULL;
  675. }
  676. OSSL_TRACE_BEGIN(DECODER) {
  677. BIO_printf(trc_out,
  678. "(ctx %p) Looking for %s decoders with selection %d\n",
  679. (void *)ctx, keytype, selection);
  680. BIO_printf(trc_out, " input type: %s, input structure: %s\n",
  681. input_type, input_structure);
  682. } OSSL_TRACE_END(DECODER);
  683. if (OSSL_DECODER_CTX_set_input_type(ctx, input_type)
  684. && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure)
  685. && OSSL_DECODER_CTX_set_selection(ctx, selection)
  686. && ossl_decoder_ctx_setup_for_pkey(ctx, keytype, libctx, propquery)
  687. && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)
  688. && (propquery == NULL
  689. || OSSL_DECODER_CTX_set_params(ctx, decoder_params))) {
  690. OSSL_TRACE_BEGIN(DECODER) {
  691. BIO_printf(trc_out, "(ctx %p) Got %d decoders\n",
  692. (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx));
  693. } OSSL_TRACE_END(DECODER);
  694. } else {
  695. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
  696. OSSL_DECODER_CTX_free(ctx);
  697. return NULL;
  698. }
  699. newcache = OPENSSL_zalloc(sizeof(*newcache));
  700. if (newcache == NULL) {
  701. OSSL_DECODER_CTX_free(ctx);
  702. return NULL;
  703. }
  704. if (input_type != NULL) {
  705. newcache->input_type = OPENSSL_strdup(input_type);
  706. if (newcache->input_type == NULL)
  707. goto err;
  708. }
  709. if (input_structure != NULL) {
  710. newcache->input_structure = OPENSSL_strdup(input_structure);
  711. if (newcache->input_structure == NULL)
  712. goto err;
  713. }
  714. if (keytype != NULL) {
  715. newcache->keytype = OPENSSL_strdup(keytype);
  716. if (newcache->keytype == NULL)
  717. goto err;
  718. }
  719. if (propquery != NULL) {
  720. newcache->propquery = OPENSSL_strdup(propquery);
  721. if (newcache->propquery == NULL)
  722. goto err;
  723. }
  724. newcache->selection = selection;
  725. newcache->template = ctx;
  726. if (!CRYPTO_THREAD_write_lock(cache->lock)) {
  727. ctx = NULL;
  728. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  729. goto err;
  730. }
  731. res = lh_DECODER_CACHE_ENTRY_retrieve(cache->hashtable, &cacheent);
  732. if (res == NULL) {
  733. (void)lh_DECODER_CACHE_ENTRY_insert(cache->hashtable, newcache);
  734. if (lh_DECODER_CACHE_ENTRY_error(cache->hashtable)) {
  735. ctx = NULL;
  736. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  737. goto err;
  738. }
  739. } else {
  740. /*
  741. * We raced with another thread to construct this and lost. Free
  742. * what we just created and use the entry from the hashtable instead
  743. */
  744. decoder_cache_entry_free(newcache);
  745. ctx = res->template;
  746. }
  747. } else {
  748. ctx = res->template;
  749. }
  750. ctx = ossl_decoder_ctx_for_pkey_dup(ctx, pkey, input_type, input_structure);
  751. CRYPTO_THREAD_unlock(cache->lock);
  752. return ctx;
  753. err:
  754. decoder_cache_entry_free(newcache);
  755. OSSL_DECODER_CTX_free(ctx);
  756. return NULL;
  757. }