property.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include <openssl/crypto.h>
  14. #include "internal/core.h"
  15. #include "internal/property.h"
  16. #include "internal/provider.h"
  17. #include "internal/tsan_assist.h"
  18. #include "crypto/ctype.h"
  19. #include <openssl/lhash.h>
  20. #include <openssl/rand.h>
  21. #include "internal/thread_once.h"
  22. #include "crypto/lhash.h"
  23. #include "crypto/sparse_array.h"
  24. #include "property_local.h"
  25. #include "crypto/context.h"
  26. /*
  27. * The number of elements in the query cache before we initiate a flush.
  28. * If reducing this, also ensure the stochastic test in test/property_test.c
  29. * isn't likely to fail.
  30. */
  31. #define IMPL_CACHE_FLUSH_THRESHOLD 500
  32. typedef struct {
  33. void *method;
  34. int (*up_ref)(void *);
  35. void (*free)(void *);
  36. } METHOD;
  37. typedef struct {
  38. const OSSL_PROVIDER *provider;
  39. OSSL_PROPERTY_LIST *properties;
  40. METHOD method;
  41. } IMPLEMENTATION;
  42. DEFINE_STACK_OF(IMPLEMENTATION)
  43. typedef struct {
  44. const OSSL_PROVIDER *provider;
  45. const char *query;
  46. METHOD method;
  47. char body[1];
  48. } QUERY;
  49. DEFINE_LHASH_OF_EX(QUERY);
  50. typedef struct {
  51. int nid;
  52. STACK_OF(IMPLEMENTATION) *impls;
  53. LHASH_OF(QUERY) *cache;
  54. } ALGORITHM;
  55. struct ossl_method_store_st {
  56. OSSL_LIB_CTX *ctx;
  57. SPARSE_ARRAY_OF(ALGORITHM) *algs;
  58. /*
  59. * Lock to protect the |algs| array from concurrent writing, when
  60. * individual implementations or queries are inserted. This is used
  61. * by the appropriate functions here.
  62. */
  63. CRYPTO_RWLOCK *lock;
  64. /*
  65. * Lock to reserve the whole store. This is used when fetching a set
  66. * of algorithms, via these functions, found in crypto/core_fetch.c:
  67. * ossl_method_construct_reserve_store()
  68. * ossl_method_construct_unreserve_store()
  69. */
  70. CRYPTO_RWLOCK *biglock;
  71. /* query cache specific values */
  72. /* Count of the query cache entries for all algs */
  73. size_t cache_nelem;
  74. /* Flag: 1 if query cache entries for all algs need flushing */
  75. int cache_need_flush;
  76. };
  77. typedef struct {
  78. LHASH_OF(QUERY) *cache;
  79. size_t nelem;
  80. uint32_t seed;
  81. unsigned char using_global_seed;
  82. } IMPL_CACHE_FLUSH;
  83. DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
  84. typedef struct ossl_global_properties_st {
  85. OSSL_PROPERTY_LIST *list;
  86. #ifndef FIPS_MODULE
  87. unsigned int no_mirrored : 1;
  88. #endif
  89. } OSSL_GLOBAL_PROPERTIES;
  90. static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
  91. ALGORITHM *alg);
  92. static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
  93. /* Global properties are stored per library context */
  94. void ossl_ctx_global_properties_free(void *vglobp)
  95. {
  96. OSSL_GLOBAL_PROPERTIES *globp = vglobp;
  97. if (globp != NULL) {
  98. ossl_property_free(globp->list);
  99. OPENSSL_free(globp);
  100. }
  101. }
  102. void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
  103. {
  104. return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
  105. }
  106. OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
  107. int loadconfig)
  108. {
  109. OSSL_GLOBAL_PROPERTIES *globp;
  110. #ifndef FIPS_MODULE
  111. if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
  112. return NULL;
  113. #endif
  114. globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
  115. return globp != NULL ? &globp->list : NULL;
  116. }
  117. #ifndef FIPS_MODULE
  118. int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
  119. {
  120. OSSL_GLOBAL_PROPERTIES *globp
  121. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
  122. return globp != NULL && globp->no_mirrored ? 1 : 0;
  123. }
  124. void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
  125. {
  126. OSSL_GLOBAL_PROPERTIES *globp
  127. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
  128. if (globp != NULL)
  129. globp->no_mirrored = 1;
  130. }
  131. #endif
  132. static int ossl_method_up_ref(METHOD *method)
  133. {
  134. return (*method->up_ref)(method->method);
  135. }
  136. static void ossl_method_free(METHOD *method)
  137. {
  138. (*method->free)(method->method);
  139. }
  140. static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
  141. {
  142. return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
  143. }
  144. static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
  145. {
  146. return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
  147. }
  148. static int ossl_property_unlock(OSSL_METHOD_STORE *p)
  149. {
  150. return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
  151. }
  152. static unsigned long query_hash(const QUERY *a)
  153. {
  154. return OPENSSL_LH_strhash(a->query);
  155. }
  156. static int query_cmp(const QUERY *a, const QUERY *b)
  157. {
  158. int res = strcmp(a->query, b->query);
  159. if (res == 0 && a->provider != NULL && b->provider != NULL)
  160. res = b->provider > a->provider ? 1
  161. : b->provider < a->provider ? -1
  162. : 0;
  163. return res;
  164. }
  165. static void impl_free(IMPLEMENTATION *impl)
  166. {
  167. if (impl != NULL) {
  168. ossl_method_free(&impl->method);
  169. OPENSSL_free(impl);
  170. }
  171. }
  172. static void impl_cache_free(QUERY *elem)
  173. {
  174. if (elem != NULL) {
  175. ossl_method_free(&elem->method);
  176. OPENSSL_free(elem);
  177. }
  178. }
  179. static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
  180. {
  181. lh_QUERY_doall(alg->cache, &impl_cache_free);
  182. lh_QUERY_flush(alg->cache);
  183. }
  184. static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
  185. {
  186. OSSL_METHOD_STORE *store = arg;
  187. if (a != NULL) {
  188. sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
  189. lh_QUERY_doall(a->cache, &impl_cache_free);
  190. lh_QUERY_free(a->cache);
  191. OPENSSL_free(a);
  192. }
  193. if (store != NULL)
  194. ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
  195. }
  196. /*
  197. * The OSSL_LIB_CTX param here allows access to underlying property data needed
  198. * for computation
  199. */
  200. OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
  201. {
  202. OSSL_METHOD_STORE *res;
  203. res = OPENSSL_zalloc(sizeof(*res));
  204. if (res != NULL) {
  205. res->ctx = ctx;
  206. if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
  207. || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
  208. || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
  209. ossl_method_store_free(res);
  210. return NULL;
  211. }
  212. }
  213. return res;
  214. }
  215. void ossl_method_store_free(OSSL_METHOD_STORE *store)
  216. {
  217. if (store != NULL) {
  218. if (store->algs != NULL)
  219. ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
  220. ossl_sa_ALGORITHM_free(store->algs);
  221. CRYPTO_THREAD_lock_free(store->lock);
  222. CRYPTO_THREAD_lock_free(store->biglock);
  223. OPENSSL_free(store);
  224. }
  225. }
  226. int ossl_method_lock_store(OSSL_METHOD_STORE *store)
  227. {
  228. return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
  229. }
  230. int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
  231. {
  232. return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
  233. }
  234. static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
  235. {
  236. return ossl_sa_ALGORITHM_get(store->algs, nid);
  237. }
  238. static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
  239. {
  240. return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
  241. }
  242. int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
  243. int nid, const char *properties, void *method,
  244. int (*method_up_ref)(void *),
  245. void (*method_destruct)(void *))
  246. {
  247. ALGORITHM *alg = NULL;
  248. IMPLEMENTATION *impl;
  249. int ret = 0;
  250. int i;
  251. if (nid <= 0 || method == NULL || store == NULL)
  252. return 0;
  253. if (properties == NULL)
  254. properties = "";
  255. if (!ossl_assert(prov != NULL))
  256. return 0;
  257. /* Create new entry */
  258. impl = OPENSSL_malloc(sizeof(*impl));
  259. if (impl == NULL)
  260. return 0;
  261. impl->method.method = method;
  262. impl->method.up_ref = method_up_ref;
  263. impl->method.free = method_destruct;
  264. if (!ossl_method_up_ref(&impl->method)) {
  265. OPENSSL_free(impl);
  266. return 0;
  267. }
  268. impl->provider = prov;
  269. /* Insert into the hash table if required */
  270. if (!ossl_property_write_lock(store)) {
  271. OPENSSL_free(impl);
  272. return 0;
  273. }
  274. ossl_method_cache_flush(store, nid);
  275. if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
  276. impl->properties = ossl_parse_property(store->ctx, properties);
  277. if (impl->properties == NULL)
  278. goto err;
  279. if (!ossl_prop_defn_set(store->ctx, properties, impl->properties)) {
  280. ossl_property_free(impl->properties);
  281. impl->properties = NULL;
  282. goto err;
  283. }
  284. }
  285. alg = ossl_method_store_retrieve(store, nid);
  286. if (alg == NULL) {
  287. if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
  288. || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
  289. || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
  290. goto err;
  291. alg->nid = nid;
  292. if (!ossl_method_store_insert(store, alg))
  293. goto err;
  294. }
  295. /* Push onto stack if there isn't one there already */
  296. for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
  297. const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
  298. if (tmpimpl->provider == impl->provider
  299. && tmpimpl->properties == impl->properties)
  300. break;
  301. }
  302. if (i == sk_IMPLEMENTATION_num(alg->impls)
  303. && sk_IMPLEMENTATION_push(alg->impls, impl))
  304. ret = 1;
  305. ossl_property_unlock(store);
  306. if (ret == 0)
  307. impl_free(impl);
  308. return ret;
  309. err:
  310. ossl_property_unlock(store);
  311. alg_cleanup(0, alg, NULL);
  312. impl_free(impl);
  313. return 0;
  314. }
  315. int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
  316. const void *method)
  317. {
  318. ALGORITHM *alg = NULL;
  319. int i;
  320. if (nid <= 0 || method == NULL || store == NULL)
  321. return 0;
  322. if (!ossl_property_write_lock(store))
  323. return 0;
  324. ossl_method_cache_flush(store, nid);
  325. alg = ossl_method_store_retrieve(store, nid);
  326. if (alg == NULL) {
  327. ossl_property_unlock(store);
  328. return 0;
  329. }
  330. /*
  331. * A sorting find then a delete could be faster but these stacks should be
  332. * relatively small, so we avoid the overhead. Sorting could also surprise
  333. * users when result orderings change (even though they are not guaranteed).
  334. */
  335. for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
  336. IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
  337. if (impl->method.method == method) {
  338. impl_free(impl);
  339. (void)sk_IMPLEMENTATION_delete(alg->impls, i);
  340. ossl_property_unlock(store);
  341. return 1;
  342. }
  343. }
  344. ossl_property_unlock(store);
  345. return 0;
  346. }
  347. struct alg_cleanup_by_provider_data_st {
  348. OSSL_METHOD_STORE *store;
  349. const OSSL_PROVIDER *prov;
  350. };
  351. static void
  352. alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
  353. {
  354. struct alg_cleanup_by_provider_data_st *data = arg;
  355. int i, count;
  356. /*
  357. * We walk the stack backwards, to avoid having to deal with stack shifts
  358. * caused by deletion
  359. */
  360. for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
  361. IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
  362. if (impl->provider == data->prov) {
  363. impl_free(impl);
  364. (void)sk_IMPLEMENTATION_delete(alg->impls, i);
  365. count++;
  366. }
  367. }
  368. /*
  369. * If we removed any implementation, we also clear the whole associated
  370. * cache, 'cause that's the sensible thing to do.
  371. * There's no point flushing the cache entries where we didn't remove
  372. * any implementation, though.
  373. */
  374. if (count > 0)
  375. ossl_method_cache_flush_alg(data->store, alg);
  376. }
  377. int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,
  378. const OSSL_PROVIDER *prov)
  379. {
  380. struct alg_cleanup_by_provider_data_st data;
  381. if (!ossl_property_write_lock(store))
  382. return 0;
  383. data.prov = prov;
  384. data.store = store;
  385. ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);
  386. ossl_property_unlock(store);
  387. return 1;
  388. }
  389. static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
  390. void (*fn)(int id, void *method, void *fnarg),
  391. void *fnarg)
  392. {
  393. fn(alg->nid, impl->method.method, fnarg);
  394. }
  395. struct alg_do_each_data_st {
  396. void (*fn)(int id, void *method, void *fnarg);
  397. void *fnarg;
  398. };
  399. static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
  400. {
  401. struct alg_do_each_data_st *data = arg;
  402. int i, end = sk_IMPLEMENTATION_num(alg->impls);
  403. for (i = 0; i < end; i++) {
  404. IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
  405. alg_do_one(alg, impl, data->fn, data->fnarg);
  406. }
  407. }
  408. void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
  409. void (*fn)(int id, void *method, void *fnarg),
  410. void *fnarg)
  411. {
  412. struct alg_do_each_data_st data;
  413. data.fn = fn;
  414. data.fnarg = fnarg;
  415. if (store != NULL)
  416. ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);
  417. }
  418. int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
  419. int nid, const char *prop_query,
  420. const OSSL_PROVIDER **prov_rw, void **method)
  421. {
  422. OSSL_PROPERTY_LIST **plp;
  423. ALGORITHM *alg;
  424. IMPLEMENTATION *impl, *best_impl = NULL;
  425. OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
  426. const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
  427. int ret = 0;
  428. int j, best = -1, score, optional;
  429. #ifndef FIPS_MODULE
  430. if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
  431. return 0;
  432. #endif
  433. if (nid <= 0 || method == NULL || store == NULL)
  434. return 0;
  435. /* This only needs to be a read lock, because the query won't create anything */
  436. if (!ossl_property_read_lock(store))
  437. return 0;
  438. alg = ossl_method_store_retrieve(store, nid);
  439. if (alg == NULL) {
  440. ossl_property_unlock(store);
  441. return 0;
  442. }
  443. if (prop_query != NULL)
  444. p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
  445. plp = ossl_ctx_global_properties(store->ctx, 0);
  446. if (plp != NULL && *plp != NULL) {
  447. if (pq == NULL) {
  448. pq = *plp;
  449. } else {
  450. p2 = ossl_property_merge(pq, *plp);
  451. ossl_property_free(pq);
  452. if (p2 == NULL)
  453. goto fin;
  454. pq = p2;
  455. }
  456. }
  457. if (pq == NULL) {
  458. for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
  459. if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
  460. && (prov == NULL || impl->provider == prov)) {
  461. best_impl = impl;
  462. ret = 1;
  463. break;
  464. }
  465. }
  466. goto fin;
  467. }
  468. optional = ossl_property_has_optional(pq);
  469. for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
  470. if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
  471. && (prov == NULL || impl->provider == prov)) {
  472. score = ossl_property_match_count(pq, impl->properties);
  473. if (score > best) {
  474. best_impl = impl;
  475. best = score;
  476. ret = 1;
  477. if (!optional)
  478. goto fin;
  479. }
  480. }
  481. }
  482. fin:
  483. if (ret && ossl_method_up_ref(&best_impl->method)) {
  484. *method = best_impl->method.method;
  485. if (prov_rw != NULL)
  486. *prov_rw = best_impl->provider;
  487. } else {
  488. ret = 0;
  489. }
  490. ossl_property_unlock(store);
  491. ossl_property_free(p2);
  492. return ret;
  493. }
  494. static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
  495. ALGORITHM *alg)
  496. {
  497. store->cache_nelem -= lh_QUERY_num_items(alg->cache);
  498. impl_cache_flush_alg(0, alg);
  499. }
  500. static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
  501. {
  502. ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
  503. if (alg != NULL)
  504. ossl_method_cache_flush_alg(store, alg);
  505. }
  506. int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
  507. {
  508. if (!ossl_property_write_lock(store))
  509. return 0;
  510. ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
  511. store->cache_nelem = 0;
  512. ossl_property_unlock(store);
  513. return 1;
  514. }
  515. IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
  516. /*
  517. * Flush an element from the query cache (perhaps).
  518. *
  519. * In order to avoid taking a write lock or using atomic operations
  520. * to keep accurate least recently used (LRU) or least frequently used
  521. * (LFU) information, the procedure used here is to stochastically
  522. * flush approximately half the cache.
  523. *
  524. * This procedure isn't ideal, LRU or LFU would be better. However,
  525. * in normal operation, reaching a full cache would be unexpected.
  526. * It means that no steady state of algorithm queries has been reached.
  527. * That is, it is most likely an attack of some form. A suboptimal clearance
  528. * strategy that doesn't degrade performance of the normal case is
  529. * preferable to a more refined approach that imposes a performance
  530. * impact.
  531. */
  532. static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
  533. {
  534. uint32_t n;
  535. /*
  536. * Implement the 32 bit xorshift as suggested by George Marsaglia in:
  537. * https://doi.org/10.18637/jss.v008.i14
  538. *
  539. * This is a very fast PRNG so there is no need to extract bits one at a
  540. * time and use the entire value each time.
  541. */
  542. n = state->seed;
  543. n ^= n << 13;
  544. n ^= n >> 17;
  545. n ^= n << 5;
  546. state->seed = n;
  547. if ((n & 1) != 0)
  548. impl_cache_free(lh_QUERY_delete(state->cache, c));
  549. else
  550. state->nelem++;
  551. }
  552. static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
  553. void *v)
  554. {
  555. IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
  556. state->cache = alg->cache;
  557. lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
  558. state);
  559. }
  560. static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
  561. {
  562. IMPL_CACHE_FLUSH state;
  563. static TSAN_QUALIFIER uint32_t global_seed = 1;
  564. state.nelem = 0;
  565. state.using_global_seed = 0;
  566. if ((state.seed = OPENSSL_rdtsc()) == 0) {
  567. /* If there is no timer available, seed another way */
  568. state.using_global_seed = 1;
  569. state.seed = tsan_load(&global_seed);
  570. }
  571. store->cache_need_flush = 0;
  572. ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
  573. store->cache_nelem = state.nelem;
  574. /* Without a timer, update the global seed */
  575. if (state.using_global_seed)
  576. tsan_add(&global_seed, state.seed);
  577. }
  578. int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
  579. int nid, const char *prop_query, void **method)
  580. {
  581. ALGORITHM *alg;
  582. QUERY elem, *r;
  583. int res = 0;
  584. if (nid <= 0 || store == NULL || prop_query == NULL)
  585. return 0;
  586. if (!ossl_property_read_lock(store))
  587. return 0;
  588. alg = ossl_method_store_retrieve(store, nid);
  589. if (alg == NULL)
  590. goto err;
  591. elem.query = prop_query;
  592. elem.provider = prov;
  593. r = lh_QUERY_retrieve(alg->cache, &elem);
  594. if (r == NULL)
  595. goto err;
  596. if (ossl_method_up_ref(&r->method)) {
  597. *method = r->method.method;
  598. res = 1;
  599. }
  600. err:
  601. ossl_property_unlock(store);
  602. return res;
  603. }
  604. int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
  605. int nid, const char *prop_query, void *method,
  606. int (*method_up_ref)(void *),
  607. void (*method_destruct)(void *))
  608. {
  609. QUERY elem, *old, *p = NULL;
  610. ALGORITHM *alg;
  611. size_t len;
  612. int res = 1;
  613. if (nid <= 0 || store == NULL || prop_query == NULL)
  614. return 0;
  615. if (!ossl_assert(prov != NULL))
  616. return 0;
  617. if (!ossl_property_write_lock(store))
  618. return 0;
  619. if (store->cache_need_flush)
  620. ossl_method_cache_flush_some(store);
  621. alg = ossl_method_store_retrieve(store, nid);
  622. if (alg == NULL)
  623. goto err;
  624. if (method == NULL) {
  625. elem.query = prop_query;
  626. elem.provider = prov;
  627. if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
  628. impl_cache_free(old);
  629. store->cache_nelem--;
  630. }
  631. goto end;
  632. }
  633. p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
  634. if (p != NULL) {
  635. p->query = p->body;
  636. p->provider = prov;
  637. p->method.method = method;
  638. p->method.up_ref = method_up_ref;
  639. p->method.free = method_destruct;
  640. if (!ossl_method_up_ref(&p->method))
  641. goto err;
  642. memcpy((char *)p->query, prop_query, len + 1);
  643. if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
  644. impl_cache_free(old);
  645. goto end;
  646. }
  647. if (!lh_QUERY_error(alg->cache)) {
  648. if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
  649. store->cache_need_flush = 1;
  650. goto end;
  651. }
  652. ossl_method_free(&p->method);
  653. }
  654. err:
  655. res = 0;
  656. OPENSSL_free(p);
  657. end:
  658. ossl_property_unlock(store);
  659. return res;
  660. }