您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

454 行
15 KiB

  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/lhash.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/thread.h>
  65. #include <openssl/x509.h>
  66. #include "../internal.h"
  67. typedef struct lookup_dir_hashes_st {
  68. unsigned long hash;
  69. int suffix;
  70. } BY_DIR_HASH;
  71. typedef struct lookup_dir_entry_st {
  72. char *dir;
  73. int dir_type;
  74. STACK_OF(BY_DIR_HASH) *hashes;
  75. } BY_DIR_ENTRY;
  76. typedef struct lookup_dir_st {
  77. BUF_MEM *buffer;
  78. STACK_OF(BY_DIR_ENTRY) *dirs;
  79. } BY_DIR;
  80. DECLARE_STACK_OF(BY_DIR_HASH)
  81. DECLARE_STACK_OF(BY_DIR_ENTRY)
  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. static 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. OPENSSL_PUT_ERROR(X509, 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->dirs = NULL;
  140. lu->method_data = (char *)a;
  141. return (1);
  142. }
  143. static void by_dir_hash_free(BY_DIR_HASH *hash)
  144. {
  145. OPENSSL_free(hash);
  146. }
  147. static int by_dir_hash_cmp(const BY_DIR_HASH **a, const BY_DIR_HASH **b)
  148. {
  149. if ((*a)->hash > (*b)->hash)
  150. return 1;
  151. if ((*a)->hash < (*b)->hash)
  152. return -1;
  153. return 0;
  154. }
  155. static void by_dir_entry_free(BY_DIR_ENTRY *ent)
  156. {
  157. if (ent->dir)
  158. OPENSSL_free(ent->dir);
  159. if (ent->hashes)
  160. sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
  161. OPENSSL_free(ent);
  162. }
  163. static void free_dir(X509_LOOKUP *lu)
  164. {
  165. BY_DIR *a;
  166. a = (BY_DIR *)lu->method_data;
  167. if (a->dirs != NULL)
  168. sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
  169. if (a->buffer != NULL)
  170. BUF_MEM_free(a->buffer);
  171. OPENSSL_free(a);
  172. }
  173. static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
  174. {
  175. size_t j, len;
  176. const char *s, *ss, *p;
  177. if (dir == NULL || !*dir) {
  178. OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY);
  179. return 0;
  180. }
  181. s = dir;
  182. p = s;
  183. do {
  184. if ((*p == ':') || (*p == '\0')) {
  185. BY_DIR_ENTRY *ent;
  186. ss = s;
  187. s = p + 1;
  188. len = p - ss;
  189. if (len == 0)
  190. continue;
  191. for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
  192. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
  193. if (strlen(ent->dir) == len &&
  194. strncmp(ent->dir, ss, len) == 0)
  195. break;
  196. }
  197. if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
  198. continue;
  199. if (ctx->dirs == NULL) {
  200. ctx->dirs = sk_BY_DIR_ENTRY_new_null();
  201. if (!ctx->dirs) {
  202. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  203. return 0;
  204. }
  205. }
  206. ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY));
  207. if (!ent)
  208. return 0;
  209. ent->dir_type = type;
  210. ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
  211. ent->dir = OPENSSL_malloc(len + 1);
  212. if (!ent->dir || !ent->hashes) {
  213. by_dir_entry_free(ent);
  214. return 0;
  215. }
  216. strncpy(ent->dir, ss, len);
  217. ent->dir[len] = '\0';
  218. if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
  219. by_dir_entry_free(ent);
  220. return 0;
  221. }
  222. }
  223. } while (*p++ != '\0');
  224. return 1;
  225. }
  226. /*
  227. * g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY|
  228. * objects.
  229. */
  230. static struct CRYPTO_STATIC_MUTEX g_ent_hashes_lock =
  231. CRYPTO_STATIC_MUTEX_INIT;
  232. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  233. X509_OBJECT *ret)
  234. {
  235. BY_DIR *ctx;
  236. union {
  237. struct {
  238. X509 st_x509;
  239. X509_CINF st_x509_cinf;
  240. } x509;
  241. struct {
  242. X509_CRL st_crl;
  243. X509_CRL_INFO st_crl_info;
  244. } crl;
  245. } data;
  246. int ok = 0;
  247. size_t i;
  248. int j, k;
  249. unsigned long h;
  250. unsigned long hash_array[2];
  251. int hash_index;
  252. BUF_MEM *b = NULL;
  253. X509_OBJECT stmp, *tmp;
  254. const char *postfix = "";
  255. if (name == NULL)
  256. return (0);
  257. stmp.type = type;
  258. if (type == X509_LU_X509) {
  259. data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
  260. data.x509.st_x509_cinf.subject = name;
  261. stmp.data.x509 = &data.x509.st_x509;
  262. postfix = "";
  263. } else if (type == X509_LU_CRL) {
  264. data.crl.st_crl.crl = &data.crl.st_crl_info;
  265. data.crl.st_crl_info.issuer = name;
  266. stmp.data.crl = &data.crl.st_crl;
  267. postfix = "r";
  268. } else {
  269. OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE);
  270. goto finish;
  271. }
  272. if ((b = BUF_MEM_new()) == NULL) {
  273. OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
  274. goto finish;
  275. }
  276. ctx = (BY_DIR *)xl->method_data;
  277. hash_array[0] = X509_NAME_hash(name);
  278. hash_array[1] = X509_NAME_hash_old(name);
  279. for (hash_index = 0; hash_index < 2; ++hash_index) {
  280. h = hash_array[hash_index];
  281. for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
  282. BY_DIR_ENTRY *ent;
  283. size_t idx;
  284. BY_DIR_HASH htmp, *hent;
  285. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
  286. j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
  287. if (!BUF_MEM_grow(b, j)) {
  288. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  289. goto finish;
  290. }
  291. if (type == X509_LU_CRL && ent->hashes) {
  292. htmp.hash = h;
  293. CRYPTO_STATIC_MUTEX_lock_read(&g_ent_hashes_lock);
  294. if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) {
  295. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  296. k = hent->suffix;
  297. } else {
  298. hent = NULL;
  299. k = 0;
  300. }
  301. CRYPTO_STATIC_MUTEX_unlock(&g_ent_hashes_lock);
  302. } else {
  303. k = 0;
  304. hent = NULL;
  305. }
  306. for (;;) {
  307. char c = '/';
  308. #ifdef OPENSSL_SYS_VMS
  309. c = ent->dir[strlen(ent->dir) - 1];
  310. if (c != ':' && c != '>' && c != ']') {
  311. /*
  312. * If no separator is present, we assume the directory
  313. * specifier is a logical name, and add a colon. We
  314. * really should use better VMS routines for merging
  315. * things like this, but this will do for now... --
  316. * Richard Levitte
  317. */
  318. c = ':';
  319. } else {
  320. c = '\0';
  321. }
  322. #endif
  323. if (c == '\0') {
  324. /*
  325. * This is special. When c == '\0', no directory
  326. * separator should be added.
  327. */
  328. BIO_snprintf(b->data, b->max,
  329. "%s%08lx.%s%d", ent->dir, h, postfix, k);
  330. } else {
  331. BIO_snprintf(b->data, b->max,
  332. "%s%c%08lx.%s%d", ent->dir, c, h,
  333. postfix, k);
  334. }
  335. #ifndef OPENSSL_NO_POSIX_IO
  336. # ifdef _WIN32
  337. # define stat _stat
  338. # endif
  339. {
  340. struct stat st;
  341. if (stat(b->data, &st) < 0)
  342. break;
  343. }
  344. #endif
  345. /* found one. */
  346. if (type == X509_LU_X509) {
  347. if ((X509_load_cert_file(xl, b->data,
  348. ent->dir_type)) == 0)
  349. break;
  350. } else if (type == X509_LU_CRL) {
  351. if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
  352. break;
  353. }
  354. /* else case will caught higher up */
  355. k++;
  356. }
  357. /*
  358. * we have added it to the cache so now pull it out again
  359. */
  360. CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock);
  361. tmp = NULL;
  362. if (sk_X509_OBJECT_find(xl->store_ctx->objs, &idx, &stmp)) {
  363. tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, idx);
  364. }
  365. CRYPTO_MUTEX_unlock(&xl->store_ctx->objs_lock);
  366. /*
  367. * If a CRL, update the last file suffix added for this
  368. */
  369. if (type == X509_LU_CRL) {
  370. CRYPTO_STATIC_MUTEX_lock_write(&g_ent_hashes_lock);
  371. /*
  372. * Look for entry again in case another thread added an entry
  373. * first.
  374. */
  375. if (!hent) {
  376. htmp.hash = h;
  377. if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp))
  378. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  379. }
  380. if (!hent) {
  381. hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
  382. if (hent == NULL) {
  383. CRYPTO_STATIC_MUTEX_unlock(&g_ent_hashes_lock);
  384. ok = 0;
  385. goto finish;
  386. }
  387. hent->hash = h;
  388. hent->suffix = k;
  389. if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
  390. CRYPTO_STATIC_MUTEX_unlock(&g_ent_hashes_lock);
  391. OPENSSL_free(hent);
  392. ok = 0;
  393. goto finish;
  394. }
  395. } else if (hent->suffix < k)
  396. hent->suffix = k;
  397. CRYPTO_STATIC_MUTEX_unlock(&g_ent_hashes_lock);
  398. }
  399. if (tmp != NULL) {
  400. ok = 1;
  401. ret->type = tmp->type;
  402. memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  403. /*
  404. * If we were going to up the reference count, we would need
  405. * to do it on a perl 'type' basis
  406. */
  407. /*
  408. * CRYPTO_add(&tmp->data.x509->references,1,
  409. * CRYPTO_LOCK_X509);
  410. */
  411. goto finish;
  412. }
  413. }
  414. }
  415. finish:
  416. if (b != NULL)
  417. BUF_MEM_free(b);
  418. return (ok);
  419. }