provider_core.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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. * TODO: We may have to reconsider this, IF we find ourselves adding
  860. * fallbacks after any previous fallback has been activated.
  861. */
  862. if (activated_fallback_count > 0)
  863. store->use_fallbacks = 0;
  864. CRYPTO_THREAD_unlock(store->lock);
  865. }
  866. int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
  867. int (*cb)(OSSL_PROVIDER *provider,
  868. void *cbdata),
  869. void *cbdata)
  870. {
  871. int ret = 0, curr, max;
  872. struct provider_store_st *store = get_provider_store(ctx);
  873. STACK_OF(OSSL_PROVIDER) *provs = NULL;
  874. #ifndef FIPS_MODULE
  875. /*
  876. * Make sure any providers are loaded from config before we try to use
  877. * them.
  878. */
  879. if (ossl_lib_ctx_is_default(ctx))
  880. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  881. #endif
  882. if (store == NULL)
  883. return 1;
  884. provider_activate_fallbacks(store);
  885. /*
  886. * Under lock, grab a copy of the provider list and up_ref each
  887. * provider so that they don't disappear underneath us.
  888. */
  889. if (!CRYPTO_THREAD_read_lock(store->lock))
  890. return 0;
  891. provs = sk_OSSL_PROVIDER_dup(store->providers);
  892. if (provs == NULL) {
  893. CRYPTO_THREAD_unlock(store->lock);
  894. return 0;
  895. }
  896. max = sk_OSSL_PROVIDER_num(provs);
  897. /*
  898. * We work backwards through the stack so that we can safely delete items
  899. * as we go.
  900. */
  901. for (curr = max - 1; curr >= 0; curr--) {
  902. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  903. if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
  904. goto err_unlock;
  905. if (prov->flag_activated) {
  906. if (!ossl_provider_up_ref(prov)){
  907. CRYPTO_THREAD_unlock(prov->flag_lock);
  908. goto err_unlock;
  909. }
  910. /*
  911. * It's already activated, but we up the activated count to ensure
  912. * it remains activated until after we've called the user callback.
  913. */
  914. if (provider_activate(prov, 0, 1) < 0) {
  915. ossl_provider_free(prov);
  916. CRYPTO_THREAD_unlock(prov->flag_lock);
  917. goto err_unlock;
  918. }
  919. } else {
  920. sk_OSSL_PROVIDER_delete(provs, curr);
  921. max--;
  922. }
  923. CRYPTO_THREAD_unlock(prov->flag_lock);
  924. }
  925. CRYPTO_THREAD_unlock(store->lock);
  926. /*
  927. * Now, we sweep through all providers not under lock
  928. */
  929. for (curr = 0; curr < max; curr++) {
  930. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  931. if (!cb(prov, cbdata))
  932. goto finish;
  933. }
  934. curr = -1;
  935. ret = 1;
  936. goto finish;
  937. err_unlock:
  938. CRYPTO_THREAD_unlock(store->lock);
  939. finish:
  940. /*
  941. * The pop_free call doesn't do what we want on an error condition. We
  942. * either start from the first item in the stack, or part way through if
  943. * we only processed some of the items.
  944. */
  945. for (curr++; curr < max; curr++) {
  946. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
  947. provider_deactivate(prov);
  948. ossl_provider_free(prov);
  949. }
  950. sk_OSSL_PROVIDER_free(provs);
  951. return ret;
  952. }
  953. int ossl_provider_available(OSSL_PROVIDER *prov)
  954. {
  955. int ret;
  956. if (prov != NULL) {
  957. provider_activate_fallbacks(prov->store);
  958. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  959. return 0;
  960. ret = prov->flag_activated;
  961. CRYPTO_THREAD_unlock(prov->flag_lock);
  962. return ret;
  963. }
  964. return 0;
  965. }
  966. /* Setters of Provider Object data */
  967. int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
  968. {
  969. if (prov == NULL)
  970. return 0;
  971. prov->flag_fallback = 1;
  972. return 1;
  973. }
  974. /* Getters of Provider Object data */
  975. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  976. {
  977. return prov->name;
  978. }
  979. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  980. {
  981. return prov->module;
  982. }
  983. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  984. {
  985. #ifdef FIPS_MODULE
  986. return NULL;
  987. #else
  988. return DSO_get_filename(prov->module);
  989. #endif
  990. }
  991. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  992. {
  993. #ifdef FIPS_MODULE
  994. return NULL;
  995. #else
  996. /* FIXME: Ensure it's a full path */
  997. return DSO_get_filename(prov->module);
  998. #endif
  999. }
  1000. void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
  1001. {
  1002. if (prov != NULL)
  1003. return prov->provctx;
  1004. return NULL;
  1005. }
  1006. const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
  1007. {
  1008. if (prov != NULL)
  1009. return prov->dispatch;
  1010. return NULL;
  1011. }
  1012. OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
  1013. {
  1014. return prov != NULL ? prov->libctx : NULL;
  1015. }
  1016. /* Wrappers around calls to the provider */
  1017. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  1018. {
  1019. if (prov->teardown != NULL
  1020. #ifndef FIPS_MODULE
  1021. && !prov->ischild
  1022. #endif
  1023. )
  1024. prov->teardown(prov->provctx);
  1025. }
  1026. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  1027. {
  1028. return prov->gettable_params == NULL
  1029. ? NULL : prov->gettable_params(prov->provctx);
  1030. }
  1031. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  1032. {
  1033. return prov->get_params == NULL
  1034. ? 0 : prov->get_params(prov->provctx, params);
  1035. }
  1036. int ossl_provider_self_test(const OSSL_PROVIDER *prov)
  1037. {
  1038. int ret;
  1039. if (prov->self_test == NULL)
  1040. return 1;
  1041. ret = prov->self_test(prov->provctx);
  1042. if (ret == 0)
  1043. (void)provider_flush_store_cache(prov);
  1044. return ret;
  1045. }
  1046. int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
  1047. const char *capability,
  1048. OSSL_CALLBACK *cb,
  1049. void *arg)
  1050. {
  1051. return prov->get_capabilities == NULL
  1052. ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
  1053. }
  1054. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  1055. int operation_id,
  1056. int *no_cache)
  1057. {
  1058. const OSSL_ALGORITHM *res;
  1059. if (prov->query_operation == NULL)
  1060. return NULL;
  1061. res = prov->query_operation(prov->provctx, operation_id, no_cache);
  1062. #if defined(OPENSSL_NO_CACHED_FETCH)
  1063. /* Forcing the non-caching of queries */
  1064. if (no_cache != NULL)
  1065. *no_cache = 1;
  1066. #endif
  1067. return res;
  1068. }
  1069. void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
  1070. int operation_id,
  1071. const OSSL_ALGORITHM *algs)
  1072. {
  1073. if (prov->unquery_operation != NULL)
  1074. prov->unquery_operation(prov->provctx, operation_id, algs);
  1075. }
  1076. int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
  1077. {
  1078. struct provider_store_st *store;
  1079. OSSL_PROVIDER *provider;
  1080. int i, num, res = 1;
  1081. if ((store = get_provider_store(libctx)) != NULL) {
  1082. if (!CRYPTO_THREAD_read_lock(store->lock))
  1083. return 0;
  1084. num = sk_OSSL_PROVIDER_num(store->providers);
  1085. for (i = 0; i < num; i++) {
  1086. provider = sk_OSSL_PROVIDER_value(store->providers, i);
  1087. if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
  1088. res = 0;
  1089. continue;
  1090. }
  1091. if (provider->operation_bits != NULL)
  1092. memset(provider->operation_bits, 0,
  1093. provider->operation_bits_sz);
  1094. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1095. }
  1096. CRYPTO_THREAD_unlock(store->lock);
  1097. return res;
  1098. }
  1099. return 0;
  1100. }
  1101. int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
  1102. {
  1103. size_t byte = bitnum / 8;
  1104. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1105. if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
  1106. return 0;
  1107. if (provider->operation_bits_sz <= byte) {
  1108. unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
  1109. byte + 1);
  1110. if (tmp == NULL) {
  1111. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1112. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  1113. return 0;
  1114. }
  1115. provider->operation_bits = tmp;
  1116. memset(provider->operation_bits + provider->operation_bits_sz,
  1117. '\0', byte + 1 - provider->operation_bits_sz);
  1118. provider->operation_bits_sz = byte + 1;
  1119. }
  1120. provider->operation_bits[byte] |= bit;
  1121. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1122. return 1;
  1123. }
  1124. int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
  1125. int *result)
  1126. {
  1127. size_t byte = bitnum / 8;
  1128. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  1129. if (!ossl_assert(result != NULL)) {
  1130. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  1131. return 0;
  1132. }
  1133. *result = 0;
  1134. if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
  1135. return 0;
  1136. if (provider->operation_bits_sz > byte)
  1137. *result = ((provider->operation_bits[byte] & bit) != 0);
  1138. CRYPTO_THREAD_unlock(provider->opbits_lock);
  1139. return 1;
  1140. }
  1141. #ifndef FIPS_MODULE
  1142. const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
  1143. {
  1144. return prov->handle;
  1145. }
  1146. int ossl_provider_is_child(const OSSL_PROVIDER *prov)
  1147. {
  1148. return prov->ischild;
  1149. }
  1150. int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
  1151. {
  1152. prov->handle = handle;
  1153. prov->ischild = 1;
  1154. return 1;
  1155. }
  1156. int ossl_provider_convert_to_child(OSSL_PROVIDER *prov,
  1157. const OSSL_CORE_HANDLE *handle,
  1158. OSSL_provider_init_fn *init_function)
  1159. {
  1160. int flush = 0;
  1161. if (!CRYPTO_THREAD_write_lock(prov->store->lock))
  1162. return 0;
  1163. if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
  1164. CRYPTO_THREAD_unlock(prov->store->lock);
  1165. return 0;
  1166. }
  1167. /*
  1168. * The provider could be in one of three states: (1) Already a child,
  1169. * (2) Not a child (but eligible to be one), or (3) Not a child (not
  1170. * eligible to be one).
  1171. */
  1172. if (prov->flag_couldbechild) {
  1173. ossl_provider_set_child(prov, handle);
  1174. prov->init_function = init_function;
  1175. }
  1176. if (prov->ischild && provider_activate(prov, 0, 0)) {
  1177. flush = 1;
  1178. prov->store->use_fallbacks = 0;
  1179. }
  1180. CRYPTO_THREAD_unlock(prov->flag_lock);
  1181. CRYPTO_THREAD_unlock(prov->store->lock);
  1182. if (flush)
  1183. provider_flush_store_cache(prov);
  1184. /*
  1185. * We report success whether or not the provider was eligible for conversion
  1186. * to a child. If its not elgibile then it has already been loaded as a non
  1187. * child provider and we should keep it like that.
  1188. */
  1189. return 1;
  1190. }
  1191. int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
  1192. {
  1193. #ifndef FIPS_MODULE
  1194. struct provider_store_st *store = NULL;
  1195. int i, max;
  1196. OSSL_PROVIDER_CHILD_CB *child_cb;
  1197. if ((store = get_provider_store(libctx)) == NULL)
  1198. return 0;
  1199. if (!CRYPTO_THREAD_read_lock(store->lock))
  1200. return 0;
  1201. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1202. for (i = 0; i < max; i++) {
  1203. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1204. child_cb->global_props_cb(props, child_cb->cbdata);
  1205. }
  1206. CRYPTO_THREAD_unlock(store->lock);
  1207. #endif
  1208. return 1;
  1209. }
  1210. static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
  1211. int (*create_cb)(
  1212. const OSSL_CORE_HANDLE *provider,
  1213. void *cbdata),
  1214. int (*remove_cb)(
  1215. const OSSL_CORE_HANDLE *provider,
  1216. void *cbdata),
  1217. int (*global_props_cb)(
  1218. const char *props,
  1219. void *cbdata),
  1220. void *cbdata)
  1221. {
  1222. /*
  1223. * This is really an OSSL_PROVIDER that we created and cast to
  1224. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1225. */
  1226. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1227. OSSL_PROVIDER *prov;
  1228. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1229. struct provider_store_st *store = NULL;
  1230. int ret = 0, i, max;
  1231. OSSL_PROVIDER_CHILD_CB *child_cb;
  1232. char *propsstr = NULL;
  1233. if ((store = get_provider_store(libctx)) == NULL)
  1234. return 0;
  1235. child_cb = OPENSSL_malloc(sizeof(*child_cb));
  1236. if (child_cb == NULL)
  1237. return 0;
  1238. child_cb->prov = thisprov;
  1239. child_cb->create_cb = create_cb;
  1240. child_cb->remove_cb = remove_cb;
  1241. child_cb->global_props_cb = global_props_cb;
  1242. child_cb->cbdata = cbdata;
  1243. if (!CRYPTO_THREAD_write_lock(store->lock)) {
  1244. OPENSSL_free(child_cb);
  1245. return 0;
  1246. }
  1247. propsstr = evp_get_global_properties_str(libctx, 0);
  1248. if (propsstr != NULL) {
  1249. global_props_cb(propsstr, cbdata);
  1250. OPENSSL_free(propsstr);
  1251. }
  1252. max = sk_OSSL_PROVIDER_num(store->providers);
  1253. for (i = 0; i < max; i++) {
  1254. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1255. /*
  1256. * We require register_child_cb to be called during a provider init
  1257. * function. The currently initing provider will never be activated yet
  1258. * and we we should not attempt to aquire the flag_lock for it.
  1259. */
  1260. if (prov == thisprov)
  1261. continue;
  1262. if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
  1263. break;
  1264. /*
  1265. * We hold the lock while calling the user callback. This means that the
  1266. * user callback must be short and simple and not do anything likely to
  1267. * cause a deadlock.
  1268. */
  1269. if (prov->flag_activated
  1270. && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
  1271. break;
  1272. CRYPTO_THREAD_unlock(prov->flag_lock);
  1273. }
  1274. if (i == max) {
  1275. /* Success */
  1276. ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
  1277. }
  1278. if (i != max || ret <= 0) {
  1279. /* Failed during creation. Remove everything we just added */
  1280. for (; i >= 0; i--) {
  1281. prov = sk_OSSL_PROVIDER_value(store->providers, i);
  1282. remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
  1283. }
  1284. OPENSSL_free(child_cb);
  1285. ret = 0;
  1286. }
  1287. CRYPTO_THREAD_unlock(store->lock);
  1288. return ret;
  1289. }
  1290. static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
  1291. {
  1292. /*
  1293. * This is really an OSSL_PROVIDER that we created and cast to
  1294. * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
  1295. */
  1296. OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
  1297. OSSL_LIB_CTX *libctx = thisprov->libctx;
  1298. struct provider_store_st *store = NULL;
  1299. int i, max;
  1300. OSSL_PROVIDER_CHILD_CB *child_cb;
  1301. if ((store = get_provider_store(libctx)) == NULL)
  1302. return;
  1303. if (!CRYPTO_THREAD_write_lock(store->lock))
  1304. return;
  1305. max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
  1306. for (i = 0; i < max; i++) {
  1307. child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
  1308. if (child_cb->prov == thisprov) {
  1309. /* Found an entry */
  1310. sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
  1311. OPENSSL_free(child_cb);
  1312. break;
  1313. }
  1314. }
  1315. CRYPTO_THREAD_unlock(store->lock);
  1316. }
  1317. #endif
  1318. /*-
  1319. * Core functions for the provider
  1320. * ===============================
  1321. *
  1322. * This is the set of functions that the core makes available to the provider
  1323. */
  1324. /*
  1325. * This returns a list of Provider Object parameters with their types, for
  1326. * discovery. We do not expect that many providers will use this, but one
  1327. * never knows.
  1328. */
  1329. static const OSSL_PARAM param_types[] = {
  1330. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  1331. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
  1332. NULL, 0),
  1333. #ifndef FIPS_MODULE
  1334. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
  1335. NULL, 0),
  1336. #endif
  1337. OSSL_PARAM_END
  1338. };
  1339. /*
  1340. * Forward declare all the functions that are provided aa dispatch.
  1341. * This ensures that the compiler will complain if they aren't defined
  1342. * with the correct signature.
  1343. */
  1344. static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
  1345. static OSSL_FUNC_core_get_params_fn core_get_params;
  1346. static OSSL_FUNC_core_thread_start_fn core_thread_start;
  1347. static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
  1348. #ifndef FIPS_MODULE
  1349. static OSSL_FUNC_core_new_error_fn core_new_error;
  1350. static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
  1351. static OSSL_FUNC_core_vset_error_fn core_vset_error;
  1352. static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
  1353. static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
  1354. static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
  1355. #endif
  1356. static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
  1357. {
  1358. return param_types;
  1359. }
  1360. static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
  1361. {
  1362. int i;
  1363. OSSL_PARAM *p;
  1364. /*
  1365. * We created this object originally and we know it is actually an
  1366. * OSSL_PROVIDER *, so the cast is safe
  1367. */
  1368. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1369. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
  1370. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  1371. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
  1372. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  1373. #ifndef FIPS_MODULE
  1374. if ((p = OSSL_PARAM_locate(params,
  1375. OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
  1376. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  1377. #endif
  1378. if (prov->parameters == NULL)
  1379. return 1;
  1380. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  1381. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  1382. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  1383. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  1384. }
  1385. return 1;
  1386. }
  1387. static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
  1388. {
  1389. /*
  1390. * We created this object originally and we know it is actually an
  1391. * OSSL_PROVIDER *, so the cast is safe
  1392. */
  1393. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1394. /*
  1395. * Using ossl_provider_libctx would be wrong as that returns
  1396. * NULL for |prov| == NULL and NULL libctx has a special meaning
  1397. * that does not apply here. Here |prov| == NULL can happen only in
  1398. * case of a coding error.
  1399. */
  1400. assert(prov != NULL);
  1401. return (OPENSSL_CORE_CTX *)prov->libctx;
  1402. }
  1403. static int core_thread_start(const OSSL_CORE_HANDLE *handle,
  1404. OSSL_thread_stop_handler_fn handfn,
  1405. void *arg)
  1406. {
  1407. /*
  1408. * We created this object originally and we know it is actually an
  1409. * OSSL_PROVIDER *, so the cast is safe
  1410. */
  1411. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1412. return ossl_init_thread_start(prov, arg, handfn);
  1413. }
  1414. /*
  1415. * The FIPS module inner provider doesn't implement these. They aren't
  1416. * needed there, since the FIPS module upcalls are always the outer provider
  1417. * ones.
  1418. */
  1419. #ifndef FIPS_MODULE
  1420. /*
  1421. * These error functions should use |handle| to select the proper
  1422. * library context to report in the correct error stack if error
  1423. * stacks become tied to the library context.
  1424. * We cannot currently do that since there's no support for it in the
  1425. * ERR subsystem.
  1426. */
  1427. static void core_new_error(const OSSL_CORE_HANDLE *handle)
  1428. {
  1429. ERR_new();
  1430. }
  1431. static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  1432. const char *file, int line, const char *func)
  1433. {
  1434. ERR_set_debug(file, line, func);
  1435. }
  1436. static void core_vset_error(const OSSL_CORE_HANDLE *handle,
  1437. uint32_t reason, const char *fmt, va_list args)
  1438. {
  1439. /*
  1440. * We created this object originally and we know it is actually an
  1441. * OSSL_PROVIDER *, so the cast is safe
  1442. */
  1443. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  1444. /*
  1445. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  1446. * error and will be treated as such. Otherwise, it's a new style
  1447. * provider error and will be treated as such.
  1448. */
  1449. if (ERR_GET_LIB(reason) != 0) {
  1450. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  1451. } else {
  1452. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  1453. }
  1454. }
  1455. static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
  1456. {
  1457. return ERR_set_mark();
  1458. }
  1459. static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
  1460. {
  1461. return ERR_clear_last_mark();
  1462. }
  1463. static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
  1464. {
  1465. return ERR_pop_to_mark();
  1466. }
  1467. #endif /* FIPS_MODULE */
  1468. /*
  1469. * Functions provided by the core.
  1470. */
  1471. static const OSSL_DISPATCH core_dispatch_[] = {
  1472. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  1473. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  1474. { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
  1475. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  1476. #ifndef FIPS_MODULE
  1477. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  1478. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  1479. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  1480. { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
  1481. { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
  1482. (void (*)(void))core_clear_last_error_mark },
  1483. { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
  1484. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
  1485. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
  1486. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
  1487. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
  1488. { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
  1489. { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
  1490. { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
  1491. { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
  1492. { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
  1493. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
  1494. { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
  1495. { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
  1496. { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
  1497. { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
  1498. { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
  1499. { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
  1500. #endif
  1501. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  1502. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  1503. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  1504. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  1505. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  1506. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  1507. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  1508. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  1509. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  1510. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  1511. (void (*)(void))CRYPTO_secure_clear_free },
  1512. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  1513. (void (*)(void))CRYPTO_secure_allocated },
  1514. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  1515. #ifndef FIPS_MODULE
  1516. { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
  1517. (void (*)(void))ossl_provider_register_child_cb },
  1518. { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
  1519. (void (*)(void))ossl_provider_deregister_child_cb },
  1520. { OSSL_FUNC_PROVIDER_NAME,
  1521. (void (*)(void))OSSL_PROVIDER_name },
  1522. { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
  1523. (void (*)(void))OSSL_PROVIDER_get0_provider_ctx },
  1524. { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
  1525. (void (*)(void))OSSL_PROVIDER_get0_dispatch },
  1526. { OSSL_FUNC_PROVIDER_UP_REF,
  1527. (void (*)(void))provider_up_ref_intern },
  1528. { OSSL_FUNC_PROVIDER_FREE,
  1529. (void (*)(void))provider_free_intern },
  1530. #endif
  1531. { 0, NULL }
  1532. };
  1533. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;