provider_core.c 35 KB

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