provider_core.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * Copyright 2019-2020 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 <openssl/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/provider.h>
  13. #include <openssl/params.h>
  14. #include <openssl/opensslv.h>
  15. #include "crypto/cryptlib.h"
  16. #include "crypto/evp.h" /* evp_method_store_flush */
  17. #include "internal/nelem.h"
  18. #include "internal/thread_once.h"
  19. #include "internal/provider.h"
  20. #include "internal/refcount.h"
  21. #include "provider_local.h"
  22. #ifndef FIPS_MODULE
  23. # include <openssl/self_test.h>
  24. #endif
  25. static OSSL_PROVIDER *provider_new(const char *name,
  26. OSSL_provider_init_fn *init_function);
  27. /*-
  28. * Provider Object structure
  29. * =========================
  30. */
  31. typedef struct {
  32. char *name;
  33. char *value;
  34. } INFOPAIR;
  35. DEFINE_STACK_OF(INFOPAIR)
  36. struct provider_store_st; /* Forward declaration */
  37. struct ossl_provider_st {
  38. /* Flag bits */
  39. unsigned int flag_initialized:1;
  40. unsigned int flag_fallback:1; /* Can be used as fallback */
  41. unsigned int flag_activated_as_fallback:1;
  42. /* OpenSSL library side data */
  43. CRYPTO_REF_COUNT refcnt;
  44. CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
  45. char *name;
  46. char *path;
  47. DSO *module;
  48. OSSL_provider_init_fn *init_function;
  49. STACK_OF(INFOPAIR) *parameters;
  50. OSSL_LIB_CTX *libctx; /* The library context this instance is in */
  51. struct provider_store_st *store; /* The store this instance belongs to */
  52. #ifndef FIPS_MODULE
  53. /*
  54. * In the FIPS module inner provider, this isn't needed, since the
  55. * error upcalls are always direct calls to the outer provider.
  56. */
  57. int error_lib; /* ERR library number, one for each provider */
  58. # ifndef OPENSSL_NO_ERR
  59. ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
  60. # endif
  61. #endif
  62. /* Provider side functions */
  63. OSSL_FUNC_provider_teardown_fn *teardown;
  64. OSSL_FUNC_provider_gettable_params_fn *gettable_params;
  65. OSSL_FUNC_provider_get_params_fn *get_params;
  66. OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
  67. OSSL_FUNC_provider_self_test_fn *self_test;
  68. OSSL_FUNC_provider_query_operation_fn *query_operation;
  69. /*
  70. * Cache of bit to indicate of query_operation() has been called on
  71. * a specific operation or not.
  72. */
  73. unsigned char *operation_bits;
  74. size_t operation_bits_sz;
  75. /* Provider side data */
  76. void *provctx;
  77. };
  78. DEFINE_STACK_OF(OSSL_PROVIDER)
  79. static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
  80. const OSSL_PROVIDER * const *b)
  81. {
  82. return strcmp((*a)->name, (*b)->name);
  83. }
  84. /*-
  85. * Provider Object store
  86. * =====================
  87. *
  88. * The Provider Object store is a library context object, and therefore needs
  89. * an index.
  90. */
  91. struct provider_store_st {
  92. STACK_OF(OSSL_PROVIDER) *providers;
  93. CRYPTO_RWLOCK *lock;
  94. char *default_path;
  95. unsigned int use_fallbacks:1;
  96. };
  97. /*
  98. * provider_deactivate_free() is a wrapper around ossl_provider_free()
  99. * that also makes sure that activated fallback providers are deactivated.
  100. * This is simply done by freeing them an extra time, to compensate for the
  101. * refcount that provider_activate_fallbacks() gives them.
  102. * Since this is only called when the provider store is being emptied, we
  103. * don't need to care about any lock.
  104. */
  105. static void provider_deactivate_free(OSSL_PROVIDER *prov)
  106. {
  107. int extra_free = (prov->flag_initialized
  108. && prov->flag_activated_as_fallback);
  109. if (extra_free)
  110. ossl_provider_free(prov);
  111. ossl_provider_free(prov);
  112. }
  113. static void provider_store_free(void *vstore)
  114. {
  115. struct provider_store_st *store = vstore;
  116. if (store == NULL)
  117. return;
  118. OPENSSL_free(store->default_path);
  119. sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
  120. CRYPTO_THREAD_lock_free(store->lock);
  121. OPENSSL_free(store);
  122. }
  123. static void *provider_store_new(OSSL_LIB_CTX *ctx)
  124. {
  125. struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
  126. const struct predefined_providers_st *p = NULL;
  127. if (store == NULL
  128. || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
  129. || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  130. provider_store_free(store);
  131. return NULL;
  132. }
  133. store->use_fallbacks = 1;
  134. for (p = predefined_providers; p->name != NULL; p++) {
  135. OSSL_PROVIDER *prov = NULL;
  136. /*
  137. * We use the internal constructor directly here,
  138. * otherwise we get a call loop
  139. */
  140. prov = provider_new(p->name, p->init);
  141. if (prov == NULL
  142. || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  143. ossl_provider_free(prov);
  144. provider_store_free(store);
  145. CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
  146. return NULL;
  147. }
  148. prov->libctx = ctx;
  149. prov->store = store;
  150. #ifndef FIPS_MODULE
  151. prov->error_lib = ERR_get_next_error_library();
  152. #endif
  153. if(p->is_fallback)
  154. ossl_provider_set_fallback(prov);
  155. }
  156. return store;
  157. }
  158. static const OSSL_LIB_CTX_METHOD provider_store_method = {
  159. provider_store_new,
  160. provider_store_free,
  161. };
  162. static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
  163. {
  164. struct provider_store_st *store = NULL;
  165. store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
  166. &provider_store_method);
  167. if (store == NULL)
  168. CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
  169. return store;
  170. }
  171. int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
  172. {
  173. struct provider_store_st *store;
  174. if ((store = get_provider_store(libctx)) != NULL) {
  175. store->use_fallbacks = 0;
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
  181. int noconfig)
  182. {
  183. struct provider_store_st *store = NULL;
  184. OSSL_PROVIDER *prov = NULL;
  185. if ((store = get_provider_store(libctx)) != NULL) {
  186. OSSL_PROVIDER tmpl = { 0, };
  187. int i;
  188. #ifndef FIPS_MODULE
  189. /*
  190. * Make sure any providers are loaded from config before we try to find
  191. * them.
  192. */
  193. if (!noconfig)
  194. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  195. #endif
  196. tmpl.name = (char *)name;
  197. CRYPTO_THREAD_write_lock(store->lock);
  198. if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
  199. || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
  200. || !ossl_provider_up_ref(prov))
  201. prov = NULL;
  202. CRYPTO_THREAD_unlock(store->lock);
  203. }
  204. return prov;
  205. }
  206. /*-
  207. * Provider Object methods
  208. * =======================
  209. */
  210. static OSSL_PROVIDER *provider_new(const char *name,
  211. OSSL_provider_init_fn *init_function)
  212. {
  213. OSSL_PROVIDER *prov = NULL;
  214. if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
  215. #ifndef HAVE_ATOMICS
  216. || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
  217. #endif
  218. || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
  219. || (prov->name = OPENSSL_strdup(name)) == NULL) {
  220. ossl_provider_free(prov);
  221. CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
  222. return NULL;
  223. }
  224. prov->init_function = init_function;
  225. return prov;
  226. }
  227. int ossl_provider_up_ref(OSSL_PROVIDER *prov)
  228. {
  229. int ref = 0;
  230. if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
  231. return 0;
  232. return ref;
  233. }
  234. OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
  235. OSSL_provider_init_fn *init_function,
  236. int noconfig)
  237. {
  238. struct provider_store_st *store = NULL;
  239. OSSL_PROVIDER *prov = NULL;
  240. if ((store = get_provider_store(libctx)) == NULL)
  241. return NULL;
  242. if ((prov = ossl_provider_find(libctx, name,
  243. noconfig)) != NULL) { /* refcount +1 */
  244. ossl_provider_free(prov); /* refcount -1 */
  245. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS, NULL,
  246. "name=%s", name);
  247. return NULL;
  248. }
  249. /* provider_new() generates an error, so no need here */
  250. if ((prov = provider_new(name, init_function)) == NULL)
  251. return NULL;
  252. CRYPTO_THREAD_write_lock(store->lock);
  253. if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
  254. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  255. prov = NULL;
  256. } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
  257. ossl_provider_free(prov); /* -1 Store reference */
  258. ossl_provider_free(prov); /* -1 Reference that was to be returned */
  259. prov = NULL;
  260. } else {
  261. prov->libctx = libctx;
  262. prov->store = store;
  263. #ifndef FIPS_MODULE
  264. prov->error_lib = ERR_get_next_error_library();
  265. #endif
  266. }
  267. CRYPTO_THREAD_unlock(store->lock);
  268. if (prov == NULL)
  269. CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
  270. /*
  271. * At this point, the provider is only partially "loaded". To be
  272. * fully "loaded", ossl_provider_activate() must also be called.
  273. */
  274. return prov;
  275. }
  276. static void free_infopair(INFOPAIR *pair)
  277. {
  278. OPENSSL_free(pair->name);
  279. OPENSSL_free(pair->value);
  280. OPENSSL_free(pair);
  281. }
  282. void ossl_provider_free(OSSL_PROVIDER *prov)
  283. {
  284. if (prov != NULL) {
  285. int ref = 0;
  286. CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
  287. /*
  288. * When the refcount drops below two, the store is the only
  289. * possible reference, or it has already been taken away from
  290. * the store (this may happen if a provider was activated
  291. * because it's a fallback, but isn't currently used)
  292. * When that happens, the provider is inactivated.
  293. */
  294. if (ref < 2 && prov->flag_initialized) {
  295. #ifndef FIPS_MODULE
  296. ossl_init_thread_deregister(prov);
  297. #endif
  298. if (prov->teardown != NULL)
  299. prov->teardown(prov->provctx);
  300. #ifndef OPENSSL_NO_ERR
  301. # ifndef FIPS_MODULE
  302. if (prov->error_strings != NULL) {
  303. ERR_unload_strings(prov->error_lib, prov->error_strings);
  304. OPENSSL_free(prov->error_strings);
  305. prov->error_strings = NULL;
  306. }
  307. # endif
  308. #endif
  309. OPENSSL_free(prov->operation_bits);
  310. prov->operation_bits = NULL;
  311. prov->operation_bits_sz = 0;
  312. prov->flag_initialized = 0;
  313. }
  314. /*
  315. * When the refcount drops to zero, it has been taken out of
  316. * the store. All we have to do here is clean it out.
  317. */
  318. if (ref == 0) {
  319. #ifndef FIPS_MODULE
  320. DSO_free(prov->module);
  321. #endif
  322. OPENSSL_free(prov->name);
  323. OPENSSL_free(prov->path);
  324. sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
  325. #ifndef HAVE_ATOMICS
  326. CRYPTO_THREAD_lock_free(prov->refcnt_lock);
  327. #endif
  328. OPENSSL_free(prov);
  329. }
  330. }
  331. }
  332. /* Setters */
  333. int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
  334. {
  335. OPENSSL_free(prov->path);
  336. if (module_path == NULL)
  337. return 1;
  338. if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
  339. return 1;
  340. CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
  341. return 0;
  342. }
  343. int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
  344. const char *name, const char *value)
  345. {
  346. INFOPAIR *pair = NULL;
  347. if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
  348. && (prov->parameters != NULL
  349. || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
  350. && (pair->name = OPENSSL_strdup(name)) != NULL
  351. && (pair->value = OPENSSL_strdup(value)) != NULL
  352. && sk_INFOPAIR_push(prov->parameters, pair) > 0)
  353. return 1;
  354. if (pair != NULL) {
  355. OPENSSL_free(pair->name);
  356. OPENSSL_free(pair->value);
  357. OPENSSL_free(pair);
  358. }
  359. CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
  360. return 0;
  361. }
  362. /*
  363. * Provider activation.
  364. *
  365. * What "activation" means depends on the provider form; for built in
  366. * providers (in the library or the application alike), the provider
  367. * can already be considered to be loaded, all that's needed is to
  368. * initialize it. However, for dynamically loadable provider modules,
  369. * we must first load that module.
  370. *
  371. * Built in modules are distinguished from dynamically loaded modules
  372. * with an already assigned init function.
  373. */
  374. static const OSSL_DISPATCH *core_dispatch; /* Define further down */
  375. int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
  376. const char *path)
  377. {
  378. struct provider_store_st *store;
  379. char *p = NULL;
  380. if (path != NULL) {
  381. p = OPENSSL_strdup(path);
  382. if (p == NULL) {
  383. CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
  384. return 0;
  385. }
  386. }
  387. if ((store = get_provider_store(libctx)) != NULL
  388. && CRYPTO_THREAD_write_lock(store->lock)) {
  389. OPENSSL_free(store->default_path);
  390. store->default_path = p;
  391. CRYPTO_THREAD_unlock(store->lock);
  392. return 1;
  393. }
  394. OPENSSL_free(p);
  395. return 0;
  396. }
  397. /*
  398. * Internal version that doesn't affect the store flags, and thereby avoid
  399. * locking. Direct callers must remember to set the store flags when
  400. * appropriate.
  401. */
  402. static int provider_activate(OSSL_PROVIDER *prov)
  403. {
  404. const OSSL_DISPATCH *provider_dispatch = NULL;
  405. void *tmp_provctx = NULL; /* safety measure */
  406. #ifndef OPENSSL_NO_ERR
  407. # ifndef FIPS_MODULE
  408. OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
  409. # endif
  410. #endif
  411. if (prov->flag_initialized)
  412. return 1;
  413. /*
  414. * If the init function isn't set, it indicates that this provider is
  415. * a loadable module.
  416. */
  417. if (prov->init_function == NULL) {
  418. #ifdef FIPS_MODULE
  419. return 0;
  420. #else
  421. if (prov->module == NULL) {
  422. char *allocated_path = NULL;
  423. const char *module_path = NULL;
  424. char *merged_path = NULL;
  425. const char *load_dir = NULL;
  426. struct provider_store_st *store;
  427. if ((prov->module = DSO_new()) == NULL) {
  428. /* DSO_new() generates an error already */
  429. return 0;
  430. }
  431. if ((store = get_provider_store(prov->libctx)) == NULL
  432. || !CRYPTO_THREAD_read_lock(store->lock))
  433. return 0;
  434. load_dir = store->default_path;
  435. if (load_dir == NULL) {
  436. load_dir = ossl_safe_getenv("OPENSSL_MODULES");
  437. if (load_dir == NULL)
  438. load_dir = MODULESDIR;
  439. }
  440. DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
  441. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
  442. module_path = prov->path;
  443. if (module_path == NULL)
  444. module_path = allocated_path =
  445. DSO_convert_filename(prov->module, prov->name);
  446. if (module_path != NULL)
  447. merged_path = DSO_merge(prov->module, module_path, load_dir);
  448. CRYPTO_THREAD_unlock(store->lock);
  449. if (merged_path == NULL
  450. || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
  451. DSO_free(prov->module);
  452. prov->module = NULL;
  453. }
  454. OPENSSL_free(merged_path);
  455. OPENSSL_free(allocated_path);
  456. }
  457. if (prov->module != NULL)
  458. prov->init_function = (OSSL_provider_init_fn *)
  459. DSO_bind_func(prov->module, "OSSL_provider_init");
  460. #endif
  461. }
  462. /* Call the initialise function for the provider. */
  463. if (prov->init_function == NULL
  464. || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
  465. &provider_dispatch, &tmp_provctx)) {
  466. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, NULL,
  467. "name=%s", prov->name);
  468. #ifndef FIPS_MODULE
  469. DSO_free(prov->module);
  470. prov->module = NULL;
  471. #endif
  472. return 0;
  473. }
  474. prov->provctx = tmp_provctx;
  475. for (; provider_dispatch->function_id != 0; provider_dispatch++) {
  476. switch (provider_dispatch->function_id) {
  477. case OSSL_FUNC_PROVIDER_TEARDOWN:
  478. prov->teardown =
  479. OSSL_FUNC_provider_teardown(provider_dispatch);
  480. break;
  481. case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
  482. prov->gettable_params =
  483. OSSL_FUNC_provider_gettable_params(provider_dispatch);
  484. break;
  485. case OSSL_FUNC_PROVIDER_GET_PARAMS:
  486. prov->get_params =
  487. OSSL_FUNC_provider_get_params(provider_dispatch);
  488. break;
  489. case OSSL_FUNC_PROVIDER_SELF_TEST:
  490. prov->self_test =
  491. OSSL_FUNC_provider_self_test(provider_dispatch);
  492. break;
  493. case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
  494. prov->get_capabilities =
  495. OSSL_FUNC_provider_get_capabilities(provider_dispatch);
  496. break;
  497. case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
  498. prov->query_operation =
  499. OSSL_FUNC_provider_query_operation(provider_dispatch);
  500. break;
  501. #ifndef OPENSSL_NO_ERR
  502. # ifndef FIPS_MODULE
  503. case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
  504. p_get_reason_strings =
  505. OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
  506. break;
  507. # endif
  508. #endif
  509. }
  510. }
  511. #ifndef OPENSSL_NO_ERR
  512. # ifndef FIPS_MODULE
  513. if (p_get_reason_strings != NULL) {
  514. const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
  515. size_t cnt, cnt2;
  516. /*
  517. * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
  518. * although they are essentially the same type.
  519. * Furthermore, ERR_load_strings() patches the array's error number
  520. * with the error library number, so we need to make a copy of that
  521. * array either way.
  522. */
  523. cnt = 0;
  524. while (reasonstrings[cnt].id != 0) {
  525. if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
  526. return 0;
  527. cnt++;
  528. }
  529. cnt++; /* One for the terminating item */
  530. /* Allocate one extra item for the "library" name */
  531. prov->error_strings =
  532. OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
  533. if (prov->error_strings == NULL)
  534. return 0;
  535. /*
  536. * Set the "library" name.
  537. */
  538. prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
  539. prov->error_strings[0].string = prov->name;
  540. /*
  541. * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
  542. * 1..cnt.
  543. */
  544. for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
  545. prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
  546. prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
  547. }
  548. ERR_load_strings(prov->error_lib, prov->error_strings);
  549. }
  550. # endif
  551. #endif
  552. /* With this flag set, this provider has become fully "loaded". */
  553. prov->flag_initialized = 1;
  554. return 1;
  555. }
  556. int ossl_provider_activate(OSSL_PROVIDER *prov)
  557. {
  558. if (provider_activate(prov)) {
  559. CRYPTO_THREAD_write_lock(prov->store->lock);
  560. prov->store->use_fallbacks = 0;
  561. CRYPTO_THREAD_unlock(prov->store->lock);
  562. return 1;
  563. }
  564. return 0;
  565. }
  566. void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
  567. {
  568. return prov->provctx;
  569. }
  570. static int provider_forall_loaded(struct provider_store_st *store,
  571. int *found_activated,
  572. int (*cb)(OSSL_PROVIDER *provider,
  573. void *cbdata),
  574. void *cbdata)
  575. {
  576. int i;
  577. int ret = 1;
  578. int num_provs;
  579. num_provs = sk_OSSL_PROVIDER_num(store->providers);
  580. if (found_activated != NULL)
  581. *found_activated = 0;
  582. for (i = 0; i < num_provs; i++) {
  583. OSSL_PROVIDER *prov =
  584. sk_OSSL_PROVIDER_value(store->providers, i);
  585. if (prov->flag_initialized) {
  586. if (found_activated != NULL)
  587. *found_activated = 1;
  588. if (!(ret = cb(prov, cbdata)))
  589. break;
  590. }
  591. }
  592. return ret;
  593. }
  594. /*
  595. * This function only does something once when store->use_fallbacks == 1,
  596. * and then sets store->use_fallbacks = 0, so the second call and so on is
  597. * effectively a no-op.
  598. */
  599. static void provider_activate_fallbacks(struct provider_store_st *store)
  600. {
  601. if (store->use_fallbacks) {
  602. int num_provs = sk_OSSL_PROVIDER_num(store->providers);
  603. int activated_fallback_count = 0;
  604. int i;
  605. for (i = 0; i < num_provs; i++) {
  606. OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
  607. /*
  608. * Activated fallback providers get an extra refcount, to
  609. * simulate a regular load.
  610. * Note that we don't care if the activation succeeds or not,
  611. * other than to maintain a correct refcount. If the activation
  612. * doesn't succeed, then any future attempt to use the fallback
  613. * provider will fail anyway.
  614. */
  615. if (prov->flag_fallback) {
  616. if (ossl_provider_up_ref(prov)) {
  617. if (!provider_activate(prov)) {
  618. ossl_provider_free(prov);
  619. } else {
  620. prov->flag_activated_as_fallback = 1;
  621. activated_fallback_count++;
  622. }
  623. }
  624. }
  625. }
  626. /*
  627. * We assume that all fallbacks have been added to the store before
  628. * any fallback is activated.
  629. * TODO: We may have to reconsider this, IF we find ourselves adding
  630. * fallbacks after any previous fallback has been activated.
  631. */
  632. if (activated_fallback_count > 0)
  633. store->use_fallbacks = 0;
  634. }
  635. }
  636. int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
  637. int (*cb)(OSSL_PROVIDER *provider,
  638. void *cbdata),
  639. void *cbdata)
  640. {
  641. int ret = 1;
  642. struct provider_store_st *store = get_provider_store(ctx);
  643. #ifndef FIPS_MODULE
  644. /*
  645. * Make sure any providers are loaded from config before we try to use
  646. * them.
  647. */
  648. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  649. #endif
  650. if (store != NULL) {
  651. CRYPTO_THREAD_read_lock(store->lock);
  652. provider_activate_fallbacks(store);
  653. /*
  654. * Now, we sweep through all providers
  655. */
  656. ret = provider_forall_loaded(store, NULL, cb, cbdata);
  657. CRYPTO_THREAD_unlock(store->lock);
  658. }
  659. return ret;
  660. }
  661. int ossl_provider_available(OSSL_PROVIDER *prov)
  662. {
  663. if (prov != NULL) {
  664. CRYPTO_THREAD_read_lock(prov->store->lock);
  665. provider_activate_fallbacks(prov->store);
  666. CRYPTO_THREAD_unlock(prov->store->lock);
  667. return prov->flag_initialized;
  668. }
  669. return 0;
  670. }
  671. /* Setters of Provider Object data */
  672. int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
  673. {
  674. if (prov == NULL)
  675. return 0;
  676. prov->flag_fallback = 1;
  677. return 1;
  678. }
  679. /* Getters of Provider Object data */
  680. const char *ossl_provider_name(const OSSL_PROVIDER *prov)
  681. {
  682. return prov->name;
  683. }
  684. const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
  685. {
  686. return prov->module;
  687. }
  688. const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
  689. {
  690. #ifdef FIPS_MODULE
  691. return NULL;
  692. #else
  693. return DSO_get_filename(prov->module);
  694. #endif
  695. }
  696. const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
  697. {
  698. #ifdef FIPS_MODULE
  699. return NULL;
  700. #else
  701. /* FIXME: Ensure it's a full path */
  702. return DSO_get_filename(prov->module);
  703. #endif
  704. }
  705. void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
  706. {
  707. if (prov != NULL)
  708. return prov->provctx;
  709. return NULL;
  710. }
  711. OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
  712. {
  713. /* TODO(3.0) just: return prov->libctx; */
  714. return prov != NULL ? prov->libctx : NULL;
  715. }
  716. /* Wrappers around calls to the provider */
  717. void ossl_provider_teardown(const OSSL_PROVIDER *prov)
  718. {
  719. if (prov->teardown != NULL)
  720. prov->teardown(prov->provctx);
  721. }
  722. const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
  723. {
  724. return prov->gettable_params == NULL
  725. ? NULL : prov->gettable_params(prov->provctx);
  726. }
  727. int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  728. {
  729. return prov->get_params == NULL
  730. ? 0 : prov->get_params(prov->provctx, params);
  731. }
  732. int ossl_provider_self_test(const OSSL_PROVIDER *prov)
  733. {
  734. int ret;
  735. if (prov->self_test == NULL)
  736. return 1;
  737. ret = prov->self_test(prov->provctx);
  738. if (ret == 0)
  739. evp_method_store_flush(ossl_provider_libctx(prov));
  740. return ret;
  741. }
  742. int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
  743. const char *capability,
  744. OSSL_CALLBACK *cb,
  745. void *arg)
  746. {
  747. return prov->get_capabilities == NULL
  748. ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
  749. }
  750. const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
  751. int operation_id,
  752. int *no_cache)
  753. {
  754. return prov->query_operation == NULL
  755. ? NULL : prov->query_operation(prov->provctx, operation_id, no_cache);
  756. }
  757. int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
  758. {
  759. size_t byte = bitnum / 8;
  760. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  761. if (provider->operation_bits_sz <= byte) {
  762. unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
  763. byte + 1);
  764. if (tmp == NULL) {
  765. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  766. return 0;
  767. }
  768. provider->operation_bits = tmp;
  769. memset(provider->operation_bits + provider->operation_bits_sz,
  770. '\0', byte + 1 - provider->operation_bits_sz);
  771. provider->operation_bits_sz = byte + 1;
  772. }
  773. provider->operation_bits[byte] |= bit;
  774. return 1;
  775. }
  776. int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
  777. int *result)
  778. {
  779. size_t byte = bitnum / 8;
  780. unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
  781. if (!ossl_assert(result != NULL)) {
  782. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  783. return 0;
  784. }
  785. *result = 0;
  786. if (provider->operation_bits_sz > byte)
  787. *result = ((provider->operation_bits[byte] & bit) != 0);
  788. return 1;
  789. }
  790. /*-
  791. * Core functions for the provider
  792. * ===============================
  793. *
  794. * This is the set of functions that the core makes available to the provider
  795. */
  796. /*
  797. * This returns a list of Provider Object parameters with their types, for
  798. * discovery. We do not expect that many providers will use this, but one
  799. * never knows.
  800. */
  801. static const OSSL_PARAM param_types[] = {
  802. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  803. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
  804. NULL, 0),
  805. #ifndef FIPS_MODULE
  806. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
  807. NULL, 0),
  808. #endif
  809. OSSL_PARAM_END
  810. };
  811. /*
  812. * Forward declare all the functions that are provided aa dispatch.
  813. * This ensures that the compiler will complain if they aren't defined
  814. * with the correct signature.
  815. */
  816. static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
  817. static OSSL_FUNC_core_get_params_fn core_get_params;
  818. static OSSL_FUNC_core_thread_start_fn core_thread_start;
  819. static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
  820. #ifndef FIPS_MODULE
  821. static OSSL_FUNC_core_new_error_fn core_new_error;
  822. static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
  823. static OSSL_FUNC_core_vset_error_fn core_vset_error;
  824. static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
  825. static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
  826. static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
  827. #endif
  828. static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
  829. {
  830. return param_types;
  831. }
  832. static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
  833. {
  834. int i;
  835. OSSL_PARAM *p;
  836. /*
  837. * We created this object originally and we know it is actually an
  838. * OSSL_PROVIDER *, so the cast is safe
  839. */
  840. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  841. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
  842. OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
  843. if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
  844. OSSL_PARAM_set_utf8_ptr(p, prov->name);
  845. #ifndef FIPS_MODULE
  846. if ((p = OSSL_PARAM_locate(params,
  847. OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
  848. OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
  849. #endif
  850. if (prov->parameters == NULL)
  851. return 1;
  852. for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
  853. INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
  854. if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
  855. OSSL_PARAM_set_utf8_ptr(p, pair->value);
  856. }
  857. return 1;
  858. }
  859. static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
  860. {
  861. /*
  862. * We created this object originally and we know it is actually an
  863. * OSSL_PROVIDER *, so the cast is safe
  864. */
  865. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  866. return (OPENSSL_CORE_CTX *)ossl_provider_libctx(prov);
  867. }
  868. static int core_thread_start(const OSSL_CORE_HANDLE *handle,
  869. OSSL_thread_stop_handler_fn handfn)
  870. {
  871. /*
  872. * We created this object originally and we know it is actually an
  873. * OSSL_PROVIDER *, so the cast is safe
  874. */
  875. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  876. return ossl_init_thread_start(prov, prov->provctx, handfn);
  877. }
  878. /*
  879. * The FIPS module inner provider doesn't implement these. They aren't
  880. * needed there, since the FIPS module upcalls are always the outer provider
  881. * ones.
  882. */
  883. #ifndef FIPS_MODULE
  884. /*
  885. * TODO(3.0) These error functions should use |handle| to select the proper
  886. * library context to report in the correct error stack, at least if error
  887. * stacks become tied to the library context.
  888. * We cannot currently do that since there's no support for it in the
  889. * ERR subsystem.
  890. */
  891. static void core_new_error(const OSSL_CORE_HANDLE *handle)
  892. {
  893. ERR_new();
  894. }
  895. static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  896. const char *file, int line, const char *func)
  897. {
  898. ERR_set_debug(file, line, func);
  899. }
  900. static void core_vset_error(const OSSL_CORE_HANDLE *handle,
  901. uint32_t reason, const char *fmt, va_list args)
  902. {
  903. /*
  904. * We created this object originally and we know it is actually an
  905. * OSSL_PROVIDER *, so the cast is safe
  906. */
  907. OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
  908. /*
  909. * If the uppermost 8 bits are non-zero, it's an OpenSSL library
  910. * error and will be treated as such. Otherwise, it's a new style
  911. * provider error and will be treated as such.
  912. */
  913. if (ERR_GET_LIB(reason) != 0) {
  914. ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
  915. } else {
  916. ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
  917. }
  918. }
  919. static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
  920. {
  921. return ERR_set_mark();
  922. }
  923. static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
  924. {
  925. return ERR_clear_last_mark();
  926. }
  927. static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
  928. {
  929. return ERR_pop_to_mark();
  930. }
  931. #endif /* FIPS_MODULE */
  932. /*
  933. * Functions provided by the core. Blank line separates "families" of related
  934. * functions.
  935. */
  936. static const OSSL_DISPATCH core_dispatch_[] = {
  937. { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
  938. { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
  939. { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
  940. { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
  941. #ifndef FIPS_MODULE
  942. { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
  943. { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
  944. { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
  945. { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
  946. { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
  947. (void (*)(void))core_clear_last_error_mark },
  948. { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
  949. { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
  950. { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
  951. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
  952. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
  953. { OSSL_FUNC_BIO_GETS, (void (*)(void))BIO_gets },
  954. { OSSL_FUNC_BIO_PUTS, (void (*)(void))BIO_puts },
  955. { OSSL_FUNC_BIO_CTRL, (void (*)(void))BIO_ctrl },
  956. { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
  957. { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
  958. { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
  959. { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
  960. #endif
  961. { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
  962. { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
  963. { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
  964. { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
  965. { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
  966. { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
  967. { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
  968. { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
  969. { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
  970. { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
  971. (void (*)(void))CRYPTO_secure_clear_free },
  972. { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
  973. (void (*)(void))CRYPTO_secure_allocated },
  974. { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
  975. { 0, NULL }
  976. };
  977. static const OSSL_DISPATCH *core_dispatch = core_dispatch_;