by_dir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  99. return 0;
  100. }
  101. if ((a->buffer = BUF_MEM_new()) == NULL) {
  102. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  103. goto err;
  104. }
  105. a->dirs = NULL;
  106. a->lock = CRYPTO_THREAD_lock_new();
  107. if (a->lock == NULL) {
  108. BUF_MEM_free(a->buffer);
  109. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  110. goto err;
  111. }
  112. lu->method_data = a;
  113. return 1;
  114. err:
  115. OPENSSL_free(a);
  116. return 0;
  117. }
  118. static void by_dir_hash_free(BY_DIR_HASH *hash)
  119. {
  120. OPENSSL_free(hash);
  121. }
  122. static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
  123. const BY_DIR_HASH *const *b)
  124. {
  125. if ((*a)->hash > (*b)->hash)
  126. return 1;
  127. if ((*a)->hash < (*b)->hash)
  128. return -1;
  129. return 0;
  130. }
  131. static void by_dir_entry_free(BY_DIR_ENTRY *ent)
  132. {
  133. OPENSSL_free(ent->dir);
  134. sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
  135. OPENSSL_free(ent);
  136. }
  137. static void free_dir(X509_LOOKUP *lu)
  138. {
  139. BY_DIR *a = (BY_DIR *)lu->method_data;
  140. sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
  141. BUF_MEM_free(a->buffer);
  142. CRYPTO_THREAD_lock_free(a->lock);
  143. OPENSSL_free(a);
  144. }
  145. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
  146. {
  147. int j;
  148. size_t len;
  149. const char *s, *ss, *p;
  150. if (dir == NULL || *dir == '\0') {
  151. ERR_raise(ERR_LIB_X509, X509_R_INVALID_DIRECTORY);
  152. return 0;
  153. }
  154. s = dir;
  155. p = s;
  156. do {
  157. if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
  158. BY_DIR_ENTRY *ent;
  159. ss = s;
  160. s = p + 1;
  161. len = p - ss;
  162. if (len == 0)
  163. continue;
  164. for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
  165. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
  166. if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
  167. break;
  168. }
  169. if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
  170. continue;
  171. if (ctx->dirs == NULL) {
  172. ctx->dirs = sk_BY_DIR_ENTRY_new_null();
  173. if (!ctx->dirs) {
  174. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  175. return 0;
  176. }
  177. }
  178. ent = OPENSSL_malloc(sizeof(*ent));
  179. if (ent == NULL) {
  180. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  181. return 0;
  182. }
  183. ent->dir_type = type;
  184. ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
  185. ent->dir = OPENSSL_strndup(ss, len);
  186. if (ent->dir == NULL || ent->hashes == NULL) {
  187. by_dir_entry_free(ent);
  188. return 0;
  189. }
  190. if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
  191. by_dir_entry_free(ent);
  192. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  193. return 0;
  194. }
  195. }
  196. } while (*p++ != '\0');
  197. return 1;
  198. }
  199. static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
  200. const X509_NAME *name, X509_OBJECT *ret,
  201. OSSL_LIB_CTX *libctx, const char *propq)
  202. {
  203. BY_DIR *ctx;
  204. union {
  205. X509 st_x509;
  206. X509_CRL crl;
  207. } data;
  208. int ok = 0;
  209. int i, j, k;
  210. unsigned long h;
  211. BUF_MEM *b = NULL;
  212. X509_OBJECT stmp, *tmp;
  213. const char *postfix = "";
  214. if (name == NULL)
  215. return 0;
  216. stmp.type = type;
  217. if (type == X509_LU_X509) {
  218. data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */
  219. stmp.data.x509 = &data.st_x509;
  220. } else if (type == X509_LU_CRL) {
  221. data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */
  222. stmp.data.crl = &data.crl;
  223. postfix = "r";
  224. } else {
  225. ERR_raise(ERR_LIB_X509, X509_R_WRONG_LOOKUP_TYPE);
  226. goto finish;
  227. }
  228. if ((b = BUF_MEM_new()) == NULL) {
  229. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  230. goto finish;
  231. }
  232. ctx = (BY_DIR *)xl->method_data;
  233. h = X509_NAME_hash_ex(name, libctx, propq, &i);
  234. if (i == 0)
  235. goto finish;
  236. for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
  237. BY_DIR_ENTRY *ent;
  238. int idx;
  239. BY_DIR_HASH htmp, *hent;
  240. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
  241. j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
  242. if (!BUF_MEM_grow(b, j)) {
  243. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  244. goto finish;
  245. }
  246. if (type == X509_LU_CRL && ent->hashes) {
  247. htmp.hash = h;
  248. if (!CRYPTO_THREAD_read_lock(ctx->lock))
  249. goto finish;
  250. idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
  251. if (idx >= 0) {
  252. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  253. k = hent->suffix;
  254. } else {
  255. hent = NULL;
  256. k = 0;
  257. }
  258. CRYPTO_THREAD_unlock(ctx->lock);
  259. } else {
  260. k = 0;
  261. hent = NULL;
  262. }
  263. for (;;) {
  264. char c = '/';
  265. #ifdef OPENSSL_SYS_VMS
  266. c = ent->dir[strlen(ent->dir) - 1];
  267. if (c != ':' && c != '>' && c != ']') {
  268. /*
  269. * If no separator is present, we assume the directory
  270. * specifier is a logical name, and add a colon. We really
  271. * should use better VMS routines for merging things like
  272. * this, but this will do for now... -- Richard Levitte
  273. */
  274. c = ':';
  275. } else {
  276. c = '\0';
  277. }
  278. if (c == '\0') {
  279. /*
  280. * This is special. When c == '\0', no directory separator
  281. * should be added.
  282. */
  283. BIO_snprintf(b->data, b->max,
  284. "%s%08lx.%s%d", ent->dir, h, postfix, k);
  285. } else
  286. #endif
  287. {
  288. BIO_snprintf(b->data, b->max,
  289. "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
  290. }
  291. #ifndef OPENSSL_NO_POSIX_IO
  292. # ifdef _WIN32
  293. # define stat _stat
  294. # endif
  295. {
  296. struct stat st;
  297. if (stat(b->data, &st) < 0)
  298. break;
  299. }
  300. #endif
  301. /* found one. */
  302. if (type == X509_LU_X509) {
  303. if ((X509_load_cert_file_ex(xl, b->data, ent->dir_type, libctx,
  304. propq)) == 0)
  305. break;
  306. } else if (type == X509_LU_CRL) {
  307. if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
  308. break;
  309. }
  310. /* else case will caught higher up */
  311. k++;
  312. }
  313. /*
  314. * we have added it to the cache so now pull it out again
  315. */
  316. if (k > 0) {
  317. if (!X509_STORE_lock(xl->store_ctx))
  318. goto finish;
  319. j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
  320. tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
  321. X509_STORE_unlock(xl->store_ctx);
  322. } else {
  323. j = -1;
  324. tmp = NULL;
  325. }
  326. /*
  327. * If a CRL, update the last file suffix added for this.
  328. * We don't need to add an entry if k is 0 as this is the initial value.
  329. * This avoids the need for a write lock and sort operation in the
  330. * simple case where no CRL is present for a hash.
  331. */
  332. if (type == X509_LU_CRL && k > 0) {
  333. if (!CRYPTO_THREAD_write_lock(ctx->lock))
  334. goto finish;
  335. /*
  336. * Look for entry again in case another thread added an entry
  337. * first.
  338. */
  339. if (hent == NULL) {
  340. htmp.hash = h;
  341. idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
  342. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  343. }
  344. if (hent == NULL) {
  345. hent = OPENSSL_malloc(sizeof(*hent));
  346. if (hent == NULL) {
  347. CRYPTO_THREAD_unlock(ctx->lock);
  348. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  349. ok = 0;
  350. goto finish;
  351. }
  352. hent->hash = h;
  353. hent->suffix = k;
  354. if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
  355. CRYPTO_THREAD_unlock(ctx->lock);
  356. OPENSSL_free(hent);
  357. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  358. ok = 0;
  359. goto finish;
  360. }
  361. /*
  362. * Ensure stack is sorted so that subsequent sk_BY_DIR_HASH_find
  363. * will not mutate the stack and therefore require a write lock.
  364. */
  365. sk_BY_DIR_HASH_sort(ent->hashes);
  366. } else if (hent->suffix < k) {
  367. hent->suffix = k;
  368. }
  369. CRYPTO_THREAD_unlock(ctx->lock);
  370. }
  371. if (tmp != NULL) {
  372. ok = 1;
  373. ret->type = tmp->type;
  374. memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  375. /*
  376. * Clear any errors that might have been raised processing empty
  377. * or malformed files.
  378. */
  379. ERR_clear_error();
  380. goto finish;
  381. }
  382. }
  383. finish:
  384. BUF_MEM_free(b);
  385. return ok;
  386. }
  387. static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
  388. const X509_NAME *name, X509_OBJECT *ret)
  389. {
  390. return get_cert_by_subject_ex(xl, type, name, ret, NULL, NULL);
  391. }