provider_core.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  1. /*
  2. * Copyright 2019-2023 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 <assert.h>
  10. #include <openssl/core.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/provider.h>
  14. #include <openssl/params.h>
  15. #include <openssl/opensslv.h>
  16. #include "crypto/cryptlib.h"
  17. #ifndef FIPS_MODULE
  18. #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
  19. #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
  20. #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
  21. #endif
  22. #include "crypto/evp.h" /* evp_method_store_cache_flush */
  23. #include "crypto/rand.h"
  24. #include "internal/nelem.h"
  25. #include "internal/thread_once.h"
  26. #include "internal/provider.h"
  27. #include "internal/refcount.h"
  28. #include "internal/bio.h"
  29. #include "internal/core.h"
  30. #include "provider_local.h"
  31. #include "crypto/context.h"
  32. #ifndef FIPS_MODULE
  33. # include <openssl/self_test.h>
  34. #endif
  35. /*
  36. * This file defines and uses a number of different structures:
  37. *
  38. * OSSL_PROVIDER (provider_st): Used to represent all information related to a
  39. * single instance of a provider.
  40. *
  41. * provider_store_st: Holds information about the collection of providers that
  42. * are available within the current library context (OSSL_LIB_CTX). It also
  43. * holds configuration information about providers that could be loaded at some
  44. * future point.
  45. *
  46. * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
  47. * that have been registered for a child library context and the associated
  48. * provider that registered those callbacks.
  49. *
  50. * Where a child library context exists then it has its own instance of the
  51. * provider store. Each provider that exists in the parent provider store, has
  52. * an associated child provider in the child library context's provider store.
  53. * As providers get activated or deactivated this needs to be mirrored in the
  54. * associated child providers.
  55. *
  56. * LOCKING
  57. * =======
  58. *
  59. * There are a number of different locks used in this file and it is important
  60. * to understand how they should be used in order to avoid deadlocks.
  61. *
  62. * Fields within a structure can often be "write once" on creation, and then
  63. * "read many". Creation of a structure is done by a single thread, and
  64. * therefore no lock is required for the "write once/read many" fields. It is
  65. * safe for multiple threads to read these fields without a lock, because they
  66. * will never be changed.
  67. *
  68. * However some fields may be changed after a structure has been created and
  69. * shared between multiple threads. Where this is the case a lock is required.
  70. *
  71. * The locks available are:
  72. *
  73. * The provider flag_lock: Used to control updates to the various provider
  74. * "flags" (flag_initialized and flag_activated).
  75. *
  76. * The provider activatecnt_lock: Used to control updates to the provider
  77. * activatecnt value.
  78. *
  79. * The provider optbits_lock: Used to control access to the provider's
  80. * operation_bits and operation_bits_sz fields.
  81. *
  82. * The store default_path_lock: Used to control access to the provider store's
  83. * default search path value (default_path)
  84. *
  85. * The store lock: Used to control the stack of provider's held within the
  86. * provider store, as well as the stack of registered child provider callbacks.
  87. *
  88. * As a general rule-of-thumb it is best to:
  89. * - keep the scope of the code that is protected by a lock to the absolute
  90. * minimum possible;
  91. * - try to keep the scope of the lock to within a single function (i.e. avoid
  92. * making calls to other functions while holding a lock);
  93. * - try to only ever hold one lock at a time.
  94. *
  95. * Unfortunately, it is not always possible to stick to the above guidelines.
  96. * Where they are not adhered to there is always a danger of inadvertently
  97. * introducing the possibility of deadlock. The following rules MUST be adhered
  98. * to in order to avoid that:
  99. * - Holding multiple locks at the same time is only allowed for the
  100. * provider store lock, the provider activatecnt_lock and the provider flag_lock.
  101. * - When holding multiple locks they must be acquired in the following order of
  102. * precedence:
  103. * 1) provider store lock
  104. * 2) provider flag_lock
  105. * 3) provider activatecnt_lock
  106. * - When releasing locks they must be released in the reverse order to which
  107. * they were acquired
  108. * - No locks may be held when making an upcall. NOTE: Some common functions
  109. * can make upcalls as part of their normal operation. If you need to call
  110. * some other function while holding a lock make sure you know whether it
  111. * will make any upcalls or not. For example ossl_provider_up_ref() can call
  112. * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
  113. * - It is permissible to hold the store and flag locks when calling child
  114. * provider callbacks. No other locks may be held during such callbacks.
  115. */
  116. static OSSL_PROVIDER *provider_new(const char *name,
  117. OSSL_provider_init_fn *init_function,
  118. STACK_OF(INFOPAIR) *parameters);
  119. /*-
  120. * Provider Object structure
  121. * =========================
  122. */
  123. #ifndef FIPS_MODULE
  124. typedef struct {
  125. OSSL_PROVIDER *prov;
  126. int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
  127. int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
  128. int (*global_props_cb)(const char *props, void *cbdata);
  129. void *cbdata;
  130. } OSSL_PROVIDER_CHILD_CB;
  131. DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
  132. #endif
  133. struct provider_store_st; /* Forward declaration */
  134. struct ossl_provider_st {
  135. /* Flag bits */
  136. unsigned int flag_initialized:1;
  137. unsigned int flag_activated:1;
  138. /* Getting and setting the flags require synchronization */
  139. CRYPTO_RWLOCK *flag_lock;
  140. /* OpenSSL library side data */
  141. CRYPTO_REF_COUNT refcnt;
  142. CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */
  143. int activatecnt;
  144. char *name;
  145. char *path;
  146. DSO *module;
  147. OSSL_provider_init_fn *init_function;
  148. STACK_OF(INFOPAIR) *parameters;
  149. OSSL_LIB_CTX *libctx; /* The library context this instance is in */
  150. struct provider_store_st *store; /* The store this instance belongs to */
  151. #ifndef FIPS_MODULE
  152. /*
  153. * In the FIPS module inner provider, this isn't needed, since the
  154. * error upcalls are always direct calls to the outer provider.
  155. */
  156. int error_lib; /* ERR library number, one for each provider */
  157. # ifndef OPENSSL_NO_ERR
  158. ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
  159. # endif
  160. #endif
  161. /* Provider side functions */
  162. OSSL_FUNC_provider_teardown_fn *teardown;
  163. OSSL_FUNC_provider_gettable_params_fn *gettable_params;
  164. OSSL_FUNC_provider_get_params_fn *get_params;
  165. OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
  166. OSSL_FUNC_provider_self_test_fn *self_test;
  167. OSSL_FUNC_provider_query_operation_fn *query_operation;
  168. OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
  169. /*
  170. * Cache of bit to indicate of query_operation() has been called on
  171. * a specific operation or not.
  172. */
  173. unsigned char *operation_bits;
  174. size_t operation_bits_sz;
  175. CRYPTO_RWLOCK *opbits_lock;
  176. #ifndef FIPS_MODULE
  177. /* Whether this provider is the child of some other provider */
  178. const OSSL_CORE_HANDLE *handle;
  179. unsigned int ischild:1;
  180. #endif
  181. /* Provider side data */
  182. void *provctx;
  183. const OSSL_DISPATCH *dispatch;
  184. };
  185. DEFINE_STACK_OF(OSSL_PROVIDER)
  186. static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
  187. const OSSL_PROVIDER * const *b)
  188. {
  189. return strcmp((*a)->name, (*b)->name);
  190. }
  191. /*-
  192. * Provider Object store
  193. * =====================
  194. *
  195. * The Provider Object store is a library context object, and therefore needs
  196. * an index.
  197. */
  198. struct provider_store_st {
  199. OSSL_LIB_CTX *libctx;
  200. STACK_OF(OSSL_PROVIDER) *providers;
  201. STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
  202. CRYPTO_RWLOCK *default_path_lock;
  203. CRYPTO_RWLOCK *lock;
  204. char *default_path;
  205. OSSL_PROVIDER_INFO *provinfo;
  206. size_t numprovinfo;
  207. size_t provinfosz;
  208. unsigned int use_fallbacks:1;
  209. unsigned int freeing:1;
  210. };
  211. /*
  212. * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
  213. * and ossl_provider_free(), called as needed.
  214. * Since this is only called when the provider store is being emptied, we
  215. * don't need to care about any lock.
  216. */
  217. static void provider_deactivate_free(OSSL_PROVIDER *prov)
  218. {
  219. if (prov->flag_activated)
  220. ossl_provider_deactivate(prov, 1);
  221. ossl_provider_free(prov);
  222. }
  223. #ifndef FIPS_MODULE
  224. static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
  225. {
  226. OPENSSL_free(cb);
  227. }
  228. #endif
  229. static void infopair_free(INFOPAIR *pair)
  230. {
  231. OPENSSL_free(pair->name);
  232. OPENSSL_free(pair->value);
  233. OPENSSL_free(pair);
  234. }
  235. static INFOPAIR *infopair_copy(const INFOPAIR *src)
  236. {
  237. INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
  238. if (dest == NULL)
  239. return NULL;
  240. if (src->name != NULL) {
  241. dest->name = OPENSSL_strdup(src->name);
  242. if (dest->name == NULL)
  243. goto err;
  244. }
  245. if (src->value != NULL) {
  246. dest->value = OPENSSL_strdup(src->value);
  247. if (dest->value == NULL)
  248. goto err;
  249. }
  250. return dest;
  251. err:
  252. OPENSSL_free(dest->name);
  253. OPENSSL_free(dest);
  254. return NULL;
  255. }
  256. void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
  257. {
  258. OPENSSL_free(info->name);
  259. OPENSSL_free(info->path);
  260. sk_INFOPAIR_pop_free(info->parameters, infopair_free);
  261. }
  262. void ossl_provider_store_free(void *vstore)
  263. {
  264. struct provider_store_st *store = vstore;
  265. size_t i;
  266. if (store == NULL)
  267. return;
  268. store->freeing = 1;
  269. OPENSSL_free(store->default_path);
  270. sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
  271. #ifndef FIPS_MODULE
  272. sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
  273. ossl_provider_child_cb_free);
  274. #endif
  275. CRYPTO_THREAD_lock_free(store->default_path_lock);
  276. CRYPTO_THREAD_lock_free(store->lock);
  277. for (i = 0; i < store->numprovinfo; i++)
  278. ossl_provider_info_clear(&store->provinfo[i]);
  279. OPENSSL_free(store->provinfo);
  280. OPENSSL_free(store);
  281. }
  282. void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
  283. {
  284. struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
  285. if (store == NULL
  286. || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
  287. || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
  288. #ifndef FIPS_MODULE
  289. || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
  290. #endif
  291. || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  292. ossl_provider_store_free(store);
  293. return NULL;
  294. }
  295. store->libctx = ctx;
  296. store->use_fallbacks = 1;
  297. return store;
  298. }
  299. static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
  300. {
  301. struct provider_store_st *store = NULL;
  302. store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
  303. if (store == NULL)
  304. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  305. return store;
  306. }
  307. int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
  308. {
  309. struct provider_store_st *store;
  310. if ((store = get_provider_store(libctx)) != NULL) {
  311. if (!CRYPTO_THREAD_write_lock(store->lock))
  312. return 0;
  313. store->use_fallbacks = 0;
  314. CRYPTO_THREAD_unlock(store->lock);
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. #define BUILTINS_BLOCK_SIZE 10
  320. int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
  321. OSSL_PROVIDER_INFO *entry)
  322. {
  323. struct provider_store_st *store = get_provider_store(libctx);
  324. int ret = 0;
  325. if (entry->name == NULL) {
  326. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  327. return 0;
  328. }
  329. if (store == NULL) {
  330. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  331. return 0;
  332. }
  333. if (!CRYPTO_THREAD_write_lock(store->lock))
  334. return 0;
  335. if (store->provinfosz == 0) {
  336. store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
  337. * BUILTINS_BLOCK_SIZE);
  338. if (store->provinfo == NULL)
  339. goto err;
  340. store->provinfosz = BUILTINS_BLOCK_SIZE;
  341. } else if (store->numprovinfo == store->provinfosz) {
  342. OSSL_PROVIDER_INFO *tmpbuiltins;
  343. size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
  344. tmpbuiltins = OPENSSL_realloc(store->provinfo,
  345. sizeof(*store->provinfo) * newsz);
  346. if (tmpbuiltins == NULL)
  347. goto err;
  348. store->provinfo = tmpbuiltins;
  349. store->provinfosz = newsz;
  350. }
  351. store->provinfo[store->numprovinfo] = *entry;
  352. store->numprovinfo++;
  353. ret = 1;
  354. err:
  355. CRYPTO_THREAD_unlock(store->lock);
  356. return ret;
  357. }
  358. OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
  359. ossl_unused int noconfig)
  360. {
  361. struct provider_store_st *store = NULL;
  362. OSSL_PROVIDER *prov = NULL;
  363. if ((store = get_provider_store(libctx)) != NULL) {
  364. OSSL_PROVIDER tmpl = { 0, };
  365. int i;
  366. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
  367. /*
  368. * Make sure any providers are loaded from config before we try to find
  369. * them.
  370. */
  371. if (!noconfig) {
  372. if (ossl_lib_ctx_is_default(libctx))
  373. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  374. }
  375. #endif
  376. tmpl.name = (char *)name;
  377. if (!CRYPTO_THREAD_write_lock(store->lock))
  378. return NULL;
  379. sk_OSSL_PROVIDER_sort(store->providers);
  380. if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
  381. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  382. CRYPTO_THREAD_unlock(store->lock);
  383. if (prov != NULL && !ossl_provider_up_ref(prov))
  384. prov = NULL;
  385. }
  386. return prov;
  387. }
  388. /*-
  389. * Provider Object methods
  390. * =======================
  391. */
  392. static OSSL_PROVIDER *provider_new(const char *name,
  393. OSSL_provider_init_fn *init_function,
  394. STACK_OF(INFOPAIR) *parameters)
  395. {
  396. OSSL_PROVIDER *prov = NULL;
  397. if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
  398. return NULL;
  399. if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
  400. OPENSSL_free(prov);
  401. return NULL;
  402. }
  403. #ifndef HAVE_ATOMICS
  404. if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
  405. ossl_provider_free(prov);
  406. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  407. return NULL;
  408. }
  409. #endif
  410. if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
  411. || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
  412. || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
  413. infopair_copy,
  414. infopair_free)) == NULL) {
  415. ossl_provider_free(prov);
  416. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  417. return NULL;
  418. }
  419. if ((prov->name = OPENSSL_strdup(name)) == NULL) {
  420. ossl_provider_free(prov);
  421. return NULL;
  422. }
  423. prov->init_function = init_function;
  424. return prov;
  425. }
  426. int ossl_provider_up_ref(OSSL_PROVIDER *prov)
  427. {
  428. int ref = 0;
  429. if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0)
  430. return 0;
  431. #ifndef FIPS_MODULE
  432. if (prov->ischild) {
  433. if (!ossl_provider_up_ref_parent(prov, 0)) {
  434. ossl_provider_free(prov);
  435. return 0;
  436. }
  437. }
  438. #endif
  439. return ref;
  440. }
  441. #ifndef FIPS_MODULE
  442. static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
  443. {
  444. if (activate)
  445. return ossl_provider_activate(prov, 1, 0);
  446. return ossl_provider_up_ref(prov);
  447. }
  448. static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
  449. {
  450. if (deactivate)
  451. return ossl_provider_deactivate(prov, 1);
  452. ossl_provider_free(prov);
  453. return 1;
  454. }
  455. #endif
  456. /*
  457. * We assume that the requested provider does not already exist in the store.
  458. * The caller should check. If it does exist then adding it to the store later
  459. * will fail.
  460. */
  461. OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
  462. OSSL_provider_init_fn *init_function,
  463. OSSL_PARAM *params, int noconfig)
  464. {
  465. struct provider_store_st *store = NULL;
  466. OSSL_PROVIDER_INFO template;
  467. OSSL_PROVIDER *prov = NULL;
  468. if ((store = get_provider_store(libctx)) == NULL)
  469. return NULL;
  470. memset(&template, 0, sizeof(template));
  471. if (init_function == NULL) {
  472. const OSSL_PROVIDER_INFO *p;
  473. size_t i;
  474. /* Check if this is a predefined builtin provider */
  475. for (p = ossl_predefined_providers; p->name != NULL; p++) {
  476. if (strcmp(p->name, name) == 0) {
  477. template = *p;
  478. break;
  479. }
  480. }
  481. if (p->name == NULL) {
  482. /* Check if this is a user added provider */
  483. if (!CRYPTO_THREAD_read_lock(store->lock))
  484. return NULL;
  485. for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
  486. if (strcmp(p->name, name) == 0) {
  487. template = *p;
  488. break;
  489. }
  490. }
  491. CRYPTO_THREAD_unlock(store->lock);
  492. }
  493. } else {
  494. template.init = init_function;
  495. }
  496. if (params != NULL) {
  497. int i;
  498. template.parameters = sk_INFOPAIR_new_null();
  499. if (template.parameters == NULL)
  500. return NULL;
  501. for (i = 0; params[i].key != NULL; i++) {
  502. if (params[i].data_type != OSSL_PARAM_UTF8_STRING)
  503. continue;
  504. if (ossl_provider_info_add_parameter(&template, params[i].key,
  505. (char *)params[i].data) <= 0)
  506. return NULL;
  507. }
  508. }
  509. /* provider_new() generates an error, so no need here */
  510. prov = provider_new(name, template.init, template.parameters);
  511. if (params != NULL) /* We copied the parameters, let's free them */
  512. sk_INFOPAIR_pop_free(template.parameters, infopair_free);
  513. if (prov == NULL)
  514. return NULL;
  515. prov->libctx = libctx;
  516. #ifndef FIPS_MODULE
  517. prov->error_lib = ERR_get_next_error_library();
  518. #endif
  519. /*
  520. * At this point, the provider is only partially "loaded". To be
  521. * fully "loaded", ossl_provider_activate() must also be called and it must
  522. * then be added to the provider store.
  523. */
  524. return prov;
  525. }
  526. /* Assumes that the store lock is held */
  527. static int create_provider_children(OSSL_PROVIDER *prov)
  528. {
  529. int ret = 1;
  530. #ifndef FIPS_MODULE
  531. struct provider_store_st *store = prov->store;
  532. OSSL_PROVIDER_CHILD_CB *child_cb;
  533. int i, max;
  534. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  535. for (i = 0; i < max; i++) {
  536. /*
  537. * This is newly activated (activatecnt == 1), so we need to
  538. * create child providers as necessary.
  539. */
  540. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  541. ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
  542. }
  543. #endif
  544. return ret;
  545. }
  546. int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
  547. int retain_fallbacks)
  548. {
  549. struct provider_store_st *store;
  550. int idx;
  551. OSSL_PROVIDER tmpl = { 0, };
  552. OSSL_PROVIDER *actualtmp = NULL;
  553. if (actualprov != NULL)
  554. *actualprov = NULL;
  555. if ((store = get_provider_store(prov->libctx)) == NULL)
  556. return 0;
  557. if (!CRYPTO_THREAD_write_lock(store->lock))
  558. return 0;
  559. tmpl.name = (char *)prov->name;
  560. idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
  561. if (idx == -1)
  562. actualtmp = prov;
  563. else
  564. actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
  565. if (idx == -1) {
  566. if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
  567. goto err;
  568. prov->store = store;
  569. if (!create_provider_children(prov)) {
  570. sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
  571. goto err;
  572. }
  573. if (!retain_fallbacks)
  574. store->use_fallbacks = 0;
  575. }
  576. CRYPTO_THREAD_unlock(store->lock);
  577. if (actualprov != NULL) {
  578. if (!ossl_provider_up_ref(actualtmp)) {
  579. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  580. actualtmp = NULL;
  581. return 0;
  582. }
  583. *actualprov = actualtmp;
  584. }
  585. if (idx >= 0) {
  586. /*
  587. * The provider is already in the store. Probably two threads
  588. * independently initialised their own provider objects with the same
  589. * name and raced to put them in the store. This thread lost. We
  590. * deactivate the one we just created and use the one that already
  591. * exists instead.
  592. * If we get here then we know we did not create provider children
  593. * above, so we inform ossl_provider_deactivate not to attempt to remove
  594. * any.
  595. */
  596. ossl_provider_deactivate(prov, 0);
  597. ossl_provider_free(prov);
  598. }
  599. #ifndef FIPS_MODULE
  600. else {
  601. /*
  602. * This can be done outside the lock. We tolerate other threads getting
  603. * the wrong result briefly when creating OSSL_DECODER_CTXs.
  604. */
  605. ossl_decoder_cache_flush(prov->libctx);
  606. }
  607. #endif
  608. return 1;
  609. err:
  610. CRYPTO_THREAD_unlock(store->lock);
  611. return 0;
  612. }
  613. void ossl_provider_free(OSSL_PROVIDER *prov)
  614. {
  615. if (prov != NULL) {
  616. int ref = 0;
  617. CRYPTO_DOWN_REF(&prov->refcnt, &ref);
  618. /*
  619. * When the refcount drops to zero, we clean up the provider.
  620. * Note that this also does teardown, which may seem late,
  621. * considering that init happens on first activation. However,
  622. * there may be other structures hanging on to the provider after
  623. * the last deactivation and may therefore need full access to the
  624. * provider's services. Therefore, we deinit late.
  625. */
  626. if (ref == 0) {
  627. if (prov->flag_initialized) {
  628. ossl_provider_teardown(prov);
  629. #ifndef OPENSSL_NO_ERR
  630. # ifndef FIPS_MODULE
  631. if (prov->error_strings != NULL) {
  632. ERR_unload_strings(prov->error_lib, prov->error_strings);
  633. OPENSSL_free(prov->error_strings);
  634. prov->error_strings = NULL;
  635. }
  636. # endif
  637. #endif
  638. OPENSSL_free(prov->operation_bits);
  639. prov->operation_bits = NULL;
  640. prov->operation_bits_sz = 0;
  641. prov->flag_initialized = 0;
  642. }
  643. #ifndef FIPS_MODULE
  644. /*
  645. * We deregister thread handling whether or not the provider was
  646. * initialized. If init was attempted but was not successful then
  647. * the provider may still have registered a thread handler.
  648. */
  649. ossl_init_thread_deregister(prov);
  650. DSO_free(prov->module);
  651. #endif
  652. OPENSSL_free(prov->name);
  653. OPENSSL_free(prov->path);
  654. sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
  655. CRYPTO_THREAD_lock_free(prov->opbits_lock);
  656. CRYPTO_THREAD_lock_free(prov->flag_lock);
  657. #ifndef HAVE_ATOMICS
  658. CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
  659. #endif
  660. CRYPTO_FREE_REF(&prov->refcnt);
  661. OPENSSL_free(prov);
  662. }
  663. #ifndef FIPS_MODULE
  664. else if (prov->ischild) {
  665. ossl_provider_free_parent(prov, 0);
  666. }
  667. #endif
  668. }
  669. }
  670. /* Setters */
  671. int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
  672. {
  673. OPENSSL_free(prov->path);
  674. prov->path = NULL;
  675. if (module_path == NULL)
  676. return 1;
  677. if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
  678. return 1;
  679. return 0;
  680. }
  681. static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
  682. const char *value)
  683. {
  684. INFOPAIR *pair = NULL;
  685. if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL
  686. || (pair->name = OPENSSL_strdup(name)) == NULL
  687. || (pair->value = OPENSSL_strdup(value)) == NULL)
  688. goto err;
  689. if ((*infopairsk == NULL
  690. && (*infopairsk = sk_INFOPAIR_new_null()) == NULL)
  691. || sk_INFOPAIR_push(*infopairsk, pair) <= 0) {
  692. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  693. goto err;
  694. }
  695. return 1;
  696. err:
  697. if (pair != NULL) {
  698. OPENSSL_free(pair->name);
  699. OPENSSL_free(pair->value);
  700. OPENSSL_free(pair);
  701. }
  702. return 0;
  703. }
  704. int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
  705. const char *name, const char *value)
  706. {
  707. return infopair_add(&prov->parameters, name, value);
  708. }
  709. int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
  710. const char *name,
  711. const char *value)
  712. {
  713. return infopair_add(&provinfo->parameters, name, value);
  714. }
  715. /*
  716. * Provider activation.
  717. *
  718. * What "activation" means depends on the provider form; for built in
  719. * providers (in the library or the application alike), the provider
  720. * can already be considered to be loaded, all that's needed is to
  721. * initialize it. However, for dynamically loadable provider modules,
  722. * we must first load that module.
  723. *
  724. * Built in modules are distinguished from dynamically loaded modules
  725. * with an already assigned init function.
  726. */
  727. static const OSSL_DISPATCH *core_dispatch; /* Define further down */
  728. int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
  729. const char *path)
  730. {
  731. struct provider_store_st *store;
  732. char *p = NULL;
  733. if (path != NULL) {
  734. p = OPENSSL_strdup(path);
  735. if (p == NULL)
  736. return 0;
  737. }
  738. if ((store = get_provider_store(libctx)) != NULL
  739. && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
  740. OPENSSL_free(store->default_path);
  741. store->default_path = p;
  742. CRYPTO_THREAD_unlock(store->default_path_lock);
  743. return 1;
  744. }
  745. OPENSSL_free(p);
  746. return 0;
  747. }
  748. const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx)
  749. {
  750. struct provider_store_st *store;
  751. char *path = NULL;
  752. if ((store = get_provider_store(libctx)) != NULL
  753. && CRYPTO_THREAD_read_lock(store->default_path_lock)) {
  754. path = store->default_path;
  755. CRYPTO_THREAD_unlock(store->default_path_lock);
  756. }
  757. return path;
  758. }
  759. /*
  760. * Internal version that doesn't affect the store flags, and thereby avoid
  761. * locking. Direct callers must remember to set the store flags when
  762. * appropriate.
  763. */
  764. static int provider_init(OSSL_PROVIDER *prov)
  765. {
  766. const OSSL_DISPATCH *provider_dispatch = NULL;
  767. void *tmp_provctx = NULL; /* safety measure */
  768. #ifndef OPENSSL_NO_ERR
  769. # ifndef FIPS_MODULE
  770. OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
  771. # endif
  772. #endif
  773. int ok = 0;
  774. if (!ossl_assert(!prov->flag_initialized)) {
  775. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  776. goto end;
  777. }
  778. /*
  779. * If the init function isn't set, it indicates that this provider is
  780. * a loadable module.
  781. */
  782. if (prov->init_function == NULL) {
  783. #ifdef FIPS_MODULE
  784. goto end;
  785. #else
  786. if (prov->module == NULL) {
  787. char *allocated_path = NULL;
  788. const char *module_path = NULL;
  789. char *merged_path = NULL;
  790. const char *load_dir = NULL;
  791. char *allocated_load_dir = NULL;
  792. struct provider_store_st *store;
  793. if ((prov->module = DSO_new()) == NULL) {
  794. /* DSO_new() generates an error already */
  795. goto end;
  796. }
  797. if ((store = get_provider_store(prov->libctx)) == NULL
  798. || !CRYPTO_THREAD_read_lock(store->default_path_lock))
  799. goto end;
  800. if (store->default_path != NULL) {
  801. allocated_load_dir = OPENSSL_strdup(store->default_path);
  802. CRYPTO_THREAD_unlock(store->default_path_lock);
  803. if (allocated_load_dir == NULL)
  804. goto end;
  805. load_dir = allocated_load_dir;
  806. } else {
  807. CRYPTO_THREAD_unlock(store->default_path_lock);
  808. }
  809. if (load_dir == NULL) {
  810. load_dir = ossl_safe_getenv("OPENSSL_MODULES");
  811. if (load_dir == NULL)
  812. load_dir = MODULESDIR;
  813. }
  814. DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
  815. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
  816. module_path = prov->path;
  817. if (module_path == NULL)
  818. module_path = allocated_path =
  819. DSO_convert_filename(prov->module, prov->name);
  820. if (module_path != NULL)
  821. merged_path = DSO_merge(prov->module, module_path, load_dir);
  822. if (merged_path == NULL
  823. || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
  824. DSO_free(prov->module);
  825. prov->module = NULL;
  826. }
  827. OPENSSL_free(merged_path);
  828. OPENSSL_free(allocated_path);
  829. OPENSSL_free(allocated_load_dir);
  830. }
  831. if (prov->module == NULL) {
  832. /* DSO has already recorded errors, this is just a tracepoint */
  833. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
  834. "name=%s", prov->name);
  835. goto end;
  836. }
  837. prov->init_function = (OSSL_provider_init_fn *)
  838. DSO_bind_func(prov->module, "OSSL_provider_init");
  839. #endif
  840. }
  841. /* Check for and call the initialise function for the provider. */
  842. if (prov->init_function == NULL) {
  843. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
  844. "name=%s, provider has no provider init function",
  845. prov->name);
  846. goto end;
  847. }
  848. if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
  849. &provider_dispatch, &tmp_provctx)) {
  850. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
  851. "name=%s", prov->name);
  852. goto end;
  853. }
  854. prov->provctx = tmp_provctx;
  855. prov->dispatch = provider_dispatch;
  856. for (; provider_dispatch->function_id != 0; provider_dispatch++) {
  857. switch (provider_dispatch->function_id) {
  858. case OSSL_FUNC_PROVIDER_TEARDOWN:
  859. prov->teardown =
  860. OSSL_FUNC_provider_teardown(provider_dispatch);
  861. break;
  862. case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
  863. prov->gettable_params =
  864. OSSL_FUNC_provider_gettable_params(provider_dispatch);
  865. break;
  866. case OSSL_FUNC_PROVIDER_GET_PARAMS:
  867. prov->get_params =
  868. OSSL_FUNC_provider_get_params(provider_dispatch);
  869. break;
  870. case OSSL_FUNC_PROVIDER_SELF_TEST:
  871. prov->self_test =
  872. OSSL_FUNC_provider_self_test(provider_dispatch);
  873. break;
  874. case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
  875. prov->get_capabilities =
  876. OSSL_FUNC_provider_get_capabilities(provider_dispatch);
  877. break;
  878. case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
  879. prov->query_operation =
  880. OSSL_FUNC_provider_query_operation(provider_dispatch);
  881. break;
  882. case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
  883. prov->unquery_operation =
  884. OSSL_FUNC_provider_unquery_operation(provider_dispatch);
  885. break;
  886. #ifndef OPENSSL_NO_ERR
  887. # ifndef FIPS_MODULE
  888. case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
  889. p_get_reason_strings =
  890. OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
  891. break;
  892. # endif
  893. #endif
  894. }
  895. }
  896. #ifndef OPENSSL_NO_ERR
  897. # ifndef FIPS_MODULE
  898. if (p_get_reason_strings != NULL) {
  899. const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
  900. size_t cnt, cnt2;
  901. /*
  902. * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
  903. * although they are essentially the same type.
  904. * Furthermore, ERR_load_strings() patches the array's error number
  905. * with the error library number, so we need to make a copy of that
  906. * array either way.
  907. */
  908. cnt = 0;
  909. while (reasonstrings[cnt].id != 0) {
  910. if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
  911. goto end;
  912. cnt++;
  913. }
  914. cnt++; /* One for the terminating item */
  915. /* Allocate one extra item for the "library" name */
  916. prov->error_strings =
  917. OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
  918. if (prov->error_strings == NULL)
  919. goto end;
  920. /*
  921. * Set the "library" name.
  922. */
  923. prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
  924. prov->error_strings[0].string = prov->name;
  925. /*
  926. * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
  927. * 1..cnt.
  928. */
  929. for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
  930. prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
  931. prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
  932. }
  933. ERR_load_strings(prov->error_lib, prov->error_strings);
  934. }
  935. # endif
  936. #endif
  937. /* With this flag set, this provider has become fully "loaded". */
  938. prov->flag_initialized = 1;
  939. ok = 1;
  940. end:
  941. return ok;
  942. }
  943. /*
  944. * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
  945. * parent provider. If removechildren is 0 then we suppress any calls to remove
  946. * child providers.
  947. * Return -1 on failure and the activation count on success
  948. */
  949. static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
  950. int removechildren)
  951. {
  952. int count;
  953. struct provider_store_st *store;
  954. #ifndef FIPS_MODULE
  955. int freeparent = 0;
  956. #endif
  957. int lock = 1;
  958. if (!ossl_assert(prov != NULL))
  959. return -1;
  960. /*
  961. * No need to lock if we've got no store because we've not been shared with
  962. * other threads.
  963. */
  964. store = get_provider_store(prov->libctx);
  965. if (store == NULL)
  966. lock = 0;
  967. if (lock && !CRYPTO_THREAD_read_lock(store->lock))
  968. return -1;
  969. if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  970. CRYPTO_THREAD_unlock(store->lock);
  971. return -1;
  972. }
  973. CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock);
  974. #ifndef FIPS_MODULE
  975. if (count >= 1 && prov->ischild && upcalls) {
  976. /*
  977. * We have had a direct activation in this child libctx so we need to
  978. * now down the ref count in the parent provider. We do the actual down
  979. * ref outside of the flag_lock, since it could involve getting other
  980. * locks.
  981. */
  982. freeparent = 1;
  983. }
  984. #endif
  985. if (count < 1)
  986. prov->flag_activated = 0;
  987. #ifndef FIPS_MODULE
  988. else
  989. removechildren = 0;
  990. #endif
  991. #ifndef FIPS_MODULE
  992. if (removechildren && store != NULL) {
  993. int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  994. OSSL_PROVIDER_CHILD_CB *child_cb;
  995. for (i = 0; i < max; i++) {
  996. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  997. child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
  998. }
  999. }
  1000. #endif
  1001. if (lock) {
  1002. CRYPTO_THREAD_unlock(prov->flag_lock);
  1003. CRYPTO_THREAD_unlock(store->lock);
  1004. /*
  1005. * This can be done outside the lock. We tolerate other threads getting
  1006. * the wrong result briefly when creating OSSL_DECODER_CTXs.
  1007. */
  1008. #ifndef FIPS_MODULE
  1009. if (count < 1)
  1010. ossl_decoder_cache_flush(prov->libctx);
  1011. #endif
  1012. }
  1013. #ifndef FIPS_MODULE
  1014. if (freeparent)
  1015. ossl_provider_free_parent(prov, 1);
  1016. #endif
  1017. /* We don't deinit here, that's done in ossl_provider_free() */
  1018. return count;
  1019. }
  1020. /*
  1021. * Activate a provider.
  1022. * Return -1 on failure and the activation count on success
  1023. */
  1024. static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
  1025. {
  1026. int count = -1;
  1027. struct provider_store_st *store;
  1028. int ret = 1;
  1029. store = prov->store;
  1030. /*
  1031. * If the provider hasn't been added to the store, then we don't need
  1032. * any locks because we've not shared it with other threads.
  1033. */
  1034. if (store == NULL) {
  1035. lock = 0;
  1036. if (!provider_init(prov))
  1037. return -1;
  1038. }
  1039. #ifndef FIPS_MODULE
  1040. if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
  1041. return -1;
  1042. #endif
  1043. if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
  1044. #ifndef FIPS_MODULE
  1045. if (prov->ischild && upcalls)
  1046. ossl_provider_free_parent(prov, 1);
  1047. #endif
  1048. return -1;
  1049. }
  1050. if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  1051. CRYPTO_THREAD_unlock(store->lock);
  1052. #ifndef FIPS_MODULE
  1053. if (prov->ischild && upcalls)
  1054. ossl_provider_free_parent(prov, 1);
  1055. #endif
  1056. return -1;
  1057. }
  1058. if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
  1059. prov->flag_activated = 1;
  1060. if (count == 1 && store != NULL) {
  1061. ret = create_provider_children(prov);
  1062. }
  1063. }
  1064. if (lock) {
  1065. CRYPTO_THREAD_unlock(prov->flag_lock);
  1066. CRYPTO_THREAD_unlock(store->lock);
  1067. /*
  1068. * This can be done outside the lock. We tolerate other threads getting
  1069. * the wrong result briefly when creating OSSL_DECODER_CTXs.
  1070. */
  1071. #ifndef FIPS_MODULE
  1072. if (count == 1)
  1073. ossl_decoder_cache_flush(prov->libctx);
  1074. #endif
  1075. }
  1076. if (!ret)
  1077. return -1;
  1078. return count;
  1079. }
  1080. static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
  1081. {
  1082. struct provider_store_st *store;
  1083. int freeing;
  1084. if ((store = get_provider_store(prov->libctx)) == NULL)
  1085. return 0;
  1086. if (!CRYPTO_THREAD_read_lock(store->lock))
  1087. return 0;
  1088. freeing = store->freeing;
  1089. CRYPTO_THREAD_unlock(store->lock);
  1090. if (!freeing) {
  1091. int acc
  1092. = evp_method_store_cache_flush(prov->libctx)
  1093. #ifndef FIPS_MODULE
  1094. + ossl_encoder_store_cache_flush(prov->libctx)
  1095. + ossl_decoder_store_cache_flush(prov->libctx)
  1096. + ossl_store_loader_store_cache_flush(prov->libctx)
  1097. #endif
  1098. ;
  1099. #ifndef FIPS_MODULE
  1100. return acc == 4;
  1101. #else
  1102. return acc == 1;
  1103. #endif
  1104. }
  1105. return 1;
  1106. }
  1107. static int provider_remove_store_methods(OSSL_PROVIDER *prov)
  1108. {
  1109. struct provider_store_st *store;
  1110. int freeing;
  1111. if ((store = get_provider_store(prov->libctx)) == NULL)
  1112. return 0;
  1113. if (!CRYPTO_THREAD_read_lock(store->lock))
  1114. return 0;
  1115. freeing = store->freeing;
  1116. CRYPTO_THREAD_unlock(store->lock);
  1117. if (!freeing) {
  1118. int acc;
  1119. if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
  1120. return 0;
  1121. OPENSSL_free(prov->operation_bits);
  1122. prov->operation_bits = NULL;
  1123. prov->operation_bits_sz = 0;
  1124. CRYPTO_THREAD_unlock(prov->opbits_lock);
  1125. acc = evp_method_store_remove_all_provided(prov)
  1126. #ifndef FIPS_MODULE
  1127. + ossl_encoder_store_remove_all_provided(prov)
  1128. + ossl_decoder_store_remove_all_provided(prov)
  1129. + ossl_store_loader_store_remove_all_provided(prov)
  1130. #endif
  1131. ;
  1132. #ifndef FIPS_MODULE
  1133. return acc == 4;
  1134. #else
  1135. return acc == 1;
  1136. #endif
  1137. }
  1138. return 1;
  1139. }
  1140. int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
  1141. {
  1142. int count;
  1143. if (prov == NULL)
  1144. return 0;
  1145. #ifndef FIPS_MODULE
  1146. /*
  1147. * If aschild is true, then we only actually do the activation if the
  1148. * provider is a child. If its not, this is still success.
  1149. */
  1150. if (aschild && !prov->ischild)
  1151. return 1;
  1152. #endif
  1153. if ((count = provider_activate(prov, 1, upcalls)) > 0)
  1154. return count == 1 ? provider_flush_store_cache(prov) : 1;
  1155. return 0;
  1156. }
  1157. int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
  1158. {
  1159. int count;
  1160. if (prov == NULL
  1161. || (count = provider_deactivate(prov, 1, removechildren)) < 0)
  1162. return 0;
  1163. return count == 0 ? provider_remove_store_methods(prov) : 1;
  1164. }
  1165. void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
  1166. {
  1167. return prov != NULL ? prov->provctx : NULL;
  1168. }
  1169. /*
  1170. * This function only does something once when store->use_fallbacks == 1,
  1171. * and then sets store->use_fallbacks = 0, so the second call and so on is
  1172. * effectively a no-op.
  1173. */
  1174. static int provider_activate_fallbacks(struct provider_store_st *store)
  1175. {
  1176. int use_fallbacks;
  1177. int activated_fallback_count = 0;
  1178. int ret = 0;
  1179. const OSSL_PROVIDER_INFO *p;
  1180. if (!CRYPTO_THREAD_read_lock(store->lock))
  1181. return 0;
  1182. use_fallbacks = store->use_fallbacks;
  1183. CRYPTO_THREAD_unlock(store->lock);
  1184. if (!use_fallbacks)
  1185. return 1;
  1186. if (!CRYPTO_THREAD_write_lock(store->lock))
  1187. return 0;
  1188. /* Check again, just in case another thread changed it */
  1189. use_fallbacks = store->use_fallbacks;
  1190. if (!use_fallbacks) {
  1191. CRYPTO_THREAD_unlock(store->lock);
  1192. return 1;
  1193. }
  1194. for (p = ossl_predefined_providers; p->name != NULL; p++) {
  1195. OSSL_PROVIDER *prov = NULL;
  1196. if (!p->is_fallback)
  1197. continue;
  1198. /*
  1199. * We use the internal constructor directly here,
  1200. * otherwise we get a call loop
  1201. */
  1202. prov = provider_new(p->name, p->init, NULL);
  1203. if (prov == NULL)
  1204. goto err;
  1205. prov->libctx = store->libctx;
  1206. #ifndef FIPS_MODULE
  1207. prov->error_lib = ERR_get_next_error_library();
  1208. #endif
  1209. /*
  1210. * We are calling provider_activate while holding the store lock. This
  1211. * means the init function will be called while holding a lock. Normally
  1212. * we try to avoid calling a user callback while holding a lock.
  1213. * However, fallbacks are never third party providers so we accept this.
  1214. */
  1215. if (provider_activate(prov, 0, 0) < 0) {
  1216. ossl_provider_free(prov);
  1217. goto err;
  1218. }
  1219. prov->store = store;
  1220. if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  1221. ossl_provider_free(prov);
  1222. goto err;
  1223. }
  1224. activated_fallback_count++;
  1225. }
  1226. if (activated_fallback_count > 0) {
  1227. store->use_fallbacks = 0;
  1228. ret = 1;
  1229. }
  1230. err:
  1231. CRYPTO_THREAD_unlock(store->lock);
  1232. return ret;
  1233. }
  1234. int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
  1235. int (*cb)(OSSL_PROVIDER *provider,
  1236. void *cbdata),
  1237. void *cbdata)
  1238. {
  1239. int ret = 0, curr, max, ref = 0;
  1240. struct provider_store_st *store = get_provider_store(ctx);
  1241. STACK_OF(OSSL_PROVIDER) *provs = NULL;
  1242. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
  1243. /*
  1244. * Make sure any providers are loaded from config before we try to use
  1245. * them.
  1246. */
  1247. if (ossl_lib_ctx_is_default(ctx))
  1248. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  1249. #endif
  1250. if (store == NULL)
  1251. return 1;
  1252. if (!provider_activate_fallbacks(store))
  1253. return 0;
  1254. /*
  1255. * Under lock, grab a copy of the provider list and up_ref each
  1256. * provider so that they don't disappear underneath us.
  1257. */
  1258. if (!CRYPTO_THREAD_read_lock(store->lock))
  1259. return 0;
  1260. provs = sk_OSSL_PROVIDER_dup(store->providers);
  1261. if (provs == NULL) {
  1262. CRYPTO_THREAD_unlock(store->lock);
  1263. return 0;
  1264. }
  1265. max = sk_OSSL_PROVIDER_num(provs);
  1266. /*
  1267. * We work backwards through the stack so that we can safely delete items
  1268. * as we go.
  1269. */
  1270. for (curr = max - 1; curr >= 0; curr--) {
  1271. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  1272. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1273. goto err_unlock;
  1274. if (prov->flag_activated) {
  1275. /*
  1276. * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
  1277. * to avoid upping the ref count on the parent provider, which we
  1278. * must not do while holding locks.
  1279. */
  1280. if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
  1281. CRYPTO_THREAD_unlock(prov->flag_lock);
  1282. goto err_unlock;
  1283. }
  1284. /*
  1285. * It's already activated, but we up the activated count to ensure
  1286. * it remains activated until after we've called the user callback.
  1287. * In theory this could mean the parent provider goes inactive,
  1288. * whilst still activated in the child for a short period. That's ok.
  1289. */
  1290. if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
  1291. prov->activatecnt_lock)) {
  1292. CRYPTO_DOWN_REF(&prov->refcnt, &ref);
  1293. CRYPTO_THREAD_unlock(prov->flag_lock);
  1294. goto err_unlock;
  1295. }
  1296. } else {
  1297. sk_OSSL_PROVIDER_delete(provs, curr);
  1298. max--;
  1299. }
  1300. CRYPTO_THREAD_unlock(prov->flag_lock);
  1301. }
  1302. CRYPTO_THREAD_unlock(store->lock);
  1303. /*
  1304. * Now, we sweep through all providers not under lock
  1305. */
  1306. for (curr = 0; curr < max; curr++) {
  1307. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  1308. if (!cb(prov, cbdata)) {
  1309. curr = -1;
  1310. goto finish;
  1311. }
  1312. }
  1313. curr = -1;
  1314. ret = 1;
  1315. goto finish;
  1316. err_unlock:
  1317. CRYPTO_THREAD_unlock(store->lock);
  1318. finish:
  1319. /*
  1320. * The pop_free call doesn't do what we want on an error condition. We
  1321. * either start from the first item in the stack, or part way through if
  1322. * we only processed some of the items.
  1323. */
  1324. for (curr++; curr < max; curr++) {
  1325. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  1326. if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
  1327. prov->activatecnt_lock)) {
  1328. ret = 0;
  1329. continue;
  1330. }
  1331. if (ref < 1) {
  1332. /*
  1333. * Looks like we need to deactivate properly. We could just have
  1334. * done this originally, but it involves taking a write lock so
  1335. * we avoid it. We up the count again and do a full deactivation
  1336. */
  1337. if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
  1338. prov->activatecnt_lock))
  1339. provider_deactivate(prov, 0, 1);
  1340. else
  1341. ret = 0;
  1342. }
  1343. /*
  1344. * As above where we did the up-ref, we don't call ossl_provider_free
  1345. * to avoid making upcalls. There should always be at least one ref
  1346. * to the provider in the store, so this should never drop to 0.
  1347. */
  1348. if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
  1349. ret = 0;
  1350. continue;
  1351. }
  1352. /*
  1353. * Not much we can do if this assert ever fails. So we don't use
  1354. * ossl_assert here.
  1355. */
  1356. assert(ref > 0);
  1357. }
  1358. sk_OSSL_PROVIDER_free(provs);
  1359. return ret;
  1360. }
  1361. int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
  1362. {
  1363. OSSL_PROVIDER *prov = NULL;
  1364. int available = 0;
  1365. struct provider_store_st *store = get_provider_store(libctx);
  1366. if (store == NULL || !provider_activate_fallbacks(store))
  1367. return 0;
  1368. prov = ossl_provider_find(libctx, name, 0);
  1369. if (prov != NULL) {
  1370. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1371. return 0;
  1372. available = prov->flag_activated;
  1373. CRYPTO_THREAD_unlock(prov->flag_lock);
  1374. ossl_provider_free(prov);
  1375. }
  1376. return available;
  1377. }
  1378. /* Getters of Provider Object data */
  1379. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  1380. {
  1381. return prov->name;
  1382. }
  1383. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  1384. {
  1385. return prov->module;
  1386. }
  1387. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  1388. {
  1389. #ifdef FIPS_MODULE
  1390. return NULL;
  1391. #else
  1392. return DSO_get_filename(prov->module);
  1393. #endif
  1394. }
  1395. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  1396. {
  1397. #ifdef FIPS_MODULE
  1398. return NULL;
  1399. #else
  1400. /* FIXME: Ensure it's a full path */
  1401. return DSO_get_filename(prov->module);
  1402. #endif
  1403. }
  1404. void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
  1405. {
  1406. if (prov != NULL)
  1407. return prov->provctx;
  1408. return NULL;
  1409. }
  1410. const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
  1411. {
  1412. if (prov != NULL)
  1413. return prov->dispatch;
  1414. return NULL;
  1415. }
  1416. OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
  1417. {
  1418. return prov != NULL ? prov->libctx : NULL;
  1419. }
  1420. /* Wrappers around calls to the provider */
  1421. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  1422. {
  1423. if (prov->teardown != NULL
  1424. #ifndef FIPS_MODULE
  1425. && !prov->ischild
  1426. #endif
  1427. )
  1428. prov->teardown(prov->provctx);
  1429. }
  1430. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  1431. {
  1432. return prov->gettable_params == NULL
  1433. ? NULL : prov->gettable_params(prov->provctx);
  1434. }
  1435. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  1436. {
  1437. return prov->get_params == NULL
  1438. ? 0 : prov->get_params(prov->provctx, params);
  1439. }
  1440. int ossl_provider_self_test(const OSSL_PROVIDER *prov)
  1441. {
  1442. int ret;
  1443. if (prov->self_test == NULL)
  1444. return 1;
  1445. ret = prov->self_test(prov->provctx);
  1446. if (ret == 0)
  1447. (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
  1448. return ret;
  1449. }
  1450. int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
  1451. const char *capability,
  1452. OSSL_CALLBACK *cb,
  1453. void *arg)
  1454. {
  1455. return prov->get_capabilities == NULL
  1456. ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
  1457. }
  1458. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  1459. int operation_id,
  1460. int *no_cache)
  1461. {
  1462. const OSSL_ALGORITHM *res;
  1463. if (prov->query_operation == NULL)
  1464. return NULL;
  1465. res = prov->query_operation(prov->provctx, operation_id, no_cache);
  1466. #if defined(OPENSSL_NO_CACHED_FETCH)
  1467. /* Forcing the non-caching of queries */
  1468. if (no_cache != NULL)
  1469. *no_cache = 1;
  1470. #endif
  1471. return res;
  1472. }
  1473. void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
  1474. int operation_id,
  1475. const OSSL_ALGORITHM *algs)
  1476. {
  1477. if (prov->unquery_operation != NULL)
  1478. prov->unquery_operation(prov->provctx, operation_id, algs);
  1479. }
  1480. int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
  1481. {
  1482. size_t byte = bitnum / 8;
  1483. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1484. if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
  1485. return 0;
  1486. if (provider->operation_bits_sz <= byte) {
  1487. unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
  1488. byte + 1);
  1489. if (tmp == NULL) {
  1490. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1491. return 0;
  1492. }
  1493. provider->operation_bits = tmp;
  1494. memset(provider->operation_bits + provider->operation_bits_sz,
  1495. '\0', byte + 1 - provider->operation_bits_sz);
  1496. provider->operation_bits_sz = byte + 1;
  1497. }
  1498. provider->operation_bits[byte] |= bit;
  1499. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1500. return 1;
  1501. }
  1502. int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
  1503. int *result)
  1504. {
  1505. size_t byte = bitnum / 8;
  1506. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1507. if (!ossl_assert(result != NULL)) {
  1508. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  1509. return 0;
  1510. }
  1511. *result = 0;
  1512. if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
  1513. return 0;
  1514. if (provider->operation_bits_sz > byte)
  1515. *result = ((provider->operation_bits[byte] & bit) != 0);
  1516. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1517. return 1;
  1518. }
  1519. #ifndef FIPS_MODULE
  1520. const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
  1521. {
  1522. return prov->handle;
  1523. }
  1524. int ossl_provider_is_child(const OSSL_PROVIDER *prov)
  1525. {
  1526. return prov->ischild;
  1527. }
  1528. int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
  1529. {
  1530. prov->handle = handle;
  1531. prov->ischild = 1;
  1532. return 1;
  1533. }
  1534. int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
  1535. {
  1536. #ifndef FIPS_MODULE
  1537. struct provider_store_st *store = NULL;
  1538. int i, max;
  1539. OSSL_PROVIDER_CHILD_CB *child_cb;
  1540. if ((store = get_provider_store(libctx)) == NULL)
  1541. return 0;
  1542. if (!CRYPTO_THREAD_read_lock(store->lock))
  1543. return 0;
  1544. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1545. for (i = 0; i < max; i++) {
  1546. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1547. child_cb->global_props_cb(props, child_cb->cbdata);
  1548. }
  1549. CRYPTO_THREAD_unlock(store->lock);
  1550. #endif
  1551. return 1;
  1552. }
  1553. static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
  1554. int (*create_cb)(
  1555. const OSSL_CORE_HANDLE *provider,
  1556. void *cbdata),
  1557. int (*remove_cb)(
  1558. const OSSL_CORE_HANDLE *provider,
  1559. void *cbdata),
  1560. int (*global_props_cb)(
  1561. const char *props,
  1562. void *cbdata),
  1563. void *cbdata)
  1564. {
  1565. /*
  1566. * This is really an OSSL_PROVIDER that we created and cast to
  1567. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1568. */
  1569. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1570. OSSL_PROVIDER *prov;
  1571. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1572. struct provider_store_st *store = NULL;
  1573. int ret = 0, i, max;
  1574. OSSL_PROVIDER_CHILD_CB *child_cb;
  1575. char *propsstr = NULL;
  1576. if ((store = get_provider_store(libctx)) == NULL)
  1577. return 0;
  1578. child_cb = OPENSSL_malloc(sizeof(*child_cb));
  1579. if (child_cb == NULL)
  1580. return 0;
  1581. child_cb->prov = thisprov;
  1582. child_cb->create_cb = create_cb;
  1583. child_cb->remove_cb = remove_cb;
  1584. child_cb->global_props_cb = global_props_cb;
  1585. child_cb->cbdata = cbdata;
  1586. if (!CRYPTO_THREAD_write_lock(store->lock)) {
  1587. OPENSSL_free(child_cb);
  1588. return 0;
  1589. }
  1590. propsstr = evp_get_global_properties_str(libctx, 0);
  1591. if (propsstr != NULL) {
  1592. global_props_cb(propsstr, cbdata);
  1593. OPENSSL_free(propsstr);
  1594. }
  1595. max = sk_OSSL_PROVIDER_num(store->providers);
  1596. for (i = 0; i < max; i++) {
  1597. int activated;
  1598. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1599. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1600. break;
  1601. activated = prov->flag_activated;
  1602. CRYPTO_THREAD_unlock(prov->flag_lock);
  1603. /*
  1604. * We hold the store lock while calling the user callback. This means
  1605. * that the user callback must be short and simple and not do anything
  1606. * likely to cause a deadlock. We don't hold the flag_lock during this
  1607. * call. In theory this means that another thread could deactivate it
  1608. * while we are calling create. This is ok because the other thread
  1609. * will also call remove_cb, but won't be able to do so until we release
  1610. * the store lock.
  1611. */
  1612. if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
  1613. break;
  1614. }
  1615. if (i == max) {
  1616. /* Success */
  1617. ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
  1618. }
  1619. if (i != max || ret <= 0) {
  1620. /* Failed during creation. Remove everything we just added */
  1621. for (; i >= 0; i--) {
  1622. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1623. remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
  1624. }
  1625. OPENSSL_free(child_cb);
  1626. ret = 0;
  1627. }
  1628. CRYPTO_THREAD_unlock(store->lock);
  1629. return ret;
  1630. }
  1631. static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
  1632. {
  1633. /*
  1634. * This is really an OSSL_PROVIDER that we created and cast to
  1635. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1636. */
  1637. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1638. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1639. struct provider_store_st *store = NULL;
  1640. int i, max;
  1641. OSSL_PROVIDER_CHILD_CB *child_cb;
  1642. if ((store = get_provider_store(libctx)) == NULL)
  1643. return;
  1644. if (!CRYPTO_THREAD_write_lock(store->lock))
  1645. return;
  1646. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1647. for (i = 0; i < max; i++) {
  1648. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1649. if (child_cb->prov == thisprov) {
  1650. /* Found an entry */
  1651. sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
  1652. OPENSSL_free(child_cb);
  1653. break;
  1654. }
  1655. }
  1656. CRYPTO_THREAD_unlock(store->lock);
  1657. }
  1658. #endif
  1659. /*-
  1660. * Core functions for the provider
  1661. * ===============================
  1662. *
  1663. * This is the set of functions that the core makes available to the provider
  1664. */
  1665. /*
  1666. * This returns a list of Provider Object parameters with their types, for
  1667. * discovery. We do not expect that many providers will use this, but one
  1668. * never knows.
  1669. */
  1670. static const OSSL_PARAM param_types[] = {
  1671. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  1672. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
  1673. NULL, 0),
  1674. #ifndef FIPS_MODULE
  1675. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
  1676. NULL, 0),
  1677. #endif
  1678. OSSL_PARAM_END
  1679. };
  1680. /*
  1681. * Forward declare all the functions that are provided aa dispatch.
  1682. * This ensures that the compiler will complain if they aren't defined
  1683. * with the correct signature.
  1684. */
  1685. static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
  1686. static OSSL_FUNC_core_get_params_fn core_get_params;
  1687. static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
  1688. static OSSL_FUNC_core_thread_start_fn core_thread_start;
  1689. #ifndef FIPS_MODULE
  1690. static OSSL_FUNC_core_new_error_fn core_new_error;
  1691. static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
  1692. static OSSL_FUNC_core_vset_error_fn core_vset_error;
  1693. static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
  1694. static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
  1695. static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
  1696. OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
  1697. OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
  1698. OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
  1699. OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
  1700. OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
  1701. OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
  1702. OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
  1703. OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
  1704. OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
  1705. OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
  1706. static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
  1707. static OSSL_FUNC_get_entropy_fn rand_get_entropy;
  1708. static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
  1709. static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
  1710. static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
  1711. static OSSL_FUNC_get_nonce_fn rand_get_nonce;
  1712. static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
  1713. static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
  1714. static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
  1715. #endif
  1716. OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
  1717. OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
  1718. OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
  1719. OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
  1720. OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
  1721. OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
  1722. OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
  1723. OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
  1724. OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
  1725. OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
  1726. OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
  1727. OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
  1728. #ifndef FIPS_MODULE
  1729. OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
  1730. OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
  1731. static OSSL_FUNC_provider_name_fn core_provider_get0_name;
  1732. static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
  1733. static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
  1734. static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
  1735. static OSSL_FUNC_provider_free_fn core_provider_free_intern;
  1736. static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
  1737. static OSSL_FUNC_core_obj_create_fn core_obj_create;
  1738. #endif
  1739. static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
  1740. {
  1741. return param_types;
  1742. }
  1743. static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
  1744. {
  1745. int i;
  1746. OSSL_PARAM *p;
  1747. /*
  1748. * We created this object originally and we know it is actually an
  1749. * OSSL_PROVIDER *, so the cast is safe
  1750. */
  1751. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1752. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
  1753. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  1754. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
  1755. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  1756. #ifndef FIPS_MODULE
  1757. if ((p = OSSL_PARAM_locate(params,
  1758. OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
  1759. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  1760. #endif
  1761. if (prov->parameters == NULL)
  1762. return 1;
  1763. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  1764. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  1765. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  1766. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  1767. }
  1768. return 1;
  1769. }
  1770. static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
  1771. {
  1772. /*
  1773. * We created this object originally and we know it is actually an
  1774. * OSSL_PROVIDER *, so the cast is safe
  1775. */
  1776. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1777. /*
  1778. * Using ossl_provider_libctx would be wrong as that returns
  1779. * NULL for |prov| == NULL and NULL libctx has a special meaning
  1780. * that does not apply here. Here |prov| == NULL can happen only in
  1781. * case of a coding error.
  1782. */
  1783. assert(prov != NULL);
  1784. return (OPENSSL_CORE_CTX *)prov->libctx;
  1785. }
  1786. static int core_thread_start(const OSSL_CORE_HANDLE *handle,
  1787. OSSL_thread_stop_handler_fn handfn,
  1788. void *arg)
  1789. {
  1790. /*
  1791. * We created this object originally and we know it is actually an
  1792. * OSSL_PROVIDER *, so the cast is safe
  1793. */
  1794. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1795. return ossl_init_thread_start(prov, arg, handfn);
  1796. }
  1797. /*
  1798. * The FIPS module inner provider doesn't implement these. They aren't
  1799. * needed there, since the FIPS module upcalls are always the outer provider
  1800. * ones.
  1801. */
  1802. #ifndef FIPS_MODULE
  1803. /*
  1804. * These error functions should use |handle| to select the proper
  1805. * library context to report in the correct error stack if error
  1806. * stacks become tied to the library context.
  1807. * We cannot currently do that since there's no support for it in the
  1808. * ERR subsystem.
  1809. */
  1810. static void core_new_error(const OSSL_CORE_HANDLE *handle)
  1811. {
  1812. ERR_new();
  1813. }
  1814. static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  1815. const char *file, int line, const char *func)
  1816. {
  1817. ERR_set_debug(file, line, func);
  1818. }
  1819. static void core_vset_error(const OSSL_CORE_HANDLE *handle,
  1820. uint32_t reason, const char *fmt, va_list args)
  1821. {
  1822. /*
  1823. * We created this object originally and we know it is actually an
  1824. * OSSL_PROVIDER *, so the cast is safe
  1825. */
  1826. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1827. /*
  1828. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  1829. * error and will be treated as such. Otherwise, it's a new style
  1830. * provider error and will be treated as such.
  1831. */
  1832. if (ERR_GET_LIB(reason) != 0) {
  1833. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  1834. } else {
  1835. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  1836. }
  1837. }
  1838. static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
  1839. {
  1840. return ERR_set_mark();
  1841. }
  1842. static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
  1843. {
  1844. return ERR_clear_last_mark();
  1845. }
  1846. static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
  1847. {
  1848. return ERR_pop_to_mark();
  1849. }
  1850. static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
  1851. OSSL_CALLBACK **cb, void **cbarg)
  1852. {
  1853. OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
  1854. }
  1855. static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
  1856. unsigned char **pout, int entropy,
  1857. size_t min_len, size_t max_len)
  1858. {
  1859. return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1860. pout, entropy, min_len, max_len);
  1861. }
  1862. static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
  1863. unsigned char **pout, int entropy,
  1864. size_t min_len, size_t max_len)
  1865. {
  1866. return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1867. pout, entropy, min_len, max_len);
  1868. }
  1869. static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
  1870. unsigned char *buf, size_t len)
  1871. {
  1872. ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1873. buf, len);
  1874. }
  1875. static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
  1876. unsigned char *buf, size_t len)
  1877. {
  1878. ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1879. buf, len);
  1880. }
  1881. static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
  1882. unsigned char **pout,
  1883. size_t min_len, size_t max_len,
  1884. const void *salt, size_t salt_len)
  1885. {
  1886. return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1887. pout, min_len, max_len, salt, salt_len);
  1888. }
  1889. static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
  1890. unsigned char **pout,
  1891. size_t min_len, size_t max_len,
  1892. const void *salt, size_t salt_len)
  1893. {
  1894. return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1895. pout, min_len, max_len, salt, salt_len);
  1896. }
  1897. static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
  1898. unsigned char *buf, size_t len)
  1899. {
  1900. ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1901. buf, len);
  1902. }
  1903. static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
  1904. unsigned char *buf, size_t len)
  1905. {
  1906. ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1907. buf, len);
  1908. }
  1909. static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
  1910. {
  1911. return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
  1912. }
  1913. static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
  1914. {
  1915. return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
  1916. }
  1917. static const OSSL_DISPATCH *
  1918. core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
  1919. {
  1920. return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
  1921. }
  1922. static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
  1923. int activate)
  1924. {
  1925. return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
  1926. }
  1927. static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
  1928. int deactivate)
  1929. {
  1930. return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
  1931. }
  1932. static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
  1933. const char *sign_name, const char *digest_name,
  1934. const char *pkey_name)
  1935. {
  1936. int sign_nid = OBJ_txt2nid(sign_name);
  1937. int digest_nid = NID_undef;
  1938. int pkey_nid = OBJ_txt2nid(pkey_name);
  1939. if (digest_name != NULL && digest_name[0] != '\0'
  1940. && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
  1941. return 0;
  1942. if (sign_nid == NID_undef)
  1943. return 0;
  1944. /*
  1945. * Check if it already exists. This is a success if so (even if we don't
  1946. * have nids for the digest/pkey)
  1947. */
  1948. if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
  1949. return 1;
  1950. if (pkey_nid == NID_undef)
  1951. return 0;
  1952. return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
  1953. }
  1954. static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
  1955. const char *sn, const char *ln)
  1956. {
  1957. /* Check if it already exists and create it if not */
  1958. return OBJ_txt2nid(oid) != NID_undef
  1959. || OBJ_create(oid, sn, ln) != NID_undef;
  1960. }
  1961. #endif /* FIPS_MODULE */
  1962. /*
  1963. * Functions provided by the core.
  1964. */
  1965. static const OSSL_DISPATCH core_dispatch_[] = {
  1966. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  1967. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  1968. { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
  1969. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  1970. #ifndef FIPS_MODULE
  1971. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  1972. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  1973. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  1974. { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
  1975. { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
  1976. (void (*)(void))core_clear_last_error_mark },
  1977. { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
  1978. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
  1979. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
  1980. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
  1981. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
  1982. { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
  1983. { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
  1984. { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
  1985. { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
  1986. { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
  1987. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
  1988. { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
  1989. { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
  1990. { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
  1991. { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
  1992. { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
  1993. { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
  1994. { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
  1995. { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
  1996. { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
  1997. { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
  1998. #endif
  1999. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  2000. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  2001. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  2002. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  2003. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  2004. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  2005. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  2006. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  2007. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  2008. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  2009. (void (*)(void))CRYPTO_secure_clear_free },
  2010. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  2011. (void (*)(void))CRYPTO_secure_allocated },
  2012. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  2013. #ifndef FIPS_MODULE
  2014. { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
  2015. (void (*)(void))ossl_provider_register_child_cb },
  2016. { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
  2017. (void (*)(void))ossl_provider_deregister_child_cb },
  2018. { OSSL_FUNC_PROVIDER_NAME,
  2019. (void (*)(void))core_provider_get0_name },
  2020. { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
  2021. (void (*)(void))core_provider_get0_provider_ctx },
  2022. { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
  2023. (void (*)(void))core_provider_get0_dispatch },
  2024. { OSSL_FUNC_PROVIDER_UP_REF,
  2025. (void (*)(void))core_provider_up_ref_intern },
  2026. { OSSL_FUNC_PROVIDER_FREE,
  2027. (void (*)(void))core_provider_free_intern },
  2028. { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
  2029. { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
  2030. #endif
  2031. OSSL_DISPATCH_END
  2032. };
  2033. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;