provider_core.c 70 KB

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