store_lib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * Copyright 2016-2018 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 "internal/store_int.h"
  19. #include "store_locl.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 = ctx->loader->close(ctx->loader_ctx);
  187. OPENSSL_free(ctx);
  188. return loader_ret;
  189. }
  190. /*
  191. * Functions to generate OSSL_STORE_INFOs, one function for each type we
  192. * support having in them as well as a generic constructor.
  193. *
  194. * In all cases, ownership of the object is transfered to the OSSL_STORE_INFO
  195. * and will therefore be freed when the OSSL_STORE_INFO is freed.
  196. */
  197. static OSSL_STORE_INFO *store_info_new(int type, void *data)
  198. {
  199. OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
  200. if (info == NULL)
  201. return NULL;
  202. info->type = type;
  203. info->_.data = data;
  204. return info;
  205. }
  206. OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
  207. {
  208. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_NAME, NULL);
  209. if (info == NULL) {
  210. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_NAME,
  211. ERR_R_MALLOC_FAILURE);
  212. return NULL;
  213. }
  214. info->_.name.name = name;
  215. info->_.name.desc = NULL;
  216. return info;
  217. }
  218. int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
  219. {
  220. if (info->type != OSSL_STORE_INFO_NAME) {
  221. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_SET0_NAME_DESCRIPTION,
  222. ERR_R_PASSED_INVALID_ARGUMENT);
  223. return 0;
  224. }
  225. info->_.name.desc = desc;
  226. return 1;
  227. }
  228. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
  229. {
  230. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PARAMS, params);
  231. if (info == NULL)
  232. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PARAMS,
  233. ERR_R_MALLOC_FAILURE);
  234. return info;
  235. }
  236. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
  237. {
  238. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PKEY, pkey);
  239. if (info == NULL)
  240. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PKEY,
  241. ERR_R_MALLOC_FAILURE);
  242. return info;
  243. }
  244. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
  245. {
  246. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CERT, x509);
  247. if (info == NULL)
  248. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CERT,
  249. ERR_R_MALLOC_FAILURE);
  250. return info;
  251. }
  252. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
  253. {
  254. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CRL, crl);
  255. if (info == NULL)
  256. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CRL,
  257. ERR_R_MALLOC_FAILURE);
  258. return info;
  259. }
  260. /*
  261. * Functions to try to extract data from a OSSL_STORE_INFO.
  262. */
  263. int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
  264. {
  265. return info->type;
  266. }
  267. const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
  268. {
  269. if (info->type == OSSL_STORE_INFO_NAME)
  270. return info->_.name.name;
  271. return NULL;
  272. }
  273. char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
  274. {
  275. if (info->type == OSSL_STORE_INFO_NAME) {
  276. char *ret = OPENSSL_strdup(info->_.name.name);
  277. if (ret == NULL)
  278. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
  279. ERR_R_MALLOC_FAILURE);
  280. return ret;
  281. }
  282. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
  283. OSSL_STORE_R_NOT_A_NAME);
  284. return NULL;
  285. }
  286. const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
  287. {
  288. if (info->type == OSSL_STORE_INFO_NAME)
  289. return info->_.name.desc;
  290. return NULL;
  291. }
  292. char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
  293. {
  294. if (info->type == OSSL_STORE_INFO_NAME) {
  295. char *ret = OPENSSL_strdup(info->_.name.desc
  296. ? info->_.name.desc : "");
  297. if (ret == NULL)
  298. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
  299. ERR_R_MALLOC_FAILURE);
  300. return ret;
  301. }
  302. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
  303. OSSL_STORE_R_NOT_A_NAME);
  304. return NULL;
  305. }
  306. EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
  307. {
  308. if (info->type == OSSL_STORE_INFO_PARAMS)
  309. return info->_.params;
  310. return NULL;
  311. }
  312. EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
  313. {
  314. if (info->type == OSSL_STORE_INFO_PARAMS) {
  315. EVP_PKEY_up_ref(info->_.params);
  316. return info->_.params;
  317. }
  318. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PARAMS,
  319. OSSL_STORE_R_NOT_PARAMETERS);
  320. return NULL;
  321. }
  322. EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
  323. {
  324. if (info->type == OSSL_STORE_INFO_PKEY)
  325. return info->_.pkey;
  326. return NULL;
  327. }
  328. EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
  329. {
  330. if (info->type == OSSL_STORE_INFO_PKEY) {
  331. EVP_PKEY_up_ref(info->_.pkey);
  332. return info->_.pkey;
  333. }
  334. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PKEY,
  335. OSSL_STORE_R_NOT_A_KEY);
  336. return NULL;
  337. }
  338. X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
  339. {
  340. if (info->type == OSSL_STORE_INFO_CERT)
  341. return info->_.x509;
  342. return NULL;
  343. }
  344. X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
  345. {
  346. if (info->type == OSSL_STORE_INFO_CERT) {
  347. X509_up_ref(info->_.x509);
  348. return info->_.x509;
  349. }
  350. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CERT,
  351. OSSL_STORE_R_NOT_A_CERTIFICATE);
  352. return NULL;
  353. }
  354. X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
  355. {
  356. if (info->type == OSSL_STORE_INFO_CRL)
  357. return info->_.crl;
  358. return NULL;
  359. }
  360. X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
  361. {
  362. if (info->type == OSSL_STORE_INFO_CRL) {
  363. X509_CRL_up_ref(info->_.crl);
  364. return info->_.crl;
  365. }
  366. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CRL,
  367. OSSL_STORE_R_NOT_A_CRL);
  368. return NULL;
  369. }
  370. /*
  371. * Free the OSSL_STORE_INFO
  372. */
  373. void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
  374. {
  375. if (info != NULL) {
  376. switch (info->type) {
  377. case OSSL_STORE_INFO_EMBEDDED:
  378. BUF_MEM_free(info->_.embedded.blob);
  379. OPENSSL_free(info->_.embedded.pem_name);
  380. break;
  381. case OSSL_STORE_INFO_NAME:
  382. OPENSSL_free(info->_.name.name);
  383. OPENSSL_free(info->_.name.desc);
  384. break;
  385. case OSSL_STORE_INFO_PARAMS:
  386. EVP_PKEY_free(info->_.params);
  387. break;
  388. case OSSL_STORE_INFO_PKEY:
  389. EVP_PKEY_free(info->_.pkey);
  390. break;
  391. case OSSL_STORE_INFO_CERT:
  392. X509_free(info->_.x509);
  393. break;
  394. case OSSL_STORE_INFO_CRL:
  395. X509_CRL_free(info->_.crl);
  396. break;
  397. }
  398. OPENSSL_free(info);
  399. }
  400. }
  401. int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type)
  402. {
  403. OSSL_STORE_SEARCH tmp_search;
  404. if (ctx->loader->find == NULL)
  405. return 0;
  406. tmp_search.search_type = search_type;
  407. return ctx->loader->find(NULL, &tmp_search);
  408. }
  409. /* Search term constructors */
  410. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name)
  411. {
  412. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  413. if (search == NULL) {
  414. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_NAME,
  415. ERR_R_MALLOC_FAILURE);
  416. return NULL;
  417. }
  418. search->search_type = OSSL_STORE_SEARCH_BY_NAME;
  419. search->name = name;
  420. return search;
  421. }
  422. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
  423. const ASN1_INTEGER *serial)
  424. {
  425. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  426. if (search == NULL) {
  427. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_ISSUER_SERIAL,
  428. ERR_R_MALLOC_FAILURE);
  429. return NULL;
  430. }
  431. search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  432. search->name = name;
  433. search->serial = serial;
  434. return search;
  435. }
  436. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
  437. const unsigned char
  438. *bytes, size_t len)
  439. {
  440. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  441. if (search == NULL) {
  442. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT,
  443. ERR_R_MALLOC_FAILURE);
  444. return NULL;
  445. }
  446. if (digest != NULL && len != (size_t)EVP_MD_size(digest)) {
  447. char buf1[20], buf2[20];
  448. BIO_snprintf(buf1, sizeof(buf1), "%d", EVP_MD_size(digest));
  449. BIO_snprintf(buf2, sizeof(buf2), "%zu", len);
  450. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT,
  451. OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST);
  452. ERR_add_error_data(5, EVP_MD_name(digest), " size is ", buf1,
  453. ", fingerprint size is ", buf2);
  454. }
  455. search->search_type = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
  456. search->digest = digest;
  457. search->string = bytes;
  458. search->stringlength = len;
  459. return search;
  460. }
  461. OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias)
  462. {
  463. OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
  464. if (search == NULL) {
  465. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_SEARCH_BY_ALIAS,
  466. ERR_R_MALLOC_FAILURE);
  467. return NULL;
  468. }
  469. search->search_type = OSSL_STORE_SEARCH_BY_ALIAS;
  470. search->string = (const unsigned char *)alias;
  471. search->stringlength = strlen(alias);
  472. return search;
  473. }
  474. /* Search term destructor */
  475. void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search)
  476. {
  477. OPENSSL_free(search);
  478. }
  479. /* Search term accessors */
  480. int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion)
  481. {
  482. return criterion->search_type;
  483. }
  484. X509_NAME *OSSL_STORE_SEARCH_get0_name(OSSL_STORE_SEARCH *criterion)
  485. {
  486. return criterion->name;
  487. }
  488. const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
  489. *criterion)
  490. {
  491. return criterion->serial;
  492. }
  493. const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
  494. *criterion, size_t *length)
  495. {
  496. *length = criterion->stringlength;
  497. return criterion->string;
  498. }
  499. const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion)
  500. {
  501. return (const char *)criterion->string;
  502. }
  503. const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion)
  504. {
  505. return criterion->digest;
  506. }
  507. /* Internal functions */
  508. OSSL_STORE_INFO *ossl_store_info_new_EMBEDDED(const char *new_pem_name,
  509. BUF_MEM *embedded)
  510. {
  511. OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_EMBEDDED, NULL);
  512. if (info == NULL) {
  513. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
  514. ERR_R_MALLOC_FAILURE);
  515. return NULL;
  516. }
  517. info->_.embedded.blob = embedded;
  518. info->_.embedded.pem_name =
  519. new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
  520. if (new_pem_name != NULL && info->_.embedded.pem_name == NULL) {
  521. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
  522. ERR_R_MALLOC_FAILURE);
  523. OSSL_STORE_INFO_free(info);
  524. info = NULL;
  525. }
  526. return info;
  527. }
  528. BUF_MEM *ossl_store_info_get0_EMBEDDED_buffer(OSSL_STORE_INFO *info)
  529. {
  530. if (info->type == OSSL_STORE_INFO_EMBEDDED)
  531. return info->_.embedded.blob;
  532. return NULL;
  533. }
  534. char *ossl_store_info_get0_EMBEDDED_pem_name(OSSL_STORE_INFO *info)
  535. {
  536. if (info->type == OSSL_STORE_INFO_EMBEDDED)
  537. return info->_.embedded.pem_name;
  538. return NULL;
  539. }
  540. OSSL_STORE_CTX *ossl_store_attach_pem_bio(BIO *bp, const UI_METHOD *ui_method,
  541. void *ui_data)
  542. {
  543. OSSL_STORE_CTX *ctx = NULL;
  544. const OSSL_STORE_LOADER *loader = NULL;
  545. OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
  546. if ((loader = ossl_store_get0_loader_int("file")) == NULL
  547. || ((loader_ctx = ossl_store_file_attach_pem_bio_int(bp)) == NULL))
  548. goto done;
  549. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  550. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_ATTACH_PEM_BIO,
  551. ERR_R_MALLOC_FAILURE);
  552. goto done;
  553. }
  554. ctx->loader = loader;
  555. ctx->loader_ctx = loader_ctx;
  556. loader_ctx = NULL;
  557. ctx->ui_method = ui_method;
  558. ctx->ui_data = ui_data;
  559. ctx->post_process = NULL;
  560. ctx->post_process_data = NULL;
  561. done:
  562. if (loader_ctx != NULL)
  563. /*
  564. * We ignore a returned error because we will return NULL anyway in
  565. * this case, so if something goes wrong when closing, that'll simply
  566. * just add another entry on the error stack.
  567. */
  568. (void)loader->close(loader_ctx);
  569. return ctx;
  570. }
  571. int ossl_store_detach_pem_bio(OSSL_STORE_CTX *ctx)
  572. {
  573. int loader_ret = ossl_store_file_detach_pem_bio_int(ctx->loader_ctx);
  574. OPENSSL_free(ctx);
  575. return loader_ret;
  576. }