provider_core.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  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. if (provider_dispatch != NULL) {
  857. for (; provider_dispatch->function_id != 0; provider_dispatch++) {
  858. switch (provider_dispatch->function_id) {
  859. case OSSL_FUNC_PROVIDER_TEARDOWN:
  860. prov->teardown =
  861. OSSL_FUNC_provider_teardown(provider_dispatch);
  862. break;
  863. case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
  864. prov->gettable_params =
  865. OSSL_FUNC_provider_gettable_params(provider_dispatch);
  866. break;
  867. case OSSL_FUNC_PROVIDER_GET_PARAMS:
  868. prov->get_params =
  869. OSSL_FUNC_provider_get_params(provider_dispatch);
  870. break;
  871. case OSSL_FUNC_PROVIDER_SELF_TEST:
  872. prov->self_test =
  873. OSSL_FUNC_provider_self_test(provider_dispatch);
  874. break;
  875. case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
  876. prov->get_capabilities =
  877. OSSL_FUNC_provider_get_capabilities(provider_dispatch);
  878. break;
  879. case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
  880. prov->query_operation =
  881. OSSL_FUNC_provider_query_operation(provider_dispatch);
  882. break;
  883. case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
  884. prov->unquery_operation =
  885. OSSL_FUNC_provider_unquery_operation(provider_dispatch);
  886. break;
  887. #ifndef OPENSSL_NO_ERR
  888. # ifndef FIPS_MODULE
  889. case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
  890. p_get_reason_strings =
  891. OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
  892. break;
  893. # endif
  894. #endif
  895. }
  896. }
  897. }
  898. #ifndef OPENSSL_NO_ERR
  899. # ifndef FIPS_MODULE
  900. if (p_get_reason_strings != NULL) {
  901. const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
  902. size_t cnt, cnt2;
  903. /*
  904. * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
  905. * although they are essentially the same type.
  906. * Furthermore, ERR_load_strings() patches the array's error number
  907. * with the error library number, so we need to make a copy of that
  908. * array either way.
  909. */
  910. cnt = 0;
  911. while (reasonstrings[cnt].id != 0) {
  912. if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
  913. goto end;
  914. cnt++;
  915. }
  916. cnt++; /* One for the terminating item */
  917. /* Allocate one extra item for the "library" name */
  918. prov->error_strings =
  919. OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
  920. if (prov->error_strings == NULL)
  921. goto end;
  922. /*
  923. * Set the "library" name.
  924. */
  925. prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
  926. prov->error_strings[0].string = prov->name;
  927. /*
  928. * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
  929. * 1..cnt.
  930. */
  931. for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
  932. prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
  933. prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
  934. }
  935. ERR_load_strings(prov->error_lib, prov->error_strings);
  936. }
  937. # endif
  938. #endif
  939. /* With this flag set, this provider has become fully "loaded". */
  940. prov->flag_initialized = 1;
  941. ok = 1;
  942. end:
  943. return ok;
  944. }
  945. /*
  946. * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
  947. * parent provider. If removechildren is 0 then we suppress any calls to remove
  948. * child providers.
  949. * Return -1 on failure and the activation count on success
  950. */
  951. static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
  952. int removechildren)
  953. {
  954. int count;
  955. struct provider_store_st *store;
  956. #ifndef FIPS_MODULE
  957. int freeparent = 0;
  958. #endif
  959. int lock = 1;
  960. if (!ossl_assert(prov != NULL))
  961. return -1;
  962. /*
  963. * No need to lock if we've got no store because we've not been shared with
  964. * other threads.
  965. */
  966. store = get_provider_store(prov->libctx);
  967. if (store == NULL)
  968. lock = 0;
  969. if (lock && !CRYPTO_THREAD_read_lock(store->lock))
  970. return -1;
  971. if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  972. CRYPTO_THREAD_unlock(store->lock);
  973. return -1;
  974. }
  975. CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock);
  976. #ifndef FIPS_MODULE
  977. if (count >= 1 && prov->ischild && upcalls) {
  978. /*
  979. * We have had a direct activation in this child libctx so we need to
  980. * now down the ref count in the parent provider. We do the actual down
  981. * ref outside of the flag_lock, since it could involve getting other
  982. * locks.
  983. */
  984. freeparent = 1;
  985. }
  986. #endif
  987. if (count < 1)
  988. prov->flag_activated = 0;
  989. #ifndef FIPS_MODULE
  990. else
  991. removechildren = 0;
  992. #endif
  993. #ifndef FIPS_MODULE
  994. if (removechildren && store != NULL) {
  995. int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  996. OSSL_PROVIDER_CHILD_CB *child_cb;
  997. for (i = 0; i < max; i++) {
  998. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  999. child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
  1000. }
  1001. }
  1002. #endif
  1003. if (lock) {
  1004. CRYPTO_THREAD_unlock(prov->flag_lock);
  1005. CRYPTO_THREAD_unlock(store->lock);
  1006. /*
  1007. * This can be done outside the lock. We tolerate other threads getting
  1008. * the wrong result briefly when creating OSSL_DECODER_CTXs.
  1009. */
  1010. #ifndef FIPS_MODULE
  1011. if (count < 1)
  1012. ossl_decoder_cache_flush(prov->libctx);
  1013. #endif
  1014. }
  1015. #ifndef FIPS_MODULE
  1016. if (freeparent)
  1017. ossl_provider_free_parent(prov, 1);
  1018. #endif
  1019. /* We don't deinit here, that's done in ossl_provider_free() */
  1020. return count;
  1021. }
  1022. /*
  1023. * Activate a provider.
  1024. * Return -1 on failure and the activation count on success
  1025. */
  1026. static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
  1027. {
  1028. int count = -1;
  1029. struct provider_store_st *store;
  1030. int ret = 1;
  1031. store = prov->store;
  1032. /*
  1033. * If the provider hasn't been added to the store, then we don't need
  1034. * any locks because we've not shared it with other threads.
  1035. */
  1036. if (store == NULL) {
  1037. lock = 0;
  1038. if (!provider_init(prov))
  1039. return -1;
  1040. }
  1041. #ifndef FIPS_MODULE
  1042. if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
  1043. return -1;
  1044. #endif
  1045. if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
  1046. #ifndef FIPS_MODULE
  1047. if (prov->ischild && upcalls)
  1048. ossl_provider_free_parent(prov, 1);
  1049. #endif
  1050. return -1;
  1051. }
  1052. if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  1053. CRYPTO_THREAD_unlock(store->lock);
  1054. #ifndef FIPS_MODULE
  1055. if (prov->ischild && upcalls)
  1056. ossl_provider_free_parent(prov, 1);
  1057. #endif
  1058. return -1;
  1059. }
  1060. if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
  1061. prov->flag_activated = 1;
  1062. if (count == 1 && store != NULL) {
  1063. ret = create_provider_children(prov);
  1064. }
  1065. }
  1066. if (lock) {
  1067. CRYPTO_THREAD_unlock(prov->flag_lock);
  1068. CRYPTO_THREAD_unlock(store->lock);
  1069. /*
  1070. * This can be done outside the lock. We tolerate other threads getting
  1071. * the wrong result briefly when creating OSSL_DECODER_CTXs.
  1072. */
  1073. #ifndef FIPS_MODULE
  1074. if (count == 1)
  1075. ossl_decoder_cache_flush(prov->libctx);
  1076. #endif
  1077. }
  1078. if (!ret)
  1079. return -1;
  1080. return count;
  1081. }
  1082. static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
  1083. {
  1084. struct provider_store_st *store;
  1085. int freeing;
  1086. if ((store = get_provider_store(prov->libctx)) == NULL)
  1087. return 0;
  1088. if (!CRYPTO_THREAD_read_lock(store->lock))
  1089. return 0;
  1090. freeing = store->freeing;
  1091. CRYPTO_THREAD_unlock(store->lock);
  1092. if (!freeing) {
  1093. int acc
  1094. = evp_method_store_cache_flush(prov->libctx)
  1095. #ifndef FIPS_MODULE
  1096. + ossl_encoder_store_cache_flush(prov->libctx)
  1097. + ossl_decoder_store_cache_flush(prov->libctx)
  1098. + ossl_store_loader_store_cache_flush(prov->libctx)
  1099. #endif
  1100. ;
  1101. #ifndef FIPS_MODULE
  1102. return acc == 4;
  1103. #else
  1104. return acc == 1;
  1105. #endif
  1106. }
  1107. return 1;
  1108. }
  1109. static int provider_remove_store_methods(OSSL_PROVIDER *prov)
  1110. {
  1111. struct provider_store_st *store;
  1112. int freeing;
  1113. if ((store = get_provider_store(prov->libctx)) == NULL)
  1114. return 0;
  1115. if (!CRYPTO_THREAD_read_lock(store->lock))
  1116. return 0;
  1117. freeing = store->freeing;
  1118. CRYPTO_THREAD_unlock(store->lock);
  1119. if (!freeing) {
  1120. int acc;
  1121. if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
  1122. return 0;
  1123. OPENSSL_free(prov->operation_bits);
  1124. prov->operation_bits = NULL;
  1125. prov->operation_bits_sz = 0;
  1126. CRYPTO_THREAD_unlock(prov->opbits_lock);
  1127. acc = evp_method_store_remove_all_provided(prov)
  1128. #ifndef FIPS_MODULE
  1129. + ossl_encoder_store_remove_all_provided(prov)
  1130. + ossl_decoder_store_remove_all_provided(prov)
  1131. + ossl_store_loader_store_remove_all_provided(prov)
  1132. #endif
  1133. ;
  1134. #ifndef FIPS_MODULE
  1135. return acc == 4;
  1136. #else
  1137. return acc == 1;
  1138. #endif
  1139. }
  1140. return 1;
  1141. }
  1142. int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
  1143. {
  1144. int count;
  1145. if (prov == NULL)
  1146. return 0;
  1147. #ifndef FIPS_MODULE
  1148. /*
  1149. * If aschild is true, then we only actually do the activation if the
  1150. * provider is a child. If its not, this is still success.
  1151. */
  1152. if (aschild && !prov->ischild)
  1153. return 1;
  1154. #endif
  1155. if ((count = provider_activate(prov, 1, upcalls)) > 0)
  1156. return count == 1 ? provider_flush_store_cache(prov) : 1;
  1157. return 0;
  1158. }
  1159. int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
  1160. {
  1161. int count;
  1162. if (prov == NULL
  1163. || (count = provider_deactivate(prov, 1, removechildren)) < 0)
  1164. return 0;
  1165. return count == 0 ? provider_remove_store_methods(prov) : 1;
  1166. }
  1167. void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
  1168. {
  1169. return prov != NULL ? prov->provctx : NULL;
  1170. }
  1171. /*
  1172. * This function only does something once when store->use_fallbacks == 1,
  1173. * and then sets store->use_fallbacks = 0, so the second call and so on is
  1174. * effectively a no-op.
  1175. */
  1176. static int provider_activate_fallbacks(struct provider_store_st *store)
  1177. {
  1178. int use_fallbacks;
  1179. int activated_fallback_count = 0;
  1180. int ret = 0;
  1181. const OSSL_PROVIDER_INFO *p;
  1182. if (!CRYPTO_THREAD_read_lock(store->lock))
  1183. return 0;
  1184. use_fallbacks = store->use_fallbacks;
  1185. CRYPTO_THREAD_unlock(store->lock);
  1186. if (!use_fallbacks)
  1187. return 1;
  1188. if (!CRYPTO_THREAD_write_lock(store->lock))
  1189. return 0;
  1190. /* Check again, just in case another thread changed it */
  1191. use_fallbacks = store->use_fallbacks;
  1192. if (!use_fallbacks) {
  1193. CRYPTO_THREAD_unlock(store->lock);
  1194. return 1;
  1195. }
  1196. for (p = ossl_predefined_providers; p->name != NULL; p++) {
  1197. OSSL_PROVIDER *prov = NULL;
  1198. if (!p->is_fallback)
  1199. continue;
  1200. /*
  1201. * We use the internal constructor directly here,
  1202. * otherwise we get a call loop
  1203. */
  1204. prov = provider_new(p->name, p->init, NULL);
  1205. if (prov == NULL)
  1206. goto err;
  1207. prov->libctx = store->libctx;
  1208. #ifndef FIPS_MODULE
  1209. prov->error_lib = ERR_get_next_error_library();
  1210. #endif
  1211. /*
  1212. * We are calling provider_activate while holding the store lock. This
  1213. * means the init function will be called while holding a lock. Normally
  1214. * we try to avoid calling a user callback while holding a lock.
  1215. * However, fallbacks are never third party providers so we accept this.
  1216. */
  1217. if (provider_activate(prov, 0, 0) < 0) {
  1218. ossl_provider_free(prov);
  1219. goto err;
  1220. }
  1221. prov->store = store;
  1222. if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  1223. ossl_provider_free(prov);
  1224. goto err;
  1225. }
  1226. activated_fallback_count++;
  1227. }
  1228. if (activated_fallback_count > 0) {
  1229. store->use_fallbacks = 0;
  1230. ret = 1;
  1231. }
  1232. err:
  1233. CRYPTO_THREAD_unlock(store->lock);
  1234. return ret;
  1235. }
  1236. int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
  1237. int (*cb)(OSSL_PROVIDER *provider,
  1238. void *cbdata),
  1239. void *cbdata)
  1240. {
  1241. int ret = 0, curr, max, ref = 0;
  1242. struct provider_store_st *store = get_provider_store(ctx);
  1243. STACK_OF(OSSL_PROVIDER) *provs = NULL;
  1244. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
  1245. /*
  1246. * Make sure any providers are loaded from config before we try to use
  1247. * them.
  1248. */
  1249. if (ossl_lib_ctx_is_default(ctx))
  1250. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  1251. #endif
  1252. if (store == NULL)
  1253. return 1;
  1254. if (!provider_activate_fallbacks(store))
  1255. return 0;
  1256. /*
  1257. * Under lock, grab a copy of the provider list and up_ref each
  1258. * provider so that they don't disappear underneath us.
  1259. */
  1260. if (!CRYPTO_THREAD_read_lock(store->lock))
  1261. return 0;
  1262. provs = sk_OSSL_PROVIDER_dup(store->providers);
  1263. if (provs == NULL) {
  1264. CRYPTO_THREAD_unlock(store->lock);
  1265. return 0;
  1266. }
  1267. max = sk_OSSL_PROVIDER_num(provs);
  1268. /*
  1269. * We work backwards through the stack so that we can safely delete items
  1270. * as we go.
  1271. */
  1272. for (curr = max - 1; curr >= 0; curr--) {
  1273. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  1274. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1275. goto err_unlock;
  1276. if (prov->flag_activated) {
  1277. /*
  1278. * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
  1279. * to avoid upping the ref count on the parent provider, which we
  1280. * must not do while holding locks.
  1281. */
  1282. if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
  1283. CRYPTO_THREAD_unlock(prov->flag_lock);
  1284. goto err_unlock;
  1285. }
  1286. /*
  1287. * It's already activated, but we up the activated count to ensure
  1288. * it remains activated until after we've called the user callback.
  1289. * In theory this could mean the parent provider goes inactive,
  1290. * whilst still activated in the child for a short period. That's ok.
  1291. */
  1292. if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
  1293. prov->activatecnt_lock)) {
  1294. CRYPTO_DOWN_REF(&prov->refcnt, &ref);
  1295. CRYPTO_THREAD_unlock(prov->flag_lock);
  1296. goto err_unlock;
  1297. }
  1298. } else {
  1299. sk_OSSL_PROVIDER_delete(provs, curr);
  1300. max--;
  1301. }
  1302. CRYPTO_THREAD_unlock(prov->flag_lock);
  1303. }
  1304. CRYPTO_THREAD_unlock(store->lock);
  1305. /*
  1306. * Now, we sweep through all providers not under lock
  1307. */
  1308. for (curr = 0; curr < max; curr++) {
  1309. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  1310. if (!cb(prov, cbdata)) {
  1311. curr = -1;
  1312. goto finish;
  1313. }
  1314. }
  1315. curr = -1;
  1316. ret = 1;
  1317. goto finish;
  1318. err_unlock:
  1319. CRYPTO_THREAD_unlock(store->lock);
  1320. finish:
  1321. /*
  1322. * The pop_free call doesn't do what we want on an error condition. We
  1323. * either start from the first item in the stack, or part way through if
  1324. * we only processed some of the items.
  1325. */
  1326. for (curr++; curr < max; curr++) {
  1327. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  1328. if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
  1329. prov->activatecnt_lock)) {
  1330. ret = 0;
  1331. continue;
  1332. }
  1333. if (ref < 1) {
  1334. /*
  1335. * Looks like we need to deactivate properly. We could just have
  1336. * done this originally, but it involves taking a write lock so
  1337. * we avoid it. We up the count again and do a full deactivation
  1338. */
  1339. if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
  1340. prov->activatecnt_lock))
  1341. provider_deactivate(prov, 0, 1);
  1342. else
  1343. ret = 0;
  1344. }
  1345. /*
  1346. * As above where we did the up-ref, we don't call ossl_provider_free
  1347. * to avoid making upcalls. There should always be at least one ref
  1348. * to the provider in the store, so this should never drop to 0.
  1349. */
  1350. if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
  1351. ret = 0;
  1352. continue;
  1353. }
  1354. /*
  1355. * Not much we can do if this assert ever fails. So we don't use
  1356. * ossl_assert here.
  1357. */
  1358. assert(ref > 0);
  1359. }
  1360. sk_OSSL_PROVIDER_free(provs);
  1361. return ret;
  1362. }
  1363. int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
  1364. {
  1365. OSSL_PROVIDER *prov = NULL;
  1366. int available = 0;
  1367. struct provider_store_st *store = get_provider_store(libctx);
  1368. if (store == NULL || !provider_activate_fallbacks(store))
  1369. return 0;
  1370. prov = ossl_provider_find(libctx, name, 0);
  1371. if (prov != NULL) {
  1372. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1373. return 0;
  1374. available = prov->flag_activated;
  1375. CRYPTO_THREAD_unlock(prov->flag_lock);
  1376. ossl_provider_free(prov);
  1377. }
  1378. return available;
  1379. }
  1380. /* Getters of Provider Object data */
  1381. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  1382. {
  1383. return prov->name;
  1384. }
  1385. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  1386. {
  1387. return prov->module;
  1388. }
  1389. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  1390. {
  1391. #ifdef FIPS_MODULE
  1392. return NULL;
  1393. #else
  1394. return DSO_get_filename(prov->module);
  1395. #endif
  1396. }
  1397. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  1398. {
  1399. #ifdef FIPS_MODULE
  1400. return NULL;
  1401. #else
  1402. /* FIXME: Ensure it's a full path */
  1403. return DSO_get_filename(prov->module);
  1404. #endif
  1405. }
  1406. void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
  1407. {
  1408. if (prov != NULL)
  1409. return prov->provctx;
  1410. return NULL;
  1411. }
  1412. const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
  1413. {
  1414. if (prov != NULL)
  1415. return prov->dispatch;
  1416. return NULL;
  1417. }
  1418. OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
  1419. {
  1420. return prov != NULL ? prov->libctx : NULL;
  1421. }
  1422. /* Wrappers around calls to the provider */
  1423. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  1424. {
  1425. if (prov->teardown != NULL
  1426. #ifndef FIPS_MODULE
  1427. && !prov->ischild
  1428. #endif
  1429. )
  1430. prov->teardown(prov->provctx);
  1431. }
  1432. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  1433. {
  1434. return prov->gettable_params == NULL
  1435. ? NULL : prov->gettable_params(prov->provctx);
  1436. }
  1437. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  1438. {
  1439. return prov->get_params == NULL
  1440. ? 0 : prov->get_params(prov->provctx, params);
  1441. }
  1442. int ossl_provider_self_test(const OSSL_PROVIDER *prov)
  1443. {
  1444. int ret;
  1445. if (prov->self_test == NULL)
  1446. return 1;
  1447. ret = prov->self_test(prov->provctx);
  1448. if (ret == 0)
  1449. (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
  1450. return ret;
  1451. }
  1452. int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
  1453. const char *capability,
  1454. OSSL_CALLBACK *cb,
  1455. void *arg)
  1456. {
  1457. return prov->get_capabilities == NULL
  1458. ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
  1459. }
  1460. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  1461. int operation_id,
  1462. int *no_cache)
  1463. {
  1464. const OSSL_ALGORITHM *res;
  1465. if (prov->query_operation == NULL)
  1466. return NULL;
  1467. res = prov->query_operation(prov->provctx, operation_id, no_cache);
  1468. #if defined(OPENSSL_NO_CACHED_FETCH)
  1469. /* Forcing the non-caching of queries */
  1470. if (no_cache != NULL)
  1471. *no_cache = 1;
  1472. #endif
  1473. return res;
  1474. }
  1475. void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
  1476. int operation_id,
  1477. const OSSL_ALGORITHM *algs)
  1478. {
  1479. if (prov->unquery_operation != NULL)
  1480. prov->unquery_operation(prov->provctx, operation_id, algs);
  1481. }
  1482. int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
  1483. {
  1484. size_t byte = bitnum / 8;
  1485. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1486. if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
  1487. return 0;
  1488. if (provider->operation_bits_sz <= byte) {
  1489. unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
  1490. byte + 1);
  1491. if (tmp == NULL) {
  1492. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1493. return 0;
  1494. }
  1495. provider->operation_bits = tmp;
  1496. memset(provider->operation_bits + provider->operation_bits_sz,
  1497. '\0', byte + 1 - provider->operation_bits_sz);
  1498. provider->operation_bits_sz = byte + 1;
  1499. }
  1500. provider->operation_bits[byte] |= bit;
  1501. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1502. return 1;
  1503. }
  1504. int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
  1505. int *result)
  1506. {
  1507. size_t byte = bitnum / 8;
  1508. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1509. if (!ossl_assert(result != NULL)) {
  1510. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  1511. return 0;
  1512. }
  1513. *result = 0;
  1514. if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
  1515. return 0;
  1516. if (provider->operation_bits_sz > byte)
  1517. *result = ((provider->operation_bits[byte] & bit) != 0);
  1518. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1519. return 1;
  1520. }
  1521. #ifndef FIPS_MODULE
  1522. const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
  1523. {
  1524. return prov->handle;
  1525. }
  1526. int ossl_provider_is_child(const OSSL_PROVIDER *prov)
  1527. {
  1528. return prov->ischild;
  1529. }
  1530. int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
  1531. {
  1532. prov->handle = handle;
  1533. prov->ischild = 1;
  1534. return 1;
  1535. }
  1536. int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
  1537. {
  1538. #ifndef FIPS_MODULE
  1539. struct provider_store_st *store = NULL;
  1540. int i, max;
  1541. OSSL_PROVIDER_CHILD_CB *child_cb;
  1542. if ((store = get_provider_store(libctx)) == NULL)
  1543. return 0;
  1544. if (!CRYPTO_THREAD_read_lock(store->lock))
  1545. return 0;
  1546. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1547. for (i = 0; i < max; i++) {
  1548. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1549. child_cb->global_props_cb(props, child_cb->cbdata);
  1550. }
  1551. CRYPTO_THREAD_unlock(store->lock);
  1552. #endif
  1553. return 1;
  1554. }
  1555. static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
  1556. int (*create_cb)(
  1557. const OSSL_CORE_HANDLE *provider,
  1558. void *cbdata),
  1559. int (*remove_cb)(
  1560. const OSSL_CORE_HANDLE *provider,
  1561. void *cbdata),
  1562. int (*global_props_cb)(
  1563. const char *props,
  1564. void *cbdata),
  1565. void *cbdata)
  1566. {
  1567. /*
  1568. * This is really an OSSL_PROVIDER that we created and cast to
  1569. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1570. */
  1571. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1572. OSSL_PROVIDER *prov;
  1573. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1574. struct provider_store_st *store = NULL;
  1575. int ret = 0, i, max;
  1576. OSSL_PROVIDER_CHILD_CB *child_cb;
  1577. char *propsstr = NULL;
  1578. if ((store = get_provider_store(libctx)) == NULL)
  1579. return 0;
  1580. child_cb = OPENSSL_malloc(sizeof(*child_cb));
  1581. if (child_cb == NULL)
  1582. return 0;
  1583. child_cb->prov = thisprov;
  1584. child_cb->create_cb = create_cb;
  1585. child_cb->remove_cb = remove_cb;
  1586. child_cb->global_props_cb = global_props_cb;
  1587. child_cb->cbdata = cbdata;
  1588. if (!CRYPTO_THREAD_write_lock(store->lock)) {
  1589. OPENSSL_free(child_cb);
  1590. return 0;
  1591. }
  1592. propsstr = evp_get_global_properties_str(libctx, 0);
  1593. if (propsstr != NULL) {
  1594. global_props_cb(propsstr, cbdata);
  1595. OPENSSL_free(propsstr);
  1596. }
  1597. max = sk_OSSL_PROVIDER_num(store->providers);
  1598. for (i = 0; i < max; i++) {
  1599. int activated;
  1600. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1601. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1602. break;
  1603. activated = prov->flag_activated;
  1604. CRYPTO_THREAD_unlock(prov->flag_lock);
  1605. /*
  1606. * We hold the store lock while calling the user callback. This means
  1607. * that the user callback must be short and simple and not do anything
  1608. * likely to cause a deadlock. We don't hold the flag_lock during this
  1609. * call. In theory this means that another thread could deactivate it
  1610. * while we are calling create. This is ok because the other thread
  1611. * will also call remove_cb, but won't be able to do so until we release
  1612. * the store lock.
  1613. */
  1614. if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
  1615. break;
  1616. }
  1617. if (i == max) {
  1618. /* Success */
  1619. ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
  1620. }
  1621. if (i != max || ret <= 0) {
  1622. /* Failed during creation. Remove everything we just added */
  1623. for (; i >= 0; i--) {
  1624. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1625. remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
  1626. }
  1627. OPENSSL_free(child_cb);
  1628. ret = 0;
  1629. }
  1630. CRYPTO_THREAD_unlock(store->lock);
  1631. return ret;
  1632. }
  1633. static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
  1634. {
  1635. /*
  1636. * This is really an OSSL_PROVIDER that we created and cast to
  1637. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1638. */
  1639. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1640. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1641. struct provider_store_st *store = NULL;
  1642. int i, max;
  1643. OSSL_PROVIDER_CHILD_CB *child_cb;
  1644. if ((store = get_provider_store(libctx)) == NULL)
  1645. return;
  1646. if (!CRYPTO_THREAD_write_lock(store->lock))
  1647. return;
  1648. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1649. for (i = 0; i < max; i++) {
  1650. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1651. if (child_cb->prov == thisprov) {
  1652. /* Found an entry */
  1653. sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
  1654. OPENSSL_free(child_cb);
  1655. break;
  1656. }
  1657. }
  1658. CRYPTO_THREAD_unlock(store->lock);
  1659. }
  1660. #endif
  1661. /*-
  1662. * Core functions for the provider
  1663. * ===============================
  1664. *
  1665. * This is the set of functions that the core makes available to the provider
  1666. */
  1667. /*
  1668. * This returns a list of Provider Object parameters with their types, for
  1669. * discovery. We do not expect that many providers will use this, but one
  1670. * never knows.
  1671. */
  1672. static const OSSL_PARAM param_types[] = {
  1673. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  1674. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
  1675. NULL, 0),
  1676. #ifndef FIPS_MODULE
  1677. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
  1678. NULL, 0),
  1679. #endif
  1680. OSSL_PARAM_END
  1681. };
  1682. /*
  1683. * Forward declare all the functions that are provided aa dispatch.
  1684. * This ensures that the compiler will complain if they aren't defined
  1685. * with the correct signature.
  1686. */
  1687. static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
  1688. static OSSL_FUNC_core_get_params_fn core_get_params;
  1689. static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
  1690. static OSSL_FUNC_core_thread_start_fn core_thread_start;
  1691. #ifndef FIPS_MODULE
  1692. static OSSL_FUNC_core_new_error_fn core_new_error;
  1693. static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
  1694. static OSSL_FUNC_core_vset_error_fn core_vset_error;
  1695. static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
  1696. static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
  1697. static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
  1698. OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
  1699. OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
  1700. OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
  1701. OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
  1702. OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
  1703. OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
  1704. OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
  1705. OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
  1706. OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
  1707. OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
  1708. static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
  1709. static OSSL_FUNC_get_entropy_fn rand_get_entropy;
  1710. static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
  1711. static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
  1712. static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
  1713. static OSSL_FUNC_get_nonce_fn rand_get_nonce;
  1714. static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
  1715. static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
  1716. static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
  1717. #endif
  1718. OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
  1719. OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
  1720. OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
  1721. OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
  1722. OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
  1723. OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
  1724. OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
  1725. OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
  1726. OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
  1727. OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
  1728. OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
  1729. OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
  1730. #ifndef FIPS_MODULE
  1731. OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
  1732. OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
  1733. static OSSL_FUNC_provider_name_fn core_provider_get0_name;
  1734. static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
  1735. static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
  1736. static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
  1737. static OSSL_FUNC_provider_free_fn core_provider_free_intern;
  1738. static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
  1739. static OSSL_FUNC_core_obj_create_fn core_obj_create;
  1740. #endif
  1741. static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
  1742. {
  1743. return param_types;
  1744. }
  1745. static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
  1746. {
  1747. int i;
  1748. OSSL_PARAM *p;
  1749. /*
  1750. * We created this object originally and we know it is actually an
  1751. * OSSL_PROVIDER *, so the cast is safe
  1752. */
  1753. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1754. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
  1755. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  1756. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
  1757. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  1758. #ifndef FIPS_MODULE
  1759. if ((p = OSSL_PARAM_locate(params,
  1760. OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
  1761. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  1762. #endif
  1763. if (prov->parameters == NULL)
  1764. return 1;
  1765. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  1766. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  1767. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  1768. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  1769. }
  1770. return 1;
  1771. }
  1772. static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
  1773. {
  1774. /*
  1775. * We created this object originally and we know it is actually an
  1776. * OSSL_PROVIDER *, so the cast is safe
  1777. */
  1778. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1779. /*
  1780. * Using ossl_provider_libctx would be wrong as that returns
  1781. * NULL for |prov| == NULL and NULL libctx has a special meaning
  1782. * that does not apply here. Here |prov| == NULL can happen only in
  1783. * case of a coding error.
  1784. */
  1785. assert(prov != NULL);
  1786. return (OPENSSL_CORE_CTX *)prov->libctx;
  1787. }
  1788. static int core_thread_start(const OSSL_CORE_HANDLE *handle,
  1789. OSSL_thread_stop_handler_fn handfn,
  1790. void *arg)
  1791. {
  1792. /*
  1793. * We created this object originally and we know it is actually an
  1794. * OSSL_PROVIDER *, so the cast is safe
  1795. */
  1796. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1797. return ossl_init_thread_start(prov, arg, handfn);
  1798. }
  1799. /*
  1800. * The FIPS module inner provider doesn't implement these. They aren't
  1801. * needed there, since the FIPS module upcalls are always the outer provider
  1802. * ones.
  1803. */
  1804. #ifndef FIPS_MODULE
  1805. /*
  1806. * These error functions should use |handle| to select the proper
  1807. * library context to report in the correct error stack if error
  1808. * stacks become tied to the library context.
  1809. * We cannot currently do that since there's no support for it in the
  1810. * ERR subsystem.
  1811. */
  1812. static void core_new_error(const OSSL_CORE_HANDLE *handle)
  1813. {
  1814. ERR_new();
  1815. }
  1816. static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  1817. const char *file, int line, const char *func)
  1818. {
  1819. ERR_set_debug(file, line, func);
  1820. }
  1821. static void core_vset_error(const OSSL_CORE_HANDLE *handle,
  1822. uint32_t reason, const char *fmt, va_list args)
  1823. {
  1824. /*
  1825. * We created this object originally and we know it is actually an
  1826. * OSSL_PROVIDER *, so the cast is safe
  1827. */
  1828. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1829. /*
  1830. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  1831. * error and will be treated as such. Otherwise, it's a new style
  1832. * provider error and will be treated as such.
  1833. */
  1834. if (ERR_GET_LIB(reason) != 0) {
  1835. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  1836. } else {
  1837. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  1838. }
  1839. }
  1840. static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
  1841. {
  1842. return ERR_set_mark();
  1843. }
  1844. static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
  1845. {
  1846. return ERR_clear_last_mark();
  1847. }
  1848. static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
  1849. {
  1850. return ERR_pop_to_mark();
  1851. }
  1852. static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
  1853. OSSL_CALLBACK **cb, void **cbarg)
  1854. {
  1855. OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
  1856. }
  1857. static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
  1858. unsigned char **pout, int entropy,
  1859. size_t min_len, size_t max_len)
  1860. {
  1861. return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1862. pout, entropy, min_len, max_len);
  1863. }
  1864. static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
  1865. unsigned char **pout, int entropy,
  1866. size_t min_len, size_t max_len)
  1867. {
  1868. return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1869. pout, entropy, min_len, max_len);
  1870. }
  1871. static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
  1872. unsigned char *buf, size_t len)
  1873. {
  1874. ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1875. buf, len);
  1876. }
  1877. static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
  1878. unsigned char *buf, size_t len)
  1879. {
  1880. ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
  1881. buf, len);
  1882. }
  1883. static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
  1884. unsigned char **pout,
  1885. size_t min_len, size_t max_len,
  1886. const void *salt, size_t salt_len)
  1887. {
  1888. return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1889. pout, min_len, max_len, salt, salt_len);
  1890. }
  1891. static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
  1892. unsigned char **pout,
  1893. size_t min_len, size_t max_len,
  1894. const void *salt, size_t salt_len)
  1895. {
  1896. return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1897. pout, min_len, max_len, salt, salt_len);
  1898. }
  1899. static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
  1900. unsigned char *buf, size_t len)
  1901. {
  1902. ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1903. buf, len);
  1904. }
  1905. static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
  1906. unsigned char *buf, size_t len)
  1907. {
  1908. ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
  1909. buf, len);
  1910. }
  1911. static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
  1912. {
  1913. return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
  1914. }
  1915. static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
  1916. {
  1917. return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
  1918. }
  1919. static const OSSL_DISPATCH *
  1920. core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
  1921. {
  1922. return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
  1923. }
  1924. static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
  1925. int activate)
  1926. {
  1927. return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
  1928. }
  1929. static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
  1930. int deactivate)
  1931. {
  1932. return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
  1933. }
  1934. static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
  1935. const char *sign_name, const char *digest_name,
  1936. const char *pkey_name)
  1937. {
  1938. int sign_nid = OBJ_txt2nid(sign_name);
  1939. int digest_nid = NID_undef;
  1940. int pkey_nid = OBJ_txt2nid(pkey_name);
  1941. if (digest_name != NULL && digest_name[0] != '\0'
  1942. && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
  1943. return 0;
  1944. if (sign_nid == NID_undef)
  1945. return 0;
  1946. /*
  1947. * Check if it already exists. This is a success if so (even if we don't
  1948. * have nids for the digest/pkey)
  1949. */
  1950. if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
  1951. return 1;
  1952. if (pkey_nid == NID_undef)
  1953. return 0;
  1954. return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
  1955. }
  1956. static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
  1957. const char *sn, const char *ln)
  1958. {
  1959. /* Check if it already exists and create it if not */
  1960. return OBJ_txt2nid(oid) != NID_undef
  1961. || OBJ_create(oid, sn, ln) != NID_undef;
  1962. }
  1963. #endif /* FIPS_MODULE */
  1964. /*
  1965. * Functions provided by the core.
  1966. */
  1967. static const OSSL_DISPATCH core_dispatch_[] = {
  1968. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  1969. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  1970. { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
  1971. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  1972. #ifndef FIPS_MODULE
  1973. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  1974. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  1975. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  1976. { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
  1977. { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
  1978. (void (*)(void))core_clear_last_error_mark },
  1979. { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
  1980. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
  1981. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
  1982. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
  1983. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
  1984. { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
  1985. { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
  1986. { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
  1987. { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
  1988. { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
  1989. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
  1990. { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
  1991. { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
  1992. { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
  1993. { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
  1994. { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
  1995. { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
  1996. { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
  1997. { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
  1998. { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
  1999. { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
  2000. #endif
  2001. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  2002. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  2003. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  2004. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  2005. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  2006. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  2007. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  2008. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  2009. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  2010. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  2011. (void (*)(void))CRYPTO_secure_clear_free },
  2012. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  2013. (void (*)(void))CRYPTO_secure_allocated },
  2014. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  2015. #ifndef FIPS_MODULE
  2016. { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
  2017. (void (*)(void))ossl_provider_register_child_cb },
  2018. { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
  2019. (void (*)(void))ossl_provider_deregister_child_cb },
  2020. { OSSL_FUNC_PROVIDER_NAME,
  2021. (void (*)(void))core_provider_get0_name },
  2022. { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
  2023. (void (*)(void))core_provider_get0_provider_ctx },
  2024. { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
  2025. (void (*)(void))core_provider_get0_dispatch },
  2026. { OSSL_FUNC_PROVIDER_UP_REF,
  2027. (void (*)(void))core_provider_up_ref_intern },
  2028. { OSSL_FUNC_PROVIDER_FREE,
  2029. (void (*)(void))core_provider_free_intern },
  2030. { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
  2031. { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
  2032. #endif
  2033. OSSL_DISPATCH_END
  2034. };
  2035. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;