2
0

by_dir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #ifdef MAC_OS_pre_X
  66. # include <stat.h>
  67. #else
  68. # include <sys/stat.h>
  69. #endif
  70. #include <openssl/lhash.h>
  71. #include <openssl/x509.h>
  72. #ifdef _WIN32
  73. # define stat _stat
  74. #endif
  75. typedef struct lookup_dir_st {
  76. BUF_MEM *buffer;
  77. int num_dirs;
  78. char **dirs;
  79. int *dirs_type;
  80. int num_dirs_alloced;
  81. } BY_DIR;
  82. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  83. char **ret);
  84. static int new_dir(X509_LOOKUP *lu);
  85. static void free_dir(X509_LOOKUP *lu);
  86. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
  87. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  88. X509_OBJECT *ret);
  89. X509_LOOKUP_METHOD x509_dir_lookup = {
  90. "Load certs from files in a directory",
  91. new_dir, /* new */
  92. free_dir, /* free */
  93. NULL, /* init */
  94. NULL, /* shutdown */
  95. dir_ctrl, /* ctrl */
  96. get_cert_by_subject, /* get_by_subject */
  97. NULL, /* get_by_issuer_serial */
  98. NULL, /* get_by_fingerprint */
  99. NULL, /* get_by_alias */
  100. };
  101. X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
  102. {
  103. return (&x509_dir_lookup);
  104. }
  105. static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
  106. char **retp)
  107. {
  108. int ret = 0;
  109. BY_DIR *ld;
  110. char *dir = NULL;
  111. ld = (BY_DIR *)ctx->method_data;
  112. switch (cmd) {
  113. case X509_L_ADD_DIR:
  114. if (argl == X509_FILETYPE_DEFAULT) {
  115. dir = (char *)Getenv(X509_get_default_cert_dir_env());
  116. if (dir)
  117. ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
  118. else
  119. ret = add_cert_dir(ld, X509_get_default_cert_dir(),
  120. X509_FILETYPE_PEM);
  121. if (!ret) {
  122. X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
  123. }
  124. } else
  125. ret = add_cert_dir(ld, argp, (int)argl);
  126. break;
  127. }
  128. return (ret);
  129. }
  130. static int new_dir(X509_LOOKUP *lu)
  131. {
  132. BY_DIR *a;
  133. if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
  134. return (0);
  135. if ((a->buffer = BUF_MEM_new()) == NULL) {
  136. OPENSSL_free(a);
  137. return (0);
  138. }
  139. a->num_dirs = 0;
  140. a->dirs = NULL;
  141. a->dirs_type = NULL;
  142. a->num_dirs_alloced = 0;
  143. lu->method_data = (char *)a;
  144. return (1);
  145. }
  146. static void free_dir(X509_LOOKUP *lu)
  147. {
  148. BY_DIR *a;
  149. int i;
  150. a = (BY_DIR *)lu->method_data;
  151. for (i = 0; i < a->num_dirs; i++)
  152. if (a->dirs[i] != NULL)
  153. OPENSSL_free(a->dirs[i]);
  154. if (a->dirs != NULL)
  155. OPENSSL_free(a->dirs);
  156. if (a->dirs_type != NULL)
  157. OPENSSL_free(a->dirs_type);
  158. if (a->buffer != NULL)
  159. BUF_MEM_free(a->buffer);
  160. OPENSSL_free(a);
  161. }
  162. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
  163. {
  164. int j, len;
  165. int *ip;
  166. const char *s, *ss, *p;
  167. char **pp;
  168. if (dir == NULL || !*dir) {
  169. X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
  170. return 0;
  171. }
  172. s = dir;
  173. p = s;
  174. for (;; p++) {
  175. if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
  176. ss = s;
  177. s = p + 1;
  178. len = (int)(p - ss);
  179. if (len == 0)
  180. continue;
  181. for (j = 0; j < ctx->num_dirs; j++)
  182. if (strlen(ctx->dirs[j]) == (size_t)len &&
  183. strncmp(ctx->dirs[j], ss, (unsigned int)len) == 0)
  184. break;
  185. if (j < ctx->num_dirs)
  186. continue;
  187. if (ctx->num_dirs_alloced < (ctx->num_dirs + 1)) {
  188. ctx->num_dirs_alloced += 10;
  189. pp = (char **)OPENSSL_malloc(ctx->num_dirs_alloced *
  190. sizeof(char *));
  191. ip = (int *)OPENSSL_malloc(ctx->num_dirs_alloced *
  192. sizeof(int));
  193. if ((pp == NULL) || (ip == NULL)) {
  194. X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
  195. return (0);
  196. }
  197. memcpy(pp, ctx->dirs, (ctx->num_dirs_alloced - 10) *
  198. sizeof(char *));
  199. memcpy(ip, ctx->dirs_type, (ctx->num_dirs_alloced - 10) *
  200. sizeof(int));
  201. if (ctx->dirs != NULL)
  202. OPENSSL_free(ctx->dirs);
  203. if (ctx->dirs_type != NULL)
  204. OPENSSL_free(ctx->dirs_type);
  205. ctx->dirs = pp;
  206. ctx->dirs_type = ip;
  207. }
  208. ctx->dirs_type[ctx->num_dirs] = type;
  209. ctx->dirs[ctx->num_dirs] =
  210. (char *)OPENSSL_malloc((unsigned int)len + 1);
  211. if (ctx->dirs[ctx->num_dirs] == NULL)
  212. return (0);
  213. strncpy(ctx->dirs[ctx->num_dirs], ss, (unsigned int)len);
  214. ctx->dirs[ctx->num_dirs][len] = '\0';
  215. ctx->num_dirs++;
  216. }
  217. if (*p == '\0')
  218. break;
  219. }
  220. return (1);
  221. }
  222. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  223. X509_OBJECT *ret)
  224. {
  225. BY_DIR *ctx;
  226. union {
  227. struct {
  228. X509 st_x509;
  229. X509_CINF st_x509_cinf;
  230. } x509;
  231. struct {
  232. X509_CRL st_crl;
  233. X509_CRL_INFO st_crl_info;
  234. } crl;
  235. } data;
  236. int ok = 0;
  237. int i, j, k;
  238. unsigned long h;
  239. BUF_MEM *b = NULL;
  240. struct stat st;
  241. X509_OBJECT stmp, *tmp;
  242. const char *postfix = "";
  243. if (name == NULL)
  244. return (0);
  245. stmp.type = type;
  246. if (type == X509_LU_X509) {
  247. data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
  248. data.x509.st_x509_cinf.subject = name;
  249. stmp.data.x509 = &data.x509.st_x509;
  250. postfix = "";
  251. } else if (type == X509_LU_CRL) {
  252. data.crl.st_crl.crl = &data.crl.st_crl_info;
  253. data.crl.st_crl_info.issuer = name;
  254. stmp.data.crl = &data.crl.st_crl;
  255. postfix = "r";
  256. } else {
  257. X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE);
  258. goto finish;
  259. }
  260. if ((b = BUF_MEM_new()) == NULL) {
  261. X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB);
  262. goto finish;
  263. }
  264. ctx = (BY_DIR *)xl->method_data;
  265. h = X509_NAME_hash(name);
  266. for (i = 0; i < ctx->num_dirs; i++) {
  267. j = strlen(ctx->dirs[i]) + 1 + 8 + 6 + 1 + 1;
  268. if (!BUF_MEM_grow(b, j)) {
  269. X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
  270. goto finish;
  271. }
  272. k = 0;
  273. for (;;) {
  274. char c = '/';
  275. #ifdef OPENSSL_SYS_VMS
  276. c = ctx->dirs[i][strlen(ctx->dirs[i]) - 1];
  277. if (c != ':' && c != '>' && c != ']') {
  278. /*
  279. * If no separator is present, we assume the directory
  280. * specifier is a logical name, and add a colon. We really
  281. * should use better VMS routines for merging things like
  282. * this, but this will do for now... -- Richard Levitte
  283. */
  284. c = ':';
  285. } else {
  286. c = '\0';
  287. }
  288. #endif
  289. if (c == '\0') {
  290. /*
  291. * This is special. When c == '\0', no directory separator
  292. * should be added.
  293. */
  294. BIO_snprintf(b->data, b->max,
  295. "%s%08lx.%s%d", ctx->dirs[i], h, postfix, k);
  296. } else {
  297. BIO_snprintf(b->data, b->max,
  298. "%s%c%08lx.%s%d", ctx->dirs[i], c, h,
  299. postfix, k);
  300. }
  301. k++;
  302. if (stat(b->data, &st) < 0)
  303. break;
  304. /* found one. */
  305. if (type == X509_LU_X509) {
  306. if ((X509_load_cert_file(xl, b->data,
  307. ctx->dirs_type[i])) == 0)
  308. break;
  309. } else if (type == X509_LU_CRL) {
  310. if ((X509_load_crl_file(xl, b->data, ctx->dirs_type[i])) == 0)
  311. break;
  312. }
  313. /* else case will caught higher up */
  314. }
  315. /*
  316. * we have added it to the cache so now pull it out again
  317. */
  318. CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
  319. j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
  320. if (j != -1)
  321. tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
  322. else
  323. tmp = NULL;
  324. CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
  325. if (tmp != NULL) {
  326. ok = 1;
  327. ret->type = tmp->type;
  328. memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  329. /*
  330. * If we were going to up the reference count, we would need to
  331. * do it on a perl 'type' basis
  332. */
  333. /*- CRYPTO_add(&tmp->data.x509->references,1,
  334. CRYPTO_LOCK_X509);*/
  335. goto finish;
  336. }
  337. }
  338. finish:
  339. if (b != NULL)
  340. BUF_MEM_free(b);
  341. return (ok);
  342. }