provider_core.c 33 KB

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