decoder_pkey.c 28 KB

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