provider_core.c 68 KB

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