provider_core.c 27 KB

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