by_dir.c 12 KB

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