by_store.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2018-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 <openssl/store.h>
  10. #include "internal/cryptlib.h"
  11. #include "crypto/x509.h"
  12. #include "x509_local.h"
  13. DEFINE_STACK_OF_STRING()
  14. /* Generic object loader, given expected type and criterion */
  15. static int cache_objects(X509_LOOKUP *lctx, const char *uri,
  16. const OSSL_STORE_SEARCH *criterion,
  17. int depth)
  18. {
  19. int ok = 0;
  20. OSSL_STORE_CTX *ctx = NULL;
  21. X509_STORE *xstore = X509_LOOKUP_get_store(lctx);
  22. if ((ctx = OSSL_STORE_open(uri, NULL, NULL, 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 a 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);
  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(X509_LOOKUP *ctx, int cmd,
  95. const char *argp, long argl,
  96. char **retp)
  97. {
  98. switch (cmd) {
  99. case X509_L_ADD_STORE:
  100. /* If no URI is given, use the default cert dir as default URI */
  101. if (argp == NULL)
  102. argp = ossl_safe_getenv(X509_get_default_cert_dir_env());
  103. if (argp == NULL)
  104. argp = X509_get_default_cert_dir();
  105. {
  106. STACK_OF(OPENSSL_STRING) *uris = X509_LOOKUP_get_method_data(ctx);
  107. if (uris == NULL) {
  108. uris = sk_OPENSSL_STRING_new_null();
  109. X509_LOOKUP_set_method_data(ctx, uris);
  110. }
  111. return sk_OPENSSL_STRING_push(uris, OPENSSL_strdup(argp)) > 0;
  112. }
  113. case X509_L_LOAD_STORE:
  114. /* This is a shortcut for quick loading of specific containers */
  115. return cache_objects(ctx, argp, NULL, 0);
  116. }
  117. return 0;
  118. }
  119. static int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  120. const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret)
  121. {
  122. STACK_OF(OPENSSL_STRING) *uris = X509_LOOKUP_get_method_data(ctx);
  123. int i;
  124. int ok = 0;
  125. for (i = 0; i < sk_OPENSSL_STRING_num(uris); i++) {
  126. ok = cache_objects(ctx, sk_OPENSSL_STRING_value(uris, i), criterion,
  127. 1 /* depth */);
  128. if (ok)
  129. break;
  130. }
  131. return ok;
  132. }
  133. static int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  134. const X509_NAME *name, X509_OBJECT *ret)
  135. {
  136. OSSL_STORE_SEARCH *criterion =
  137. OSSL_STORE_SEARCH_by_name((X509_NAME *)name); /* won't modify it */
  138. int ok = by_store(ctx, type, criterion, ret);
  139. STACK_OF(X509_OBJECT) *store_objects =
  140. X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx));
  141. X509_OBJECT *tmp = NULL;
  142. OSSL_STORE_SEARCH_free(criterion);
  143. if (ok)
  144. tmp = X509_OBJECT_retrieve_by_subject(store_objects, type, name);
  145. ok = 0;
  146. if (tmp != NULL) {
  147. /*
  148. * This could also be done like this:
  149. *
  150. * if (tmp != NULL) {
  151. * *ret = *tmp;
  152. * ok = 1;
  153. * }
  154. *
  155. * However, we want to exercise the documented API to the max, so
  156. * we do it the hard way.
  157. *
  158. * To be noted is that X509_OBJECT_set1_* increment the refcount,
  159. * but so does X509_STORE_CTX_get_by_subject upon return of this
  160. * function, so we must ensure the the refcount is decremented
  161. * before we return, or we will get a refcount leak. We cannot do
  162. * this with X509_OBJECT_free(), though, as that will free a bit
  163. * too much.
  164. */
  165. switch (type) {
  166. case X509_LU_X509:
  167. ok = X509_OBJECT_set1_X509(ret, tmp->data.x509);
  168. if (ok)
  169. X509_free(tmp->data.x509);
  170. break;
  171. case X509_LU_CRL:
  172. ok = X509_OBJECT_set1_X509_CRL(ret, tmp->data.crl);
  173. if (ok)
  174. X509_CRL_free(tmp->data.crl);
  175. break;
  176. case X509_LU_NONE:
  177. break;
  178. }
  179. }
  180. return ok;
  181. }
  182. /*
  183. * We lack the implementations for get_by_issuer_serial, get_by_fingerprint
  184. * and get_by_alias. There's simply not enough support in the X509_LOOKUP
  185. * or X509_STORE APIs.
  186. */
  187. static X509_LOOKUP_METHOD x509_store_lookup = {
  188. "Load certs from STORE URIs",
  189. NULL, /* new_item */
  190. by_store_free, /* free */
  191. NULL, /* init */
  192. NULL, /* shutdown */
  193. by_store_ctrl, /* ctrl */
  194. by_store_subject, /* get_by_subject */
  195. NULL, /* get_by_issuer_serial */
  196. NULL, /* get_by_fingerprint */
  197. NULL, /* get_by_alias */
  198. };
  199. X509_LOOKUP_METHOD *X509_LOOKUP_store(void)
  200. {
  201. return &x509_store_lookup;
  202. }