x509_lu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/refcount.h"
  12. #include <openssl/x509.h>
  13. #include "internal/x509_int.h"
  14. #include <openssl/x509v3.h>
  15. #include "x509_lcl.h"
  16. X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method)
  17. {
  18. X509_LOOKUP *ret;
  19. ret = OPENSSL_zalloc(sizeof(*ret));
  20. if (ret == NULL)
  21. return NULL;
  22. ret->method = method;
  23. if ((method->new_item != NULL) && !method->new_item(ret)) {
  24. OPENSSL_free(ret);
  25. return NULL;
  26. }
  27. return ret;
  28. }
  29. void X509_LOOKUP_free(X509_LOOKUP *ctx)
  30. {
  31. if (ctx == NULL)
  32. return;
  33. if ((ctx->method != NULL) && (ctx->method->free != NULL))
  34. (*ctx->method->free) (ctx);
  35. OPENSSL_free(ctx);
  36. }
  37. int X509_STORE_lock(X509_STORE *s)
  38. {
  39. return CRYPTO_THREAD_write_lock(s->lock);
  40. }
  41. int X509_STORE_unlock(X509_STORE *s)
  42. {
  43. return CRYPTO_THREAD_unlock(s->lock);
  44. }
  45. int X509_LOOKUP_init(X509_LOOKUP *ctx)
  46. {
  47. if (ctx->method == NULL)
  48. return 0;
  49. if (ctx->method->init != NULL)
  50. return ctx->method->init(ctx);
  51. else
  52. return 1;
  53. }
  54. int X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
  55. {
  56. if (ctx->method == NULL)
  57. return 0;
  58. if (ctx->method->shutdown != NULL)
  59. return ctx->method->shutdown(ctx);
  60. else
  61. return 1;
  62. }
  63. int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
  64. char **ret)
  65. {
  66. if (ctx->method == NULL)
  67. return -1;
  68. if (ctx->method->ctrl != NULL)
  69. return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
  70. else
  71. return 1;
  72. }
  73. int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  74. X509_NAME *name, X509_OBJECT *ret)
  75. {
  76. if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
  77. return 0;
  78. if (ctx->skip)
  79. return 0;
  80. return ctx->method->get_by_subject(ctx, type, name, ret);
  81. }
  82. int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  83. X509_NAME *name, ASN1_INTEGER *serial,
  84. X509_OBJECT *ret)
  85. {
  86. if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL))
  87. return 0;
  88. return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
  89. }
  90. int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  91. const unsigned char *bytes, int len,
  92. X509_OBJECT *ret)
  93. {
  94. if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
  95. return 0;
  96. return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
  97. }
  98. int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
  99. const char *str, int len, X509_OBJECT *ret)
  100. {
  101. if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
  102. return 0;
  103. return ctx->method->get_by_alias(ctx, type, str, len, ret);
  104. }
  105. static int x509_object_cmp(const X509_OBJECT *const *a,
  106. const X509_OBJECT *const *b)
  107. {
  108. int ret;
  109. ret = ((*a)->type - (*b)->type);
  110. if (ret)
  111. return ret;
  112. switch ((*a)->type) {
  113. case X509_LU_X509:
  114. ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509);
  115. break;
  116. case X509_LU_CRL:
  117. ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl);
  118. break;
  119. case X509_LU_NONE:
  120. /* abort(); */
  121. return 0;
  122. }
  123. return ret;
  124. }
  125. X509_STORE *X509_STORE_new(void)
  126. {
  127. X509_STORE *ret;
  128. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  129. return NULL;
  130. if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL)
  131. goto err;
  132. ret->cache = 1;
  133. if ((ret->get_cert_methods = sk_X509_LOOKUP_new_null()) == NULL)
  134. goto err;
  135. if ((ret->param = X509_VERIFY_PARAM_new()) == NULL)
  136. goto err;
  137. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data))
  138. goto err;
  139. ret->lock = CRYPTO_THREAD_lock_new();
  140. if (ret->lock == NULL)
  141. goto err;
  142. ret->references = 1;
  143. return ret;
  144. err:
  145. X509_VERIFY_PARAM_free(ret->param);
  146. sk_X509_OBJECT_free(ret->objs);
  147. sk_X509_LOOKUP_free(ret->get_cert_methods);
  148. OPENSSL_free(ret);
  149. return NULL;
  150. }
  151. void X509_STORE_free(X509_STORE *vfy)
  152. {
  153. int i;
  154. STACK_OF(X509_LOOKUP) *sk;
  155. X509_LOOKUP *lu;
  156. if (vfy == NULL)
  157. return;
  158. CRYPTO_DOWN_REF(&vfy->references, &i, vfy->lock);
  159. REF_PRINT_COUNT("X509_STORE", vfy);
  160. if (i > 0)
  161. return;
  162. REF_ASSERT_ISNT(i < 0);
  163. sk = vfy->get_cert_methods;
  164. for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
  165. lu = sk_X509_LOOKUP_value(sk, i);
  166. X509_LOOKUP_shutdown(lu);
  167. X509_LOOKUP_free(lu);
  168. }
  169. sk_X509_LOOKUP_free(sk);
  170. sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free);
  171. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data);
  172. X509_VERIFY_PARAM_free(vfy->param);
  173. CRYPTO_THREAD_lock_free(vfy->lock);
  174. OPENSSL_free(vfy);
  175. }
  176. int X509_STORE_up_ref(X509_STORE *vfy)
  177. {
  178. int i;
  179. if (CRYPTO_UP_REF(&vfy->references, &i, vfy->lock) <= 0)
  180. return 0;
  181. REF_PRINT_COUNT("X509_STORE", a);
  182. REF_ASSERT_ISNT(i < 2);
  183. return ((i > 1) ? 1 : 0);
  184. }
  185. X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m)
  186. {
  187. int i;
  188. STACK_OF(X509_LOOKUP) *sk;
  189. X509_LOOKUP *lu;
  190. sk = v->get_cert_methods;
  191. for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
  192. lu = sk_X509_LOOKUP_value(sk, i);
  193. if (m == lu->method) {
  194. return lu;
  195. }
  196. }
  197. /* a new one */
  198. lu = X509_LOOKUP_new(m);
  199. if (lu == NULL)
  200. return NULL;
  201. else {
  202. lu->store_ctx = v;
  203. if (sk_X509_LOOKUP_push(v->get_cert_methods, lu))
  204. return lu;
  205. else {
  206. X509_LOOKUP_free(lu);
  207. return NULL;
  208. }
  209. }
  210. }
  211. X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *vs,
  212. X509_LOOKUP_TYPE type,
  213. X509_NAME *name)
  214. {
  215. X509_OBJECT *ret = X509_OBJECT_new();
  216. if (ret == NULL)
  217. return NULL;
  218. if (!X509_STORE_CTX_get_by_subject(vs, type, name, ret)) {
  219. X509_OBJECT_free(ret);
  220. return NULL;
  221. }
  222. return ret;
  223. }
  224. int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
  225. X509_NAME *name, X509_OBJECT *ret)
  226. {
  227. X509_STORE *ctx = vs->ctx;
  228. X509_LOOKUP *lu;
  229. X509_OBJECT stmp, *tmp;
  230. int i, j;
  231. CRYPTO_THREAD_write_lock(ctx->lock);
  232. tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
  233. CRYPTO_THREAD_unlock(ctx->lock);
  234. if (tmp == NULL || type == X509_LU_CRL) {
  235. for (i = 0; i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
  236. lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
  237. j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
  238. if (j) {
  239. tmp = &stmp;
  240. break;
  241. }
  242. }
  243. if (tmp == NULL)
  244. return 0;
  245. }
  246. ret->type = tmp->type;
  247. ret->data.ptr = tmp->data.ptr;
  248. X509_OBJECT_up_ref_count(ret);
  249. return 1;
  250. }
  251. static int x509_store_add(X509_STORE *ctx, void *x, int crl) {
  252. X509_OBJECT *obj;
  253. int ret = 0, added = 0;
  254. if (x == NULL)
  255. return 0;
  256. obj = X509_OBJECT_new();
  257. if (obj == NULL)
  258. return 0;
  259. if (crl) {
  260. obj->type = X509_LU_CRL;
  261. obj->data.crl = (X509_CRL *)x;
  262. } else {
  263. obj->type = X509_LU_X509;
  264. obj->data.x509 = (X509 *)x;
  265. }
  266. X509_OBJECT_up_ref_count(obj);
  267. CRYPTO_THREAD_write_lock(ctx->lock);
  268. if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
  269. ret = 1;
  270. } else {
  271. added = sk_X509_OBJECT_push(ctx->objs, obj);
  272. ret = added != 0;
  273. }
  274. CRYPTO_THREAD_unlock(ctx->lock);
  275. if (added == 0) /* obj not pushed */
  276. X509_OBJECT_free(obj);
  277. return ret;
  278. }
  279. int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
  280. {
  281. if (!x509_store_add(ctx, x, 0)) {
  282. X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
  288. {
  289. if (!x509_store_add(ctx, x, 1)) {
  290. X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
  291. return 0;
  292. }
  293. return 1;
  294. }
  295. int X509_OBJECT_up_ref_count(X509_OBJECT *a)
  296. {
  297. switch (a->type) {
  298. case X509_LU_NONE:
  299. break;
  300. case X509_LU_X509:
  301. return X509_up_ref(a->data.x509);
  302. case X509_LU_CRL:
  303. return X509_CRL_up_ref(a->data.crl);
  304. }
  305. return 1;
  306. }
  307. X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
  308. {
  309. if (a == NULL || a->type != X509_LU_X509)
  310. return NULL;
  311. return a->data.x509;
  312. }
  313. X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a)
  314. {
  315. if (a == NULL || a->type != X509_LU_CRL)
  316. return NULL;
  317. return a->data.crl;
  318. }
  319. X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a)
  320. {
  321. return a->type;
  322. }
  323. X509_OBJECT *X509_OBJECT_new()
  324. {
  325. X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
  326. if (ret == NULL) {
  327. X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
  328. return NULL;
  329. }
  330. ret->type = X509_LU_NONE;
  331. return ret;
  332. }
  333. void X509_OBJECT_free(X509_OBJECT *a)
  334. {
  335. if (a == NULL)
  336. return;
  337. switch (a->type) {
  338. case X509_LU_NONE:
  339. break;
  340. case X509_LU_X509:
  341. X509_free(a->data.x509);
  342. break;
  343. case X509_LU_CRL:
  344. X509_CRL_free(a->data.crl);
  345. break;
  346. }
  347. OPENSSL_free(a);
  348. }
  349. static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
  350. X509_NAME *name, int *pnmatch)
  351. {
  352. X509_OBJECT stmp;
  353. X509 x509_s;
  354. X509_CRL crl_s;
  355. int idx;
  356. stmp.type = type;
  357. switch (type) {
  358. case X509_LU_X509:
  359. stmp.data.x509 = &x509_s;
  360. x509_s.cert_info.subject = name;
  361. break;
  362. case X509_LU_CRL:
  363. stmp.data.crl = &crl_s;
  364. crl_s.crl.issuer = name;
  365. break;
  366. case X509_LU_NONE:
  367. /* abort(); */
  368. return -1;
  369. }
  370. idx = sk_X509_OBJECT_find(h, &stmp);
  371. if (idx >= 0 && pnmatch) {
  372. int tidx;
  373. const X509_OBJECT *tobj, *pstmp;
  374. *pnmatch = 1;
  375. pstmp = &stmp;
  376. for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
  377. tobj = sk_X509_OBJECT_value(h, tidx);
  378. if (x509_object_cmp(&tobj, &pstmp))
  379. break;
  380. (*pnmatch)++;
  381. }
  382. }
  383. return idx;
  384. }
  385. int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
  386. X509_NAME *name)
  387. {
  388. return x509_object_idx_cnt(h, type, name, NULL);
  389. }
  390. X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
  391. X509_LOOKUP_TYPE type,
  392. X509_NAME *name)
  393. {
  394. int idx;
  395. idx = X509_OBJECT_idx_by_subject(h, type, name);
  396. if (idx == -1)
  397. return NULL;
  398. return sk_X509_OBJECT_value(h, idx);
  399. }
  400. STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
  401. {
  402. return v->objs;
  403. }
  404. STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
  405. {
  406. int i, idx, cnt;
  407. STACK_OF(X509) *sk = NULL;
  408. X509 *x;
  409. X509_OBJECT *obj;
  410. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  411. idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
  412. if (idx < 0) {
  413. /*
  414. * Nothing found in cache: do lookup to possibly add new objects to
  415. * cache
  416. */
  417. X509_OBJECT *xobj = X509_OBJECT_new();
  418. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  419. if (xobj == NULL)
  420. return NULL;
  421. if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
  422. X509_OBJECT_free(xobj);
  423. return NULL;
  424. }
  425. X509_OBJECT_free(xobj);
  426. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  427. idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
  428. if (idx < 0) {
  429. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  430. return NULL;
  431. }
  432. }
  433. sk = sk_X509_new_null();
  434. for (i = 0; i < cnt; i++, idx++) {
  435. obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
  436. x = obj->data.x509;
  437. X509_up_ref(x);
  438. if (!sk_X509_push(sk, x)) {
  439. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  440. X509_free(x);
  441. sk_X509_pop_free(sk, X509_free);
  442. return NULL;
  443. }
  444. }
  445. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  446. return sk;
  447. }
  448. STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
  449. {
  450. int i, idx, cnt;
  451. STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
  452. X509_CRL *x;
  453. X509_OBJECT *obj, *xobj = X509_OBJECT_new();
  454. /* Always do lookup to possibly add new CRLs to cache */
  455. if (sk == NULL || xobj == NULL ||
  456. !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
  457. X509_OBJECT_free(xobj);
  458. sk_X509_CRL_free(sk);
  459. return NULL;
  460. }
  461. X509_OBJECT_free(xobj);
  462. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  463. idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
  464. if (idx < 0) {
  465. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  466. sk_X509_CRL_free(sk);
  467. return NULL;
  468. }
  469. for (i = 0; i < cnt; i++, idx++) {
  470. obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
  471. x = obj->data.crl;
  472. X509_CRL_up_ref(x);
  473. if (!sk_X509_CRL_push(sk, x)) {
  474. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  475. X509_CRL_free(x);
  476. sk_X509_CRL_pop_free(sk, X509_CRL_free);
  477. return NULL;
  478. }
  479. }
  480. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  481. return sk;
  482. }
  483. X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
  484. X509_OBJECT *x)
  485. {
  486. int idx, i;
  487. X509_OBJECT *obj;
  488. idx = sk_X509_OBJECT_find(h, x);
  489. if (idx == -1)
  490. return NULL;
  491. if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
  492. return sk_X509_OBJECT_value(h, idx);
  493. for (i = idx; i < sk_X509_OBJECT_num(h); i++) {
  494. obj = sk_X509_OBJECT_value(h, i);
  495. if (x509_object_cmp
  496. ((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x))
  497. return NULL;
  498. if (x->type == X509_LU_X509) {
  499. if (!X509_cmp(obj->data.x509, x->data.x509))
  500. return obj;
  501. } else if (x->type == X509_LU_CRL) {
  502. if (!X509_CRL_match(obj->data.crl, x->data.crl))
  503. return obj;
  504. } else
  505. return obj;
  506. }
  507. return NULL;
  508. }
  509. /*-
  510. * Try to get issuer certificate from store. Due to limitations
  511. * of the API this can only retrieve a single certificate matching
  512. * a given subject name. However it will fill the cache with all
  513. * matching certificates, so we can examine the cache for all
  514. * matches.
  515. *
  516. * Return values are:
  517. * 1 lookup successful.
  518. * 0 certificate not found.
  519. * -1 some other error.
  520. */
  521. int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  522. {
  523. X509_NAME *xn;
  524. X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
  525. int i, ok, idx, ret;
  526. if (obj == NULL)
  527. return -1;
  528. *issuer = NULL;
  529. xn = X509_get_issuer_name(x);
  530. ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
  531. if (ok != 1) {
  532. X509_OBJECT_free(obj);
  533. return 0;
  534. }
  535. /* If certificate matches all OK */
  536. if (ctx->check_issued(ctx, x, obj->data.x509)) {
  537. if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
  538. *issuer = obj->data.x509;
  539. X509_up_ref(*issuer);
  540. X509_OBJECT_free(obj);
  541. return 1;
  542. }
  543. }
  544. X509_OBJECT_free(obj);
  545. /* Else find index of first cert accepted by 'check_issued' */
  546. ret = 0;
  547. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  548. idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
  549. if (idx != -1) { /* should be true as we've had at least one
  550. * match */
  551. /* Look through all matching certs for suitable issuer */
  552. for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
  553. pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
  554. /* See if we've run past the matches */
  555. if (pobj->type != X509_LU_X509)
  556. break;
  557. if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
  558. break;
  559. if (ctx->check_issued(ctx, x, pobj->data.x509)) {
  560. *issuer = pobj->data.x509;
  561. ret = 1;
  562. /*
  563. * If times check, exit with match,
  564. * otherwise keep looking. Leave last
  565. * match in issuer so we return nearest
  566. * match if no certificate time is OK.
  567. */
  568. if (x509_check_cert_time(ctx, *issuer, -1))
  569. break;
  570. }
  571. }
  572. }
  573. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  574. if (*issuer)
  575. X509_up_ref(*issuer);
  576. return ret;
  577. }
  578. int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
  579. {
  580. return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  581. }
  582. int X509_STORE_set_depth(X509_STORE *ctx, int depth)
  583. {
  584. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  585. return 1;
  586. }
  587. int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
  588. {
  589. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  590. }
  591. int X509_STORE_set_trust(X509_STORE *ctx, int trust)
  592. {
  593. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  594. }
  595. int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
  596. {
  597. return X509_VERIFY_PARAM_set1(ctx->param, param);
  598. }
  599. X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
  600. {
  601. return ctx->param;
  602. }
  603. void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
  604. {
  605. ctx->verify = verify;
  606. }
  607. X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx)
  608. {
  609. return ctx->verify;
  610. }
  611. void X509_STORE_set_verify_cb(X509_STORE *ctx,
  612. X509_STORE_CTX_verify_cb verify_cb)
  613. {
  614. ctx->verify_cb = verify_cb;
  615. }
  616. X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx)
  617. {
  618. return ctx->verify_cb;
  619. }
  620. void X509_STORE_set_get_issuer(X509_STORE *ctx,
  621. X509_STORE_CTX_get_issuer_fn get_issuer)
  622. {
  623. ctx->get_issuer = get_issuer;
  624. }
  625. X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx)
  626. {
  627. return ctx->get_issuer;
  628. }
  629. void X509_STORE_set_check_issued(X509_STORE *ctx,
  630. X509_STORE_CTX_check_issued_fn check_issued)
  631. {
  632. ctx->check_issued = check_issued;
  633. }
  634. X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx)
  635. {
  636. return ctx->check_issued;
  637. }
  638. void X509_STORE_set_check_revocation(X509_STORE *ctx,
  639. X509_STORE_CTX_check_revocation_fn check_revocation)
  640. {
  641. ctx->check_revocation = check_revocation;
  642. }
  643. X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx)
  644. {
  645. return ctx->check_revocation;
  646. }
  647. void X509_STORE_set_get_crl(X509_STORE *ctx,
  648. X509_STORE_CTX_get_crl_fn get_crl)
  649. {
  650. ctx->get_crl = get_crl;
  651. }
  652. X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx)
  653. {
  654. return ctx->get_crl;
  655. }
  656. void X509_STORE_set_check_crl(X509_STORE *ctx,
  657. X509_STORE_CTX_check_crl_fn check_crl)
  658. {
  659. ctx->check_crl = check_crl;
  660. }
  661. X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx)
  662. {
  663. return ctx->check_crl;
  664. }
  665. void X509_STORE_set_cert_crl(X509_STORE *ctx,
  666. X509_STORE_CTX_cert_crl_fn cert_crl)
  667. {
  668. ctx->cert_crl = cert_crl;
  669. }
  670. X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx)
  671. {
  672. return ctx->cert_crl;
  673. }
  674. void X509_STORE_set_check_policy(X509_STORE *ctx,
  675. X509_STORE_CTX_check_policy_fn check_policy)
  676. {
  677. ctx->check_policy = check_policy;
  678. }
  679. X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx)
  680. {
  681. return ctx->check_policy;
  682. }
  683. void X509_STORE_set_lookup_certs(X509_STORE *ctx,
  684. X509_STORE_CTX_lookup_certs_fn lookup_certs)
  685. {
  686. ctx->lookup_certs = lookup_certs;
  687. }
  688. X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx)
  689. {
  690. return ctx->lookup_certs;
  691. }
  692. void X509_STORE_set_lookup_crls(X509_STORE *ctx,
  693. X509_STORE_CTX_lookup_crls_fn lookup_crls)
  694. {
  695. ctx->lookup_crls = lookup_crls;
  696. }
  697. X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx)
  698. {
  699. return ctx->lookup_crls;
  700. }
  701. void X509_STORE_set_cleanup(X509_STORE *ctx,
  702. X509_STORE_CTX_cleanup_fn ctx_cleanup)
  703. {
  704. ctx->cleanup = ctx_cleanup;
  705. }
  706. X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx)
  707. {
  708. return ctx->cleanup;
  709. }
  710. int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
  711. {
  712. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  713. }
  714. void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
  715. {
  716. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  717. }
  718. X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)
  719. {
  720. return ctx->ctx;
  721. }