property.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Copyright 2019-2021 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 "crypto/ctype.h"
  18. #include <openssl/lhash.h>
  19. #include <openssl/rand.h>
  20. #include "internal/thread_once.h"
  21. #include "crypto/lhash.h"
  22. #include "crypto/sparse_array.h"
  23. #include "property_local.h"
  24. /*
  25. * The number of elements in the query cache before we initiate a flush.
  26. * If reducing this, also ensure the stochastic test in test/property_test.c
  27. * isn't likely to fail.
  28. */
  29. #define IMPL_CACHE_FLUSH_THRESHOLD 500
  30. typedef struct {
  31. void *method;
  32. int (*up_ref)(void *);
  33. void (*free)(void *);
  34. } METHOD;
  35. typedef struct {
  36. const OSSL_PROVIDER *provider;
  37. OSSL_PROPERTY_LIST *properties;
  38. METHOD method;
  39. } IMPLEMENTATION;
  40. DEFINE_STACK_OF(IMPLEMENTATION)
  41. typedef struct {
  42. const char *query;
  43. METHOD method;
  44. char body[1];
  45. } QUERY;
  46. DEFINE_LHASH_OF(QUERY);
  47. typedef struct {
  48. int nid;
  49. STACK_OF(IMPLEMENTATION) *impls;
  50. LHASH_OF(QUERY) *cache;
  51. } ALGORITHM;
  52. struct ossl_method_store_st {
  53. OSSL_LIB_CTX *ctx;
  54. size_t nelem;
  55. SPARSE_ARRAY_OF(ALGORITHM) *algs;
  56. int need_flush;
  57. CRYPTO_RWLOCK *lock;
  58. };
  59. typedef struct {
  60. LHASH_OF(QUERY) *cache;
  61. size_t nelem;
  62. uint32_t seed;
  63. } IMPL_CACHE_FLUSH;
  64. DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
  65. typedef struct ossl_global_properties_st {
  66. OSSL_PROPERTY_LIST *list;
  67. #ifndef FIPS_MODULE
  68. unsigned int no_mirrored : 1;
  69. #endif
  70. } OSSL_GLOBAL_PROPERTIES;
  71. static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
  72. /* Global properties are stored per library context */
  73. static void ossl_ctx_global_properties_free(void *vglobp)
  74. {
  75. OSSL_GLOBAL_PROPERTIES *globp = vglobp;
  76. if (globp != NULL) {
  77. ossl_property_free(globp->list);
  78. OPENSSL_free(globp);
  79. }
  80. }
  81. static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
  82. {
  83. return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
  84. }
  85. static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
  86. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  87. ossl_ctx_global_properties_new,
  88. ossl_ctx_global_properties_free,
  89. };
  90. OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
  91. int loadconfig)
  92. {
  93. OSSL_GLOBAL_PROPERTIES *globp;
  94. #ifndef FIPS_MODULE
  95. if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
  96. return NULL;
  97. #endif
  98. globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
  99. &ossl_ctx_global_properties_method);
  100. return &globp->list;
  101. }
  102. #ifndef FIPS_MODULE
  103. int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
  104. {
  105. OSSL_GLOBAL_PROPERTIES *globp
  106. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
  107. &ossl_ctx_global_properties_method);
  108. return globp->no_mirrored ? 1 : 0;
  109. }
  110. void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
  111. {
  112. OSSL_GLOBAL_PROPERTIES *globp
  113. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
  114. &ossl_ctx_global_properties_method);
  115. globp->no_mirrored = 1;
  116. }
  117. #endif
  118. static int ossl_method_up_ref(METHOD *method)
  119. {
  120. return (*method->up_ref)(method->method);
  121. }
  122. static void ossl_method_free(METHOD *method)
  123. {
  124. (*method->free)(method->method);
  125. }
  126. static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
  127. {
  128. return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
  129. }
  130. static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
  131. {
  132. return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
  133. }
  134. static int ossl_property_unlock(OSSL_METHOD_STORE *p)
  135. {
  136. return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
  137. }
  138. static unsigned long query_hash(const QUERY *a)
  139. {
  140. return OPENSSL_LH_strhash(a->query);
  141. }
  142. static int query_cmp(const QUERY *a, const QUERY *b)
  143. {
  144. return strcmp(a->query, b->query);
  145. }
  146. static void impl_free(IMPLEMENTATION *impl)
  147. {
  148. if (impl != NULL) {
  149. ossl_method_free(&impl->method);
  150. OPENSSL_free(impl);
  151. }
  152. }
  153. static void impl_cache_free(QUERY *elem)
  154. {
  155. if (elem != NULL) {
  156. ossl_method_free(&elem->method);
  157. OPENSSL_free(elem);
  158. }
  159. }
  160. static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
  161. {
  162. if (a != NULL) {
  163. sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
  164. lh_QUERY_doall(a->cache, &impl_cache_free);
  165. lh_QUERY_free(a->cache);
  166. OPENSSL_free(a);
  167. }
  168. }
  169. /*
  170. * The OSSL_LIB_CTX param here allows access to underlying property data needed
  171. * for computation
  172. */
  173. OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
  174. {
  175. OSSL_METHOD_STORE *res;
  176. res = OPENSSL_zalloc(sizeof(*res));
  177. if (res != NULL) {
  178. res->ctx = ctx;
  179. if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
  180. OPENSSL_free(res);
  181. return NULL;
  182. }
  183. if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  184. ossl_sa_ALGORITHM_free(res->algs);
  185. OPENSSL_free(res);
  186. return NULL;
  187. }
  188. }
  189. return res;
  190. }
  191. void ossl_method_store_free(OSSL_METHOD_STORE *store)
  192. {
  193. if (store != NULL) {
  194. ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup);
  195. ossl_sa_ALGORITHM_free(store->algs);
  196. CRYPTO_THREAD_lock_free(store->lock);
  197. OPENSSL_free(store);
  198. }
  199. }
  200. static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
  201. {
  202. return ossl_sa_ALGORITHM_get(store->algs, nid);
  203. }
  204. static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
  205. {
  206. return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
  207. }
  208. int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
  209. int nid, const char *properties, void *method,
  210. int (*method_up_ref)(void *),
  211. void (*method_destruct)(void *))
  212. {
  213. ALGORITHM *alg = NULL;
  214. IMPLEMENTATION *impl;
  215. int ret = 0;
  216. int i;
  217. if (nid <= 0 || method == NULL || store == NULL)
  218. return 0;
  219. if (properties == NULL)
  220. properties = "";
  221. /* Create new entry */
  222. impl = OPENSSL_malloc(sizeof(*impl));
  223. if (impl == NULL)
  224. return 0;
  225. impl->method.method = method;
  226. impl->method.up_ref = method_up_ref;
  227. impl->method.free = method_destruct;
  228. if (!ossl_method_up_ref(&impl->method)) {
  229. OPENSSL_free(impl);
  230. return 0;
  231. }
  232. impl->provider = prov;
  233. /*
  234. * Insert into the hash table if required.
  235. *
  236. * A write lock is used unconditionally because we wend our way down to the
  237. * property string code which isn't locking friendly.
  238. */
  239. if (!ossl_property_write_lock(store)) {
  240. OPENSSL_free(impl);
  241. return 0;
  242. }
  243. ossl_method_cache_flush(store, nid);
  244. if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
  245. impl->properties = ossl_parse_property(store->ctx, properties);
  246. if (impl->properties == NULL)
  247. goto err;
  248. ossl_prop_defn_set(store->ctx, properties, impl->properties);
  249. }
  250. alg = ossl_method_store_retrieve(store, nid);
  251. if (alg == NULL) {
  252. if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
  253. || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
  254. || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
  255. goto err;
  256. alg->nid = nid;
  257. if (!ossl_method_store_insert(store, alg))
  258. goto err;
  259. }
  260. /* Push onto stack if there isn't one there already */
  261. for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
  262. const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
  263. if (tmpimpl->provider == impl->provider
  264. && tmpimpl->properties == impl->properties)
  265. break;
  266. }
  267. if (i == sk_IMPLEMENTATION_num(alg->impls)
  268. && sk_IMPLEMENTATION_push(alg->impls, impl))
  269. ret = 1;
  270. ossl_property_unlock(store);
  271. if (ret == 0)
  272. impl_free(impl);
  273. return ret;
  274. err:
  275. ossl_property_unlock(store);
  276. alg_cleanup(0, alg);
  277. impl_free(impl);
  278. return 0;
  279. }
  280. int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
  281. const void *method)
  282. {
  283. ALGORITHM *alg = NULL;
  284. int i;
  285. if (nid <= 0 || method == NULL || store == NULL)
  286. return 0;
  287. if (!ossl_property_write_lock(store))
  288. return 0;
  289. ossl_method_cache_flush(store, nid);
  290. alg = ossl_method_store_retrieve(store, nid);
  291. if (alg == NULL) {
  292. ossl_property_unlock(store);
  293. return 0;
  294. }
  295. /*
  296. * A sorting find then a delete could be faster but these stacks should be
  297. * relatively small, so we avoid the overhead. Sorting could also surprise
  298. * users when result orderings change (even though they are not guaranteed).
  299. */
  300. for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
  301. IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
  302. if (impl->method.method == method) {
  303. impl_free(impl);
  304. (void)sk_IMPLEMENTATION_delete(alg->impls, i);
  305. ossl_property_unlock(store);
  306. return 1;
  307. }
  308. }
  309. ossl_property_unlock(store);
  310. return 0;
  311. }
  312. int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
  313. const char *prop_query,
  314. void **method)
  315. {
  316. OSSL_PROPERTY_LIST **plp;
  317. ALGORITHM *alg;
  318. IMPLEMENTATION *impl;
  319. OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
  320. METHOD *best_method = NULL;
  321. int ret = 0;
  322. int j, best = -1, score, optional;
  323. #ifndef FIPS_MODULE
  324. if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
  325. return 0;
  326. #endif
  327. if (nid <= 0 || method == NULL || store == NULL)
  328. return 0;
  329. /*
  330. * This only needs to be a read lock, because queries never create property
  331. * names or value and thus don't modify any of the property string layer.
  332. */
  333. if (!ossl_property_read_lock(store))
  334. return 0;
  335. alg = ossl_method_store_retrieve(store, nid);
  336. if (alg == NULL) {
  337. ossl_property_unlock(store);
  338. return 0;
  339. }
  340. if (prop_query != NULL)
  341. p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
  342. plp = ossl_ctx_global_properties(store->ctx, 0);
  343. if (plp != NULL && *plp != NULL) {
  344. if (pq == NULL) {
  345. pq = *plp;
  346. } else {
  347. p2 = ossl_property_merge(pq, *plp);
  348. ossl_property_free(pq);
  349. if (p2 == NULL)
  350. goto fin;
  351. pq = p2;
  352. }
  353. }
  354. if (pq == NULL) {
  355. if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
  356. best_method = &impl->method;
  357. ret = 1;
  358. }
  359. goto fin;
  360. }
  361. optional = ossl_property_has_optional(pq);
  362. for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
  363. impl = sk_IMPLEMENTATION_value(alg->impls, j);
  364. score = ossl_property_match_count(pq, impl->properties);
  365. if (score > best) {
  366. best_method = &impl->method;
  367. best = score;
  368. ret = 1;
  369. if (!optional)
  370. goto fin;
  371. }
  372. }
  373. fin:
  374. if (ret && ossl_method_up_ref(best_method))
  375. *method = best_method->method;
  376. else
  377. ret = 0;
  378. ossl_property_unlock(store);
  379. ossl_property_free(p2);
  380. return ret;
  381. }
  382. static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
  383. {
  384. SPARSE_ARRAY_OF(ALGORITHM) *algs = arg;
  385. lh_QUERY_doall(alg->cache, &impl_cache_free);
  386. if (algs != NULL) {
  387. sk_IMPLEMENTATION_pop_free(alg->impls, &impl_free);
  388. lh_QUERY_free(alg->cache);
  389. OPENSSL_free(alg);
  390. ossl_sa_ALGORITHM_set(algs, idx, NULL);
  391. } else {
  392. lh_QUERY_flush(alg->cache);
  393. }
  394. }
  395. static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
  396. {
  397. ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
  398. if (alg != NULL) {
  399. ossl_provider_clear_all_operation_bits(store->ctx);
  400. store->nelem -= lh_QUERY_num_items(alg->cache);
  401. impl_cache_flush_alg(0, alg, NULL);
  402. }
  403. }
  404. int ossl_method_store_flush_cache(OSSL_METHOD_STORE *store, int all)
  405. {
  406. void *arg = (all != 0 ? store->algs : NULL);
  407. if (!ossl_property_write_lock(store))
  408. return 0;
  409. ossl_provider_clear_all_operation_bits(store->ctx);
  410. ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_alg, arg);
  411. store->nelem = 0;
  412. ossl_property_unlock(store);
  413. return 1;
  414. }
  415. IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
  416. /*
  417. * Flush an element from the query cache (perhaps).
  418. *
  419. * In order to avoid taking a write lock or using atomic operations
  420. * to keep accurate least recently used (LRU) or least frequently used
  421. * (LFU) information, the procedure used here is to stochastically
  422. * flush approximately half the cache.
  423. *
  424. * This procedure isn't ideal, LRU or LFU would be better. However,
  425. * in normal operation, reaching a full cache would be unexpected.
  426. * It means that no steady state of algorithm queries has been reached.
  427. * That is, it is most likely an attack of some form. A suboptimal clearance
  428. * strategy that doesn't degrade performance of the normal case is
  429. * preferable to a more refined approach that imposes a performance
  430. * impact.
  431. */
  432. static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
  433. {
  434. uint32_t n;
  435. /*
  436. * Implement the 32 bit xorshift as suggested by George Marsaglia in:
  437. * https://doi.org/10.18637/jss.v008.i14
  438. *
  439. * This is a very fast PRNG so there is no need to extract bits one at a
  440. * time and use the entire value each time.
  441. */
  442. n = state->seed;
  443. n ^= n << 13;
  444. n ^= n >> 17;
  445. n ^= n << 5;
  446. state->seed = n;
  447. if ((n & 1) != 0)
  448. impl_cache_free(lh_QUERY_delete(state->cache, c));
  449. else
  450. state->nelem++;
  451. }
  452. static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
  453. void *v)
  454. {
  455. IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
  456. state->cache = alg->cache;
  457. lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
  458. state);
  459. }
  460. static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
  461. {
  462. IMPL_CACHE_FLUSH state;
  463. state.nelem = 0;
  464. if ((state.seed = OPENSSL_rdtsc()) == 0)
  465. state.seed = 1;
  466. ossl_provider_clear_all_operation_bits(store->ctx);
  467. store->need_flush = 0;
  468. ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
  469. store->nelem = state.nelem;
  470. }
  471. int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
  472. const char *prop_query, void **method)
  473. {
  474. ALGORITHM *alg;
  475. QUERY elem, *r;
  476. int res = 0;
  477. if (nid <= 0 || store == NULL)
  478. return 0;
  479. if (!ossl_property_read_lock(store))
  480. return 0;
  481. alg = ossl_method_store_retrieve(store, nid);
  482. if (alg == NULL)
  483. goto err;
  484. elem.query = prop_query != NULL ? prop_query : "";
  485. r = lh_QUERY_retrieve(alg->cache, &elem);
  486. if (r == NULL)
  487. goto err;
  488. if (ossl_method_up_ref(&r->method)) {
  489. *method = r->method.method;
  490. res = 1;
  491. }
  492. err:
  493. ossl_property_unlock(store);
  494. return res;
  495. }
  496. int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
  497. const char *prop_query, void *method,
  498. int (*method_up_ref)(void *),
  499. void (*method_destruct)(void *))
  500. {
  501. QUERY elem, *old, *p = NULL;
  502. ALGORITHM *alg;
  503. size_t len;
  504. int res = 1;
  505. if (nid <= 0 || store == NULL)
  506. return 0;
  507. if (prop_query == NULL)
  508. return 1;
  509. if (!ossl_property_write_lock(store))
  510. return 0;
  511. if (store->need_flush)
  512. ossl_method_cache_flush_some(store);
  513. alg = ossl_method_store_retrieve(store, nid);
  514. if (alg == NULL)
  515. goto err;
  516. if (method == NULL) {
  517. elem.query = prop_query;
  518. if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
  519. impl_cache_free(old);
  520. store->nelem--;
  521. }
  522. goto end;
  523. }
  524. p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
  525. if (p != NULL) {
  526. p->query = p->body;
  527. p->method.method = method;
  528. p->method.up_ref = method_up_ref;
  529. p->method.free = method_destruct;
  530. if (!ossl_method_up_ref(&p->method))
  531. goto err;
  532. memcpy((char *)p->query, prop_query, len + 1);
  533. if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
  534. impl_cache_free(old);
  535. goto end;
  536. }
  537. if (!lh_QUERY_error(alg->cache)) {
  538. if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
  539. store->need_flush = 1;
  540. goto end;
  541. }
  542. ossl_method_free(&p->method);
  543. }
  544. err:
  545. res = 0;
  546. OPENSSL_free(p);
  547. end:
  548. ossl_property_unlock(store);
  549. return res;
  550. }