provider_core.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. /*
  2. * Copyright 2019-2021 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. #include "crypto/evp.h" /* evp_method_store_flush */
  18. #include "crypto/rand.h"
  19. #include "internal/nelem.h"
  20. #include "internal/thread_once.h"
  21. #include "internal/provider.h"
  22. #include "internal/refcount.h"
  23. #include "internal/bio.h"
  24. #include "internal/core.h"
  25. #include "provider_local.h"
  26. #ifndef FIPS_MODULE
  27. # include <openssl/self_test.h>
  28. #endif
  29. static OSSL_PROVIDER *provider_new(const char *name,
  30. OSSL_provider_init_fn *init_function);
  31. /*-
  32. * Provider Object structure
  33. * =========================
  34. */
  35. typedef struct {
  36. char *name;
  37. char *value;
  38. } INFOPAIR;
  39. DEFINE_STACK_OF(INFOPAIR)
  40. #ifndef FIPS_MODULE
  41. typedef struct {
  42. OSSL_PROVIDER *prov;
  43. int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
  44. int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
  45. int (*global_props_cb)(const char *props, void *cbdata);
  46. void *cbdata;
  47. } OSSL_PROVIDER_CHILD_CB;
  48. DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
  49. #endif
  50. struct provider_store_st; /* Forward declaration */
  51. struct ossl_provider_st {
  52. /* Flag bits */
  53. unsigned int flag_initialized:1;
  54. unsigned int flag_activated:1;
  55. unsigned int flag_fallback:1; /* Can be used as fallback */
  56. #ifndef FIPS_MODULE
  57. unsigned int flag_couldbechild:1;
  58. #endif
  59. /* Getting and setting the flags require synchronization */
  60. CRYPTO_RWLOCK *flag_lock;
  61. /* OpenSSL library side data */
  62. CRYPTO_REF_COUNT refcnt;
  63. CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
  64. int activatecnt;
  65. char *name;
  66. char *path;
  67. DSO *module;
  68. OSSL_provider_init_fn *init_function;
  69. STACK_OF(INFOPAIR) *parameters;
  70. OSSL_LIB_CTX *libctx; /* The library context this instance is in */
  71. struct provider_store_st *store; /* The store this instance belongs to */
  72. #ifndef FIPS_MODULE
  73. /*
  74. * In the FIPS module inner provider, this isn't needed, since the
  75. * error upcalls are always direct calls to the outer provider.
  76. */
  77. int error_lib; /* ERR library number, one for each provider */
  78. # ifndef OPENSSL_NO_ERR
  79. ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
  80. # endif
  81. #endif
  82. /* Provider side functions */
  83. OSSL_FUNC_provider_teardown_fn *teardown;
  84. OSSL_FUNC_provider_gettable_params_fn *gettable_params;
  85. OSSL_FUNC_provider_get_params_fn *get_params;
  86. OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
  87. OSSL_FUNC_provider_self_test_fn *self_test;
  88. OSSL_FUNC_provider_query_operation_fn *query_operation;
  89. OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
  90. /*
  91. * Cache of bit to indicate of query_operation() has been called on
  92. * a specific operation or not.
  93. */
  94. unsigned char *operation_bits;
  95. size_t operation_bits_sz;
  96. CRYPTO_RWLOCK *opbits_lock;
  97. #ifndef FIPS_MODULE
  98. /* Whether this provider is the child of some other provider */
  99. const OSSL_CORE_HANDLE *handle;
  100. unsigned int ischild:1;
  101. #endif
  102. /* Provider side data */
  103. void *provctx;
  104. const OSSL_DISPATCH *dispatch;
  105. };
  106. DEFINE_STACK_OF(OSSL_PROVIDER)
  107. static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
  108. const OSSL_PROVIDER * const *b)
  109. {
  110. return strcmp((*a)->name, (*b)->name);
  111. }
  112. /*-
  113. * Provider Object store
  114. * =====================
  115. *
  116. * The Provider Object store is a library context object, and therefore needs
  117. * an index.
  118. */
  119. struct provider_store_st {
  120. OSSL_LIB_CTX *libctx;
  121. STACK_OF(OSSL_PROVIDER) *providers;
  122. STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
  123. CRYPTO_RWLOCK *default_path_lock;
  124. CRYPTO_RWLOCK *lock;
  125. char *default_path;
  126. unsigned int use_fallbacks:1;
  127. unsigned int freeing:1;
  128. };
  129. /*
  130. * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
  131. * and ossl_provider_free(), called as needed.
  132. * Since this is only called when the provider store is being emptied, we
  133. * don't need to care about any lock.
  134. */
  135. static void provider_deactivate_free(OSSL_PROVIDER *prov)
  136. {
  137. if (prov->flag_activated)
  138. ossl_provider_deactivate(prov);
  139. ossl_provider_free(prov);
  140. }
  141. #ifndef FIPS_MODULE
  142. static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
  143. {
  144. OPENSSL_free(cb);
  145. }
  146. #endif
  147. static void provider_store_free(void *vstore)
  148. {
  149. struct provider_store_st *store = vstore;
  150. if (store == NULL)
  151. return;
  152. store->freeing = 1;
  153. OPENSSL_free(store->default_path);
  154. sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
  155. #ifndef FIPS_MODULE
  156. sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
  157. ossl_provider_child_cb_free);
  158. #endif
  159. CRYPTO_THREAD_lock_free(store->default_path_lock);
  160. CRYPTO_THREAD_lock_free(store->lock);
  161. OPENSSL_free(store);
  162. }
  163. static void *provider_store_new(OSSL_LIB_CTX *ctx)
  164. {
  165. struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
  166. const struct predefined_providers_st *p = NULL;
  167. if (store == NULL
  168. || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
  169. || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
  170. #ifndef FIPS_MODULE
  171. || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
  172. #endif
  173. || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  174. provider_store_free(store);
  175. return NULL;
  176. }
  177. store->libctx = ctx;
  178. store->use_fallbacks = 1;
  179. for (p = ossl_predefined_providers; p->name != NULL; p++) {
  180. OSSL_PROVIDER *prov = NULL;
  181. /*
  182. * We use the internal constructor directly here,
  183. * otherwise we get a call loop
  184. */
  185. prov = provider_new(p->name, p->init);
  186. if (prov == NULL
  187. || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  188. ossl_provider_free(prov);
  189. provider_store_free(store);
  190. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  191. return NULL;
  192. }
  193. prov->libctx = ctx;
  194. prov->store = store;
  195. #ifndef FIPS_MODULE
  196. prov->error_lib = ERR_get_next_error_library();
  197. #endif
  198. if(p->is_fallback)
  199. ossl_provider_set_fallback(prov);
  200. }
  201. return store;
  202. }
  203. static const OSSL_LIB_CTX_METHOD provider_store_method = {
  204. /* Needs to be freed before the child provider data is freed */
  205. OSSL_LIB_CTX_METHOD_PRIORITY_1,
  206. provider_store_new,
  207. provider_store_free,
  208. };
  209. static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
  210. {
  211. struct provider_store_st *store = NULL;
  212. store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
  213. &provider_store_method);
  214. if (store == NULL)
  215. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  216. return store;
  217. }
  218. int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
  219. {
  220. struct provider_store_st *store;
  221. if ((store = get_provider_store(libctx)) != NULL) {
  222. if (!CRYPTO_THREAD_write_lock(store->lock))
  223. return 0;
  224. store->use_fallbacks = 0;
  225. CRYPTO_THREAD_unlock(store->lock);
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
  231. int noconfig)
  232. {
  233. struct provider_store_st *store = NULL;
  234. OSSL_PROVIDER *prov = NULL;
  235. if ((store = get_provider_store(libctx)) != NULL) {
  236. OSSL_PROVIDER tmpl = { 0, };
  237. int i;
  238. #ifndef FIPS_MODULE
  239. /*
  240. * Make sure any providers are loaded from config before we try to find
  241. * them.
  242. */
  243. if (!noconfig) {
  244. if (ossl_lib_ctx_is_default(libctx))
  245. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  246. }
  247. #endif
  248. tmpl.name = (char *)name;
  249. if (!CRYPTO_THREAD_read_lock(store->lock))
  250. return NULL;
  251. if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
  252. || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
  253. || !ossl_provider_up_ref(prov))
  254. prov = NULL;
  255. CRYPTO_THREAD_unlock(store->lock);
  256. }
  257. return prov;
  258. }
  259. /*-
  260. * Provider Object methods
  261. * =======================
  262. */
  263. static OSSL_PROVIDER *provider_new(const char *name,
  264. OSSL_provider_init_fn *init_function)
  265. {
  266. OSSL_PROVIDER *prov = NULL;
  267. if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
  268. #ifndef HAVE_ATOMICS
  269. || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
  270. #endif
  271. || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
  272. || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
  273. || (prov->name = OPENSSL_strdup(name)) == NULL) {
  274. ossl_provider_free(prov);
  275. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  276. return NULL;
  277. }
  278. prov->refcnt = 1; /* 1 One reference to be returned */
  279. prov->init_function = init_function;
  280. #ifndef FIPS_MODULE
  281. prov->flag_couldbechild = 1;
  282. #endif
  283. return prov;
  284. }
  285. int ossl_provider_up_ref(OSSL_PROVIDER *prov)
  286. {
  287. int ref = 0;
  288. if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
  289. return 0;
  290. #ifndef FIPS_MODULE
  291. if (prov->ischild) {
  292. if (!ossl_provider_up_ref_parent(prov, 0)) {
  293. ossl_provider_free(prov);
  294. return 0;
  295. }
  296. }
  297. #endif
  298. return ref;
  299. }
  300. #ifndef FIPS_MODULE
  301. static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
  302. {
  303. if (activate)
  304. return ossl_provider_activate(prov, 0, 1);
  305. return ossl_provider_up_ref(prov);
  306. }
  307. static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
  308. {
  309. if (deactivate)
  310. return ossl_provider_deactivate(prov);
  311. ossl_provider_free(prov);
  312. return 1;
  313. }
  314. #endif
  315. OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
  316. OSSL_provider_init_fn *init_function,
  317. int noconfig)
  318. {
  319. struct provider_store_st *store = NULL;
  320. OSSL_PROVIDER *prov = NULL;
  321. if ((store = get_provider_store(libctx)) == NULL)
  322. return NULL;
  323. if ((prov = ossl_provider_find(libctx, name,
  324. noconfig)) != NULL) { /* refcount +1 */
  325. ossl_provider_free(prov); /* refcount -1 */
  326. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
  327. "name=%s", name);
  328. return NULL;
  329. }
  330. /* provider_new() generates an error, so no need here */
  331. if ((prov = provider_new(name, init_function)) == NULL)
  332. return NULL;
  333. if (!CRYPTO_THREAD_write_lock(store->lock))
  334. return NULL;
  335. if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
  336. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  337. prov = NULL;
  338. } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  339. ossl_provider_free(prov); /* -1 Store reference */
  340. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  341. prov = NULL;
  342. } else {
  343. prov->libctx = libctx;
  344. prov->store = store;
  345. #ifndef FIPS_MODULE
  346. prov->error_lib = ERR_get_next_error_library();
  347. #endif
  348. }
  349. CRYPTO_THREAD_unlock(store->lock);
  350. if (prov == NULL)
  351. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  352. /*
  353. * At this point, the provider is only partially "loaded". To be
  354. * fully "loaded", ossl_provider_activate() must also be called.
  355. */
  356. return prov;
  357. }
  358. static void free_infopair(INFOPAIR *pair)
  359. {
  360. OPENSSL_free(pair->name);
  361. OPENSSL_free(pair->value);
  362. OPENSSL_free(pair);
  363. }
  364. void ossl_provider_free(OSSL_PROVIDER *prov)
  365. {
  366. if (prov != NULL) {
  367. int ref = 0;
  368. CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
  369. /*
  370. * When the refcount drops to zero, we clean up the provider.
  371. * Note that this also does teardown, which may seem late,
  372. * considering that init happens on first activation. However,
  373. * there may be other structures hanging on to the provider after
  374. * the last deactivation and may therefore need full access to the
  375. * provider's services. Therefore, we deinit late.
  376. */
  377. if (ref == 0) {
  378. if (prov->flag_initialized) {
  379. ossl_provider_teardown(prov);
  380. #ifndef OPENSSL_NO_ERR
  381. # ifndef FIPS_MODULE
  382. if (prov->error_strings != NULL) {
  383. ERR_unload_strings(prov->error_lib, prov->error_strings);
  384. OPENSSL_free(prov->error_strings);
  385. prov->error_strings = NULL;
  386. }
  387. # endif
  388. #endif
  389. OPENSSL_free(prov->operation_bits);
  390. prov->operation_bits = NULL;
  391. prov->operation_bits_sz = 0;
  392. prov->flag_initialized = 0;
  393. }
  394. #ifndef FIPS_MODULE
  395. /*
  396. * We deregister thread handling whether or not the provider was
  397. * initialized. If init was attempted but was not successful then
  398. * the provider may still have registered a thread handler.
  399. */
  400. ossl_init_thread_deregister(prov);
  401. DSO_free(prov->module);
  402. #endif
  403. OPENSSL_free(prov->name);
  404. OPENSSL_free(prov->path);
  405. sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
  406. CRYPTO_THREAD_lock_free(prov->opbits_lock);
  407. CRYPTO_THREAD_lock_free(prov->flag_lock);
  408. #ifndef HAVE_ATOMICS
  409. CRYPTO_THREAD_lock_free(prov->refcnt_lock);
  410. #endif
  411. OPENSSL_free(prov);
  412. }
  413. #ifndef FIPS_MODULE
  414. else if (prov->ischild) {
  415. ossl_provider_free_parent(prov, 0);
  416. }
  417. #endif
  418. }
  419. }
  420. /* Setters */
  421. int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
  422. {
  423. OPENSSL_free(prov->path);
  424. if (module_path == NULL)
  425. return 1;
  426. if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
  427. return 1;
  428. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  429. return 0;
  430. }
  431. int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
  432. const char *name, const char *value)
  433. {
  434. INFOPAIR *pair = NULL;
  435. if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
  436. && (prov->parameters != NULL
  437. || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
  438. && (pair->name = OPENSSL_strdup(name)) != NULL
  439. && (pair->value = OPENSSL_strdup(value)) != NULL
  440. && sk_INFOPAIR_push(prov->parameters, pair) > 0)
  441. return 1;
  442. if (pair != NULL) {
  443. OPENSSL_free(pair->name);
  444. OPENSSL_free(pair->value);
  445. OPENSSL_free(pair);
  446. }
  447. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  448. return 0;
  449. }
  450. /*
  451. * Provider activation.
  452. *
  453. * What "activation" means depends on the provider form; for built in
  454. * providers (in the library or the application alike), the provider
  455. * can already be considered to be loaded, all that's needed is to
  456. * initialize it. However, for dynamically loadable provider modules,
  457. * we must first load that module.
  458. *
  459. * Built in modules are distinguished from dynamically loaded modules
  460. * with an already assigned init function.
  461. */
  462. static const OSSL_DISPATCH *core_dispatch; /* Define further down */
  463. int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
  464. const char *path)
  465. {
  466. struct provider_store_st *store;
  467. char *p = NULL;
  468. if (path != NULL) {
  469. p = OPENSSL_strdup(path);
  470. if (p == NULL) {
  471. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  472. return 0;
  473. }
  474. }
  475. if ((store = get_provider_store(libctx)) != NULL
  476. && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
  477. OPENSSL_free(store->default_path);
  478. store->default_path = p;
  479. CRYPTO_THREAD_unlock(store->default_path_lock);
  480. return 1;
  481. }
  482. OPENSSL_free(p);
  483. return 0;
  484. }
  485. /*
  486. * Internal version that doesn't affect the store flags, and thereby avoid
  487. * locking. Direct callers must remember to set the store flags when
  488. * appropriate.
  489. */
  490. static int provider_init(OSSL_PROVIDER *prov, int flag_lock)
  491. {
  492. const OSSL_DISPATCH *provider_dispatch = NULL;
  493. void *tmp_provctx = NULL; /* safety measure */
  494. #ifndef OPENSSL_NO_ERR
  495. # ifndef FIPS_MODULE
  496. OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
  497. # endif
  498. #endif
  499. int ok = 0;
  500. /*
  501. * The flag lock is used to lock init, not only because the flag is
  502. * checked here and set at the end, but also because this function
  503. * modifies a number of things in the provider structure that this
  504. * function needs to perform under lock anyway.
  505. */
  506. if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
  507. goto end;
  508. if (prov->flag_initialized) {
  509. ok = 1;
  510. goto end;
  511. }
  512. /*
  513. * If the init function isn't set, it indicates that this provider is
  514. * a loadable module.
  515. */
  516. if (prov->init_function == NULL) {
  517. #ifdef FIPS_MODULE
  518. goto end;
  519. #else
  520. if (prov->module == NULL) {
  521. char *allocated_path = NULL;
  522. const char *module_path = NULL;
  523. char *merged_path = NULL;
  524. const char *load_dir = NULL;
  525. char *allocated_load_dir = NULL;
  526. struct provider_store_st *store;
  527. if ((prov->module = DSO_new()) == NULL) {
  528. /* DSO_new() generates an error already */
  529. goto end;
  530. }
  531. if ((store = get_provider_store(prov->libctx)) == NULL
  532. || !CRYPTO_THREAD_read_lock(store->default_path_lock))
  533. goto end;
  534. if (store->default_path != NULL) {
  535. allocated_load_dir = OPENSSL_strdup(store->default_path);
  536. CRYPTO_THREAD_unlock(store->default_path_lock);
  537. if (allocated_load_dir == NULL) {
  538. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  539. goto end;
  540. }
  541. load_dir = allocated_load_dir;
  542. } else {
  543. CRYPTO_THREAD_unlock(store->default_path_lock);
  544. }
  545. if (load_dir == NULL) {
  546. load_dir = ossl_safe_getenv("OPENSSL_MODULES");
  547. if (load_dir == NULL)
  548. load_dir = MODULESDIR;
  549. }
  550. DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
  551. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
  552. module_path = prov->path;
  553. if (module_path == NULL)
  554. module_path = allocated_path =
  555. DSO_convert_filename(prov->module, prov->name);
  556. if (module_path != NULL)
  557. merged_path = DSO_merge(prov->module, module_path, load_dir);
  558. if (merged_path == NULL
  559. || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
  560. DSO_free(prov->module);
  561. prov->module = NULL;
  562. }
  563. OPENSSL_free(merged_path);
  564. OPENSSL_free(allocated_path);
  565. OPENSSL_free(allocated_load_dir);
  566. }
  567. if (prov->module != NULL)
  568. prov->init_function = (OSSL_provider_init_fn *)
  569. DSO_bind_func(prov->module, "OSSL_provider_init");
  570. #endif
  571. }
  572. /* Call the initialise function for the provider. */
  573. if (prov->init_function == NULL
  574. || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
  575. &provider_dispatch, &tmp_provctx)) {
  576. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
  577. "name=%s", prov->name);
  578. goto end;
  579. }
  580. prov->provctx = tmp_provctx;
  581. prov->dispatch = provider_dispatch;
  582. #ifndef FIPS_MODULE
  583. prov->flag_couldbechild = 0;
  584. #endif
  585. for (; provider_dispatch->function_id != 0; provider_dispatch++) {
  586. switch (provider_dispatch->function_id) {
  587. case OSSL_FUNC_PROVIDER_TEARDOWN:
  588. prov->teardown =
  589. OSSL_FUNC_provider_teardown(provider_dispatch);
  590. break;
  591. case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
  592. prov->gettable_params =
  593. OSSL_FUNC_provider_gettable_params(provider_dispatch);
  594. break;
  595. case OSSL_FUNC_PROVIDER_GET_PARAMS:
  596. prov->get_params =
  597. OSSL_FUNC_provider_get_params(provider_dispatch);
  598. break;
  599. case OSSL_FUNC_PROVIDER_SELF_TEST:
  600. prov->self_test =
  601. OSSL_FUNC_provider_self_test(provider_dispatch);
  602. break;
  603. case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
  604. prov->get_capabilities =
  605. OSSL_FUNC_provider_get_capabilities(provider_dispatch);
  606. break;
  607. case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
  608. prov->query_operation =
  609. OSSL_FUNC_provider_query_operation(provider_dispatch);
  610. break;
  611. case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
  612. prov->unquery_operation =
  613. OSSL_FUNC_provider_unquery_operation(provider_dispatch);
  614. break;
  615. #ifndef OPENSSL_NO_ERR
  616. # ifndef FIPS_MODULE
  617. case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
  618. p_get_reason_strings =
  619. OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
  620. break;
  621. # endif
  622. #endif
  623. }
  624. }
  625. #ifndef OPENSSL_NO_ERR
  626. # ifndef FIPS_MODULE
  627. if (p_get_reason_strings != NULL) {
  628. const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
  629. size_t cnt, cnt2;
  630. /*
  631. * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
  632. * although they are essentially the same type.
  633. * Furthermore, ERR_load_strings() patches the array's error number
  634. * with the error library number, so we need to make a copy of that
  635. * array either way.
  636. */
  637. cnt = 0;
  638. while (reasonstrings[cnt].id != 0) {
  639. if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
  640. goto end;
  641. cnt++;
  642. }
  643. cnt++; /* One for the terminating item */
  644. /* Allocate one extra item for the "library" name */
  645. prov->error_strings =
  646. OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
  647. if (prov->error_strings == NULL)
  648. goto end;
  649. /*
  650. * Set the "library" name.
  651. */
  652. prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
  653. prov->error_strings[0].string = prov->name;
  654. /*
  655. * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
  656. * 1..cnt.
  657. */
  658. for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
  659. prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
  660. prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
  661. }
  662. ERR_load_strings(prov->error_lib, prov->error_strings);
  663. }
  664. # endif
  665. #endif
  666. /* With this flag set, this provider has become fully "loaded". */
  667. prov->flag_initialized = 1;
  668. ok = 1;
  669. end:
  670. if (flag_lock)
  671. CRYPTO_THREAD_unlock(prov->flag_lock);
  672. return ok;
  673. }
  674. /*
  675. * Deactivate a provider.
  676. * Return -1 on failure and the activation count on success
  677. */
  678. static int provider_deactivate(OSSL_PROVIDER *prov)
  679. {
  680. int count;
  681. struct provider_store_st *store;
  682. if (!ossl_assert(prov != NULL))
  683. return -1;
  684. store = get_provider_store(prov->libctx);
  685. if (store == NULL)
  686. return -1;
  687. if (!CRYPTO_THREAD_read_lock(store->lock))
  688. return -1;
  689. if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  690. CRYPTO_THREAD_unlock(store->lock);
  691. return -1;
  692. }
  693. #ifndef FIPS_MODULE
  694. if (prov->activatecnt == 2 && prov->ischild) {
  695. /*
  696. * We have had a direct activation in this child libctx so we need to
  697. * now down the ref count in the parent provider.
  698. */
  699. ossl_provider_free_parent(prov, 1);
  700. }
  701. #endif
  702. if ((count = --prov->activatecnt) < 1) {
  703. prov->flag_activated = 0;
  704. #ifndef FIPS_MODULE
  705. {
  706. int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  707. OSSL_PROVIDER_CHILD_CB *child_cb;
  708. for (i = 0; i < max; i++) {
  709. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  710. child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
  711. }
  712. }
  713. #endif
  714. }
  715. CRYPTO_THREAD_unlock(prov->flag_lock);
  716. CRYPTO_THREAD_unlock(store->lock);
  717. /* We don't deinit here, that's done in ossl_provider_free() */
  718. return count;
  719. }
  720. /*
  721. * Activate a provider.
  722. * Return -1 on failure and the activation count on success
  723. */
  724. static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
  725. {
  726. int count = -1;
  727. if (provider_init(prov, lock)) {
  728. int ret = 1;
  729. struct provider_store_st *store;
  730. store = get_provider_store(prov->libctx);
  731. if (store == NULL)
  732. return -1;
  733. if (lock && !CRYPTO_THREAD_read_lock(store->lock))
  734. return -1;
  735. if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  736. CRYPTO_THREAD_unlock(store->lock);
  737. return -1;
  738. }
  739. #ifndef FIPS_MODULE
  740. if (prov->ischild && upcalls)
  741. ret = ossl_provider_up_ref_parent(prov, 1);
  742. #endif
  743. if (ret) {
  744. count = ++prov->activatecnt;
  745. prov->flag_activated = 1;
  746. #ifndef FIPS_MODULE
  747. if (prov->activatecnt == 1) {
  748. OSSL_PROVIDER_CHILD_CB *child_cb;
  749. int i, max;
  750. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  751. for (i = 0; i < max; i++) {
  752. /*
  753. * This is newly activated (activatecnt == 1), so we need to
  754. * create child providers as necessary.
  755. */
  756. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs,
  757. i);
  758. ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov,
  759. child_cb->cbdata);
  760. }
  761. }
  762. #endif
  763. }
  764. if (lock) {
  765. CRYPTO_THREAD_unlock(prov->flag_lock);
  766. CRYPTO_THREAD_unlock(store->lock);
  767. }
  768. if (!ret)
  769. return -1;
  770. }
  771. return count;
  772. }
  773. static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
  774. {
  775. struct provider_store_st *store;
  776. int freeing;
  777. if ((store = get_provider_store(prov->libctx)) == NULL)
  778. return 0;
  779. if (!CRYPTO_THREAD_read_lock(store->lock))
  780. return 0;
  781. freeing = store->freeing;
  782. CRYPTO_THREAD_unlock(store->lock);
  783. if (!freeing)
  784. return evp_method_store_flush(prov->libctx);
  785. return 1;
  786. }
  787. int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks,
  788. int upcalls)
  789. {
  790. int count;
  791. if (prov == NULL)
  792. return 0;
  793. if ((count = provider_activate(prov, 1, upcalls)) > 0) {
  794. if (!retain_fallbacks) {
  795. if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
  796. provider_deactivate(prov);
  797. return 0;
  798. }
  799. prov->store->use_fallbacks = 0;
  800. CRYPTO_THREAD_unlock(prov->store->lock);
  801. }
  802. return count == 1 ? provider_flush_store_cache(prov) : 1;
  803. }
  804. return 0;
  805. }
  806. int ossl_provider_deactivate(OSSL_PROVIDER *prov)
  807. {
  808. int count;
  809. if (prov == NULL || (count = provider_deactivate(prov)) < 0)
  810. return 0;
  811. return count == 0 ? provider_flush_store_cache(prov) : 1;
  812. }
  813. void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
  814. {
  815. return prov->provctx;
  816. }
  817. /*
  818. * This function only does something once when store->use_fallbacks == 1,
  819. * and then sets store->use_fallbacks = 0, so the second call and so on is
  820. * effectively a no-op.
  821. */
  822. static void provider_activate_fallbacks(struct provider_store_st *store)
  823. {
  824. int use_fallbacks;
  825. int num_provs;
  826. int activated_fallback_count = 0;
  827. int i;
  828. if (!CRYPTO_THREAD_read_lock(store->lock))
  829. return;
  830. use_fallbacks = store->use_fallbacks;
  831. CRYPTO_THREAD_unlock(store->lock);
  832. if (!use_fallbacks)
  833. return;
  834. if (!CRYPTO_THREAD_write_lock(store->lock))
  835. return;
  836. /* Check again, just in case another thread changed it */
  837. use_fallbacks = store->use_fallbacks;
  838. if (!use_fallbacks) {
  839. CRYPTO_THREAD_unlock(store->lock);
  840. return;
  841. }
  842. num_provs = sk_OSSL_PROVIDER_num(store->providers);
  843. for (i = 0; i < num_provs; i++) {
  844. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
  845. if (ossl_provider_up_ref(prov)) {
  846. if (CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  847. if (prov->flag_fallback) {
  848. if (provider_activate(prov, 0, 0) > 0)
  849. activated_fallback_count++;
  850. }
  851. CRYPTO_THREAD_unlock(prov->flag_lock);
  852. }
  853. ossl_provider_free(prov);
  854. }
  855. }
  856. /*
  857. * We assume that all fallbacks have been added to the store before
  858. * any fallback is activated.
  859. */
  860. if (activated_fallback_count > 0)
  861. store->use_fallbacks = 0;
  862. CRYPTO_THREAD_unlock(store->lock);
  863. }
  864. int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
  865. int (*cb)(OSSL_PROVIDER *provider,
  866. void *cbdata),
  867. void *cbdata)
  868. {
  869. int ret = 0, curr, max;
  870. struct provider_store_st *store = get_provider_store(ctx);
  871. STACK_OF(OSSL_PROVIDER) *provs = NULL;
  872. #ifndef FIPS_MODULE
  873. /*
  874. * Make sure any providers are loaded from config before we try to use
  875. * them.
  876. */
  877. if (ossl_lib_ctx_is_default(ctx))
  878. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  879. #endif
  880. if (store == NULL)
  881. return 1;
  882. provider_activate_fallbacks(store);
  883. /*
  884. * Under lock, grab a copy of the provider list and up_ref each
  885. * provider so that they don't disappear underneath us.
  886. */
  887. if (!CRYPTO_THREAD_read_lock(store->lock))
  888. return 0;
  889. provs = sk_OSSL_PROVIDER_dup(store->providers);
  890. if (provs == NULL) {
  891. CRYPTO_THREAD_unlock(store->lock);
  892. return 0;
  893. }
  894. max = sk_OSSL_PROVIDER_num(provs);
  895. /*
  896. * We work backwards through the stack so that we can safely delete items
  897. * as we go.
  898. */
  899. for (curr = max - 1; curr >= 0; curr--) {
  900. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  901. if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
  902. goto err_unlock;
  903. if (prov->flag_activated) {
  904. if (!ossl_provider_up_ref(prov)){
  905. CRYPTO_THREAD_unlock(prov->flag_lock);
  906. goto err_unlock;
  907. }
  908. /*
  909. * It's already activated, but we up the activated count to ensure
  910. * it remains activated until after we've called the user callback.
  911. */
  912. if (provider_activate(prov, 0, 1) < 0) {
  913. ossl_provider_free(prov);
  914. CRYPTO_THREAD_unlock(prov->flag_lock);
  915. goto err_unlock;
  916. }
  917. } else {
  918. sk_OSSL_PROVIDER_delete(provs, curr);
  919. max--;
  920. }
  921. CRYPTO_THREAD_unlock(prov->flag_lock);
  922. }
  923. CRYPTO_THREAD_unlock(store->lock);
  924. /*
  925. * Now, we sweep through all providers not under lock
  926. */
  927. for (curr = 0; curr < max; curr++) {
  928. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  929. if (!cb(prov, cbdata))
  930. goto finish;
  931. }
  932. curr = -1;
  933. ret = 1;
  934. goto finish;
  935. err_unlock:
  936. CRYPTO_THREAD_unlock(store->lock);
  937. finish:
  938. /*
  939. * The pop_free call doesn't do what we want on an error condition. We
  940. * either start from the first item in the stack, or part way through if
  941. * we only processed some of the items.
  942. */
  943. for (curr++; curr < max; curr++) {
  944. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  945. provider_deactivate(prov);
  946. ossl_provider_free(prov);
  947. }
  948. sk_OSSL_PROVIDER_free(provs);
  949. return ret;
  950. }
  951. int ossl_provider_available(OSSL_PROVIDER *prov)
  952. {
  953. int ret;
  954. if (prov != NULL) {
  955. provider_activate_fallbacks(prov->store);
  956. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  957. return 0;
  958. ret = prov->flag_activated;
  959. CRYPTO_THREAD_unlock(prov->flag_lock);
  960. return ret;
  961. }
  962. return 0;
  963. }
  964. /* Setters of Provider Object data */
  965. int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
  966. {
  967. if (prov == NULL)
  968. return 0;
  969. prov->flag_fallback = 1;
  970. return 1;
  971. }
  972. /* Getters of Provider Object data */
  973. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  974. {
  975. return prov->name;
  976. }
  977. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  978. {
  979. return prov->module;
  980. }
  981. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  982. {
  983. #ifdef FIPS_MODULE
  984. return NULL;
  985. #else
  986. return DSO_get_filename(prov->module);
  987. #endif
  988. }
  989. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  990. {
  991. #ifdef FIPS_MODULE
  992. return NULL;
  993. #else
  994. /* FIXME: Ensure it's a full path */
  995. return DSO_get_filename(prov->module);
  996. #endif
  997. }
  998. void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
  999. {
  1000. if (prov != NULL)
  1001. return prov->provctx;
  1002. return NULL;
  1003. }
  1004. const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
  1005. {
  1006. if (prov != NULL)
  1007. return prov->dispatch;
  1008. return NULL;
  1009. }
  1010. OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
  1011. {
  1012. return prov != NULL ? prov->libctx : NULL;
  1013. }
  1014. /* Wrappers around calls to the provider */
  1015. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  1016. {
  1017. if (prov->teardown != NULL
  1018. #ifndef FIPS_MODULE
  1019. && !prov->ischild
  1020. #endif
  1021. )
  1022. prov->teardown(prov->provctx);
  1023. }
  1024. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  1025. {
  1026. return prov->gettable_params == NULL
  1027. ? NULL : prov->gettable_params(prov->provctx);
  1028. }
  1029. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  1030. {
  1031. return prov->get_params == NULL
  1032. ? 0 : prov->get_params(prov->provctx, params);
  1033. }
  1034. int ossl_provider_self_test(const OSSL_PROVIDER *prov)
  1035. {
  1036. int ret;
  1037. if (prov->self_test == NULL)
  1038. return 1;
  1039. ret = prov->self_test(prov->provctx);
  1040. if (ret == 0)
  1041. (void)provider_flush_store_cache(prov);
  1042. return ret;
  1043. }
  1044. int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
  1045. const char *capability,
  1046. OSSL_CALLBACK *cb,
  1047. void *arg)
  1048. {
  1049. return prov->get_capabilities == NULL
  1050. ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
  1051. }
  1052. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  1053. int operation_id,
  1054. int *no_cache)
  1055. {
  1056. const OSSL_ALGORITHM *res;
  1057. if (prov->query_operation == NULL)
  1058. return NULL;
  1059. res = prov->query_operation(prov->provctx, operation_id, no_cache);
  1060. #if defined(OPENSSL_NO_CACHED_FETCH)
  1061. /* Forcing the non-caching of queries */
  1062. if (no_cache != NULL)
  1063. *no_cache = 1;
  1064. #endif
  1065. return res;
  1066. }
  1067. void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
  1068. int operation_id,
  1069. const OSSL_ALGORITHM *algs)
  1070. {
  1071. if (prov->unquery_operation != NULL)
  1072. prov->unquery_operation(prov->provctx, operation_id, algs);
  1073. }
  1074. int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
  1075. {
  1076. struct provider_store_st *store;
  1077. OSSL_PROVIDER *provider;
  1078. int i, num, res = 1;
  1079. if ((store = get_provider_store(libctx)) != NULL) {
  1080. if (!CRYPTO_THREAD_read_lock(store->lock))
  1081. return 0;
  1082. num = sk_OSSL_PROVIDER_num(store->providers);
  1083. for (i = 0; i < num; i++) {
  1084. provider = sk_OSSL_PROVIDER_value(store->providers, i);
  1085. if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
  1086. res = 0;
  1087. continue;
  1088. }
  1089. if (provider->operation_bits != NULL)
  1090. memset(provider->operation_bits, 0,
  1091. provider->operation_bits_sz);
  1092. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1093. }
  1094. CRYPTO_THREAD_unlock(store->lock);
  1095. return res;
  1096. }
  1097. return 0;
  1098. }
  1099. int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
  1100. {
  1101. size_t byte = bitnum / 8;
  1102. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1103. if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
  1104. return 0;
  1105. if (provider->operation_bits_sz <= byte) {
  1106. unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
  1107. byte + 1);
  1108. if (tmp == NULL) {
  1109. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1110. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  1111. return 0;
  1112. }
  1113. provider->operation_bits = tmp;
  1114. memset(provider->operation_bits + provider->operation_bits_sz,
  1115. '\0', byte + 1 - provider->operation_bits_sz);
  1116. provider->operation_bits_sz = byte + 1;
  1117. }
  1118. provider->operation_bits[byte] |= bit;
  1119. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1120. return 1;
  1121. }
  1122. int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
  1123. int *result)
  1124. {
  1125. size_t byte = bitnum / 8;
  1126. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1127. if (!ossl_assert(result != NULL)) {
  1128. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  1129. return 0;
  1130. }
  1131. *result = 0;
  1132. if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
  1133. return 0;
  1134. if (provider->operation_bits_sz > byte)
  1135. *result = ((provider->operation_bits[byte] & bit) != 0);
  1136. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1137. return 1;
  1138. }
  1139. #ifndef FIPS_MODULE
  1140. const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
  1141. {
  1142. return prov->handle;
  1143. }
  1144. int ossl_provider_is_child(const OSSL_PROVIDER *prov)
  1145. {
  1146. return prov->ischild;
  1147. }
  1148. int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
  1149. {
  1150. prov->handle = handle;
  1151. prov->ischild = 1;
  1152. return 1;
  1153. }
  1154. int ossl_provider_convert_to_child(OSSL_PROVIDER *prov,
  1155. const OSSL_CORE_HANDLE *handle,
  1156. OSSL_provider_init_fn *init_function)
  1157. {
  1158. int flush = 0;
  1159. if (!CRYPTO_THREAD_write_lock(prov->store->lock))
  1160. return 0;
  1161. if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  1162. CRYPTO_THREAD_unlock(prov->store->lock);
  1163. return 0;
  1164. }
  1165. /*
  1166. * The provider could be in one of three states: (1) Already a child,
  1167. * (2) Not a child (but eligible to be one), or (3) Not a child (not
  1168. * eligible to be one).
  1169. */
  1170. if (prov->flag_couldbechild) {
  1171. ossl_provider_set_child(prov, handle);
  1172. prov->init_function = init_function;
  1173. }
  1174. if (prov->ischild && provider_activate(prov, 0, 0)) {
  1175. flush = 1;
  1176. prov->store->use_fallbacks = 0;
  1177. }
  1178. CRYPTO_THREAD_unlock(prov->flag_lock);
  1179. CRYPTO_THREAD_unlock(prov->store->lock);
  1180. if (flush)
  1181. provider_flush_store_cache(prov);
  1182. /*
  1183. * We report success whether or not the provider was eligible for conversion
  1184. * to a child. If its not elgibile then it has already been loaded as a non
  1185. * child provider and we should keep it like that.
  1186. */
  1187. return 1;
  1188. }
  1189. int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
  1190. {
  1191. #ifndef FIPS_MODULE
  1192. struct provider_store_st *store = NULL;
  1193. int i, max;
  1194. OSSL_PROVIDER_CHILD_CB *child_cb;
  1195. if ((store = get_provider_store(libctx)) == NULL)
  1196. return 0;
  1197. if (!CRYPTO_THREAD_read_lock(store->lock))
  1198. return 0;
  1199. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1200. for (i = 0; i < max; i++) {
  1201. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1202. child_cb->global_props_cb(props, child_cb->cbdata);
  1203. }
  1204. CRYPTO_THREAD_unlock(store->lock);
  1205. #endif
  1206. return 1;
  1207. }
  1208. static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
  1209. int (*create_cb)(
  1210. const OSSL_CORE_HANDLE *provider,
  1211. void *cbdata),
  1212. int (*remove_cb)(
  1213. const OSSL_CORE_HANDLE *provider,
  1214. void *cbdata),
  1215. int (*global_props_cb)(
  1216. const char *props,
  1217. void *cbdata),
  1218. void *cbdata)
  1219. {
  1220. /*
  1221. * This is really an OSSL_PROVIDER that we created and cast to
  1222. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1223. */
  1224. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1225. OSSL_PROVIDER *prov;
  1226. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1227. struct provider_store_st *store = NULL;
  1228. int ret = 0, i, max;
  1229. OSSL_PROVIDER_CHILD_CB *child_cb;
  1230. char *propsstr = NULL;
  1231. if ((store = get_provider_store(libctx)) == NULL)
  1232. return 0;
  1233. child_cb = OPENSSL_malloc(sizeof(*child_cb));
  1234. if (child_cb == NULL)
  1235. return 0;
  1236. child_cb->prov = thisprov;
  1237. child_cb->create_cb = create_cb;
  1238. child_cb->remove_cb = remove_cb;
  1239. child_cb->global_props_cb = global_props_cb;
  1240. child_cb->cbdata = cbdata;
  1241. if (!CRYPTO_THREAD_write_lock(store->lock)) {
  1242. OPENSSL_free(child_cb);
  1243. return 0;
  1244. }
  1245. propsstr = evp_get_global_properties_str(libctx, 0);
  1246. if (propsstr != NULL) {
  1247. global_props_cb(propsstr, cbdata);
  1248. OPENSSL_free(propsstr);
  1249. }
  1250. max = sk_OSSL_PROVIDER_num(store->providers);
  1251. for (i = 0; i < max; i++) {
  1252. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1253. /*
  1254. * We require register_child_cb to be called during a provider init
  1255. * function. The currently initing provider will never be activated yet
  1256. * and we we should not attempt to aquire the flag_lock for it.
  1257. */
  1258. if (prov == thisprov)
  1259. continue;
  1260. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1261. break;
  1262. /*
  1263. * We hold the lock while calling the user callback. This means that the
  1264. * user callback must be short and simple and not do anything likely to
  1265. * cause a deadlock.
  1266. */
  1267. if (prov->flag_activated
  1268. && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
  1269. break;
  1270. CRYPTO_THREAD_unlock(prov->flag_lock);
  1271. }
  1272. if (i == max) {
  1273. /* Success */
  1274. ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
  1275. }
  1276. if (i != max || ret <= 0) {
  1277. /* Failed during creation. Remove everything we just added */
  1278. for (; i >= 0; i--) {
  1279. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1280. remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
  1281. }
  1282. OPENSSL_free(child_cb);
  1283. ret = 0;
  1284. }
  1285. CRYPTO_THREAD_unlock(store->lock);
  1286. return ret;
  1287. }
  1288. static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
  1289. {
  1290. /*
  1291. * This is really an OSSL_PROVIDER that we created and cast to
  1292. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1293. */
  1294. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1295. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1296. struct provider_store_st *store = NULL;
  1297. int i, max;
  1298. OSSL_PROVIDER_CHILD_CB *child_cb;
  1299. if ((store = get_provider_store(libctx)) == NULL)
  1300. return;
  1301. if (!CRYPTO_THREAD_write_lock(store->lock))
  1302. return;
  1303. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1304. for (i = 0; i < max; i++) {
  1305. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1306. if (child_cb->prov == thisprov) {
  1307. /* Found an entry */
  1308. sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
  1309. OPENSSL_free(child_cb);
  1310. break;
  1311. }
  1312. }
  1313. CRYPTO_THREAD_unlock(store->lock);
  1314. }
  1315. #endif
  1316. /*-
  1317. * Core functions for the provider
  1318. * ===============================
  1319. *
  1320. * This is the set of functions that the core makes available to the provider
  1321. */
  1322. /*
  1323. * This returns a list of Provider Object parameters with their types, for
  1324. * discovery. We do not expect that many providers will use this, but one
  1325. * never knows.
  1326. */
  1327. static const OSSL_PARAM param_types[] = {
  1328. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  1329. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
  1330. NULL, 0),
  1331. #ifndef FIPS_MODULE
  1332. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
  1333. NULL, 0),
  1334. #endif
  1335. OSSL_PARAM_END
  1336. };
  1337. /*
  1338. * Forward declare all the functions that are provided aa dispatch.
  1339. * This ensures that the compiler will complain if they aren't defined
  1340. * with the correct signature.
  1341. */
  1342. static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
  1343. static OSSL_FUNC_core_get_params_fn core_get_params;
  1344. static OSSL_FUNC_core_thread_start_fn core_thread_start;
  1345. static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
  1346. #ifndef FIPS_MODULE
  1347. static OSSL_FUNC_core_new_error_fn core_new_error;
  1348. static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
  1349. static OSSL_FUNC_core_vset_error_fn core_vset_error;
  1350. static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
  1351. static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
  1352. static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
  1353. #endif
  1354. static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
  1355. {
  1356. return param_types;
  1357. }
  1358. static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
  1359. {
  1360. int i;
  1361. OSSL_PARAM *p;
  1362. /*
  1363. * We created this object originally and we know it is actually an
  1364. * OSSL_PROVIDER *, so the cast is safe
  1365. */
  1366. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1367. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
  1368. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  1369. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
  1370. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  1371. #ifndef FIPS_MODULE
  1372. if ((p = OSSL_PARAM_locate(params,
  1373. OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
  1374. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  1375. #endif
  1376. if (prov->parameters == NULL)
  1377. return 1;
  1378. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  1379. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  1380. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  1381. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  1382. }
  1383. return 1;
  1384. }
  1385. static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
  1386. {
  1387. /*
  1388. * We created this object originally and we know it is actually an
  1389. * OSSL_PROVIDER *, so the cast is safe
  1390. */
  1391. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1392. /*
  1393. * Using ossl_provider_libctx would be wrong as that returns
  1394. * NULL for |prov| == NULL and NULL libctx has a special meaning
  1395. * that does not apply here. Here |prov| == NULL can happen only in
  1396. * case of a coding error.
  1397. */
  1398. assert(prov != NULL);
  1399. return (OPENSSL_CORE_CTX *)prov->libctx;
  1400. }
  1401. static int core_thread_start(const OSSL_CORE_HANDLE *handle,
  1402. OSSL_thread_stop_handler_fn handfn,
  1403. void *arg)
  1404. {
  1405. /*
  1406. * We created this object originally and we know it is actually an
  1407. * OSSL_PROVIDER *, so the cast is safe
  1408. */
  1409. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1410. return ossl_init_thread_start(prov, arg, handfn);
  1411. }
  1412. /*
  1413. * The FIPS module inner provider doesn't implement these. They aren't
  1414. * needed there, since the FIPS module upcalls are always the outer provider
  1415. * ones.
  1416. */
  1417. #ifndef FIPS_MODULE
  1418. /*
  1419. * These error functions should use |handle| to select the proper
  1420. * library context to report in the correct error stack if error
  1421. * stacks become tied to the library context.
  1422. * We cannot currently do that since there's no support for it in the
  1423. * ERR subsystem.
  1424. */
  1425. static void core_new_error(const OSSL_CORE_HANDLE *handle)
  1426. {
  1427. ERR_new();
  1428. }
  1429. static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  1430. const char *file, int line, const char *func)
  1431. {
  1432. ERR_set_debug(file, line, func);
  1433. }
  1434. static void core_vset_error(const OSSL_CORE_HANDLE *handle,
  1435. uint32_t reason, const char *fmt, va_list args)
  1436. {
  1437. /*
  1438. * We created this object originally and we know it is actually an
  1439. * OSSL_PROVIDER *, so the cast is safe
  1440. */
  1441. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1442. /*
  1443. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  1444. * error and will be treated as such. Otherwise, it's a new style
  1445. * provider error and will be treated as such.
  1446. */
  1447. if (ERR_GET_LIB(reason) != 0) {
  1448. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  1449. } else {
  1450. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  1451. }
  1452. }
  1453. static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
  1454. {
  1455. return ERR_set_mark();
  1456. }
  1457. static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
  1458. {
  1459. return ERR_clear_last_mark();
  1460. }
  1461. static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
  1462. {
  1463. return ERR_pop_to_mark();
  1464. }
  1465. #endif /* FIPS_MODULE */
  1466. /*
  1467. * Functions provided by the core.
  1468. */
  1469. static const OSSL_DISPATCH core_dispatch_[] = {
  1470. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  1471. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  1472. { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
  1473. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  1474. #ifndef FIPS_MODULE
  1475. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  1476. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  1477. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  1478. { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
  1479. { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
  1480. (void (*)(void))core_clear_last_error_mark },
  1481. { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
  1482. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
  1483. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
  1484. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
  1485. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
  1486. { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
  1487. { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
  1488. { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
  1489. { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
  1490. { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
  1491. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
  1492. { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
  1493. { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
  1494. { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
  1495. { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
  1496. { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
  1497. { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
  1498. #endif
  1499. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  1500. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  1501. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  1502. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  1503. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  1504. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  1505. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  1506. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  1507. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  1508. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  1509. (void (*)(void))CRYPTO_secure_clear_free },
  1510. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  1511. (void (*)(void))CRYPTO_secure_allocated },
  1512. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  1513. #ifndef FIPS_MODULE
  1514. { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
  1515. (void (*)(void))ossl_provider_register_child_cb },
  1516. { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
  1517. (void (*)(void))ossl_provider_deregister_child_cb },
  1518. { OSSL_FUNC_PROVIDER_NAME,
  1519. (void (*)(void))OSSL_PROVIDER_get0_name },
  1520. { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
  1521. (void (*)(void))OSSL_PROVIDER_get0_provider_ctx },
  1522. { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
  1523. (void (*)(void))OSSL_PROVIDER_get0_dispatch },
  1524. { OSSL_FUNC_PROVIDER_UP_REF,
  1525. (void (*)(void))provider_up_ref_intern },
  1526. { OSSL_FUNC_PROVIDER_FREE,
  1527. (void (*)(void))provider_free_intern },
  1528. #endif
  1529. { 0, NULL }
  1530. };
  1531. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;