by_store.c 8.2 KB

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