by_dir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright 1995-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. #if defined (__TANDEM) && defined (_SPT_MODEL_)
  10. /*
  11. * These definitions have to come first in SPT due to scoping of the
  12. * declarations in c99 associated with SPT use of stat.
  13. */
  14. # include <sys/types.h>
  15. # include <sys/stat.h>
  16. #endif
  17. #include "internal/e_os.h"
  18. #include "internal/cryptlib.h"
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <errno.h>
  22. #include <sys/types.h>
  23. #ifndef OPENSSL_NO_POSIX_IO
  24. # include <sys/stat.h>
  25. #endif
  26. #include <openssl/x509.h>
  27. #include "crypto/x509.h"
  28. #include "x509_local.h"
  29. struct lookup_dir_hashes_st {
  30. unsigned long hash;
  31. int suffix;
  32. };
  33. struct lookup_dir_entry_st {
  34. char *dir;
  35. int dir_type;
  36. STACK_OF(BY_DIR_HASH) *hashes;
  37. };
  38. typedef struct lookup_dir_st {
  39. BUF_MEM *buffer;
  40. STACK_OF(BY_DIR_ENTRY) *dirs;
  41. CRYPTO_RWLOCK *lock;
  42. } BY_DIR;
  43. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  44. char **retp);
  45. static int new_dir(X509_LOOKUP *lu);
  46. static void free_dir(X509_LOOKUP *lu);
  47. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
  48. static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
  49. const X509_NAME *name, X509_OBJECT *ret);
  50. static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
  51. const X509_NAME *name, X509_OBJECT *ret,
  52. OSSL_LIB_CTX *libctx, const char *propq);
  53. static X509_LOOKUP_METHOD x509_dir_lookup = {
  54. "Load certs from files in a directory",
  55. new_dir, /* new_item */
  56. free_dir, /* free */
  57. NULL, /* init */
  58. NULL, /* shutdown */
  59. dir_ctrl, /* ctrl */
  60. get_cert_by_subject, /* get_by_subject */
  61. NULL, /* get_by_issuer_serial */
  62. NULL, /* get_by_fingerprint */
  63. NULL, /* get_by_alias */
  64. get_cert_by_subject_ex, /* get_by_subject_ex */
  65. NULL, /* ctrl_ex */
  66. };
  67. X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
  68. {
  69. return &x509_dir_lookup;
  70. }
  71. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  72. char **retp)
  73. {
  74. int ret = 0;
  75. BY_DIR *ld = (BY_DIR *)ctx->method_data;
  76. switch (cmd) {
  77. case X509_L_ADD_DIR:
  78. if (argl == X509_FILETYPE_DEFAULT) {
  79. const char *dir = ossl_safe_getenv(X509_get_default_cert_dir_env());
  80. if (dir)
  81. ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
  82. else
  83. ret = add_cert_dir(ld, X509_get_default_cert_dir(),
  84. X509_FILETYPE_PEM);
  85. if (!ret) {
  86. ERR_raise(ERR_LIB_X509, X509_R_LOADING_CERT_DIR);
  87. }
  88. } else
  89. ret = add_cert_dir(ld, argp, (int)argl);
  90. break;
  91. }
  92. return ret;
  93. }
  94. static int new_dir(X509_LOOKUP *lu)
  95. {
  96. BY_DIR *a = OPENSSL_malloc(sizeof(*a));
  97. if (a == NULL)
  98. return 0;
  99. if ((a->buffer = BUF_MEM_new()) == NULL) {
  100. ERR_raise(ERR_LIB_X509, ERR_R_BN_LIB);
  101. goto err;
  102. }
  103. a->dirs = NULL;
  104. a->lock = CRYPTO_THREAD_lock_new();
  105. if (a->lock == NULL) {
  106. BUF_MEM_free(a->buffer);
  107. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  108. goto err;
  109. }
  110. lu->method_data = a;
  111. return 1;
  112. err:
  113. OPENSSL_free(a);
  114. return 0;
  115. }
  116. static void by_dir_hash_free(BY_DIR_HASH *hash)
  117. {
  118. OPENSSL_free(hash);
  119. }
  120. static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
  121. const BY_DIR_HASH *const *b)
  122. {
  123. if ((*a)->hash > (*b)->hash)
  124. return 1;
  125. if ((*a)->hash < (*b)->hash)
  126. return -1;
  127. return 0;
  128. }
  129. static void by_dir_entry_free(BY_DIR_ENTRY *ent)
  130. {
  131. OPENSSL_free(ent->dir);
  132. sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
  133. OPENSSL_free(ent);
  134. }
  135. static void free_dir(X509_LOOKUP *lu)
  136. {
  137. BY_DIR *a = (BY_DIR *)lu->method_data;
  138. sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
  139. BUF_MEM_free(a->buffer);
  140. CRYPTO_THREAD_lock_free(a->lock);
  141. OPENSSL_free(a);
  142. }
  143. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
  144. {
  145. int j;
  146. size_t len;
  147. const char *s, *ss, *p;
  148. if (dir == NULL || *dir == '\0') {
  149. ERR_raise(ERR_LIB_X509, X509_R_INVALID_DIRECTORY);
  150. return 0;
  151. }
  152. s = dir;
  153. p = s;
  154. do {
  155. if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
  156. BY_DIR_ENTRY *ent;
  157. ss = s;
  158. s = p + 1;
  159. len = p - ss;
  160. if (len == 0)
  161. continue;
  162. for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
  163. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
  164. if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
  165. break;
  166. }
  167. if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
  168. continue;
  169. if (ctx->dirs == NULL) {
  170. ctx->dirs = sk_BY_DIR_ENTRY_new_null();
  171. if (!ctx->dirs) {
  172. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  173. return 0;
  174. }
  175. }
  176. ent = OPENSSL_malloc(sizeof(*ent));
  177. if (ent == NULL)
  178. return 0;
  179. ent->dir_type = type;
  180. ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
  181. ent->dir = OPENSSL_strndup(ss, len);
  182. if (ent->dir == NULL || ent->hashes == NULL) {
  183. by_dir_entry_free(ent);
  184. return 0;
  185. }
  186. if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
  187. by_dir_entry_free(ent);
  188. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  189. return 0;
  190. }
  191. }
  192. } while (*p++ != '\0');
  193. return 1;
  194. }
  195. static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
  196. const X509_NAME *name, X509_OBJECT *ret,
  197. OSSL_LIB_CTX *libctx, const char *propq)
  198. {
  199. BY_DIR *ctx;
  200. union {
  201. X509 st_x509;
  202. X509_CRL crl;
  203. } data;
  204. int ok = 0;
  205. int i, j, k;
  206. unsigned long h;
  207. BUF_MEM *b = NULL;
  208. X509_OBJECT stmp, *tmp;
  209. const char *postfix = "";
  210. if (name == NULL)
  211. return 0;
  212. stmp.type = type;
  213. if (type == X509_LU_X509) {
  214. data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */
  215. stmp.data.x509 = &data.st_x509;
  216. } else if (type == X509_LU_CRL) {
  217. data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */
  218. stmp.data.crl = &data.crl;
  219. postfix = "r";
  220. } else {
  221. ERR_raise(ERR_LIB_X509, X509_R_WRONG_LOOKUP_TYPE);
  222. goto finish;
  223. }
  224. if ((b = BUF_MEM_new()) == NULL) {
  225. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  226. goto finish;
  227. }
  228. ctx = (BY_DIR *)xl->method_data;
  229. h = X509_NAME_hash_ex(name, libctx, propq, &i);
  230. if (i == 0)
  231. goto finish;
  232. for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
  233. BY_DIR_ENTRY *ent;
  234. int idx;
  235. BY_DIR_HASH htmp, *hent;
  236. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
  237. j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
  238. if (!BUF_MEM_grow(b, j)) {
  239. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  240. goto finish;
  241. }
  242. if (type == X509_LU_CRL && ent->hashes) {
  243. htmp.hash = h;
  244. if (!CRYPTO_THREAD_read_lock(ctx->lock))
  245. goto finish;
  246. idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
  247. if (idx >= 0) {
  248. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  249. k = hent->suffix;
  250. } else {
  251. hent = NULL;
  252. k = 0;
  253. }
  254. CRYPTO_THREAD_unlock(ctx->lock);
  255. } else {
  256. k = 0;
  257. hent = NULL;
  258. }
  259. for (;;) {
  260. char c = '/';
  261. #ifdef OPENSSL_SYS_VMS
  262. c = ent->dir[strlen(ent->dir) - 1];
  263. if (c != ':' && c != '>' && c != ']') {
  264. /*
  265. * If no separator is present, we assume the directory
  266. * specifier is a logical name, and add a colon. We really
  267. * should use better VMS routines for merging things like
  268. * this, but this will do for now... -- Richard Levitte
  269. */
  270. c = ':';
  271. } else {
  272. c = '\0';
  273. }
  274. if (c == '\0') {
  275. /*
  276. * This is special. When c == '\0', no directory separator
  277. * should be added.
  278. */
  279. BIO_snprintf(b->data, b->max,
  280. "%s%08lx.%s%d", ent->dir, h, postfix, k);
  281. } else
  282. #endif
  283. {
  284. BIO_snprintf(b->data, b->max,
  285. "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
  286. }
  287. #ifndef OPENSSL_NO_POSIX_IO
  288. # ifdef _WIN32
  289. # define stat _stat
  290. # endif
  291. {
  292. struct stat st;
  293. if (stat(b->data, &st) < 0)
  294. break;
  295. }
  296. #endif
  297. /* found one. */
  298. if (type == X509_LU_X509) {
  299. if ((X509_load_cert_file_ex(xl, b->data, ent->dir_type, libctx,
  300. propq)) == 0)
  301. break;
  302. } else if (type == X509_LU_CRL) {
  303. if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
  304. break;
  305. }
  306. /* else case will caught higher up */
  307. k++;
  308. }
  309. /*
  310. * we have added it to the cache so now pull it out again
  311. *
  312. * Note: quadratic time find here since the objects won't generally be
  313. * sorted and sorting the would result in O(n^2 log n) complexity.
  314. */
  315. if (k > 0) {
  316. if (!X509_STORE_lock(xl->store_ctx))
  317. goto finish;
  318. j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
  319. tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
  320. X509_STORE_unlock(xl->store_ctx);
  321. } else {
  322. tmp = NULL;
  323. }
  324. /*
  325. * If a CRL, update the last file suffix added for this.
  326. * We don't need to add an entry if k is 0 as this is the initial value.
  327. * This avoids the need for a write lock and sort operation in the
  328. * simple case where no CRL is present for a hash.
  329. */
  330. if (type == X509_LU_CRL && k > 0) {
  331. if (!CRYPTO_THREAD_write_lock(ctx->lock))
  332. goto finish;
  333. /*
  334. * Look for entry again in case another thread added an entry
  335. * first.
  336. */
  337. if (hent == NULL) {
  338. htmp.hash = h;
  339. idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
  340. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  341. }
  342. if (hent == NULL) {
  343. hent = OPENSSL_malloc(sizeof(*hent));
  344. if (hent == NULL) {
  345. CRYPTO_THREAD_unlock(ctx->lock);
  346. ok = 0;
  347. goto finish;
  348. }
  349. hent->hash = h;
  350. hent->suffix = k;
  351. if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
  352. CRYPTO_THREAD_unlock(ctx->lock);
  353. OPENSSL_free(hent);
  354. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  355. ok = 0;
  356. goto finish;
  357. }
  358. /*
  359. * Ensure stack is sorted so that subsequent sk_BY_DIR_HASH_find
  360. * will not mutate the stack and therefore require a write lock.
  361. */
  362. sk_BY_DIR_HASH_sort(ent->hashes);
  363. } else if (hent->suffix < k) {
  364. hent->suffix = k;
  365. }
  366. CRYPTO_THREAD_unlock(ctx->lock);
  367. }
  368. if (tmp != NULL) {
  369. ok = 1;
  370. ret->type = tmp->type;
  371. memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  372. /*
  373. * Clear any errors that might have been raised processing empty
  374. * or malformed files.
  375. */
  376. ERR_clear_error();
  377. goto finish;
  378. }
  379. }
  380. finish:
  381. /* If we changed anything, resort the objects for faster lookup */
  382. if (!sk_X509_OBJECT_is_sorted(xl->store_ctx->objs)) {
  383. if (X509_STORE_lock(xl->store_ctx)) {
  384. sk_X509_OBJECT_sort(xl->store_ctx->objs);
  385. X509_STORE_unlock(xl->store_ctx);
  386. }
  387. }
  388. BUF_MEM_free(b);
  389. return ok;
  390. }
  391. static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
  392. const X509_NAME *name, X509_OBJECT *ret)
  393. {
  394. return get_cert_by_subject_ex(xl, type, name, ret, NULL, NULL);
  395. }