provider_core.c 73 KB

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