provider_core.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Copyright 2019-2021 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_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/provider.h>
  13. #include <openssl/params.h>
  14. #include <openssl/opensslv.h>
  15. #include "crypto/cryptlib.h"
  16. #include "crypto/evp.h" /* evp_method_store_flush */
  17. #include "crypto/rand.h"
  18. #include "internal/nelem.h"
  19. #include "internal/thread_once.h"
  20. #include "internal/provider.h"
  21. #include "internal/refcount.h"
  22. #include "provider_local.h"
  23. #ifndef FIPS_MODULE
  24. # include <openssl/self_test.h>
  25. #endif
  26. static OSSL_PROVIDER *provider_new(const char *name,
  27. OSSL_provider_init_fn *init_function);
  28. /*-
  29. * Provider Object structure
  30. * =========================
  31. */
  32. typedef struct {
  33. char *name;
  34. char *value;
  35. } INFOPAIR;
  36. DEFINE_STACK_OF(INFOPAIR)
  37. struct provider_store_st; /* Forward declaration */
  38. struct ossl_provider_st {
  39. /* Flag bits */
  40. unsigned int flag_initialized:1;
  41. unsigned int flag_activated:1;
  42. unsigned int flag_fallback:1; /* Can be used as fallback */
  43. unsigned int flag_activated_as_fallback:1;
  44. /* OpenSSL library side data */
  45. CRYPTO_REF_COUNT refcnt;
  46. CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
  47. CRYPTO_REF_COUNT activatecnt;
  48. CRYPTO_RWLOCK *activatecnt_lock; /* For the activate counter */
  49. char *name;
  50. char *path;
  51. DSO *module;
  52. OSSL_provider_init_fn *init_function;
  53. STACK_OF(INFOPAIR) *parameters;
  54. OSSL_LIB_CTX *libctx; /* The library context this instance is in */
  55. struct provider_store_st *store; /* The store this instance belongs to */
  56. #ifndef FIPS_MODULE
  57. /*
  58. * In the FIPS module inner provider, this isn't needed, since the
  59. * error upcalls are always direct calls to the outer provider.
  60. */
  61. int error_lib; /* ERR library number, one for each provider */
  62. # ifndef OPENSSL_NO_ERR
  63. ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
  64. # endif
  65. #endif
  66. /* Provider side functions */
  67. OSSL_FUNC_provider_teardown_fn *teardown;
  68. OSSL_FUNC_provider_gettable_params_fn *gettable_params;
  69. OSSL_FUNC_provider_get_params_fn *get_params;
  70. OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
  71. OSSL_FUNC_provider_self_test_fn *self_test;
  72. OSSL_FUNC_provider_query_operation_fn *query_operation;
  73. /*
  74. * Cache of bit to indicate of query_operation() has been called on
  75. * a specific operation or not.
  76. */
  77. unsigned char *operation_bits;
  78. size_t operation_bits_sz;
  79. CRYPTO_RWLOCK *opbits_lock;
  80. /* Provider side data */
  81. void *provctx;
  82. };
  83. DEFINE_STACK_OF(OSSL_PROVIDER)
  84. static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
  85. const OSSL_PROVIDER * const *b)
  86. {
  87. return strcmp((*a)->name, (*b)->name);
  88. }
  89. /*-
  90. * Provider Object store
  91. * =====================
  92. *
  93. * The Provider Object store is a library context object, and therefore needs
  94. * an index.
  95. */
  96. struct provider_store_st {
  97. STACK_OF(OSSL_PROVIDER) *providers;
  98. CRYPTO_RWLOCK *lock;
  99. char *default_path;
  100. unsigned int use_fallbacks:1;
  101. };
  102. /*
  103. * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
  104. * and ossl_provider_free(), called as needed.
  105. * Since this is only called when the provider store is being emptied, we
  106. * don't need to care about any lock.
  107. */
  108. static void provider_deactivate_free(OSSL_PROVIDER *prov)
  109. {
  110. if (prov->flag_activated)
  111. ossl_provider_deactivate(prov);
  112. ossl_provider_free(prov);
  113. }
  114. static void provider_store_free(void *vstore)
  115. {
  116. struct provider_store_st *store = vstore;
  117. if (store == NULL)
  118. return;
  119. OPENSSL_free(store->default_path);
  120. sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
  121. CRYPTO_THREAD_lock_free(store->lock);
  122. OPENSSL_free(store);
  123. }
  124. static void *provider_store_new(OSSL_LIB_CTX *ctx)
  125. {
  126. struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
  127. const struct predefined_providers_st *p = NULL;
  128. if (store == NULL
  129. || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
  130. || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  131. provider_store_free(store);
  132. return NULL;
  133. }
  134. store->use_fallbacks = 1;
  135. for (p = predefined_providers; p->name != NULL; p++) {
  136. OSSL_PROVIDER *prov = NULL;
  137. /*
  138. * We use the internal constructor directly here,
  139. * otherwise we get a call loop
  140. */
  141. prov = provider_new(p->name, p->init);
  142. if (prov == NULL
  143. || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  144. ossl_provider_free(prov);
  145. provider_store_free(store);
  146. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  147. return NULL;
  148. }
  149. prov->libctx = ctx;
  150. prov->store = store;
  151. #ifndef FIPS_MODULE
  152. prov->error_lib = ERR_get_next_error_library();
  153. #endif
  154. if(p->is_fallback)
  155. ossl_provider_set_fallback(prov);
  156. }
  157. return store;
  158. }
  159. static const OSSL_LIB_CTX_METHOD provider_store_method = {
  160. provider_store_new,
  161. provider_store_free,
  162. };
  163. static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
  164. {
  165. struct provider_store_st *store = NULL;
  166. store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
  167. &provider_store_method);
  168. if (store == NULL)
  169. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  170. return store;
  171. }
  172. int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
  173. {
  174. struct provider_store_st *store;
  175. if ((store = get_provider_store(libctx)) != NULL) {
  176. store->use_fallbacks = 0;
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
  182. int noconfig)
  183. {
  184. struct provider_store_st *store = NULL;
  185. OSSL_PROVIDER *prov = NULL;
  186. if ((store = get_provider_store(libctx)) != NULL) {
  187. OSSL_PROVIDER tmpl = { 0, };
  188. int i;
  189. #ifndef FIPS_MODULE
  190. /*
  191. * Make sure any providers are loaded from config before we try to find
  192. * them.
  193. */
  194. if (!noconfig)
  195. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  196. #endif
  197. tmpl.name = (char *)name;
  198. CRYPTO_THREAD_write_lock(store->lock);
  199. if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
  200. || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
  201. || !ossl_provider_up_ref(prov))
  202. prov = NULL;
  203. CRYPTO_THREAD_unlock(store->lock);
  204. }
  205. return prov;
  206. }
  207. /*-
  208. * Provider Object methods
  209. * =======================
  210. */
  211. static OSSL_PROVIDER *provider_new(const char *name,
  212. OSSL_provider_init_fn *init_function)
  213. {
  214. OSSL_PROVIDER *prov = NULL;
  215. if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
  216. #ifndef HAVE_ATOMICS
  217. || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
  218. || (prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL
  219. #endif
  220. || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
  221. || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
  222. || (prov->name = OPENSSL_strdup(name)) == NULL) {
  223. ossl_provider_free(prov);
  224. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  225. return NULL;
  226. }
  227. prov->init_function = init_function;
  228. return prov;
  229. }
  230. int ossl_provider_up_ref(OSSL_PROVIDER *prov)
  231. {
  232. int ref = 0;
  233. if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
  234. return 0;
  235. return ref;
  236. }
  237. OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
  238. OSSL_provider_init_fn *init_function,
  239. int noconfig)
  240. {
  241. struct provider_store_st *store = NULL;
  242. OSSL_PROVIDER *prov = NULL;
  243. if ((store = get_provider_store(libctx)) == NULL)
  244. return NULL;
  245. if ((prov = ossl_provider_find(libctx, name,
  246. noconfig)) != NULL) { /* refcount +1 */
  247. ossl_provider_free(prov); /* refcount -1 */
  248. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
  249. "name=%s", name);
  250. return NULL;
  251. }
  252. /* provider_new() generates an error, so no need here */
  253. if ((prov = provider_new(name, init_function)) == NULL)
  254. return NULL;
  255. CRYPTO_THREAD_write_lock(store->lock);
  256. if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
  257. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  258. prov = NULL;
  259. } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  260. ossl_provider_free(prov); /* -1 Store reference */
  261. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  262. prov = NULL;
  263. } else {
  264. prov->libctx = libctx;
  265. prov->store = store;
  266. #ifndef FIPS_MODULE
  267. prov->error_lib = ERR_get_next_error_library();
  268. #endif
  269. }
  270. CRYPTO_THREAD_unlock(store->lock);
  271. if (prov == NULL)
  272. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  273. /*
  274. * At this point, the provider is only partially "loaded". To be
  275. * fully "loaded", ossl_provider_activate() must also be called.
  276. */
  277. return prov;
  278. }
  279. static void free_infopair(INFOPAIR *pair)
  280. {
  281. OPENSSL_free(pair->name);
  282. OPENSSL_free(pair->value);
  283. OPENSSL_free(pair);
  284. }
  285. void ossl_provider_free(OSSL_PROVIDER *prov)
  286. {
  287. if (prov != NULL) {
  288. int ref = 0;
  289. CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
  290. /*
  291. * When the refcount drops to zero, we clean up the provider.
  292. * Note that this also does teardown, which may seem late,
  293. * considering that init happens on first activation. However,
  294. * there may be other structures hanging on to the provider after
  295. * the last deactivation and may therefore need full access to the
  296. * provider's services. Therefore, we deinit late.
  297. */
  298. if (ref == 0) {
  299. if (prov->flag_initialized) {
  300. #ifndef FIPS_MODULE
  301. ossl_init_thread_deregister(prov);
  302. #endif
  303. if (prov->teardown != NULL)
  304. prov->teardown(prov->provctx);
  305. #ifndef OPENSSL_NO_ERR
  306. # ifndef FIPS_MODULE
  307. if (prov->error_strings != NULL) {
  308. ERR_unload_strings(prov->error_lib, prov->error_strings);
  309. OPENSSL_free(prov->error_strings);
  310. prov->error_strings = NULL;
  311. }
  312. # endif
  313. #endif
  314. OPENSSL_free(prov->operation_bits);
  315. prov->operation_bits = NULL;
  316. prov->operation_bits_sz = 0;
  317. prov->flag_initialized = 0;
  318. }
  319. #ifndef FIPS_MODULE
  320. DSO_free(prov->module);
  321. #endif
  322. OPENSSL_free(prov->name);
  323. OPENSSL_free(prov->path);
  324. sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
  325. CRYPTO_THREAD_lock_free(prov->opbits_lock);
  326. #ifndef HAVE_ATOMICS
  327. CRYPTO_THREAD_lock_free(prov->refcnt_lock);
  328. CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
  329. #endif
  330. OPENSSL_free(prov);
  331. }
  332. }
  333. }
  334. /* Setters */
  335. int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
  336. {
  337. OPENSSL_free(prov->path);
  338. if (module_path == NULL)
  339. return 1;
  340. if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
  341. return 1;
  342. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  343. return 0;
  344. }
  345. int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
  346. const char *name, const char *value)
  347. {
  348. INFOPAIR *pair = NULL;
  349. if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
  350. && (prov->parameters != NULL
  351. || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
  352. && (pair->name = OPENSSL_strdup(name)) != NULL
  353. && (pair->value = OPENSSL_strdup(value)) != NULL
  354. && sk_INFOPAIR_push(prov->parameters, pair) > 0)
  355. return 1;
  356. if (pair != NULL) {
  357. OPENSSL_free(pair->name);
  358. OPENSSL_free(pair->value);
  359. OPENSSL_free(pair);
  360. }
  361. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  362. return 0;
  363. }
  364. /*
  365. * Provider activation.
  366. *
  367. * What "activation" means depends on the provider form; for built in
  368. * providers (in the library or the application alike), the provider
  369. * can already be considered to be loaded, all that's needed is to
  370. * initialize it. However, for dynamically loadable provider modules,
  371. * we must first load that module.
  372. *
  373. * Built in modules are distinguished from dynamically loaded modules
  374. * with an already assigned init function.
  375. */
  376. static const OSSL_DISPATCH *core_dispatch; /* Define further down */
  377. int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
  378. const char *path)
  379. {
  380. struct provider_store_st *store;
  381. char *p = NULL;
  382. if (path != NULL) {
  383. p = OPENSSL_strdup(path);
  384. if (p == NULL) {
  385. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  386. return 0;
  387. }
  388. }
  389. if ((store = get_provider_store(libctx)) != NULL
  390. && CRYPTO_THREAD_write_lock(store->lock)) {
  391. OPENSSL_free(store->default_path);
  392. store->default_path = p;
  393. CRYPTO_THREAD_unlock(store->lock);
  394. return 1;
  395. }
  396. OPENSSL_free(p);
  397. return 0;
  398. }
  399. /*
  400. * Internal version that doesn't affect the store flags, and thereby avoid
  401. * locking. Direct callers must remember to set the store flags when
  402. * appropriate.
  403. */
  404. static int provider_init(OSSL_PROVIDER *prov)
  405. {
  406. const OSSL_DISPATCH *provider_dispatch = NULL;
  407. void *tmp_provctx = NULL; /* safety measure */
  408. #ifndef OPENSSL_NO_ERR
  409. # ifndef FIPS_MODULE
  410. OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
  411. # endif
  412. #endif
  413. if (prov->flag_initialized)
  414. return 1;
  415. /*
  416. * If the init function isn't set, it indicates that this provider is
  417. * a loadable module.
  418. */
  419. if (prov->init_function == NULL) {
  420. #ifdef FIPS_MODULE
  421. return 0;
  422. #else
  423. if (prov->module == NULL) {
  424. char *allocated_path = NULL;
  425. const char *module_path = NULL;
  426. char *merged_path = NULL;
  427. const char *load_dir = NULL;
  428. struct provider_store_st *store;
  429. if ((prov->module = DSO_new()) == NULL) {
  430. /* DSO_new() generates an error already */
  431. return 0;
  432. }
  433. if ((store = get_provider_store(prov->libctx)) == NULL
  434. || !CRYPTO_THREAD_read_lock(store->lock))
  435. return 0;
  436. load_dir = store->default_path;
  437. if (load_dir == NULL) {
  438. load_dir = ossl_safe_getenv("OPENSSL_MODULES");
  439. if (load_dir == NULL)
  440. load_dir = MODULESDIR;
  441. }
  442. DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
  443. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
  444. module_path = prov->path;
  445. if (module_path == NULL)
  446. module_path = allocated_path =
  447. DSO_convert_filename(prov->module, prov->name);
  448. if (module_path != NULL)
  449. merged_path = DSO_merge(prov->module, module_path, load_dir);
  450. CRYPTO_THREAD_unlock(store->lock);
  451. if (merged_path == NULL
  452. || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
  453. DSO_free(prov->module);
  454. prov->module = NULL;
  455. }
  456. OPENSSL_free(merged_path);
  457. OPENSSL_free(allocated_path);
  458. }
  459. if (prov->module != NULL)
  460. prov->init_function = (OSSL_provider_init_fn *)
  461. DSO_bind_func(prov->module, "OSSL_provider_init");
  462. #endif
  463. }
  464. /* Call the initialise function for the provider. */
  465. if (prov->init_function == NULL
  466. || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
  467. &provider_dispatch, &tmp_provctx)) {
  468. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
  469. "name=%s", prov->name);
  470. #ifndef FIPS_MODULE
  471. DSO_free(prov->module);
  472. prov->module = NULL;
  473. #endif
  474. return 0;
  475. }
  476. prov->provctx = tmp_provctx;
  477. for (; provider_dispatch->function_id != 0; provider_dispatch++) {
  478. switch (provider_dispatch->function_id) {
  479. case OSSL_FUNC_PROVIDER_TEARDOWN:
  480. prov->teardown =
  481. OSSL_FUNC_provider_teardown(provider_dispatch);
  482. break;
  483. case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
  484. prov->gettable_params =
  485. OSSL_FUNC_provider_gettable_params(provider_dispatch);
  486. break;
  487. case OSSL_FUNC_PROVIDER_GET_PARAMS:
  488. prov->get_params =
  489. OSSL_FUNC_provider_get_params(provider_dispatch);
  490. break;
  491. case OSSL_FUNC_PROVIDER_SELF_TEST:
  492. prov->self_test =
  493. OSSL_FUNC_provider_self_test(provider_dispatch);
  494. break;
  495. case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
  496. prov->get_capabilities =
  497. OSSL_FUNC_provider_get_capabilities(provider_dispatch);
  498. break;
  499. case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
  500. prov->query_operation =
  501. OSSL_FUNC_provider_query_operation(provider_dispatch);
  502. break;
  503. #ifndef OPENSSL_NO_ERR
  504. # ifndef FIPS_MODULE
  505. case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
  506. p_get_reason_strings =
  507. OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
  508. break;
  509. # endif
  510. #endif
  511. }
  512. }
  513. #ifndef OPENSSL_NO_ERR
  514. # ifndef FIPS_MODULE
  515. if (p_get_reason_strings != NULL) {
  516. const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
  517. size_t cnt, cnt2;
  518. /*
  519. * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
  520. * although they are essentially the same type.
  521. * Furthermore, ERR_load_strings() patches the array's error number
  522. * with the error library number, so we need to make a copy of that
  523. * array either way.
  524. */
  525. cnt = 0;
  526. while (reasonstrings[cnt].id != 0) {
  527. if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
  528. return 0;
  529. cnt++;
  530. }
  531. cnt++; /* One for the terminating item */
  532. /* Allocate one extra item for the "library" name */
  533. prov->error_strings =
  534. OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
  535. if (prov->error_strings == NULL)
  536. return 0;
  537. /*
  538. * Set the "library" name.
  539. */
  540. prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
  541. prov->error_strings[0].string = prov->name;
  542. /*
  543. * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
  544. * 1..cnt.
  545. */
  546. for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
  547. prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
  548. prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
  549. }
  550. ERR_load_strings(prov->error_lib, prov->error_strings);
  551. }
  552. # endif
  553. #endif
  554. /* With this flag set, this provider has become fully "loaded". */
  555. prov->flag_initialized = 1;
  556. return 1;
  557. }
  558. static int provider_deactivate(OSSL_PROVIDER *prov)
  559. {
  560. int ref = 0;
  561. if (!ossl_assert(prov != NULL))
  562. return 0;
  563. if (CRYPTO_DOWN_REF(&prov->activatecnt, &ref, prov->activatecnt_lock) <= 0)
  564. return 0;
  565. if (ref < 1)
  566. prov->flag_activated = 0;
  567. /* We don't deinit here, that's done in ossl_provider_free() */
  568. return 1;
  569. }
  570. static int provider_activate(OSSL_PROVIDER *prov)
  571. {
  572. int ref = 0;
  573. if (CRYPTO_UP_REF(&prov->activatecnt, &ref, prov->activatecnt_lock) <= 0)
  574. return 0;
  575. if (provider_init(prov)) {
  576. prov->flag_activated = 1;
  577. return 1;
  578. }
  579. provider_deactivate(prov);
  580. return 0;
  581. }
  582. int ossl_provider_activate(OSSL_PROVIDER *prov)
  583. {
  584. if (prov == NULL)
  585. return 0;
  586. if (provider_activate(prov)) {
  587. CRYPTO_THREAD_write_lock(prov->store->lock);
  588. prov->store->use_fallbacks = 0;
  589. CRYPTO_THREAD_unlock(prov->store->lock);
  590. return 1;
  591. }
  592. return 0;
  593. }
  594. int ossl_provider_deactivate(OSSL_PROVIDER *prov)
  595. {
  596. if (prov == NULL)
  597. return 0;
  598. return provider_deactivate(prov);
  599. }
  600. void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
  601. {
  602. return prov->provctx;
  603. }
  604. static int provider_forall_loaded(struct provider_store_st *store,
  605. int *found_activated,
  606. int (*cb)(OSSL_PROVIDER *provider,
  607. void *cbdata),
  608. void *cbdata)
  609. {
  610. int i;
  611. int ret = 1;
  612. int num_provs;
  613. num_provs = sk_OSSL_PROVIDER_num(store->providers);
  614. if (found_activated != NULL)
  615. *found_activated = 0;
  616. for (i = 0; i < num_provs; i++) {
  617. OSSL_PROVIDER *prov =
  618. sk_OSSL_PROVIDER_value(store->providers, i);
  619. if (prov->flag_activated) {
  620. if (found_activated != NULL)
  621. *found_activated = 1;
  622. if (!(ret = cb(prov, cbdata)))
  623. break;
  624. }
  625. }
  626. return ret;
  627. }
  628. /*
  629. * This function only does something once when store->use_fallbacks == 1,
  630. * and then sets store->use_fallbacks = 0, so the second call and so on is
  631. * effectively a no-op.
  632. */
  633. static void provider_activate_fallbacks(struct provider_store_st *store)
  634. {
  635. int use_fallbacks;
  636. int num_provs;
  637. int activated_fallback_count = 0;
  638. int i;
  639. CRYPTO_THREAD_read_lock(store->lock);
  640. use_fallbacks = store->use_fallbacks;
  641. CRYPTO_THREAD_unlock(store->lock);
  642. if (!use_fallbacks)
  643. return;
  644. CRYPTO_THREAD_write_lock(store->lock);
  645. /* Check again, just in case another thread changed it */
  646. use_fallbacks = store->use_fallbacks;
  647. if (!use_fallbacks) {
  648. CRYPTO_THREAD_unlock(store->lock);
  649. return;
  650. }
  651. num_provs = sk_OSSL_PROVIDER_num(store->providers);
  652. for (i = 0; i < num_provs; i++) {
  653. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
  654. if (ossl_provider_up_ref(prov)) {
  655. if (prov->flag_fallback) {
  656. if (provider_activate(prov)) {
  657. prov->flag_activated_as_fallback = 1;
  658. activated_fallback_count++;
  659. }
  660. }
  661. ossl_provider_free(prov);
  662. }
  663. }
  664. /*
  665. * We assume that all fallbacks have been added to the store before
  666. * any fallback is activated.
  667. * TODO: We may have to reconsider this, IF we find ourselves adding
  668. * fallbacks after any previous fallback has been activated.
  669. */
  670. if (activated_fallback_count > 0)
  671. store->use_fallbacks = 0;
  672. CRYPTO_THREAD_unlock(store->lock);
  673. }
  674. int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
  675. int (*cb)(OSSL_PROVIDER *provider,
  676. void *cbdata),
  677. void *cbdata)
  678. {
  679. int ret = 1;
  680. struct provider_store_st *store = get_provider_store(ctx);
  681. #ifndef FIPS_MODULE
  682. /*
  683. * Make sure any providers are loaded from config before we try to use
  684. * them.
  685. */
  686. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  687. #endif
  688. if (store != NULL) {
  689. provider_activate_fallbacks(store);
  690. CRYPTO_THREAD_read_lock(store->lock);
  691. /*
  692. * Now, we sweep through all providers
  693. */
  694. ret = provider_forall_loaded(store, NULL, cb, cbdata);
  695. CRYPTO_THREAD_unlock(store->lock);
  696. }
  697. return ret;
  698. }
  699. int ossl_provider_available(OSSL_PROVIDER *prov)
  700. {
  701. if (prov != NULL) {
  702. provider_activate_fallbacks(prov->store);
  703. return prov->flag_activated;
  704. }
  705. return 0;
  706. }
  707. /* Setters of Provider Object data */
  708. int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
  709. {
  710. if (prov == NULL)
  711. return 0;
  712. prov->flag_fallback = 1;
  713. return 1;
  714. }
  715. /* Getters of Provider Object data */
  716. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  717. {
  718. return prov->name;
  719. }
  720. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  721. {
  722. return prov->module;
  723. }
  724. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  725. {
  726. #ifdef FIPS_MODULE
  727. return NULL;
  728. #else
  729. return DSO_get_filename(prov->module);
  730. #endif
  731. }
  732. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  733. {
  734. #ifdef FIPS_MODULE
  735. return NULL;
  736. #else
  737. /* FIXME: Ensure it's a full path */
  738. return DSO_get_filename(prov->module);
  739. #endif
  740. }
  741. void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
  742. {
  743. if (prov != NULL)
  744. return prov->provctx;
  745. return NULL;
  746. }
  747. OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
  748. {
  749. /* TODO(3.0) just: return prov->libctx; */
  750. return prov != NULL ? prov->libctx : NULL;
  751. }
  752. /* Wrappers around calls to the provider */
  753. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  754. {
  755. if (prov->teardown != NULL)
  756. prov->teardown(prov->provctx);
  757. }
  758. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  759. {
  760. return prov->gettable_params == NULL
  761. ? NULL : prov->gettable_params(prov->provctx);
  762. }
  763. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  764. {
  765. return prov->get_params == NULL
  766. ? 0 : prov->get_params(prov->provctx, params);
  767. }
  768. int ossl_provider_self_test(const OSSL_PROVIDER *prov)
  769. {
  770. int ret;
  771. if (prov->self_test == NULL)
  772. return 1;
  773. ret = prov->self_test(prov->provctx);
  774. if (ret == 0)
  775. evp_method_store_flush(ossl_provider_libctx(prov));
  776. return ret;
  777. }
  778. int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
  779. const char *capability,
  780. OSSL_CALLBACK *cb,
  781. void *arg)
  782. {
  783. return prov->get_capabilities == NULL
  784. ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
  785. }
  786. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  787. int operation_id,
  788. int *no_cache)
  789. {
  790. return prov->query_operation == NULL
  791. ? NULL : prov->query_operation(prov->provctx, operation_id, no_cache);
  792. }
  793. int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
  794. {
  795. size_t byte = bitnum / 8;
  796. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  797. CRYPTO_THREAD_write_lock(provider->opbits_lock);
  798. if (provider->operation_bits_sz <= byte) {
  799. unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
  800. byte + 1);
  801. if (tmp == NULL) {
  802. CRYPTO_THREAD_unlock(provider->opbits_lock);
  803. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  804. return 0;
  805. }
  806. provider->operation_bits = tmp;
  807. memset(provider->operation_bits + provider->operation_bits_sz,
  808. '\0', byte + 1 - provider->operation_bits_sz);
  809. provider->operation_bits_sz = byte + 1;
  810. }
  811. provider->operation_bits[byte] |= bit;
  812. CRYPTO_THREAD_unlock(provider->opbits_lock);
  813. return 1;
  814. }
  815. int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
  816. int *result)
  817. {
  818. size_t byte = bitnum / 8;
  819. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  820. if (!ossl_assert(result != NULL)) {
  821. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  822. return 0;
  823. }
  824. *result = 0;
  825. CRYPTO_THREAD_read_lock(provider->opbits_lock);
  826. if (provider->operation_bits_sz > byte)
  827. *result = ((provider->operation_bits[byte] & bit) != 0);
  828. CRYPTO_THREAD_unlock(provider->opbits_lock);
  829. return 1;
  830. }
  831. /*-
  832. * Core functions for the provider
  833. * ===============================
  834. *
  835. * This is the set of functions that the core makes available to the provider
  836. */
  837. /*
  838. * This returns a list of Provider Object parameters with their types, for
  839. * discovery. We do not expect that many providers will use this, but one
  840. * never knows.
  841. */
  842. static const OSSL_PARAM param_types[] = {
  843. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  844. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
  845. NULL, 0),
  846. #ifndef FIPS_MODULE
  847. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
  848. NULL, 0),
  849. #endif
  850. OSSL_PARAM_END
  851. };
  852. /*
  853. * Forward declare all the functions that are provided aa dispatch.
  854. * This ensures that the compiler will complain if they aren't defined
  855. * with the correct signature.
  856. */
  857. static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
  858. static OSSL_FUNC_core_get_params_fn core_get_params;
  859. static OSSL_FUNC_core_thread_start_fn core_thread_start;
  860. static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
  861. #ifndef FIPS_MODULE
  862. static OSSL_FUNC_core_new_error_fn core_new_error;
  863. static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
  864. static OSSL_FUNC_core_vset_error_fn core_vset_error;
  865. static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
  866. static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
  867. static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
  868. #endif
  869. static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
  870. {
  871. return param_types;
  872. }
  873. static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
  874. {
  875. int i;
  876. OSSL_PARAM *p;
  877. /*
  878. * We created this object originally and we know it is actually an
  879. * OSSL_PROVIDER *, so the cast is safe
  880. */
  881. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  882. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
  883. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  884. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
  885. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  886. #ifndef FIPS_MODULE
  887. if ((p = OSSL_PARAM_locate(params,
  888. OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
  889. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  890. #endif
  891. if (prov->parameters == NULL)
  892. return 1;
  893. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  894. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  895. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  896. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  897. }
  898. return 1;
  899. }
  900. static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
  901. {
  902. /*
  903. * We created this object originally and we know it is actually an
  904. * OSSL_PROVIDER *, so the cast is safe
  905. */
  906. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  907. return (OPENSSL_CORE_CTX *)ossl_provider_libctx(prov);
  908. }
  909. static int core_thread_start(const OSSL_CORE_HANDLE *handle,
  910. OSSL_thread_stop_handler_fn handfn)
  911. {
  912. /*
  913. * We created this object originally and we know it is actually an
  914. * OSSL_PROVIDER *, so the cast is safe
  915. */
  916. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  917. return ossl_init_thread_start(prov, prov->provctx, handfn);
  918. }
  919. /*
  920. * The FIPS module inner provider doesn't implement these. They aren't
  921. * needed there, since the FIPS module upcalls are always the outer provider
  922. * ones.
  923. */
  924. #ifndef FIPS_MODULE
  925. /*
  926. * TODO(3.0) These error functions should use |handle| to select the proper
  927. * library context to report in the correct error stack, at least if error
  928. * stacks become tied to the library context.
  929. * We cannot currently do that since there's no support for it in the
  930. * ERR subsystem.
  931. */
  932. static void core_new_error(const OSSL_CORE_HANDLE *handle)
  933. {
  934. ERR_new();
  935. }
  936. static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  937. const char *file, int line, const char *func)
  938. {
  939. ERR_set_debug(file, line, func);
  940. }
  941. static void core_vset_error(const OSSL_CORE_HANDLE *handle,
  942. uint32_t reason, const char *fmt, va_list args)
  943. {
  944. /*
  945. * We created this object originally and we know it is actually an
  946. * OSSL_PROVIDER *, so the cast is safe
  947. */
  948. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  949. /*
  950. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  951. * error and will be treated as such. Otherwise, it's a new style
  952. * provider error and will be treated as such.
  953. */
  954. if (ERR_GET_LIB(reason) != 0) {
  955. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  956. } else {
  957. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  958. }
  959. }
  960. static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
  961. {
  962. return ERR_set_mark();
  963. }
  964. static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
  965. {
  966. return ERR_clear_last_mark();
  967. }
  968. static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
  969. {
  970. return ERR_pop_to_mark();
  971. }
  972. #endif /* FIPS_MODULE */
  973. /*
  974. * Functions provided by the core.
  975. */
  976. static const OSSL_DISPATCH core_dispatch_[] = {
  977. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  978. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  979. { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
  980. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  981. #ifndef FIPS_MODULE
  982. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  983. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  984. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  985. { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
  986. { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
  987. (void (*)(void))core_clear_last_error_mark },
  988. { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
  989. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
  990. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
  991. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
  992. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
  993. { OSSL_FUNC_BIO_GETS, (void (*)(void))BIO_gets },
  994. { OSSL_FUNC_BIO_PUTS, (void (*)(void))BIO_puts },
  995. { OSSL_FUNC_BIO_CTRL, (void (*)(void))BIO_ctrl },
  996. { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
  997. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
  998. { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
  999. { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
  1000. { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
  1001. { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
  1002. { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
  1003. { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
  1004. #endif
  1005. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  1006. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  1007. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  1008. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  1009. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  1010. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  1011. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  1012. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  1013. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  1014. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  1015. (void (*)(void))CRYPTO_secure_clear_free },
  1016. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  1017. (void (*)(void))CRYPTO_secure_allocated },
  1018. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  1019. { 0, NULL }
  1020. };
  1021. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;