store_lib.c 19 KB

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