property.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Copyright 2019-2023 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. ossl_unused int loadconfig)
  108. {
  109. OSSL_GLOBAL_PROPERTIES *globp;
  110. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
  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. if (nid <= 0 || method == NULL || store == NULL)
  430. return 0;
  431. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
  432. if (ossl_lib_ctx_is_default(store->ctx)
  433. && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
  434. return 0;
  435. #endif
  436. /* This only needs to be a read lock, because the query won't create anything */
  437. if (!ossl_property_read_lock(store))
  438. return 0;
  439. alg = ossl_method_store_retrieve(store, nid);
  440. if (alg == NULL) {
  441. ossl_property_unlock(store);
  442. return 0;
  443. }
  444. if (prop_query != NULL)
  445. p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
  446. plp = ossl_ctx_global_properties(store->ctx, 0);
  447. if (plp != NULL && *plp != NULL) {
  448. if (pq == NULL) {
  449. pq = *plp;
  450. } else {
  451. p2 = ossl_property_merge(pq, *plp);
  452. ossl_property_free(pq);
  453. if (p2 == NULL)
  454. goto fin;
  455. pq = p2;
  456. }
  457. }
  458. if (pq == NULL) {
  459. for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
  460. if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
  461. && (prov == NULL || impl->provider == prov)) {
  462. best_impl = impl;
  463. ret = 1;
  464. break;
  465. }
  466. }
  467. goto fin;
  468. }
  469. optional = ossl_property_has_optional(pq);
  470. for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
  471. if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
  472. && (prov == NULL || impl->provider == prov)) {
  473. score = ossl_property_match_count(pq, impl->properties);
  474. if (score > best) {
  475. best_impl = impl;
  476. best = score;
  477. ret = 1;
  478. if (!optional)
  479. goto fin;
  480. }
  481. }
  482. }
  483. fin:
  484. if (ret && ossl_method_up_ref(&best_impl->method)) {
  485. *method = best_impl->method.method;
  486. if (prov_rw != NULL)
  487. *prov_rw = best_impl->provider;
  488. } else {
  489. ret = 0;
  490. }
  491. ossl_property_unlock(store);
  492. ossl_property_free(p2);
  493. return ret;
  494. }
  495. static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
  496. ALGORITHM *alg)
  497. {
  498. store->cache_nelem -= lh_QUERY_num_items(alg->cache);
  499. impl_cache_flush_alg(0, alg);
  500. }
  501. static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
  502. {
  503. ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
  504. if (alg != NULL)
  505. ossl_method_cache_flush_alg(store, alg);
  506. }
  507. int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
  508. {
  509. if (!ossl_property_write_lock(store))
  510. return 0;
  511. ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
  512. store->cache_nelem = 0;
  513. ossl_property_unlock(store);
  514. return 1;
  515. }
  516. IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
  517. /*
  518. * Flush an element from the query cache (perhaps).
  519. *
  520. * In order to avoid taking a write lock or using atomic operations
  521. * to keep accurate least recently used (LRU) or least frequently used
  522. * (LFU) information, the procedure used here is to stochastically
  523. * flush approximately half the cache.
  524. *
  525. * This procedure isn't ideal, LRU or LFU would be better. However,
  526. * in normal operation, reaching a full cache would be unexpected.
  527. * It means that no steady state of algorithm queries has been reached.
  528. * That is, it is most likely an attack of some form. A suboptimal clearance
  529. * strategy that doesn't degrade performance of the normal case is
  530. * preferable to a more refined approach that imposes a performance
  531. * impact.
  532. */
  533. static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
  534. {
  535. uint32_t n;
  536. /*
  537. * Implement the 32 bit xorshift as suggested by George Marsaglia in:
  538. * https://doi.org/10.18637/jss.v008.i14
  539. *
  540. * This is a very fast PRNG so there is no need to extract bits one at a
  541. * time and use the entire value each time.
  542. */
  543. n = state->seed;
  544. n ^= n << 13;
  545. n ^= n >> 17;
  546. n ^= n << 5;
  547. state->seed = n;
  548. if ((n & 1) != 0)
  549. impl_cache_free(lh_QUERY_delete(state->cache, c));
  550. else
  551. state->nelem++;
  552. }
  553. static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
  554. void *v)
  555. {
  556. IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
  557. state->cache = alg->cache;
  558. lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
  559. state);
  560. }
  561. static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
  562. {
  563. IMPL_CACHE_FLUSH state;
  564. static TSAN_QUALIFIER uint32_t global_seed = 1;
  565. state.nelem = 0;
  566. state.using_global_seed = 0;
  567. if ((state.seed = OPENSSL_rdtsc()) == 0) {
  568. /* If there is no timer available, seed another way */
  569. state.using_global_seed = 1;
  570. state.seed = tsan_load(&global_seed);
  571. }
  572. store->cache_need_flush = 0;
  573. ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
  574. store->cache_nelem = state.nelem;
  575. /* Without a timer, update the global seed */
  576. if (state.using_global_seed)
  577. tsan_add(&global_seed, state.seed);
  578. }
  579. int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
  580. int nid, const char *prop_query, void **method)
  581. {
  582. ALGORITHM *alg;
  583. QUERY elem, *r;
  584. int res = 0;
  585. if (nid <= 0 || store == NULL || prop_query == NULL)
  586. return 0;
  587. if (!ossl_property_read_lock(store))
  588. return 0;
  589. alg = ossl_method_store_retrieve(store, nid);
  590. if (alg == NULL)
  591. goto err;
  592. elem.query = prop_query;
  593. elem.provider = prov;
  594. r = lh_QUERY_retrieve(alg->cache, &elem);
  595. if (r == NULL)
  596. goto err;
  597. if (ossl_method_up_ref(&r->method)) {
  598. *method = r->method.method;
  599. res = 1;
  600. }
  601. err:
  602. ossl_property_unlock(store);
  603. return res;
  604. }
  605. int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
  606. int nid, const char *prop_query, void *method,
  607. int (*method_up_ref)(void *),
  608. void (*method_destruct)(void *))
  609. {
  610. QUERY elem, *old, *p = NULL;
  611. ALGORITHM *alg;
  612. size_t len;
  613. int res = 1;
  614. if (nid <= 0 || store == NULL || prop_query == NULL)
  615. return 0;
  616. if (!ossl_assert(prov != NULL))
  617. return 0;
  618. if (!ossl_property_write_lock(store))
  619. return 0;
  620. if (store->cache_need_flush)
  621. ossl_method_cache_flush_some(store);
  622. alg = ossl_method_store_retrieve(store, nid);
  623. if (alg == NULL)
  624. goto err;
  625. if (method == NULL) {
  626. elem.query = prop_query;
  627. elem.provider = prov;
  628. if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
  629. impl_cache_free(old);
  630. store->cache_nelem--;
  631. }
  632. goto end;
  633. }
  634. p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
  635. if (p != NULL) {
  636. p->query = p->body;
  637. p->provider = prov;
  638. p->method.method = method;
  639. p->method.up_ref = method_up_ref;
  640. p->method.free = method_destruct;
  641. if (!ossl_method_up_ref(&p->method))
  642. goto err;
  643. memcpy((char *)p->query, prop_query, len + 1);
  644. if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
  645. impl_cache_free(old);
  646. goto end;
  647. }
  648. if (!lh_QUERY_error(alg->cache)) {
  649. if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
  650. store->cache_need_flush = 1;
  651. goto end;
  652. }
  653. ossl_method_free(&p->method);
  654. }
  655. err:
  656. res = 0;
  657. OPENSSL_free(p);
  658. end:
  659. ossl_property_unlock(store);
  660. return res;
  661. }