decoder_pkey.c 31 KB

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