by_dir.c 10 KB

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