x509_lu.c 23 KB

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