by_dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. */
  58. #include <stdio.h>
  59. #include <time.h>
  60. #include <errno.h>
  61. #include "cryptlib.h"
  62. #ifndef NO_SYS_TYPES_H
  63. # include <sys/types.h>
  64. #endif
  65. #ifndef OPENSSL_NO_POSIX_IO
  66. # include <sys/stat.h>
  67. #endif
  68. #include <openssl/lhash.h>
  69. #include <openssl/x509.h>
  70. typedef struct lookup_dir_hashes_st {
  71. unsigned long hash;
  72. int suffix;
  73. } BY_DIR_HASH;
  74. typedef struct lookup_dir_entry_st {
  75. char *dir;
  76. int dir_type;
  77. STACK_OF(BY_DIR_HASH) *hashes;
  78. } BY_DIR_ENTRY;
  79. typedef struct lookup_dir_st {
  80. BUF_MEM *buffer;
  81. STACK_OF(BY_DIR_ENTRY) *dirs;
  82. } BY_DIR;
  83. DECLARE_STACK_OF(BY_DIR_HASH)
  84. DECLARE_STACK_OF(BY_DIR_ENTRY)
  85. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  86. char **ret);
  87. static int new_dir(X509_LOOKUP *lu);
  88. static void free_dir(X509_LOOKUP *lu);
  89. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
  90. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  91. X509_OBJECT *ret);
  92. X509_LOOKUP_METHOD x509_dir_lookup = {
  93. "Load certs from files in a directory",
  94. new_dir, /* new */
  95. free_dir, /* free */
  96. NULL, /* init */
  97. NULL, /* shutdown */
  98. dir_ctrl, /* ctrl */
  99. get_cert_by_subject, /* get_by_subject */
  100. NULL, /* get_by_issuer_serial */
  101. NULL, /* get_by_fingerprint */
  102. NULL, /* get_by_alias */
  103. };
  104. X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
  105. {
  106. return (&x509_dir_lookup);
  107. }
  108. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  109. char **retp)
  110. {
  111. int ret = 0;
  112. BY_DIR *ld;
  113. char *dir = NULL;
  114. ld = (BY_DIR *)ctx->method_data;
  115. switch (cmd) {
  116. case X509_L_ADD_DIR:
  117. if (argl == X509_FILETYPE_DEFAULT) {
  118. dir = (char *)getenv(X509_get_default_cert_dir_env());
  119. if (dir)
  120. ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
  121. else
  122. ret = add_cert_dir(ld, X509_get_default_cert_dir(),
  123. X509_FILETYPE_PEM);
  124. if (!ret) {
  125. X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
  126. }
  127. } else
  128. ret = add_cert_dir(ld, argp, (int)argl);
  129. break;
  130. }
  131. return (ret);
  132. }
  133. static int new_dir(X509_LOOKUP *lu)
  134. {
  135. BY_DIR *a;
  136. if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
  137. return (0);
  138. if ((a->buffer = BUF_MEM_new()) == NULL) {
  139. OPENSSL_free(a);
  140. return (0);
  141. }
  142. a->dirs = NULL;
  143. lu->method_data = (char *)a;
  144. return (1);
  145. }
  146. static void by_dir_hash_free(BY_DIR_HASH *hash)
  147. {
  148. OPENSSL_free(hash);
  149. }
  150. static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
  151. const BY_DIR_HASH *const *b)
  152. {
  153. if ((*a)->hash > (*b)->hash)
  154. return 1;
  155. if ((*a)->hash < (*b)->hash)
  156. return -1;
  157. return 0;
  158. }
  159. static void by_dir_entry_free(BY_DIR_ENTRY *ent)
  160. {
  161. if (ent->dir)
  162. OPENSSL_free(ent->dir);
  163. if (ent->hashes)
  164. sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
  165. OPENSSL_free(ent);
  166. }
  167. static void free_dir(X509_LOOKUP *lu)
  168. {
  169. BY_DIR *a;
  170. a = (BY_DIR *)lu->method_data;
  171. if (a->dirs != NULL)
  172. sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
  173. if (a->buffer != NULL)
  174. BUF_MEM_free(a->buffer);
  175. OPENSSL_free(a);
  176. }
  177. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
  178. {
  179. int j, len;
  180. const char *s, *ss, *p;
  181. if (dir == NULL || !*dir) {
  182. X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
  183. return 0;
  184. }
  185. s = dir;
  186. p = s;
  187. do {
  188. if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
  189. BY_DIR_ENTRY *ent;
  190. ss = s;
  191. s = p + 1;
  192. len = (int)(p - ss);
  193. if (len == 0)
  194. continue;
  195. for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
  196. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
  197. if (strlen(ent->dir) == (size_t)len &&
  198. strncmp(ent->dir, ss, (unsigned int)len) == 0)
  199. break;
  200. }
  201. if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
  202. continue;
  203. if (ctx->dirs == NULL) {
  204. ctx->dirs = sk_BY_DIR_ENTRY_new_null();
  205. if (!ctx->dirs) {
  206. X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
  207. return 0;
  208. }
  209. }
  210. ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY));
  211. if (!ent)
  212. return 0;
  213. ent->dir_type = type;
  214. ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
  215. ent->dir = OPENSSL_malloc((unsigned int)len + 1);
  216. if (!ent->dir || !ent->hashes) {
  217. by_dir_entry_free(ent);
  218. return 0;
  219. }
  220. strncpy(ent->dir, ss, (unsigned int)len);
  221. ent->dir[len] = '\0';
  222. if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
  223. by_dir_entry_free(ent);
  224. return 0;
  225. }
  226. }
  227. } while (*p++ != '\0');
  228. return 1;
  229. }
  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. int i, j, k;
  246. unsigned long h;
  247. BUF_MEM *b = NULL;
  248. X509_OBJECT stmp, *tmp;
  249. const char *postfix = "";
  250. if (name == NULL)
  251. return (0);
  252. stmp.type = type;
  253. if (type == X509_LU_X509) {
  254. data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
  255. data.x509.st_x509_cinf.subject = name;
  256. stmp.data.x509 = &data.x509.st_x509;
  257. postfix = "";
  258. } else if (type == X509_LU_CRL) {
  259. data.crl.st_crl.crl = &data.crl.st_crl_info;
  260. data.crl.st_crl_info.issuer = name;
  261. stmp.data.crl = &data.crl.st_crl;
  262. postfix = "r";
  263. } else {
  264. X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE);
  265. goto finish;
  266. }
  267. if ((b = BUF_MEM_new()) == NULL) {
  268. X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB);
  269. goto finish;
  270. }
  271. ctx = (BY_DIR *)xl->method_data;
  272. h = X509_NAME_hash(name);
  273. for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
  274. BY_DIR_ENTRY *ent;
  275. int idx;
  276. BY_DIR_HASH htmp, *hent;
  277. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
  278. j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
  279. if (!BUF_MEM_grow(b, j)) {
  280. X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
  281. goto finish;
  282. }
  283. if (type == X509_LU_CRL && ent->hashes) {
  284. htmp.hash = h;
  285. CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE);
  286. idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
  287. if (idx >= 0) {
  288. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  289. k = hent->suffix;
  290. } else {
  291. hent = NULL;
  292. k = 0;
  293. }
  294. CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE);
  295. } else {
  296. k = 0;
  297. hent = NULL;
  298. }
  299. for (;;) {
  300. char c = '/';
  301. #ifdef OPENSSL_SYS_VMS
  302. c = ent->dir[strlen(ent->dir) - 1];
  303. if (c != ':' && c != '>' && c != ']') {
  304. /*
  305. * If no separator is present, we assume the directory
  306. * specifier is a logical name, and add a colon. We really
  307. * should use better VMS routines for merging things like
  308. * this, but this will do for now... -- Richard Levitte
  309. */
  310. c = ':';
  311. } else {
  312. c = '\0';
  313. }
  314. #endif
  315. if (c == '\0') {
  316. /*
  317. * This is special. When c == '\0', no directory separator
  318. * should be added.
  319. */
  320. BIO_snprintf(b->data, b->max,
  321. "%s%08lx.%s%d", ent->dir, h, postfix, k);
  322. } else {
  323. BIO_snprintf(b->data, b->max,
  324. "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
  325. }
  326. #ifndef OPENSSL_NO_POSIX_IO
  327. # ifdef _WIN32
  328. # define stat _stat
  329. # endif
  330. {
  331. struct stat st;
  332. if (stat(b->data, &st) < 0)
  333. break;
  334. }
  335. #endif
  336. /* found one. */
  337. if (type == X509_LU_X509) {
  338. if ((X509_load_cert_file(xl, b->data, ent->dir_type)) == 0)
  339. break;
  340. } else if (type == X509_LU_CRL) {
  341. if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
  342. break;
  343. }
  344. /* else case will caught higher up */
  345. k++;
  346. }
  347. /*
  348. * we have added it to the cache so now pull it out again
  349. */
  350. CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
  351. j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
  352. if (j != -1)
  353. tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
  354. else
  355. tmp = NULL;
  356. CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
  357. /* If a CRL, update the last file suffix added for this */
  358. if (type == X509_LU_CRL) {
  359. CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
  360. /*
  361. * Look for entry again in case another thread added an entry
  362. * first.
  363. */
  364. if (!hent) {
  365. htmp.hash = h;
  366. idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
  367. if (idx >= 0)
  368. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  369. }
  370. if (!hent) {
  371. hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
  372. if (hent == NULL) {
  373. X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
  374. goto finish;
  375. }
  376. hent->hash = h;
  377. hent->suffix = k;
  378. if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
  379. CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
  380. OPENSSL_free(hent);
  381. ok = 0;
  382. goto finish;
  383. }
  384. } else if (hent->suffix < k)
  385. hent->suffix = k;
  386. CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
  387. }
  388. if (tmp != NULL) {
  389. ok = 1;
  390. ret->type = tmp->type;
  391. memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  392. /*
  393. * If we were going to up the reference count, we would need to
  394. * do it on a perl 'type' basis
  395. */
  396. /*- CRYPTO_add(&tmp->data.x509->references,1,
  397. CRYPTO_LOCK_X509);*/
  398. goto finish;
  399. }
  400. }
  401. finish:
  402. if (b != NULL)
  403. BUF_MEM_free(b);
  404. return (ok);
  405. }