by_store.c 9.8 KB

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