store_lib.c 19 KB

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