store_lib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "e_os.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include "e_os.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/err.h>
  16. #include <openssl/store.h>
  17. #include "internal/thread_once.h"
  18. #include "crypto/store.h"
  19. #include "store_local.h"
  20. struct ossl_store_ctx_st {
  21. const OSSL_STORE_LOADER *loader;
  22. OSSL_STORE_LOADER_CTX *loader_ctx;
  23. const UI_METHOD *ui_method;
  24. void *ui_data;
  25. OSSL_STORE_post_process_info_fn post_process;
  26. void *post_process_data;
  27. int expected_type;
  28. /* 0 before the first STORE_load(), 1 otherwise */
  29. int loading;
  30. };
  31. OSSL_STORE_CTX *OSSL_STORE_open(const char *uri, const UI_METHOD *ui_method,
  32. void *ui_data,
  33. OSSL_STORE_post_process_info_fn post_process,
  34. void *post_process_data)
  35. {
  36. const OSSL_STORE_LOADER *loader = NULL;
  37. OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
  38. OSSL_STORE_CTX *ctx = NULL;
  39. char scheme_copy[256], *p, *schemes[2];
  40. size_t schemes_n = 0;
  41. size_t i;
  42. /*
  43. * Put the file scheme first. If the uri does represent an existing file,
  44. * possible device name and all, then it should be loaded. Only a failed
  45. * attempt at loading a local file should have us try something else.
  46. */
  47. schemes[schemes_n++] = "file";
  48. /*
  49. * Now, check if we have something that looks like a scheme, and add it
  50. * as a second scheme. However, also check if there's an authority start
  51. * (://), because that will invalidate the previous file scheme. Also,
  52. * check that this isn't actually the file scheme, as there's no point
  53. * going through that one twice!
  54. */
  55. OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
  56. if ((p = strchr(scheme_copy, ':')) != NULL) {
  57. *p++ = '\0';
  58. if (strcasecmp(scheme_copy, "file") != 0) {
  59. if (strncmp(p, "//", 2) == 0)
  60. schemes_n--; /* Invalidate the file scheme */
  61. schemes[schemes_n++] = scheme_copy;
  62. }
  63. }
  64. ERR_set_mark();
  65. /* Try each scheme until we find one that could open the URI */
  66. for (i = 0; loader_ctx == NULL && i < schemes_n; i++) {
  67. if ((loader = ossl_store_get0_loader_int(schemes[i])) != NULL)
  68. loader_ctx = loader->open(loader, uri, ui_method, ui_data);
  69. }
  70. if (loader_ctx == NULL)
  71. goto err;
  72. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  73. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_OPEN, ERR_R_MALLOC_FAILURE);
  74. goto err;
  75. }
  76. ctx->loader = loader;
  77. ctx->loader_ctx = loader_ctx;
  78. ctx->ui_method = ui_method;
  79. ctx->ui_data = ui_data;
  80. ctx->post_process = post_process;
  81. ctx->post_process_data = post_process_data;
  82. /*
  83. * If the attempt to open with the 'file' scheme loader failed and the
  84. * other scheme loader succeeded, the failure to open with the 'file'
  85. * scheme loader leaves an error on the error stack. Let's remove it.
  86. */
  87. ERR_pop_to_mark();
  88. return ctx;
  89. err:
  90. ERR_clear_last_mark();
  91. if (loader_ctx != NULL) {
  92. /*
  93. * We ignore a returned error because we will return NULL anyway in
  94. * this case, so if something goes wrong when closing, that'll simply
  95. * just add another entry on the error stack.
  96. */
  97. (void)loader->close(loader_ctx);
  98. }
  99. return NULL;
  100. }
  101. int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
  102. {
  103. va_list args;
  104. int ret;
  105. va_start(args, cmd);
  106. ret = OSSL_STORE_vctrl(ctx, cmd, args);
  107. va_end(args);
  108. return ret;
  109. }
  110. int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args)
  111. {
  112. if (ctx->loader->ctrl != NULL)
  113. return ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
  114. return 0;
  115. }
  116. int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type)
  117. {
  118. if (ctx->loading) {
  119. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_EXPECT,
  120. OSSL_STORE_R_LOADING_STARTED);
  121. return 0;
  122. }
  123. ctx->expected_type = expected_type;
  124. if (ctx->loader->expect != NULL)
  125. return ctx->loader->expect(ctx->loader_ctx, expected_type);
  126. return 1;
  127. }
  128. int OSSL_STORE_find(OSSL_STORE_CTX *ctx, OSSL_STORE_SEARCH *search)
  129. {
  130. if (ctx->loading) {
  131. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FIND,
  132. OSSL_STORE_R_LOADING_STARTED);
  133. return 0;
  134. }
  135. if (ctx->loader->find == NULL) {
  136. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FIND,
  137. OSSL_STORE_R_UNSUPPORTED_OPERATION);
  138. return 0;
  139. }
  140. return ctx->loader->find(ctx->loader_ctx, search);
  141. }
  142. OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx)
  143. {
  144. OSSL_STORE_INFO *v = NULL;
  145. ctx->loading = 1;
  146. again:
  147. if (OSSL_STORE_eof(ctx))
  148. return NULL;
  149. v = ctx->loader->load(ctx->loader_ctx, ctx->ui_method, ctx->ui_data);
  150. if (ctx->post_process != NULL && v != NULL) {
  151. v = ctx->post_process(v, ctx->post_process_data);
  152. /*
  153. * By returning NULL, the callback decides that this object should
  154. * be ignored.
  155. */
  156. if (v == NULL)
  157. goto again;
  158. }
  159. if (v != NULL && ctx->expected_type != 0) {
  160. int returned_type = OSSL_STORE_INFO_get_type(v);
  161. if (returned_type != OSSL_STORE_INFO_NAME && returned_type != 0) {
  162. /*
  163. * Soft assert here so those who want to harsly weed out faulty
  164. * loaders can do so using a debugging version of libcrypto.
  165. */
  166. if (ctx->loader->expect != NULL)
  167. assert(ctx->expected_type == returned_type);
  168. if (ctx->expected_type != returned_type) {
  169. OSSL_STORE_INFO_free(v);
  170. goto again;
  171. }
  172. }
  173. }
  174. return v;
  175. }
  176. int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
  177. {
  178. return ctx->loader->error(ctx->loader_ctx);
  179. }
  180. int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
  181. {
  182. return ctx->loader->eof(ctx->loader_ctx);
  183. }
  184. int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
  185. {
  186. int loader_ret;
  187. if (ctx == NULL)
  188. return 1;
  189. loader_ret = ctx->loader->close(ctx->loader_ctx);
  190. OPENSSL_free(ctx);
  191. return loader_ret;
  192. }
  193. /*
  194. * Functions to generate OSSL_STORE_INFOs, one function for each type we
  195. * support having in them as well as a generic constructor.
  196. *
  197. * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
  198. * and will therefore be freed when the OSSL_STORE_INFO is freed.
  199. */
  200. static OSSL_STORE_INFO *store_info_new(int type, void *data)
  201. {
  202. OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
  203. if (info == NULL)
  204. return NULL;
  205. info->type = type;
  206. info->_.data = data;
  207. return info;
  208. }
  209. OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
  210. {
  211. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_NAME, NULL);
  212. if (info == NULL) {
  213. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_NAME,
  214. ERR_R_MALLOC_FAILURE);
  215. return NULL;
  216. }
  217. info->_.name.name = name;
  218. info->_.name.desc = NULL;
  219. return info;
  220. }
  221. int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
  222. {
  223. if (info->type != OSSL_STORE_INFO_NAME) {
  224. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_SET0_NAME_DESCRIPTION,
  225. ERR_R_PASSED_INVALID_ARGUMENT);
  226. return 0;
  227. }
  228. info->_.name.desc = desc;
  229. return 1;
  230. }
  231. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
  232. {
  233. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PARAMS, params);
  234. if (info == NULL)
  235. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PARAMS,
  236. ERR_R_MALLOC_FAILURE);
  237. return info;
  238. }
  239. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
  240. {
  241. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PKEY, pkey);
  242. if (info == NULL)
  243. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PKEY,
  244. ERR_R_MALLOC_FAILURE);
  245. return info;
  246. }
  247. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
  248. {
  249. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CERT, x509);
  250. if (info == NULL)
  251. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CERT,
  252. ERR_R_MALLOC_FAILURE);
  253. return info;
  254. }
  255. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
  256. {
  257. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CRL, crl);
  258. if (info == NULL)
  259. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CRL,
  260. ERR_R_MALLOC_FAILURE);
  261. return info;
  262. }
  263. /*
  264. * Functions to try to extract data from a OSSL_STORE_INFO.
  265. */
  266. int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
  267. {
  268. return info->type;
  269. }
  270. const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
  271. {
  272. if (info->type == OSSL_STORE_INFO_NAME)
  273. return info->_.name.name;
  274. return NULL;
  275. }
  276. char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
  277. {
  278. if (info->type == OSSL_STORE_INFO_NAME) {
  279. char *ret = OPENSSL_strdup(info->_.name.name);
  280. if (ret == NULL)
  281. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
  282. ERR_R_MALLOC_FAILURE);
  283. return ret;
  284. }
  285. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
  286. OSSL_STORE_R_NOT_A_NAME);
  287. return NULL;
  288. }
  289. const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
  290. {
  291. if (info->type == OSSL_STORE_INFO_NAME)
  292. return info->_.name.desc;
  293. return NULL;
  294. }
  295. char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
  296. {
  297. if (info->type == OSSL_STORE_INFO_NAME) {
  298. char *ret = OPENSSL_strdup(info->_.name.desc
  299. ? info->_.name.desc : "");
  300. if (ret == NULL)
  301. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
  302. ERR_R_MALLOC_FAILURE);
  303. return ret;
  304. }
  305. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
  306. OSSL_STORE_R_NOT_A_NAME);
  307. return NULL;
  308. }
  309. EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
  310. {
  311. if (info->type == OSSL_STORE_INFO_PARAMS)
  312. return info->_.params;
  313. return NULL;
  314. }
  315. EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
  316. {
  317. if (info->type == OSSL_STORE_INFO_PARAMS) {
  318. EVP_PKEY_up_ref(info->_.params);
  319. return info->_.params;
  320. }
  321. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PARAMS,
  322. OSSL_STORE_R_NOT_PARAMETERS);
  323. return NULL;
  324. }
  325. EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
  326. {
  327. if (info->type == OSSL_STORE_INFO_PKEY)
  328. return info->_.pkey;
  329. return NULL;
  330. }
  331. EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
  332. {
  333. if (info->type == OSSL_STORE_INFO_PKEY) {
  334. EVP_PKEY_up_ref(info->_.pkey);
  335. return info->_.pkey;
  336. }
  337. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PKEY,
  338. OSSL_STORE_R_NOT_A_KEY);
  339. return NULL;
  340. }
  341. X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
  342. {
  343. if (info->type == OSSL_STORE_INFO_CERT)
  344. return info->_.x509;
  345. return NULL;
  346. }
  347. X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
  348. {
  349. if (info->type == OSSL_STORE_INFO_CERT) {
  350. X509_up_ref(info->_.x509);
  351. return info->_.x509;
  352. }
  353. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CERT,
  354. OSSL_STORE_R_NOT_A_CERTIFICATE);
  355. return NULL;
  356. }
  357. X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
  358. {
  359. if (info->type == OSSL_STORE_INFO_CRL)
  360. return info->_.crl;
  361. return NULL;
  362. }
  363. X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
  364. {
  365. if (info->type == OSSL_STORE_INFO_CRL) {
  366. X509_CRL_up_ref(info->_.crl);
  367. return info->_.crl;
  368. }
  369. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CRL,
  370. OSSL_STORE_R_NOT_A_CRL);
  371. return NULL;
  372. }
  373. /*
  374. * Free the OSSL_STORE_INFO
  375. */
  376. void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
  377. {
  378. if (info != NULL) {
  379. switch (info->type) {
  380. case OSSL_STORE_INFO_EMBEDDED:
  381. BUF_MEM_free(info->_.embedded.blob);
  382. OPENSSL_free(info->_.embedded.pem_name);
  383. break;
  384. case OSSL_STORE_INFO_NAME:
  385. OPENSSL_free(info->_.name.name);
  386. OPENSSL_free(info->_.name.desc);
  387. break;
  388. case OSSL_STORE_INFO_PARAMS:
  389. EVP_PKEY_free(info->_.params);
  390. break;
  391. case OSSL_STORE_INFO_PKEY:
  392. EVP_PKEY_free(info->_.pkey);
  393. break;
  394. case OSSL_STORE_INFO_CERT:
  395. X509_free(info->_.x509);
  396. break;
  397. case OSSL_STORE_INFO_CRL:
  398. X509_CRL_free(info->_.crl);
  399. break;
  400. }
  401. OPENSSL_free(info);
  402. }
  403. }
  404. int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type)
  405. {
  406. OSSL_STORE_SEARCH tmp_search;
  407. if (ctx->loader->find == NULL)
  408. return 0;
  409. tmp_search.search_type = search_type;
  410. return ctx->loader->find(NULL, &tmp_search);
  411. }
  412. /* Search term constructors */
  413. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name)
  414. {
  415. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  416. if (search == NULL) {
  417. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_NAME,
  418. ERR_R_MALLOC_FAILURE);
  419. return NULL;
  420. }
  421. search->search_type = OSSL_STORE_SEARCH_BY_NAME;
  422. search->name = name;
  423. return search;
  424. }
  425. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
  426. const ASN1_INTEGER *serial)
  427. {
  428. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  429. if (search == NULL) {
  430. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_ISSUER_SERIAL,
  431. ERR_R_MALLOC_FAILURE);
  432. return NULL;
  433. }
  434. search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  435. search->name = name;
  436. search->serial = serial;
  437. return search;
  438. }
  439. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
  440. const unsigned char
  441. *bytes, size_t len)
  442. {
  443. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  444. if (search == NULL) {
  445. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT,
  446. ERR_R_MALLOC_FAILURE);
  447. return NULL;
  448. }
  449. if (digest != NULL && len != (size_t)EVP_MD_size(digest)) {
  450. char buf1[20], buf2[20];
  451. BIO_snprintf(buf1, sizeof(buf1), "%d", EVP_MD_size(digest));
  452. BIO_snprintf(buf2, sizeof(buf2), "%zu", len);
  453. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT,
  454. OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST);
  455. ERR_add_error_data(5, EVP_MD_name(digest), " size is ", buf1,
  456. ", fingerprint size is ", buf2);
  457. }
  458. search->search_type = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
  459. search->digest = digest;
  460. search->string = bytes;
  461. search->stringlength = len;
  462. return search;
  463. }
  464. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias)
  465. {
  466. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  467. if (search == NULL) {
  468. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_ALIAS,
  469. ERR_R_MALLOC_FAILURE);
  470. return NULL;
  471. }
  472. search->search_type = OSSL_STORE_SEARCH_BY_ALIAS;
  473. search->string = (const unsigned char *)alias;
  474. search->stringlength = strlen(alias);
  475. return search;
  476. }
  477. /* Search term destructor */
  478. void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search)
  479. {
  480. OPENSSL_free(search);
  481. }
  482. /* Search term accessors */
  483. int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion)
  484. {
  485. return criterion->search_type;
  486. }
  487. X509_NAME *OSSL_STORE_SEARCH_get0_name(OSSL_STORE_SEARCH *criterion)
  488. {
  489. return criterion->name;
  490. }
  491. const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
  492. *criterion)
  493. {
  494. return criterion->serial;
  495. }
  496. const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
  497. *criterion, size_t *length)
  498. {
  499. *length = criterion->stringlength;
  500. return criterion->string;
  501. }
  502. const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion)
  503. {
  504. return (const char *)criterion->string;
  505. }
  506. const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion)
  507. {
  508. return criterion->digest;
  509. }
  510. /* Internal functions */
  511. OSSL_STORE_INFO *ossl_store_info_new_EMBEDDED(const char *new_pem_name,
  512. BUF_MEM *embedded)
  513. {
  514. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_EMBEDDED, NULL);
  515. if (info == NULL) {
  516. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
  517. ERR_R_MALLOC_FAILURE);
  518. return NULL;
  519. }
  520. info->_.embedded.blob = embedded;
  521. info->_.embedded.pem_name =
  522. new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
  523. if (new_pem_name != NULL && info->_.embedded.pem_name == NULL) {
  524. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
  525. ERR_R_MALLOC_FAILURE);
  526. OSSL_STORE_INFO_free(info);
  527. info = NULL;
  528. }
  529. return info;
  530. }
  531. BUF_MEM *ossl_store_info_get0_EMBEDDED_buffer(OSSL_STORE_INFO *info)
  532. {
  533. if (info->type == OSSL_STORE_INFO_EMBEDDED)
  534. return info->_.embedded.blob;
  535. return NULL;
  536. }
  537. char *ossl_store_info_get0_EMBEDDED_pem_name(OSSL_STORE_INFO *info)
  538. {
  539. if (info->type == OSSL_STORE_INFO_EMBEDDED)
  540. return info->_.embedded.pem_name;
  541. return NULL;
  542. }
  543. OSSL_STORE_CTX *ossl_store_attach_pem_bio(BIO *bp, const UI_METHOD *ui_method,
  544. void *ui_data)
  545. {
  546. OSSL_STORE_CTX *ctx = NULL;
  547. const OSSL_STORE_LOADER *loader = NULL;
  548. OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
  549. if ((loader = ossl_store_get0_loader_int("file")) == NULL
  550. || ((loader_ctx = ossl_store_file_attach_pem_bio_int(bp)) == NULL))
  551. goto done;
  552. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  553. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_ATTACH_PEM_BIO,
  554. ERR_R_MALLOC_FAILURE);
  555. goto done;
  556. }
  557. ctx->loader = loader;
  558. ctx->loader_ctx = loader_ctx;
  559. loader_ctx = NULL;
  560. ctx->ui_method = ui_method;
  561. ctx->ui_data = ui_data;
  562. ctx->post_process = NULL;
  563. ctx->post_process_data = NULL;
  564. done:
  565. if (loader_ctx != NULL)
  566. /*
  567. * We ignore a returned error because we will return NULL anyway in
  568. * this case, so if something goes wrong when closing, that'll simply
  569. * just add another entry on the error stack.
  570. */
  571. (void)loader->close(loader_ctx);
  572. return ctx;
  573. }
  574. int ossl_store_detach_pem_bio(OSSL_STORE_CTX *ctx)
  575. {
  576. int loader_ret = ossl_store_file_detach_pem_bio_int(ctx->loader_ctx);
  577. OPENSSL_free(ctx);
  578. return loader_ret;
  579. }