store_lib.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * Copyright 2016-2025 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 <stdlib.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. /* We need to use some STORE deprecated APIs */
  13. #define OPENSSL_SUPPRESS_DEPRECATED
  14. #include "internal/e_os.h"
  15. #include <openssl/crypto.h>
  16. #include <openssl/err.h>
  17. #include <openssl/trace.h>
  18. #include <openssl/core_names.h>
  19. #include <openssl/provider.h>
  20. #include <openssl/param_build.h>
  21. #include <openssl/store.h>
  22. #include "internal/thread_once.h"
  23. #include "internal/cryptlib.h"
  24. #include "internal/provider.h"
  25. #include "internal/bio.h"
  26. #include "crypto/store.h"
  27. #include "store_local.h"
  28. static int ossl_store_close_it(OSSL_STORE_CTX *ctx);
  29. static int loader_set_params(OSSL_STORE_LOADER *loader,
  30. OSSL_STORE_LOADER_CTX *loader_ctx,
  31. const OSSL_PARAM params[], const char *propq)
  32. {
  33. if (params != NULL) {
  34. if (!loader->p_set_ctx_params(loader_ctx, params))
  35. return 0;
  36. }
  37. if (propq != NULL) {
  38. OSSL_PARAM propp[2];
  39. if (OSSL_PARAM_locate_const(params,
  40. OSSL_STORE_PARAM_PROPERTIES) != NULL)
  41. /* use the propq from params */
  42. return 1;
  43. propp[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_PROPERTIES,
  44. (char *)propq, 0);
  45. propp[1] = OSSL_PARAM_construct_end();
  46. if (!loader->p_set_ctx_params(loader_ctx, propp))
  47. return 0;
  48. }
  49. return 1;
  50. }
  51. OSSL_STORE_CTX *
  52. OSSL_STORE_open_ex(const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
  53. const UI_METHOD *ui_method, void *ui_data,
  54. const OSSL_PARAM params[],
  55. OSSL_STORE_post_process_info_fn post_process,
  56. void *post_process_data)
  57. {
  58. struct ossl_passphrase_data_st pwdata = { 0 };
  59. const OSSL_STORE_LOADER *loader = NULL;
  60. OSSL_STORE_LOADER *fetched_loader = NULL;
  61. OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
  62. OSSL_STORE_CTX *ctx = NULL;
  63. char *propq_copy = NULL;
  64. int no_loader_found = 1;
  65. char scheme_copy[256], *p, *schemes[2], *scheme = NULL;
  66. size_t schemes_n = 0;
  67. size_t i;
  68. /*
  69. * Put the file scheme first. If the uri does represent an existing file,
  70. * possible device name and all, then it should be loaded. Only a failed
  71. * attempt at loading a local file should have us try something else.
  72. */
  73. schemes[schemes_n++] = "file";
  74. /*
  75. * Now, check if we have something that looks like a scheme, and add it
  76. * as a second scheme. However, also check if there's an authority start
  77. * (://), because that will invalidate the previous file scheme. Also,
  78. * check that this isn't actually the file scheme, as there's no point
  79. * going through that one twice!
  80. */
  81. OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
  82. if ((p = strchr(scheme_copy, ':')) != NULL) {
  83. *p++ = '\0';
  84. if (OPENSSL_strcasecmp(scheme_copy, "file") != 0) {
  85. if (HAS_PREFIX(p, "//"))
  86. schemes_n--; /* Invalidate the file scheme */
  87. schemes[schemes_n++] = scheme_copy;
  88. }
  89. }
  90. ERR_set_mark();
  91. if (ui_method != NULL
  92. && (!ossl_pw_set_ui_method(&pwdata, ui_method, ui_data)
  93. || !ossl_pw_enable_passphrase_caching(&pwdata))) {
  94. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
  95. goto err;
  96. }
  97. /*
  98. * Try each scheme until we find one that could open the URI.
  99. *
  100. * For each scheme, we look for the engine implementation first, and
  101. * failing that, we then try to fetch a provided implementation.
  102. * This is consistent with how we handle legacy / engine implementations
  103. * elsewhere.
  104. */
  105. for (i = 0; loader_ctx == NULL && i < schemes_n; i++) {
  106. scheme = schemes[i];
  107. OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
  108. #ifndef OPENSSL_NO_DEPRECATED_3_0
  109. ERR_set_mark();
  110. if ((loader = ossl_store_get0_loader_int(scheme)) != NULL) {
  111. ERR_clear_last_mark();
  112. no_loader_found = 0;
  113. if (loader->open_ex != NULL)
  114. loader_ctx = loader->open_ex(loader, uri, libctx, propq,
  115. ui_method, ui_data);
  116. else
  117. loader_ctx = loader->open(loader, uri, ui_method, ui_data);
  118. } else {
  119. ERR_pop_to_mark();
  120. }
  121. #endif
  122. if (loader == NULL
  123. && (fetched_loader =
  124. OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
  125. const OSSL_PROVIDER *provider =
  126. OSSL_STORE_LOADER_get0_provider(fetched_loader);
  127. void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
  128. no_loader_found = 0;
  129. if (fetched_loader->p_open_ex != NULL) {
  130. loader_ctx =
  131. fetched_loader->p_open_ex(provctx, uri, params,
  132. ossl_pw_passphrase_callback_dec,
  133. &pwdata);
  134. } else {
  135. if (fetched_loader->p_open != NULL &&
  136. (loader_ctx = fetched_loader->p_open(provctx, uri)) != NULL &&
  137. !loader_set_params(fetched_loader, loader_ctx,
  138. params, propq)) {
  139. (void)fetched_loader->p_close(loader_ctx);
  140. loader_ctx = NULL;
  141. }
  142. }
  143. if (loader_ctx == NULL) {
  144. OSSL_STORE_LOADER_free(fetched_loader);
  145. fetched_loader = NULL;
  146. }
  147. loader = fetched_loader;
  148. /* Clear any internally cached passphrase */
  149. (void)ossl_pw_clear_passphrase_cache(&pwdata);
  150. }
  151. }
  152. if (no_loader_found)
  153. /*
  154. * It's assumed that ossl_store_get0_loader_int() and
  155. * OSSL_STORE_LOADER_fetch() report their own errors
  156. */
  157. goto err;
  158. OSSL_TRACE1(STORE, "Found loader for scheme %s\n", scheme);
  159. if (loader_ctx == NULL)
  160. /*
  161. * It's assumed that the loader's open() method reports its own
  162. * errors
  163. */
  164. goto err;
  165. OSSL_TRACE2(STORE, "Opened %s => %p\n", uri, (void *)loader_ctx);
  166. if ((propq != NULL && (propq_copy = OPENSSL_strdup(propq)) == NULL)
  167. || (ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  168. goto err;
  169. ctx->properties = propq_copy;
  170. ctx->fetched_loader = fetched_loader;
  171. ctx->loader = loader;
  172. ctx->loader_ctx = loader_ctx;
  173. ctx->post_process = post_process;
  174. ctx->post_process_data = post_process_data;
  175. ctx->pwdata = pwdata;
  176. /*
  177. * If the attempt to open with the 'file' scheme loader failed and the
  178. * other scheme loader succeeded, the failure to open with the 'file'
  179. * scheme loader leaves an error on the error stack. Let's remove it.
  180. */
  181. ERR_pop_to_mark();
  182. return ctx;
  183. err:
  184. ERR_clear_last_mark();
  185. if (loader_ctx != NULL) {
  186. /*
  187. * Temporary structure so OSSL_STORE_close() can work even when
  188. * |ctx| couldn't be allocated properly
  189. */
  190. OSSL_STORE_CTX tmpctx = { NULL, };
  191. tmpctx.fetched_loader = fetched_loader;
  192. tmpctx.loader = loader;
  193. tmpctx.loader_ctx = loader_ctx;
  194. /*
  195. * We ignore a returned error because we will return NULL anyway in
  196. * this case, so if something goes wrong when closing, that'll simply
  197. * just add another entry on the error stack.
  198. */
  199. (void)ossl_store_close_it(&tmpctx);
  200. }
  201. /* Coverity false positive, the reference counting is confusing it */
  202. /* coverity[pass_freed_arg] */
  203. OSSL_STORE_LOADER_free(fetched_loader);
  204. OPENSSL_free(propq_copy);
  205. OPENSSL_free(ctx);
  206. return NULL;
  207. }
  208. OSSL_STORE_CTX *OSSL_STORE_open(const char *uri,
  209. const UI_METHOD *ui_method, void *ui_data,
  210. OSSL_STORE_post_process_info_fn post_process,
  211. void *post_process_data)
  212. {
  213. return OSSL_STORE_open_ex(uri, NULL, NULL, ui_method, ui_data, NULL,
  214. post_process, post_process_data);
  215. }
  216. #ifndef OPENSSL_NO_DEPRECATED_3_0
  217. int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
  218. {
  219. va_list args;
  220. int ret;
  221. va_start(args, cmd);
  222. ret = OSSL_STORE_vctrl(ctx, cmd, args);
  223. va_end(args);
  224. return ret;
  225. }
  226. int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args)
  227. {
  228. if (ctx->fetched_loader != NULL) {
  229. if (ctx->fetched_loader->p_set_ctx_params != NULL) {
  230. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  231. switch (cmd) {
  232. case OSSL_STORE_C_USE_SECMEM:
  233. {
  234. int on = *(va_arg(args, int *));
  235. params[0] = OSSL_PARAM_construct_int("use_secmem", &on);
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. return ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
  242. params);
  243. }
  244. } else if (ctx->loader->ctrl != NULL) {
  245. return ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
  246. }
  247. /*
  248. * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as
  249. * if there was one that ignored our params, which usually returns 1.
  250. */
  251. return 1;
  252. }
  253. #endif
  254. int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type)
  255. {
  256. int ret = 1;
  257. if (ctx == NULL
  258. || expected_type < 0 || expected_type > OSSL_STORE_INFO_CRL) {
  259. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
  260. return 0;
  261. }
  262. if (ctx->loading) {
  263. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
  264. return 0;
  265. }
  266. ctx->expected_type = expected_type;
  267. if (ctx->fetched_loader != NULL
  268. && ctx->fetched_loader->p_set_ctx_params != NULL) {
  269. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  270. params[0] =
  271. OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT, &expected_type);
  272. ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, params);
  273. }
  274. #ifndef OPENSSL_NO_DEPRECATED_3_0
  275. if (ctx->fetched_loader == NULL
  276. && ctx->loader->expect != NULL) {
  277. ret = ctx->loader->expect(ctx->loader_ctx, expected_type);
  278. }
  279. #endif
  280. return ret;
  281. }
  282. int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search)
  283. {
  284. int ret = 1;
  285. if (ctx->loading) {
  286. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
  287. return 0;
  288. }
  289. if (search == NULL) {
  290. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  291. return 0;
  292. }
  293. if (ctx->fetched_loader != NULL) {
  294. OSSL_PARAM_BLD *bld;
  295. OSSL_PARAM *params;
  296. /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
  297. void *name_der = NULL;
  298. int name_der_sz;
  299. /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
  300. BIGNUM *number = NULL;
  301. if (ctx->fetched_loader->p_set_ctx_params == NULL) {
  302. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
  303. return 0;
  304. }
  305. if ((bld = OSSL_PARAM_BLD_new()) == NULL) {
  306. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
  307. return 0;
  308. }
  309. ret = 0; /* Assume the worst */
  310. switch (search->search_type) {
  311. case OSSL_STORE_SEARCH_BY_NAME:
  312. if ((name_der_sz = i2d_X509_NAME(search->name,
  313. (unsigned char **)&name_der)) > 0
  314. && OSSL_PARAM_BLD_push_octet_string(bld,
  315. OSSL_STORE_PARAM_SUBJECT,
  316. name_der, name_der_sz))
  317. ret = 1;
  318. break;
  319. case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
  320. if ((name_der_sz = i2d_X509_NAME(search->name,
  321. (unsigned char **)&name_der)) > 0
  322. && (number = ASN1_INTEGER_to_BN(search->serial, NULL)) != NULL
  323. && OSSL_PARAM_BLD_push_octet_string(bld,
  324. OSSL_STORE_PARAM_ISSUER,
  325. name_der, name_der_sz)
  326. && OSSL_PARAM_BLD_push_BN(bld, OSSL_STORE_PARAM_SERIAL,
  327. number))
  328. ret = 1;
  329. break;
  330. case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
  331. if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_DIGEST,
  332. EVP_MD_get0_name(search->digest),
  333. 0)
  334. && OSSL_PARAM_BLD_push_octet_string(bld,
  335. OSSL_STORE_PARAM_FINGERPRINT,
  336. search->string,
  337. search->stringlength))
  338. ret = 1;
  339. break;
  340. case OSSL_STORE_SEARCH_BY_ALIAS:
  341. if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_ALIAS,
  342. (char *)search->string,
  343. search->stringlength))
  344. ret = 1;
  345. break;
  346. }
  347. if (ret) {
  348. params = OSSL_PARAM_BLD_to_param(bld);
  349. ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
  350. params);
  351. OSSL_PARAM_free(params);
  352. }
  353. OSSL_PARAM_BLD_free(bld);
  354. OPENSSL_free(name_der);
  355. BN_free(number);
  356. } else {
  357. #ifndef OPENSSL_NO_DEPRECATED_3_0
  358. /* legacy loader section */
  359. if (ctx->loader->find == NULL) {
  360. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
  361. return 0;
  362. }
  363. ret = ctx->loader->find(ctx->loader_ctx, search);
  364. #endif
  365. }
  366. return ret;
  367. }
  368. OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx)
  369. {
  370. OSSL_STORE_INFO *v = NULL;
  371. ctx->loading = 1;
  372. again:
  373. if (OSSL_STORE_eof(ctx))
  374. return NULL;
  375. if (ctx->loader != NULL)
  376. OSSL_TRACE(STORE, "Loading next object\n");
  377. if (ctx->cached_info != NULL) {
  378. v = sk_OSSL_STORE_INFO_shift(ctx->cached_info);
  379. } else {
  380. if (ctx->fetched_loader != NULL) {
  381. struct ossl_load_result_data_st load_data;
  382. load_data.v = NULL;
  383. load_data.ctx = ctx;
  384. ctx->error_flag = 0;
  385. if (!ctx->fetched_loader->p_load(ctx->loader_ctx,
  386. ossl_store_handle_load_result,
  387. &load_data,
  388. ossl_pw_passphrase_callback_dec,
  389. &ctx->pwdata)) {
  390. ctx->error_flag = 1;
  391. return NULL;
  392. }
  393. v = load_data.v;
  394. }
  395. #ifndef OPENSSL_NO_DEPRECATED_3_0
  396. if (ctx->fetched_loader == NULL)
  397. v = ctx->loader->load(ctx->loader_ctx,
  398. ctx->pwdata._.ui_method.ui_method,
  399. ctx->pwdata._.ui_method.ui_method_data);
  400. #endif
  401. }
  402. if (ctx->post_process != NULL && v != NULL) {
  403. v = ctx->post_process(v, ctx->post_process_data);
  404. /*
  405. * By returning NULL, the callback decides that this object should
  406. * be ignored.
  407. */
  408. if (v == NULL)
  409. goto again;
  410. }
  411. /* Clear any internally cached passphrase */
  412. (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
  413. if (v != NULL && ctx->expected_type != 0) {
  414. int returned_type = OSSL_STORE_INFO_get_type(v);
  415. if (returned_type != OSSL_STORE_INFO_NAME && returned_type != 0) {
  416. if (ctx->expected_type != returned_type) {
  417. OSSL_STORE_INFO_free(v);
  418. goto again;
  419. }
  420. }
  421. }
  422. if (v != NULL)
  423. OSSL_TRACE1(STORE, "Got a %s\n",
  424. OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v)));
  425. return v;
  426. }
  427. int OSSL_STORE_delete(const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
  428. const UI_METHOD *ui_method, void *ui_data,
  429. const OSSL_PARAM params[])
  430. {
  431. OSSL_STORE_LOADER *fetched_loader = NULL;
  432. char scheme[256], *p;
  433. int res = 0;
  434. struct ossl_passphrase_data_st pwdata = {0};
  435. OPENSSL_strlcpy(scheme, uri, sizeof(scheme));
  436. if ((p = strchr(scheme, ':')) != NULL)
  437. *p++ = '\0';
  438. else /* We don't work without explicit scheme */
  439. return 0;
  440. if (ui_method != NULL
  441. && (!ossl_pw_set_ui_method(&pwdata, ui_method, ui_data)
  442. || !ossl_pw_enable_passphrase_caching(&pwdata))) {
  443. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
  444. return 0;
  445. }
  446. OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
  447. fetched_loader = OSSL_STORE_LOADER_fetch(libctx, scheme, propq);
  448. if (fetched_loader != NULL && fetched_loader->p_delete != NULL) {
  449. const OSSL_PROVIDER *provider =
  450. OSSL_STORE_LOADER_get0_provider(fetched_loader);
  451. void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
  452. /*
  453. * It's assumed that the loader's delete() method reports its own
  454. * errors
  455. */
  456. OSSL_TRACE1(STORE, "Performing URI delete %s\n", uri);
  457. res = fetched_loader->p_delete(provctx, uri, params,
  458. ossl_pw_passphrase_callback_dec,
  459. &pwdata);
  460. }
  461. /* Clear any internally cached passphrase */
  462. (void)ossl_pw_clear_passphrase_cache(&pwdata);
  463. OSSL_STORE_LOADER_free(fetched_loader);
  464. return res;
  465. }
  466. int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
  467. {
  468. int ret = 1;
  469. if (ctx->fetched_loader != NULL)
  470. ret = ctx->error_flag;
  471. #ifndef OPENSSL_NO_DEPRECATED_3_0
  472. if (ctx->fetched_loader == NULL)
  473. ret = ctx->loader->error(ctx->loader_ctx);
  474. #endif
  475. return ret;
  476. }
  477. int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
  478. {
  479. int ret = 0;
  480. if (ctx->cached_info != NULL
  481. && sk_OSSL_STORE_INFO_num(ctx->cached_info) == 0) {
  482. sk_OSSL_STORE_INFO_free(ctx->cached_info);
  483. ctx->cached_info = NULL;
  484. }
  485. if (ctx->cached_info == NULL) {
  486. ret = 1;
  487. if (ctx->fetched_loader != NULL)
  488. ret = ctx->loader->p_eof(ctx->loader_ctx);
  489. #ifndef OPENSSL_NO_DEPRECATED_3_0
  490. if (ctx->fetched_loader == NULL)
  491. ret = ctx->loader->eof(ctx->loader_ctx);
  492. #endif
  493. }
  494. return ret != 0;
  495. }
  496. static int ossl_store_close_it(OSSL_STORE_CTX *ctx)
  497. {
  498. int ret = 0;
  499. if (ctx == NULL)
  500. return 1;
  501. OSSL_TRACE1(STORE, "Closing %p\n", (void *)ctx->loader_ctx);
  502. if (ctx->fetched_loader != NULL)
  503. ret = ctx->loader->p_close(ctx->loader_ctx);
  504. #ifndef OPENSSL_NO_DEPRECATED_3_0
  505. if (ctx->fetched_loader == NULL)
  506. ret = ctx->loader->closefn(ctx->loader_ctx);
  507. #endif
  508. sk_OSSL_STORE_INFO_pop_free(ctx->cached_info, OSSL_STORE_INFO_free);
  509. OSSL_STORE_LOADER_free(ctx->fetched_loader);
  510. OPENSSL_free(ctx->properties);
  511. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  512. return ret;
  513. }
  514. int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
  515. {
  516. int ret = ossl_store_close_it(ctx);
  517. OPENSSL_free(ctx);
  518. return ret;
  519. }
  520. /*
  521. * Functions to generate OSSL_STORE_INFOs, one function for each type we
  522. * support having in them as well as a generic constructor.
  523. *
  524. * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
  525. * and will therefore be freed when the OSSL_STORE_INFO is freed.
  526. */
  527. OSSL_STORE_INFO *OSSL_STORE_INFO_new(int type, void *data)
  528. {
  529. OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
  530. if (info == NULL)
  531. return NULL;
  532. info->type = type;
  533. info->_.data = data;
  534. return info;
  535. }
  536. OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
  537. {
  538. OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME, NULL);
  539. if (info == NULL) {
  540. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
  541. return NULL;
  542. }
  543. info->_.name.name = name;
  544. info->_.name.desc = NULL;
  545. return info;
  546. }
  547. int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
  548. {
  549. if (info->type != OSSL_STORE_INFO_NAME) {
  550. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
  551. return 0;
  552. }
  553. info->_.name.desc = desc;
  554. return 1;
  555. }
  556. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
  557. {
  558. OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS, params);
  559. if (info == NULL)
  560. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
  561. return info;
  562. }
  563. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY *pkey)
  564. {
  565. OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY, pkey);
  566. if (info == NULL)
  567. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
  568. return info;
  569. }
  570. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
  571. {
  572. OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY, pkey);
  573. if (info == NULL)
  574. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
  575. return info;
  576. }
  577. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
  578. {
  579. OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT, x509);
  580. if (info == NULL)
  581. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
  582. return info;
  583. }
  584. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
  585. {
  586. OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL, crl);
  587. if (info == NULL)
  588. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
  589. return info;
  590. }
  591. /*
  592. * Functions to try to extract data from an OSSL_STORE_INFO.
  593. */
  594. int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
  595. {
  596. return info->type;
  597. }
  598. void *OSSL_STORE_INFO_get0_data(int type, const OSSL_STORE_INFO *info)
  599. {
  600. if (info->type == type)
  601. return info->_.data;
  602. return NULL;
  603. }
  604. const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
  605. {
  606. if (info->type == OSSL_STORE_INFO_NAME)
  607. return info->_.name.name;
  608. return NULL;
  609. }
  610. char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
  611. {
  612. if (info->type == OSSL_STORE_INFO_NAME)
  613. return OPENSSL_strdup(info->_.name.name);
  614. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
  615. return NULL;
  616. }
  617. const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
  618. {
  619. if (info->type == OSSL_STORE_INFO_NAME)
  620. return info->_.name.desc;
  621. return NULL;
  622. }
  623. char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
  624. {
  625. if (info->type == OSSL_STORE_INFO_NAME)
  626. return OPENSSL_strdup(info->_.name.desc ? info->_.name.desc : "");
  627. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
  628. return NULL;
  629. }
  630. EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
  631. {
  632. if (info->type == OSSL_STORE_INFO_PARAMS)
  633. return info->_.params;
  634. return NULL;
  635. }
  636. EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
  637. {
  638. if (info->type == OSSL_STORE_INFO_PARAMS) {
  639. EVP_PKEY_up_ref(info->_.params);
  640. return info->_.params;
  641. }
  642. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_PARAMETERS);
  643. return NULL;
  644. }
  645. EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info)
  646. {
  647. if (info->type == OSSL_STORE_INFO_PUBKEY)
  648. return info->_.pubkey;
  649. return NULL;
  650. }
  651. EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info)
  652. {
  653. if (info->type == OSSL_STORE_INFO_PUBKEY) {
  654. EVP_PKEY_up_ref(info->_.pubkey);
  655. return info->_.pubkey;
  656. }
  657. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PUBLIC_KEY);
  658. return NULL;
  659. }
  660. EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
  661. {
  662. if (info->type == OSSL_STORE_INFO_PKEY)
  663. return info->_.pkey;
  664. return NULL;
  665. }
  666. EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
  667. {
  668. if (info->type == OSSL_STORE_INFO_PKEY) {
  669. EVP_PKEY_up_ref(info->_.pkey);
  670. return info->_.pkey;
  671. }
  672. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PRIVATE_KEY);
  673. return NULL;
  674. }
  675. X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
  676. {
  677. if (info->type == OSSL_STORE_INFO_CERT)
  678. return info->_.x509;
  679. return NULL;
  680. }
  681. X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
  682. {
  683. if (info->type == OSSL_STORE_INFO_CERT) {
  684. X509_up_ref(info->_.x509);
  685. return info->_.x509;
  686. }
  687. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CERTIFICATE);
  688. return NULL;
  689. }
  690. X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
  691. {
  692. if (info->type == OSSL_STORE_INFO_CRL)
  693. return info->_.crl;
  694. return NULL;
  695. }
  696. X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
  697. {
  698. if (info->type == OSSL_STORE_INFO_CRL) {
  699. X509_CRL_up_ref(info->_.crl);
  700. return info->_.crl;
  701. }
  702. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CRL);
  703. return NULL;
  704. }
  705. /*
  706. * Free the OSSL_STORE_INFO
  707. */
  708. void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
  709. {
  710. if (info != NULL) {
  711. switch (info->type) {
  712. case OSSL_STORE_INFO_NAME:
  713. OPENSSL_free(info->_.name.name);
  714. OPENSSL_free(info->_.name.desc);
  715. break;
  716. case OSSL_STORE_INFO_PARAMS:
  717. EVP_PKEY_free(info->_.params);
  718. break;
  719. case OSSL_STORE_INFO_PUBKEY:
  720. EVP_PKEY_free(info->_.pubkey);
  721. break;
  722. case OSSL_STORE_INFO_PKEY:
  723. EVP_PKEY_free(info->_.pkey);
  724. break;
  725. case OSSL_STORE_INFO_CERT:
  726. X509_free(info->_.x509);
  727. break;
  728. case OSSL_STORE_INFO_CRL:
  729. X509_CRL_free(info->_.crl);
  730. break;
  731. }
  732. OPENSSL_free(info);
  733. }
  734. }
  735. int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type)
  736. {
  737. int ret = 0;
  738. if (ctx->fetched_loader != NULL) {
  739. void *provctx =
  740. ossl_provider_ctx(OSSL_STORE_LOADER_get0_provider(ctx->fetched_loader));
  741. const OSSL_PARAM *params;
  742. const OSSL_PARAM *p_subject = NULL;
  743. const OSSL_PARAM *p_issuer = NULL;
  744. const OSSL_PARAM *p_serial = NULL;
  745. const OSSL_PARAM *p_fingerprint = NULL;
  746. const OSSL_PARAM *p_alias = NULL;
  747. if (ctx->fetched_loader->p_settable_ctx_params == NULL)
  748. return 0;
  749. params = ctx->fetched_loader->p_settable_ctx_params(provctx);
  750. p_subject = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT);
  751. p_issuer = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ISSUER);
  752. p_serial = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SERIAL);
  753. p_fingerprint =
  754. OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_FINGERPRINT);
  755. p_alias = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ALIAS);
  756. switch (search_type) {
  757. case OSSL_STORE_SEARCH_BY_NAME:
  758. ret = (p_subject != NULL);
  759. break;
  760. case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
  761. ret = (p_issuer != NULL && p_serial != NULL);
  762. break;
  763. case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
  764. ret = (p_fingerprint != NULL);
  765. break;
  766. case OSSL_STORE_SEARCH_BY_ALIAS:
  767. ret = (p_alias != NULL);
  768. break;
  769. }
  770. }
  771. #ifndef OPENSSL_NO_DEPRECATED_3_0
  772. if (ctx->fetched_loader == NULL) {
  773. OSSL_STORE_SEARCH tmp_search;
  774. if (ctx->loader->find == NULL)
  775. return 0;
  776. tmp_search.search_type = search_type;
  777. ret = ctx->loader->find(NULL, &tmp_search);
  778. }
  779. #endif
  780. return ret;
  781. }
  782. /* Search term constructors */
  783. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name)
  784. {
  785. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  786. if (search == NULL)
  787. return NULL;
  788. search->search_type = OSSL_STORE_SEARCH_BY_NAME;
  789. search->name = name;
  790. return search;
  791. }
  792. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
  793. const ASN1_INTEGER *serial)
  794. {
  795. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  796. if (search == NULL)
  797. return NULL;
  798. search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  799. search->name = name;
  800. search->serial = serial;
  801. return search;
  802. }
  803. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
  804. const unsigned char
  805. *bytes, size_t len)
  806. {
  807. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  808. if (search == NULL)
  809. return NULL;
  810. if (digest != NULL && len != (size_t)EVP_MD_get_size(digest)) {
  811. ERR_raise_data(ERR_LIB_OSSL_STORE,
  812. OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST,
  813. "%s size is %d, fingerprint size is %zu",
  814. EVP_MD_get0_name(digest), EVP_MD_get_size(digest), len);
  815. OPENSSL_free(search);
  816. return NULL;
  817. }
  818. search->search_type = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
  819. search->digest = digest;
  820. search->string = bytes;
  821. search->stringlength = len;
  822. return search;
  823. }
  824. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias)
  825. {
  826. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  827. if (search == NULL)
  828. return NULL;
  829. search->search_type = OSSL_STORE_SEARCH_BY_ALIAS;
  830. search->string = (const unsigned char *)alias;
  831. search->stringlength = strlen(alias);
  832. return search;
  833. }
  834. /* Search term destructor */
  835. void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search)
  836. {
  837. OPENSSL_free(search);
  838. }
  839. /* Search term accessors */
  840. int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion)
  841. {
  842. return criterion->search_type;
  843. }
  844. X509_NAME *OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH *criterion)
  845. {
  846. return criterion->name;
  847. }
  848. const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
  849. *criterion)
  850. {
  851. return criterion->serial;
  852. }
  853. const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
  854. *criterion, size_t *length)
  855. {
  856. *length = criterion->stringlength;
  857. return criterion->string;
  858. }
  859. const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion)
  860. {
  861. return (const char *)criterion->string;
  862. }
  863. const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion)
  864. {
  865. return criterion->digest;
  866. }
  867. OSSL_STORE_CTX *OSSL_STORE_attach(BIO *bp, const char *scheme,
  868. OSSL_LIB_CTX *libctx, const char *propq,
  869. const UI_METHOD *ui_method, void *ui_data,
  870. const OSSL_PARAM params[],
  871. OSSL_STORE_post_process_info_fn post_process,
  872. void *post_process_data)
  873. {
  874. const OSSL_STORE_LOADER *loader = NULL;
  875. OSSL_STORE_LOADER *fetched_loader = NULL;
  876. OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
  877. OSSL_STORE_CTX *ctx = NULL;
  878. if (scheme == NULL)
  879. scheme = "file";
  880. OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
  881. ERR_set_mark();
  882. #ifndef OPENSSL_NO_DEPRECATED_3_0
  883. if ((loader = ossl_store_get0_loader_int(scheme)) != NULL)
  884. loader_ctx = loader->attach(loader, bp, libctx, propq,
  885. ui_method, ui_data);
  886. #endif
  887. if (loader == NULL
  888. && (fetched_loader =
  889. OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
  890. const OSSL_PROVIDER *provider =
  891. OSSL_STORE_LOADER_get0_provider(fetched_loader);
  892. void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
  893. OSSL_CORE_BIO *cbio = ossl_core_bio_new_from_bio(bp);
  894. if (cbio == NULL
  895. || fetched_loader->p_attach == NULL
  896. || (loader_ctx = fetched_loader->p_attach(provctx, cbio)) == NULL) {
  897. OSSL_STORE_LOADER_free(fetched_loader);
  898. fetched_loader = NULL;
  899. } else if (!loader_set_params(fetched_loader, loader_ctx,
  900. params, propq)) {
  901. (void)fetched_loader->p_close(loader_ctx);
  902. OSSL_STORE_LOADER_free(fetched_loader);
  903. fetched_loader = NULL;
  904. }
  905. loader = fetched_loader;
  906. ossl_core_bio_free(cbio);
  907. }
  908. if (loader_ctx == NULL) {
  909. ERR_clear_last_mark();
  910. return NULL;
  911. }
  912. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  913. ERR_clear_last_mark();
  914. return NULL;
  915. }
  916. if (ui_method != NULL
  917. && !ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)) {
  918. ERR_clear_last_mark();
  919. OPENSSL_free(ctx);
  920. return NULL;
  921. }
  922. ctx->fetched_loader = fetched_loader;
  923. ctx->loader = loader;
  924. ctx->loader_ctx = loader_ctx;
  925. ctx->post_process = post_process;
  926. ctx->post_process_data = post_process_data;
  927. /*
  928. * ossl_store_get0_loader_int will raise an error if the loader for
  929. * the scheme cannot be retrieved. But if a loader was successfully
  930. * fetched then we remove this error from the error stack.
  931. */
  932. ERR_pop_to_mark();
  933. return ctx;
  934. }