Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

459 рядки
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/mem.h>
  63. #include <openssl/thread.h>
  64. #include <openssl/x509.h>
  65. #if !defined(OPENSSL_TRUSTY)
  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. DEFINE_STACK_OF(BY_DIR_HASH)
  81. DEFINE_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. BUF_strlcpy(ent->dir, ss, len + 1);
  217. if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
  218. by_dir_entry_free(ent);
  219. return 0;
  220. }
  221. }
  222. } while (*p++ != '\0');
  223. return 1;
  224. }
  225. /*
  226. * g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY|
  227. * objects.
  228. */
  229. static struct CRYPTO_STATIC_MUTEX g_ent_hashes_lock =
  230. CRYPTO_STATIC_MUTEX_INIT;
  231. static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
  232. X509_OBJECT *ret)
  233. {
  234. BY_DIR *ctx;
  235. union {
  236. struct {
  237. X509 st_x509;
  238. X509_CINF st_x509_cinf;
  239. } x509;
  240. struct {
  241. X509_CRL st_crl;
  242. X509_CRL_INFO st_crl_info;
  243. } crl;
  244. } data;
  245. int ok = 0;
  246. size_t i;
  247. int j, k;
  248. unsigned long h;
  249. unsigned long hash_array[2];
  250. int hash_index;
  251. BUF_MEM *b = NULL;
  252. X509_OBJECT stmp, *tmp;
  253. const char *postfix = "";
  254. if (name == NULL)
  255. return (0);
  256. stmp.type = type;
  257. if (type == X509_LU_X509) {
  258. data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
  259. data.x509.st_x509_cinf.subject = name;
  260. stmp.data.x509 = &data.x509.st_x509;
  261. postfix = "";
  262. } else if (type == X509_LU_CRL) {
  263. data.crl.st_crl.crl = &data.crl.st_crl_info;
  264. data.crl.st_crl_info.issuer = name;
  265. stmp.data.crl = &data.crl.st_crl;
  266. postfix = "r";
  267. } else {
  268. OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE);
  269. goto finish;
  270. }
  271. if ((b = BUF_MEM_new()) == NULL) {
  272. OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
  273. goto finish;
  274. }
  275. ctx = (BY_DIR *)xl->method_data;
  276. hash_array[0] = X509_NAME_hash(name);
  277. hash_array[1] = X509_NAME_hash_old(name);
  278. for (hash_index = 0; hash_index < 2; ++hash_index) {
  279. h = hash_array[hash_index];
  280. for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
  281. BY_DIR_ENTRY *ent;
  282. size_t idx;
  283. BY_DIR_HASH htmp, *hent;
  284. ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
  285. j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
  286. if (!BUF_MEM_grow(b, j)) {
  287. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  288. goto finish;
  289. }
  290. if (type == X509_LU_CRL && ent->hashes) {
  291. htmp.hash = h;
  292. CRYPTO_STATIC_MUTEX_lock_read(&g_ent_hashes_lock);
  293. if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) {
  294. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  295. k = hent->suffix;
  296. } else {
  297. hent = NULL;
  298. k = 0;
  299. }
  300. CRYPTO_STATIC_MUTEX_unlock_read(&g_ent_hashes_lock);
  301. } else {
  302. k = 0;
  303. hent = NULL;
  304. }
  305. for (;;) {
  306. char c = '/';
  307. #ifdef OPENSSL_SYS_VMS
  308. c = ent->dir[strlen(ent->dir) - 1];
  309. if (c != ':' && c != '>' && c != ']') {
  310. /*
  311. * If no separator is present, we assume the directory
  312. * specifier is a logical name, and add a colon. We
  313. * really should use better VMS routines for merging
  314. * things like this, but this will do for now... --
  315. * Richard Levitte
  316. */
  317. c = ':';
  318. } else {
  319. c = '\0';
  320. }
  321. #endif
  322. if (c == '\0') {
  323. /*
  324. * This is special. When c == '\0', no directory
  325. * separator should be added.
  326. */
  327. BIO_snprintf(b->data, b->max,
  328. "%s%08lx.%s%d", ent->dir, h, postfix, k);
  329. } else {
  330. BIO_snprintf(b->data, b->max,
  331. "%s%c%08lx.%s%d", ent->dir, c, h,
  332. postfix, k);
  333. }
  334. #ifndef OPENSSL_NO_POSIX_IO
  335. # if defined(_WIN32) && !defined(stat)
  336. # define stat _stat
  337. # endif
  338. {
  339. struct stat st;
  340. if (stat(b->data, &st) < 0)
  341. break;
  342. }
  343. #endif
  344. /* found one. */
  345. if (type == X509_LU_X509) {
  346. if ((X509_load_cert_file(xl, b->data,
  347. ent->dir_type)) == 0)
  348. break;
  349. } else if (type == X509_LU_CRL) {
  350. if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
  351. break;
  352. }
  353. /* else case will caught higher up */
  354. k++;
  355. }
  356. /*
  357. * we have added it to the cache so now pull it out again
  358. */
  359. CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock);
  360. tmp = NULL;
  361. sk_X509_OBJECT_sort(xl->store_ctx->objs);
  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_write(&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. sk_BY_DIR_HASH_sort(ent->hashes);
  378. if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp))
  379. hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
  380. }
  381. if (!hent) {
  382. hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
  383. if (hent == NULL) {
  384. CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
  385. ok = 0;
  386. goto finish;
  387. }
  388. hent->hash = h;
  389. hent->suffix = k;
  390. if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
  391. CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
  392. OPENSSL_free(hent);
  393. ok = 0;
  394. goto finish;
  395. }
  396. sk_BY_DIR_HASH_sort(ent->hashes);
  397. } else if (hent->suffix < k)
  398. hent->suffix = k;
  399. CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
  400. }
  401. if (tmp != NULL) {
  402. ok = 1;
  403. ret->type = tmp->type;
  404. OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data));
  405. /*
  406. * If we were going to up the reference count, we would need
  407. * to do it on a perl 'type' basis
  408. */
  409. /*
  410. * CRYPTO_add(&tmp->data.x509->references,1,
  411. * CRYPTO_LOCK_X509);
  412. */
  413. goto finish;
  414. }
  415. }
  416. }
  417. finish:
  418. if (b != NULL)
  419. BUF_MEM_free(b);
  420. return (ok);
  421. }
  422. #endif // OPENSSL_TRUSTY