provider_core.c 68 KB

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