by_dir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* crypto/x509/by_dir.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.] */
  57. #include <string.h>
  58. #include <sys/stat.h>
  59. #include <sys/types.h>
  60. #include <openssl/buf.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/thread.h>
  64. #include <openssl/x509.h>
  65. #include "../internal.h"
  66. typedef struct lookup_dir_hashes_st {
  67. unsigned long hash;
  68. int suffix;
  69. } BY_DIR_HASH;
  70. typedef struct lookup_dir_entry_st {
  71. char *dir;
  72. int dir_type;
  73. STACK_OF(BY_DIR_HASH) *hashes;
  74. } BY_DIR_ENTRY;
  75. typedef struct lookup_dir_st {
  76. BUF_MEM *buffer;
  77. STACK_OF(BY_DIR_ENTRY) *dirs;
  78. } BY_DIR;
  79. DEFINE_STACK_OF(BY_DIR_HASH)
  80. DEFINE_STACK_OF(BY_DIR_ENTRY)
  81. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  82. char **ret);
  83. static int new_dir(X509_LOOKUP *lu);
  84. static void free_dir(X509_LOOKUP *lu);
  85. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
  86. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  87. X509_OBJECT *ret);
  88. static X509_LOOKUP_METHOD x509_dir_lookup = {
  89. "Load certs from files in a directory",
  90. new_dir, /* new */
  91. free_dir, /* free */
  92. NULL, /* init */
  93. NULL, /* shutdown */
  94. dir_ctrl, /* ctrl */
  95. get_cert_by_subject, /* get_by_subject */
  96. NULL, /* get_by_issuer_serial */
  97. NULL, /* get_by_fingerprint */
  98. NULL, /* get_by_alias */
  99. };
  100. X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
  101. {
  102. return (&x509_dir_lookup);
  103. }
  104. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  105. char **retp)
  106. {
  107. int ret = 0;
  108. BY_DIR *ld;
  109. char *dir = NULL;
  110. ld = (BY_DIR *)ctx->method_data;
  111. switch (cmd) {
  112. case X509_L_ADD_DIR:
  113. if (argl == X509_FILETYPE_DEFAULT) {
  114. dir = (char *)getenv(X509_get_default_cert_dir_env());
  115. if (dir)
  116. ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
  117. else
  118. ret = add_cert_dir(ld, X509_get_default_cert_dir(),
  119. X509_FILETYPE_PEM);
  120. if (!ret) {
  121. OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR);
  122. }
  123. } else
  124. ret = add_cert_dir(ld, argp, (int)argl);
  125. break;
  126. }
  127. return (ret);
  128. }
  129. static int new_dir(X509_LOOKUP *lu)
  130. {
  131. BY_DIR *a;
  132. if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
  133. return (0);
  134. if ((a->buffer = BUF_MEM_new()) == NULL) {
  135. OPENSSL_free(a);
  136. return (0);
  137. }
  138. a->dirs = NULL;
  139. lu->method_data = (char *)a;
  140. return (1);
  141. }
  142. static void by_dir_hash_free(BY_DIR_HASH *hash)
  143. {
  144. OPENSSL_free(hash);
  145. }
  146. static int by_dir_hash_cmp(const BY_DIR_HASH **a, const BY_DIR_HASH **b)
  147. {
  148. if ((*a)->hash > (*b)->hash)
  149. return 1;
  150. if ((*a)->hash < (*b)->hash)
  151. return -1;
  152. return 0;
  153. }
  154. static void by_dir_entry_free(BY_DIR_ENTRY *ent)
  155. {
  156. if (ent->dir)
  157. OPENSSL_free(ent->dir);
  158. if (ent->hashes)
  159. sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
  160. OPENSSL_free(ent);
  161. }
  162. static void free_dir(X509_LOOKUP *lu)
  163. {
  164. BY_DIR *a;
  165. a = (BY_DIR *)lu->method_data;
  166. if (a->dirs != NULL)
  167. sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
  168. if (a->buffer != NULL)
  169. BUF_MEM_free(a->buffer);
  170. OPENSSL_free(a);
  171. }
  172. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
  173. {
  174. size_t j, len;
  175. const char *s, *ss, *p;
  176. if (dir == NULL || !*dir) {
  177. OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY);
  178. return 0;
  179. }
  180. s = dir;
  181. p = s;
  182. do {
  183. if ((*p == ':') || (*p == '\0')) {
  184. BY_DIR_ENTRY *ent;
  185. ss = s;
  186. s = p + 1;
  187. len = p - ss;
  188. if (len == 0)
  189. continue;
  190. for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
  191. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
  192. if (strlen(ent->dir) == len &&
  193. strncmp(ent->dir, ss, len) == 0)
  194. break;
  195. }
  196. if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
  197. continue;
  198. if (ctx->dirs == NULL) {
  199. ctx->dirs = sk_BY_DIR_ENTRY_new_null();
  200. if (!ctx->dirs) {
  201. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  202. return 0;
  203. }
  204. }
  205. ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY));
  206. if (!ent)
  207. return 0;
  208. ent->dir_type = type;
  209. ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
  210. ent->dir = OPENSSL_malloc(len + 1);
  211. if (!ent->dir || !ent->hashes) {
  212. by_dir_entry_free(ent);
  213. return 0;
  214. }
  215. BUF_strlcpy(ent->dir, ss, len + 1);
  216. if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
  217. by_dir_entry_free(ent);
  218. return 0;
  219. }
  220. }
  221. } while (*p++ != '\0');
  222. return 1;
  223. }
  224. /*
  225. * g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY|
  226. * objects.
  227. */
  228. static struct CRYPTO_STATIC_MUTEX g_ent_hashes_lock =
  229. CRYPTO_STATIC_MUTEX_INIT;
  230. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  231. X509_OBJECT *ret)
  232. {
  233. BY_DIR *ctx;
  234. union {
  235. struct {
  236. X509 st_x509;
  237. X509_CINF st_x509_cinf;
  238. } x509;
  239. struct {
  240. X509_CRL st_crl;
  241. X509_CRL_INFO st_crl_info;
  242. } crl;
  243. } data;
  244. int ok = 0;
  245. size_t i;
  246. int j, k;
  247. unsigned long h;
  248. unsigned long hash_array[2];
  249. int hash_index;
  250. BUF_MEM *b = NULL;
  251. X509_OBJECT stmp, *tmp;
  252. const char *postfix = "";
  253. if (name == NULL)
  254. return (0);
  255. stmp.type = type;
  256. if (type == X509_LU_X509) {
  257. data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
  258. data.x509.st_x509_cinf.subject = name;
  259. stmp.data.x509 = &data.x509.st_x509;
  260. postfix = "";
  261. } else if (type == X509_LU_CRL) {
  262. data.crl.st_crl.crl = &data.crl.st_crl_info;
  263. data.crl.st_crl_info.issuer = name;
  264. stmp.data.crl = &data.crl.st_crl;
  265. postfix = "r";
  266. } else {
  267. OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE);
  268. goto finish;
  269. }
  270. if ((b = BUF_MEM_new()) == NULL) {
  271. OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
  272. goto finish;
  273. }
  274. ctx = (BY_DIR *)xl->method_data;
  275. hash_array[0] = X509_NAME_hash(name);
  276. hash_array[1] = X509_NAME_hash_old(name);
  277. for (hash_index = 0; hash_index < 2; ++hash_index) {
  278. h = hash_array[hash_index];
  279. for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
  280. BY_DIR_ENTRY *ent;
  281. size_t idx;
  282. BY_DIR_HASH htmp, *hent;
  283. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
  284. j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
  285. if (!BUF_MEM_grow(b, j)) {
  286. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  287. goto finish;
  288. }
  289. if (type == X509_LU_CRL && ent->hashes) {
  290. htmp.hash = h;
  291. CRYPTO_STATIC_MUTEX_lock_read(&g_ent_hashes_lock);
  292. if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) {
  293. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  294. k = hent->suffix;
  295. } else {
  296. hent = NULL;
  297. k = 0;
  298. }
  299. CRYPTO_STATIC_MUTEX_unlock_read(&g_ent_hashes_lock);
  300. } else {
  301. k = 0;
  302. hent = NULL;
  303. }
  304. for (;;) {
  305. char c = '/';
  306. #ifdef OPENSSL_SYS_VMS
  307. c = ent->dir[strlen(ent->dir) - 1];
  308. if (c != ':' && c != '>' && c != ']') {
  309. /*
  310. * If no separator is present, we assume the directory
  311. * specifier is a logical name, and add a colon. We
  312. * really should use better VMS routines for merging
  313. * things like this, but this will do for now... --
  314. * Richard Levitte
  315. */
  316. c = ':';
  317. } else {
  318. c = '\0';
  319. }
  320. #endif
  321. if (c == '\0') {
  322. /*
  323. * This is special. When c == '\0', no directory
  324. * separator should be added.
  325. */
  326. BIO_snprintf(b->data, b->max,
  327. "%s%08lx.%s%d", ent->dir, h, postfix, k);
  328. } else {
  329. BIO_snprintf(b->data, b->max,
  330. "%s%c%08lx.%s%d", ent->dir, c, h,
  331. postfix, k);
  332. }
  333. #ifndef OPENSSL_NO_POSIX_IO
  334. # if defined(_WIN32) && !defined(stat)
  335. # define stat _stat
  336. # endif
  337. {
  338. struct stat st;
  339. if (stat(b->data, &st) < 0)
  340. break;
  341. }
  342. #endif
  343. /* found one. */
  344. if (type == X509_LU_X509) {
  345. if ((X509_load_cert_file(xl, b->data,
  346. ent->dir_type)) == 0)
  347. break;
  348. } else if (type == X509_LU_CRL) {
  349. if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
  350. break;
  351. }
  352. /* else case will caught higher up */
  353. k++;
  354. }
  355. /*
  356. * we have added it to the cache so now pull it out again
  357. */
  358. CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock);
  359. tmp = NULL;
  360. if (sk_X509_OBJECT_find(xl->store_ctx->objs, &idx, &stmp)) {
  361. tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, idx);
  362. }
  363. CRYPTO_MUTEX_unlock_write(&xl->store_ctx->objs_lock);
  364. /*
  365. * If a CRL, update the last file suffix added for this
  366. */
  367. if (type == X509_LU_CRL) {
  368. CRYPTO_STATIC_MUTEX_lock_write(&g_ent_hashes_lock);
  369. /*
  370. * Look for entry again in case another thread added an entry
  371. * first.
  372. */
  373. if (!hent) {
  374. htmp.hash = h;
  375. if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp))
  376. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  377. }
  378. if (!hent) {
  379. hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
  380. if (hent == NULL) {
  381. CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
  382. ok = 0;
  383. goto finish;
  384. }
  385. hent->hash = h;
  386. hent->suffix = k;
  387. if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
  388. CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
  389. OPENSSL_free(hent);
  390. ok = 0;
  391. goto finish;
  392. }
  393. } else if (hent->suffix < k)
  394. hent->suffix = k;
  395. CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
  396. }
  397. if (tmp != NULL) {
  398. ok = 1;
  399. ret->type = tmp->type;
  400. OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  401. /*
  402. * If we were going to up the reference count, we would need
  403. * to do it on a perl 'type' basis
  404. */
  405. /*
  406. * CRYPTO_add(&tmp->data.x509->references,1,
  407. * CRYPTO_LOCK_X509);
  408. */
  409. goto finish;
  410. }
  411. }
  412. }
  413. finish:
  414. if (b != NULL)
  415. BUF_MEM_free(b);
  416. return (ok);
  417. }