provider_core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Copyright 2019 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.h>
  10. #include <openssl/core_numbers.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/params.h>
  13. #include <openssl/opensslv.h>
  14. #include "crypto/cryptlib.h"
  15. #include "internal/nelem.h"
  16. #include "internal/thread_once.h"
  17. #include "internal/provider.h"
  18. #include "internal/refcount.h"
  19. #include "provider_local.h"
  20. static OSSL_PROVIDER *provider_new(const char *name,
  21. OSSL_provider_init_fn *init_function);
  22. /*-
  23. * Provider Object structure
  24. * =========================
  25. */
  26. typedef struct {
  27. char *name;
  28. char *value;
  29. } INFOPAIR;
  30. DEFINE_STACK_OF(INFOPAIR)
  31. struct provider_store_st; /* Forward declaration */
  32. struct ossl_provider_st {
  33. /* Flag bits */
  34. unsigned int flag_initialized:1;
  35. unsigned int flag_fallback:1;
  36. /* OpenSSL library side data */
  37. CRYPTO_REF_COUNT refcnt;
  38. CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
  39. char *name;
  40. char *path;
  41. DSO *module;
  42. OSSL_provider_init_fn *init_function;
  43. STACK_OF(INFOPAIR) *parameters;
  44. OPENSSL_CTX *libctx; /* The library context this instance is in */
  45. struct provider_store_st *store; /* The store this instance belongs to */
  46. #ifndef FIPS_MODE
  47. /*
  48. * In the FIPS module inner provider, this isn't needed, since the
  49. * error upcalls are always direct calls to the outer provider.
  50. */
  51. int error_lib; /* ERR library number, one for each provider */
  52. # ifndef OPENSSL_NO_ERR
  53. ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
  54. # endif
  55. #endif
  56. /* Provider side functions */
  57. OSSL_provider_teardown_fn *teardown;
  58. OSSL_provider_gettable_params_fn *gettable_params;
  59. OSSL_provider_get_params_fn *get_params;
  60. OSSL_provider_query_operation_fn *query_operation;
  61. /* Provider side data */
  62. void *provctx;
  63. };
  64. DEFINE_STACK_OF(OSSL_PROVIDER)
  65. static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
  66. const OSSL_PROVIDER * const *b)
  67. {
  68. return strcmp((*a)->name, (*b)->name);
  69. }
  70. /*-
  71. * Provider Object store
  72. * =====================
  73. *
  74. * The Provider Object store is a library context object, and therefore needs
  75. * an index.
  76. */
  77. struct provider_store_st {
  78. STACK_OF(OSSL_PROVIDER) *providers;
  79. CRYPTO_RWLOCK *lock;
  80. unsigned int use_fallbacks:1;
  81. };
  82. static void provider_store_free(void *vstore)
  83. {
  84. struct provider_store_st *store = vstore;
  85. if (store == NULL)
  86. return;
  87. sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
  88. CRYPTO_THREAD_lock_free(store->lock);
  89. OPENSSL_free(store);
  90. }
  91. static void *provider_store_new(OPENSSL_CTX *ctx)
  92. {
  93. struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
  94. const struct predefined_providers_st *p = NULL;
  95. if (store == NULL
  96. || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
  97. || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  98. provider_store_free(store);
  99. return NULL;
  100. }
  101. store->use_fallbacks = 1;
  102. for (p = predefined_providers; p->name != NULL; p++) {
  103. OSSL_PROVIDER *prov = NULL;
  104. /*
  105. * We use the internal constructor directly here,
  106. * otherwise we get a call loop
  107. */
  108. prov = provider_new(p->name, p->init);
  109. if (prov == NULL
  110. || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  111. ossl_provider_free(prov);
  112. provider_store_free(store);
  113. CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
  114. return NULL;
  115. }
  116. prov->libctx = ctx;
  117. prov->store = store;
  118. #ifndef FIPS_MODE
  119. prov->error_lib = ERR_get_next_error_library();
  120. #endif
  121. if(p->is_fallback)
  122. ossl_provider_set_fallback(prov);
  123. }
  124. return store;
  125. }
  126. static const OPENSSL_CTX_METHOD provider_store_method = {
  127. provider_store_new,
  128. provider_store_free,
  129. };
  130. static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
  131. {
  132. struct provider_store_st *store = NULL;
  133. store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
  134. &provider_store_method);
  135. if (store == NULL)
  136. CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
  137. return store;
  138. }
  139. OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name,
  140. int noconfig)
  141. {
  142. struct provider_store_st *store = NULL;
  143. OSSL_PROVIDER *prov = NULL;
  144. if ((store = get_provider_store(libctx)) != NULL) {
  145. OSSL_PROVIDER tmpl = { 0, };
  146. int i;
  147. #ifndef FIPS_MODE
  148. /*
  149. * Make sure any providers are loaded from config before we try to find
  150. * them.
  151. */
  152. if (!noconfig)
  153. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  154. #endif
  155. tmpl.name = (char *)name;
  156. CRYPTO_THREAD_write_lock(store->lock);
  157. if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
  158. || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
  159. || !ossl_provider_up_ref(prov))
  160. prov = NULL;
  161. CRYPTO_THREAD_unlock(store->lock);
  162. }
  163. return prov;
  164. }
  165. /*-
  166. * Provider Object methods
  167. * =======================
  168. */
  169. static OSSL_PROVIDER *provider_new(const char *name,
  170. OSSL_provider_init_fn *init_function)
  171. {
  172. OSSL_PROVIDER *prov = NULL;
  173. if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
  174. #ifndef HAVE_ATOMICS
  175. || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
  176. #endif
  177. || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
  178. || (prov->name = OPENSSL_strdup(name)) == NULL) {
  179. ossl_provider_free(prov);
  180. CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
  181. return NULL;
  182. }
  183. prov->init_function = init_function;
  184. return prov;
  185. }
  186. int ossl_provider_up_ref(OSSL_PROVIDER *prov)
  187. {
  188. int ref = 0;
  189. if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
  190. return 0;
  191. return ref;
  192. }
  193. OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
  194. OSSL_provider_init_fn *init_function,
  195. int noconfig)
  196. {
  197. struct provider_store_st *store = NULL;
  198. OSSL_PROVIDER *prov = NULL;
  199. if ((store = get_provider_store(libctx)) == NULL)
  200. return NULL;
  201. if ((prov = ossl_provider_find(libctx, name,
  202. noconfig)) != NULL) { /* refcount +1 */
  203. ossl_provider_free(prov); /* refcount -1 */
  204. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS, NULL,
  205. "name=%s", name);
  206. return NULL;
  207. }
  208. /* provider_new() generates an error, so no need here */
  209. if ((prov = provider_new(name, init_function)) == NULL)
  210. return NULL;
  211. CRYPTO_THREAD_write_lock(store->lock);
  212. if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
  213. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  214. prov = NULL;
  215. } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  216. ossl_provider_free(prov); /* -1 Store reference */
  217. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  218. prov = NULL;
  219. } else {
  220. prov->libctx = libctx;
  221. prov->store = store;
  222. #ifndef FIPS_MODE
  223. prov->error_lib = ERR_get_next_error_library();
  224. #endif
  225. }
  226. CRYPTO_THREAD_unlock(store->lock);
  227. if (prov == NULL)
  228. CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
  229. /*
  230. * At this point, the provider is only partially "loaded". To be
  231. * fully "loaded", ossl_provider_activate() must also be called.
  232. */
  233. return prov;
  234. }
  235. static void free_infopair(INFOPAIR *pair)
  236. {
  237. OPENSSL_free(pair->name);
  238. OPENSSL_free(pair->value);
  239. OPENSSL_free(pair);
  240. }
  241. void ossl_provider_free(OSSL_PROVIDER *prov)
  242. {
  243. if (prov != NULL) {
  244. int ref = 0;
  245. CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
  246. /*
  247. * When the refcount drops below two, the store is the only
  248. * possible reference, or it has already been taken away from
  249. * the store (this may happen if a provider was activated
  250. * because it's a fallback, but isn't currently used)
  251. * When that happens, the provider is inactivated.
  252. */
  253. if (ref < 2 && prov->flag_initialized) {
  254. #ifndef FIPS_MODE
  255. ossl_init_thread_deregister(prov);
  256. #endif
  257. if (prov->teardown != NULL)
  258. prov->teardown(prov->provctx);
  259. #ifndef OPENSSL_NO_ERR
  260. # ifndef FIPS_MODE
  261. if (prov->error_strings != NULL) {
  262. ERR_unload_strings(prov->error_lib, prov->error_strings);
  263. OPENSSL_free(prov->error_strings);
  264. prov->error_strings = NULL;
  265. }
  266. # endif
  267. #endif
  268. prov->flag_initialized = 0;
  269. }
  270. /*
  271. * When the refcount drops to zero, it has been taken out of
  272. * the store. All we have to do here is clean it out.
  273. */
  274. if (ref == 0) {
  275. #ifndef FIPS_MODE
  276. DSO_free(prov->module);
  277. #endif
  278. OPENSSL_free(prov->name);
  279. OPENSSL_free(prov->path);
  280. sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
  281. #ifndef HAVE_ATOMICS
  282. CRYPTO_THREAD_lock_free(prov->refcnt_lock);
  283. #endif
  284. OPENSSL_free(prov);
  285. }
  286. }
  287. }
  288. /* Setters */
  289. int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
  290. {
  291. OPENSSL_free(prov->path);
  292. if (module_path == NULL)
  293. return 1;
  294. if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
  295. return 1;
  296. CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
  297. return 0;
  298. }
  299. int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
  300. const char *name, const char *value)
  301. {
  302. INFOPAIR *pair = NULL;
  303. if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
  304. && (prov->parameters != NULL
  305. || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
  306. && (pair->name = OPENSSL_strdup(name)) != NULL
  307. && (pair->value = OPENSSL_strdup(value)) != NULL
  308. && sk_INFOPAIR_push(prov->parameters, pair) > 0)
  309. return 1;
  310. if (pair != NULL) {
  311. OPENSSL_free(pair->name);
  312. OPENSSL_free(pair->value);
  313. OPENSSL_free(pair);
  314. }
  315. CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
  316. return 0;
  317. }
  318. /*
  319. * Provider activation.
  320. *
  321. * What "activation" means depends on the provider form; for built in
  322. * providers (in the library or the application alike), the provider
  323. * can already be considered to be loaded, all that's needed is to
  324. * initialize it. However, for dynamically loadable provider modules,
  325. * we must first load that module.
  326. *
  327. * Built in modules are distinguished from dynamically loaded modules
  328. * with an already assigned init function.
  329. */
  330. static const OSSL_DISPATCH *core_dispatch; /* Define further down */
  331. /*
  332. * Internal version that doesn't affect the store flags, and thereby avoid
  333. * locking. Direct callers must remember to set the store flags when
  334. * appropriate.
  335. */
  336. static int provider_activate(OSSL_PROVIDER *prov)
  337. {
  338. const OSSL_DISPATCH *provider_dispatch = NULL;
  339. #ifndef OPENSSL_NO_ERR
  340. # ifndef FIPS_MODE
  341. OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
  342. # endif
  343. #endif
  344. if (prov->flag_initialized)
  345. return 1;
  346. /*
  347. * If the init function isn't set, it indicates that this provider is
  348. * a loadable module.
  349. */
  350. if (prov->init_function == NULL) {
  351. #ifdef FIPS_MODE
  352. return 0;
  353. #else
  354. if (prov->module == NULL) {
  355. char *allocated_path = NULL;
  356. const char *module_path = NULL;
  357. char *merged_path = NULL;
  358. const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
  359. if ((prov->module = DSO_new()) == NULL) {
  360. /* DSO_new() generates an error already */
  361. return 0;
  362. }
  363. if (load_dir == NULL)
  364. load_dir = MODULESDIR;
  365. DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
  366. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
  367. module_path = prov->path;
  368. if (module_path == NULL)
  369. module_path = allocated_path =
  370. DSO_convert_filename(prov->module, prov->name);
  371. if (module_path != NULL)
  372. merged_path = DSO_merge(prov->module, module_path, load_dir);
  373. if (merged_path == NULL
  374. || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
  375. DSO_free(prov->module);
  376. prov->module = NULL;
  377. }
  378. OPENSSL_free(merged_path);
  379. OPENSSL_free(allocated_path);
  380. }
  381. if (prov->module != NULL)
  382. prov->init_function = (OSSL_provider_init_fn *)
  383. DSO_bind_func(prov->module, "OSSL_provider_init");
  384. #endif
  385. }
  386. /* Call the initialise function for the provider. */
  387. if (prov->init_function == NULL
  388. || !prov->init_function(prov, core_dispatch, &provider_dispatch,
  389. &prov->provctx)) {
  390. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, NULL,
  391. "name=%s", prov->name);
  392. #ifndef FIPS_MODE
  393. DSO_free(prov->module);
  394. prov->module = NULL;
  395. #endif
  396. return 0;
  397. }
  398. for (; provider_dispatch->function_id != 0; provider_dispatch++) {
  399. switch (provider_dispatch->function_id) {
  400. case OSSL_FUNC_PROVIDER_TEARDOWN:
  401. prov->teardown =
  402. OSSL_get_provider_teardown(provider_dispatch);
  403. break;
  404. case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
  405. prov->gettable_params =
  406. OSSL_get_provider_gettable_params(provider_dispatch);
  407. break;
  408. case OSSL_FUNC_PROVIDER_GET_PARAMS:
  409. prov->get_params =
  410. OSSL_get_provider_get_params(provider_dispatch);
  411. break;
  412. case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
  413. prov->query_operation =
  414. OSSL_get_provider_query_operation(provider_dispatch);
  415. break;
  416. #ifndef OPENSSL_NO_ERR
  417. # ifndef FIPS_MODE
  418. case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
  419. p_get_reason_strings =
  420. OSSL_get_provider_get_reason_strings(provider_dispatch);
  421. break;
  422. # endif
  423. #endif
  424. }
  425. }
  426. #ifndef OPENSSL_NO_ERR
  427. # ifndef FIPS_MODE
  428. if (p_get_reason_strings != NULL) {
  429. const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
  430. size_t cnt, cnt2;
  431. /*
  432. * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
  433. * although they are essentially the same type.
  434. * Furthermore, ERR_load_strings() patches the array's error number
  435. * with the error library number, so we need to make a copy of that
  436. * array either way.
  437. */
  438. cnt = 1; /* One for the terminating item */
  439. while (reasonstrings[cnt].id != 0) {
  440. if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
  441. return 0;
  442. cnt++;
  443. }
  444. /* Allocate one extra item for the "library" name */
  445. prov->error_strings =
  446. OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
  447. if (prov->error_strings == NULL)
  448. return 0;
  449. /*
  450. * Set the "library" name.
  451. */
  452. prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
  453. prov->error_strings[0].string = prov->name;
  454. /*
  455. * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
  456. * 1..cnt.
  457. */
  458. for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
  459. prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
  460. prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
  461. }
  462. ERR_load_strings(prov->error_lib, prov->error_strings);
  463. }
  464. # endif
  465. #endif
  466. /* With this flag set, this provider has become fully "loaded". */
  467. prov->flag_initialized = 1;
  468. return 1;
  469. }
  470. int ossl_provider_activate(OSSL_PROVIDER *prov)
  471. {
  472. if (provider_activate(prov)) {
  473. CRYPTO_THREAD_write_lock(prov->store->lock);
  474. prov->store->use_fallbacks = 0;
  475. CRYPTO_THREAD_unlock(prov->store->lock);
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
  481. {
  482. return prov->provctx;
  483. }
  484. static int provider_forall_loaded(struct provider_store_st *store,
  485. int *found_activated,
  486. int (*cb)(OSSL_PROVIDER *provider,
  487. void *cbdata),
  488. void *cbdata)
  489. {
  490. int i;
  491. int ret = 1;
  492. int num_provs;
  493. num_provs = sk_OSSL_PROVIDER_num(store->providers);
  494. if (found_activated != NULL)
  495. *found_activated = 0;
  496. for (i = 0; i < num_provs; i++) {
  497. OSSL_PROVIDER *prov =
  498. sk_OSSL_PROVIDER_value(store->providers, i);
  499. if (prov->flag_initialized) {
  500. if (found_activated != NULL)
  501. *found_activated = 1;
  502. if (!(ret = cb(prov, cbdata)))
  503. break;
  504. }
  505. }
  506. return ret;
  507. }
  508. /*
  509. * This function only does something once when store->use_fallbacks == 1,
  510. * and then sets store->use_fallbacks = 0, so the second call and so on is
  511. * effectively a no-op.
  512. */
  513. static void provider_activate_fallbacks(struct provider_store_st *store)
  514. {
  515. if (store->use_fallbacks) {
  516. int num_provs = sk_OSSL_PROVIDER_num(store->providers);
  517. int activated_fallback_count = 0;
  518. int i;
  519. for (i = 0; i < num_provs; i++) {
  520. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
  521. /*
  522. * Note that we don't care if the activation succeeds or not.
  523. * If it doesn't succeed, then any attempt to use any of the
  524. * fallback providers will fail anyway.
  525. */
  526. if (prov->flag_fallback) {
  527. activated_fallback_count++;
  528. provider_activate(prov);
  529. }
  530. }
  531. /*
  532. * We assume that all fallbacks have been added to the store before
  533. * any fallback is activated.
  534. * TODO: We may have to reconsider this, IF we find ourselves adding
  535. * fallbacks after any previous fallback has been activated.
  536. */
  537. if (activated_fallback_count > 0)
  538. store->use_fallbacks = 0;
  539. }
  540. }
  541. int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
  542. int (*cb)(OSSL_PROVIDER *provider,
  543. void *cbdata),
  544. void *cbdata)
  545. {
  546. int ret = 1;
  547. struct provider_store_st *store = get_provider_store(ctx);
  548. #ifndef FIPS_MODE
  549. /*
  550. * Make sure any providers are loaded from config before we try to use
  551. * them.
  552. */
  553. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  554. #endif
  555. if (store != NULL) {
  556. CRYPTO_THREAD_read_lock(store->lock);
  557. provider_activate_fallbacks(store);
  558. /*
  559. * Now, we sweep through all providers
  560. */
  561. ret = provider_forall_loaded(store, NULL, cb, cbdata);
  562. CRYPTO_THREAD_unlock(store->lock);
  563. }
  564. return ret;
  565. }
  566. int ossl_provider_available(OSSL_PROVIDER *prov)
  567. {
  568. if (prov != NULL) {
  569. CRYPTO_THREAD_read_lock(prov->store->lock);
  570. provider_activate_fallbacks(prov->store);
  571. CRYPTO_THREAD_unlock(prov->store->lock);
  572. return prov->flag_initialized;
  573. }
  574. return 0;
  575. }
  576. /* Setters of Provider Object data */
  577. int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
  578. {
  579. if (prov == NULL)
  580. return 0;
  581. prov->flag_fallback = 1;
  582. return 1;
  583. }
  584. /* Getters of Provider Object data */
  585. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  586. {
  587. return prov->name;
  588. }
  589. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  590. {
  591. return prov->module;
  592. }
  593. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  594. {
  595. #ifdef FIPS_MODE
  596. return NULL;
  597. #else
  598. return DSO_get_filename(prov->module);
  599. #endif
  600. }
  601. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  602. {
  603. #ifdef FIPS_MODE
  604. return NULL;
  605. #else
  606. /* FIXME: Ensure it's a full path */
  607. return DSO_get_filename(prov->module);
  608. #endif
  609. }
  610. OPENSSL_CTX *ossl_provider_library_context(const OSSL_PROVIDER *prov)
  611. {
  612. /* TODO(3.0) just: return prov->libctx; */
  613. return prov != NULL ? prov->libctx : NULL;
  614. }
  615. /* Wrappers around calls to the provider */
  616. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  617. {
  618. if (prov->teardown != NULL)
  619. prov->teardown(prov->provctx);
  620. }
  621. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  622. {
  623. return prov->gettable_params == NULL
  624. ? NULL : prov->gettable_params(prov->provctx);
  625. }
  626. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  627. {
  628. return prov->get_params == NULL
  629. ? 0 : prov->get_params(prov->provctx, params);
  630. }
  631. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  632. int operation_id,
  633. int *no_cache)
  634. {
  635. return prov->query_operation(prov->provctx, operation_id, no_cache);
  636. }
  637. /*-
  638. * Core functions for the provider
  639. * ===============================
  640. *
  641. * This is the set of functions that the core makes available to the provider
  642. */
  643. /*
  644. * This returns a list of Provider Object parameters with their types, for
  645. * discovery. We do not expect that many providers will use this, but one
  646. * never knows.
  647. */
  648. static const OSSL_PARAM param_types[] = {
  649. OSSL_PARAM_DEFN("openssl-version", OSSL_PARAM_UTF8_PTR, NULL, 0),
  650. OSSL_PARAM_DEFN("provider-name", OSSL_PARAM_UTF8_PTR, NULL, 0),
  651. OSSL_PARAM_END
  652. };
  653. /*
  654. * Forward declare all the functions that are provided aa dispatch.
  655. * This ensures that the compiler will complain if they aren't defined
  656. * with the correct signature.
  657. */
  658. static OSSL_core_gettable_params_fn core_gettable_params;
  659. static OSSL_core_get_params_fn core_get_params;
  660. static OSSL_core_thread_start_fn core_thread_start;
  661. static OSSL_core_get_library_context_fn core_get_libctx;
  662. #ifndef FIPS_MODE
  663. static OSSL_core_new_error_fn core_new_error;
  664. static OSSL_core_set_error_debug_fn core_set_error_debug;
  665. static OSSL_core_vset_error_fn core_vset_error;
  666. #endif
  667. static const OSSL_PARAM *core_gettable_params(const OSSL_PROVIDER *prov)
  668. {
  669. return param_types;
  670. }
  671. static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  672. {
  673. int i;
  674. OSSL_PARAM *p;
  675. if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
  676. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  677. if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
  678. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  679. #ifndef FIPS_MODE
  680. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_MODULE_FILENAME)) != NULL)
  681. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  682. #endif
  683. if (prov->parameters == NULL)
  684. return 1;
  685. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  686. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  687. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  688. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  689. }
  690. return 1;
  691. }
  692. static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
  693. {
  694. return ossl_provider_library_context(prov);
  695. }
  696. static int core_thread_start(const OSSL_PROVIDER *prov,
  697. OSSL_thread_stop_handler_fn handfn)
  698. {
  699. return ossl_init_thread_start(prov, prov->provctx, handfn);
  700. }
  701. /*
  702. * The FIPS module inner provider doesn't implement these. They aren't
  703. * needed there, since the FIPS module upcalls are always the outer provider
  704. * ones.
  705. */
  706. #ifndef FIPS_MODE
  707. /*
  708. * TODO(3.0) These error functions should use |prov| to select the proper
  709. * library context to report in the correct error stack, at least if error
  710. * stacks become tied to the library context.
  711. * We cannot currently do that since there's no support for it in the
  712. * ERR subsystem.
  713. */
  714. static void core_new_error(const OSSL_PROVIDER *prov)
  715. {
  716. ERR_new();
  717. }
  718. static void core_set_error_debug(const OSSL_PROVIDER *prov,
  719. const char *file, int line, const char *func)
  720. {
  721. ERR_set_debug(file, line, func);
  722. }
  723. static void core_vset_error(const OSSL_PROVIDER *prov,
  724. uint32_t reason, const char *fmt, va_list args)
  725. {
  726. /*
  727. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  728. * error and will be treated as such. Otherwise, it's a new style
  729. * provider error and will be treated as such.
  730. */
  731. if (ERR_GET_LIB(reason) != 0) {
  732. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  733. } else {
  734. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  735. }
  736. }
  737. #endif
  738. /*
  739. * Functions provided by the core. Blank line separates "families" of related
  740. * functions.
  741. */
  742. static const OSSL_DISPATCH core_dispatch_[] = {
  743. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  744. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  745. { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
  746. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  747. #ifndef FIPS_MODE
  748. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  749. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  750. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  751. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
  752. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
  753. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
  754. { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
  755. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
  756. #endif
  757. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  758. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  759. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  760. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  761. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  762. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  763. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  764. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  765. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  766. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  767. (void (*)(void))CRYPTO_secure_clear_free },
  768. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  769. (void (*)(void))CRYPTO_secure_allocated },
  770. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  771. { OSSL_FUNC_CRYPTO_MEM_CTRL, (void (*)(void))CRYPTO_mem_ctrl },
  772. { 0, NULL }
  773. };
  774. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;