by_dir.c 12 KB

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