provider_core.c 67 KB

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