by_store.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2018-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 <openssl/safestack.h>
  10. #include <openssl/store.h>
  11. #include "internal/cryptlib.h"
  12. #include "crypto/x509.h"
  13. #include "x509_local.h"
  14. typedef struct cached_store_st {
  15. char *uri;
  16. OSSL_LIB_CTX *libctx;
  17. char *propq;
  18. OSSL_STORE_CTX *ctx;
  19. } CACHED_STORE;
  20. DEFINE_STACK_OF(CACHED_STORE)
  21. /* Generic object loader, given expected type and criterion */
  22. static int cache_objects(X509_LOOKUP *lctx, CACHED_STORE *store,
  23. const OSSL_STORE_SEARCH *criterion, int depth)
  24. {
  25. int ok = 0;
  26. OSSL_STORE_CTX *ctx = store->ctx;
  27. X509_STORE *xstore = X509_LOOKUP_get_store(lctx);
  28. if (ctx == NULL
  29. && (ctx = OSSL_STORE_open_ex(store->uri, store->libctx, store->propq,
  30. NULL, NULL, NULL, NULL, NULL)) == NULL)
  31. return 0;
  32. store->ctx = ctx;
  33. /*
  34. * We try to set the criterion, but don't care if it was valid or not.
  35. * For an OSSL_STORE, it merely serves as an optimization, the expectation
  36. * being that if the criterion couldn't be used, we will get *everything*
  37. * from the container that the URI represents rather than the subset that
  38. * the criterion indicates, so the biggest harm is that we cache more
  39. * objects certs and CRLs than we may expect, but that's ok.
  40. *
  41. * Specifically for OpenSSL's own file: scheme, the only workable
  42. * criterion is the BY_NAME one, which it can only apply on directories,
  43. * but it's possible that the URI is a single file rather than a directory,
  44. * and in that case, the BY_NAME criterion is pointless.
  45. *
  46. * We could very simply not apply any criterion at all here, and just let
  47. * the code that selects certs and CRLs from the cached objects do its job,
  48. * but it's a nice optimization when it can be applied (such as on an
  49. * actual directory with a thousand CA certs).
  50. */
  51. if (criterion != NULL)
  52. OSSL_STORE_find(ctx, criterion);
  53. for (;;) {
  54. OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
  55. int infotype;
  56. /* NULL means error or "end of file". Either way, we break. */
  57. if (info == NULL)
  58. break;
  59. infotype = OSSL_STORE_INFO_get_type(info);
  60. ok = 0;
  61. if (infotype == OSSL_STORE_INFO_NAME) {
  62. /*
  63. * This is an entry in the "directory" represented by the current
  64. * uri. if |depth| allows, dive into it.
  65. */
  66. if (depth > 0) {
  67. CACHED_STORE substore;
  68. substore.uri = (char *)OSSL_STORE_INFO_get0_NAME(info);
  69. substore.libctx = store->libctx;
  70. substore.propq = store->propq;
  71. substore.ctx = NULL;
  72. ok = cache_objects(lctx, &substore, criterion, depth - 1);
  73. }
  74. } else {
  75. /*
  76. * We know that X509_STORE_add_{cert|crl} increments the object's
  77. * refcount, so we can safely use OSSL_STORE_INFO_get0_{cert,crl}
  78. * to get them.
  79. */
  80. switch (infotype) {
  81. case OSSL_STORE_INFO_CERT:
  82. ok = X509_STORE_add_cert(xstore,
  83. OSSL_STORE_INFO_get0_CERT(info));
  84. break;
  85. case OSSL_STORE_INFO_CRL:
  86. ok = X509_STORE_add_crl(xstore,
  87. OSSL_STORE_INFO_get0_CRL(info));
  88. break;
  89. }
  90. }
  91. OSSL_STORE_INFO_free(info);
  92. if (!ok)
  93. break;
  94. }
  95. OSSL_STORE_close(ctx);
  96. store->ctx = NULL;
  97. return ok;
  98. }
  99. static void free_store(CACHED_STORE *store)
  100. {
  101. if (store != NULL) {
  102. OSSL_STORE_close(store->ctx);
  103. OPENSSL_free(store->uri);
  104. OPENSSL_free(store->propq);
  105. OPENSSL_free(store);
  106. }
  107. }
  108. static void by_store_free(X509_LOOKUP *ctx)
  109. {
  110. STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
  111. sk_CACHED_STORE_pop_free(stores, free_store);
  112. }
  113. static int by_store_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
  114. long argl, char **retp, OSSL_LIB_CTX *libctx,
  115. const char *propq)
  116. {
  117. /*
  118. * In some cases below, failing to use the defaults shouldn't result in
  119. * an error. |use_default| is used as the return code in those cases.
  120. */
  121. int use_default = argp == NULL;
  122. switch (cmd) {
  123. case X509_L_ADD_STORE:
  124. /* If no URI is given, use the default cert dir as default URI */
  125. if (argp == NULL)
  126. argp = ossl_safe_getenv(X509_get_default_cert_dir_env());
  127. if (argp == NULL)
  128. argp = X509_get_default_cert_dir();
  129. {
  130. STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
  131. CACHED_STORE *store = OPENSSL_zalloc(sizeof(*store));
  132. if (store == NULL) {
  133. return 0;
  134. }
  135. store->uri = OPENSSL_strdup(argp);
  136. store->libctx = libctx;
  137. if (propq != NULL)
  138. store->propq = OPENSSL_strdup(propq);
  139. store->ctx = OSSL_STORE_open_ex(argp, libctx, propq, NULL, NULL,
  140. NULL, NULL, NULL);
  141. if (store->ctx == NULL
  142. || (propq != NULL && store->propq == NULL)
  143. || store->uri == NULL) {
  144. free_store(store);
  145. return use_default;
  146. }
  147. if (stores == NULL) {
  148. stores = sk_CACHED_STORE_new_null();
  149. if (stores != NULL)
  150. X509_LOOKUP_set_method_data(ctx, stores);
  151. }
  152. if (stores == NULL || sk_CACHED_STORE_push(stores, store) <= 0) {
  153. free_store(store);
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. case X509_L_LOAD_STORE: {
  159. /* This is a shortcut for quick loading of specific containers */
  160. CACHED_STORE store;
  161. store.uri = (char *)argp;
  162. store.libctx = libctx;
  163. store.propq = (char *)propq;
  164. store.ctx = NULL;
  165. return cache_objects(ctx, &store, NULL, 0);
  166. }
  167. default:
  168. /* Unsupported command */
  169. return 0;
  170. }
  171. return 0;
  172. }
  173. static int by_store_ctrl(X509_LOOKUP *ctx, int cmd,
  174. const char *argp, long argl, char **retp)
  175. {
  176. return by_store_ctrl_ex(ctx, cmd, argp, argl, retp, NULL, NULL);
  177. }
  178. static int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  179. const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret)
  180. {
  181. STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
  182. int i;
  183. int ok = 0;
  184. for (i = 0; i < sk_CACHED_STORE_num(stores); i++) {
  185. ok = cache_objects(ctx, sk_CACHED_STORE_value(stores, i), criterion,
  186. 1 /* depth */);
  187. if (ok)
  188. break;
  189. }
  190. return ok;
  191. }
  192. static int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  193. const X509_NAME *name, X509_OBJECT *ret)
  194. {
  195. OSSL_STORE_SEARCH *criterion =
  196. OSSL_STORE_SEARCH_by_name((X509_NAME *)name); /* won't modify it */
  197. int ok = by_store(ctx, type, criterion, ret);
  198. STACK_OF(X509_OBJECT) *store_objects =
  199. X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx));
  200. X509_OBJECT *tmp = NULL;
  201. OSSL_STORE_SEARCH_free(criterion);
  202. if (ok)
  203. tmp = X509_OBJECT_retrieve_by_subject(store_objects, type, name);
  204. ok = 0;
  205. if (tmp != NULL) {
  206. /*
  207. * This could also be done like this:
  208. *
  209. * if (tmp != NULL) {
  210. * *ret = *tmp;
  211. * ok = 1;
  212. * }
  213. *
  214. * However, we want to exercise the documented API to the max, so
  215. * we do it the hard way.
  216. *
  217. * To be noted is that X509_OBJECT_set1_* increment the refcount,
  218. * but so does X509_STORE_CTX_get_by_subject upon return of this
  219. * function, so we must ensure the refcount is decremented
  220. * before we return, or we will get a refcount leak. We cannot do
  221. * this with X509_OBJECT_free(), though, as that will free a bit
  222. * too much.
  223. */
  224. switch (type) {
  225. case X509_LU_X509:
  226. ok = X509_OBJECT_set1_X509(ret, tmp->data.x509);
  227. if (ok)
  228. X509_free(tmp->data.x509);
  229. break;
  230. case X509_LU_CRL:
  231. ok = X509_OBJECT_set1_X509_CRL(ret, tmp->data.crl);
  232. if (ok)
  233. X509_CRL_free(tmp->data.crl);
  234. break;
  235. case X509_LU_NONE:
  236. break;
  237. }
  238. }
  239. return ok;
  240. }
  241. /*
  242. * We lack the implementations for get_by_issuer_serial, get_by_fingerprint
  243. * and get_by_alias. There's simply not enough support in the X509_LOOKUP
  244. * or X509_STORE APIs.
  245. */
  246. static X509_LOOKUP_METHOD x509_store_lookup = {
  247. "Load certs from STORE URIs",
  248. NULL, /* new_item */
  249. by_store_free, /* free */
  250. NULL, /* init */
  251. NULL, /* shutdown */
  252. by_store_ctrl, /* ctrl */
  253. by_store_subject, /* get_by_subject */
  254. NULL, /* get_by_issuer_serial */
  255. NULL, /* get_by_fingerprint */
  256. NULL, /* get_by_alias */
  257. NULL, /* get_by_subject_ex */
  258. by_store_ctrl_ex
  259. };
  260. X509_LOOKUP_METHOD *X509_LOOKUP_store(void)
  261. {
  262. return &x509_store_lookup;
  263. }