x509_lu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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", vfy);
  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. stmp.type = X509_LU_NONE;
  260. stmp.data.ptr = NULL;
  261. CRYPTO_THREAD_write_lock(ctx->lock);
  262. tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
  263. CRYPTO_THREAD_unlock(ctx->lock);
  264. if (tmp == NULL || type == X509_LU_CRL) {
  265. for (i = 0; i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
  266. lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
  267. j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
  268. if (j) {
  269. tmp = &stmp;
  270. break;
  271. }
  272. }
  273. if (tmp == NULL)
  274. return 0;
  275. }
  276. ret->type = tmp->type;
  277. ret->data.ptr = tmp->data.ptr;
  278. X509_OBJECT_up_ref_count(ret);
  279. return 1;
  280. }
  281. static int x509_store_add(X509_STORE *ctx, void *x, int crl) {
  282. X509_OBJECT *obj;
  283. int ret = 0, added = 0;
  284. if (x == NULL)
  285. return 0;
  286. obj = X509_OBJECT_new();
  287. if (obj == NULL)
  288. return 0;
  289. if (crl) {
  290. obj->type = X509_LU_CRL;
  291. obj->data.crl = (X509_CRL *)x;
  292. } else {
  293. obj->type = X509_LU_X509;
  294. obj->data.x509 = (X509 *)x;
  295. }
  296. X509_OBJECT_up_ref_count(obj);
  297. CRYPTO_THREAD_write_lock(ctx->lock);
  298. if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
  299. ret = 1;
  300. } else {
  301. added = sk_X509_OBJECT_push(ctx->objs, obj);
  302. ret = added != 0;
  303. }
  304. CRYPTO_THREAD_unlock(ctx->lock);
  305. if (added == 0) /* obj not pushed */
  306. X509_OBJECT_free(obj);
  307. return ret;
  308. }
  309. int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
  310. {
  311. if (!x509_store_add(ctx, x, 0)) {
  312. X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
  318. {
  319. if (!x509_store_add(ctx, x, 1)) {
  320. X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. int X509_OBJECT_up_ref_count(X509_OBJECT *a)
  326. {
  327. switch (a->type) {
  328. case X509_LU_NONE:
  329. break;
  330. case X509_LU_X509:
  331. return X509_up_ref(a->data.x509);
  332. case X509_LU_CRL:
  333. return X509_CRL_up_ref(a->data.crl);
  334. }
  335. return 1;
  336. }
  337. X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
  338. {
  339. if (a == NULL || a->type != X509_LU_X509)
  340. return NULL;
  341. return a->data.x509;
  342. }
  343. X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a)
  344. {
  345. if (a == NULL || a->type != X509_LU_CRL)
  346. return NULL;
  347. return a->data.crl;
  348. }
  349. X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a)
  350. {
  351. return a->type;
  352. }
  353. X509_OBJECT *X509_OBJECT_new(void)
  354. {
  355. X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
  356. if (ret == NULL) {
  357. X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
  358. return NULL;
  359. }
  360. ret->type = X509_LU_NONE;
  361. return ret;
  362. }
  363. static void x509_object_free_internal(X509_OBJECT *a)
  364. {
  365. if (a == NULL)
  366. return;
  367. switch (a->type) {
  368. case X509_LU_NONE:
  369. break;
  370. case X509_LU_X509:
  371. X509_free(a->data.x509);
  372. break;
  373. case X509_LU_CRL:
  374. X509_CRL_free(a->data.crl);
  375. break;
  376. }
  377. }
  378. int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj)
  379. {
  380. if (a == NULL || !X509_up_ref(obj))
  381. return 0;
  382. x509_object_free_internal(a);
  383. a->type = X509_LU_X509;
  384. a->data.x509 = obj;
  385. return 1;
  386. }
  387. int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj)
  388. {
  389. if (a == NULL || !X509_CRL_up_ref(obj))
  390. return 0;
  391. x509_object_free_internal(a);
  392. a->type = X509_LU_CRL;
  393. a->data.crl = obj;
  394. return 1;
  395. }
  396. void X509_OBJECT_free(X509_OBJECT *a)
  397. {
  398. x509_object_free_internal(a);
  399. OPENSSL_free(a);
  400. }
  401. static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
  402. X509_NAME *name, int *pnmatch)
  403. {
  404. X509_OBJECT stmp;
  405. X509 x509_s;
  406. X509_CRL crl_s;
  407. int idx;
  408. stmp.type = type;
  409. switch (type) {
  410. case X509_LU_X509:
  411. stmp.data.x509 = &x509_s;
  412. x509_s.cert_info.subject = name;
  413. break;
  414. case X509_LU_CRL:
  415. stmp.data.crl = &crl_s;
  416. crl_s.crl.issuer = name;
  417. break;
  418. case X509_LU_NONE:
  419. /* abort(); */
  420. return -1;
  421. }
  422. idx = sk_X509_OBJECT_find(h, &stmp);
  423. if (idx >= 0 && pnmatch) {
  424. int tidx;
  425. const X509_OBJECT *tobj, *pstmp;
  426. *pnmatch = 1;
  427. pstmp = &stmp;
  428. for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
  429. tobj = sk_X509_OBJECT_value(h, tidx);
  430. if (x509_object_cmp(&tobj, &pstmp))
  431. break;
  432. (*pnmatch)++;
  433. }
  434. }
  435. return idx;
  436. }
  437. int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
  438. X509_NAME *name)
  439. {
  440. return x509_object_idx_cnt(h, type, name, NULL);
  441. }
  442. X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
  443. X509_LOOKUP_TYPE type,
  444. X509_NAME *name)
  445. {
  446. int idx;
  447. idx = X509_OBJECT_idx_by_subject(h, type, name);
  448. if (idx == -1)
  449. return NULL;
  450. return sk_X509_OBJECT_value(h, idx);
  451. }
  452. STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
  453. {
  454. return v->objs;
  455. }
  456. STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
  457. {
  458. int i, idx, cnt;
  459. STACK_OF(X509) *sk = NULL;
  460. X509 *x;
  461. X509_OBJECT *obj;
  462. if (ctx->ctx == NULL)
  463. return NULL;
  464. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  465. idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
  466. if (idx < 0) {
  467. /*
  468. * Nothing found in cache: do lookup to possibly add new objects to
  469. * cache
  470. */
  471. X509_OBJECT *xobj = X509_OBJECT_new();
  472. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  473. if (xobj == NULL)
  474. return NULL;
  475. if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
  476. X509_OBJECT_free(xobj);
  477. return NULL;
  478. }
  479. X509_OBJECT_free(xobj);
  480. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  481. idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
  482. if (idx < 0) {
  483. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  484. return NULL;
  485. }
  486. }
  487. sk = sk_X509_new_null();
  488. for (i = 0; i < cnt; i++, idx++) {
  489. obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
  490. x = obj->data.x509;
  491. X509_up_ref(x);
  492. if (!sk_X509_push(sk, x)) {
  493. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  494. X509_free(x);
  495. sk_X509_pop_free(sk, X509_free);
  496. return NULL;
  497. }
  498. }
  499. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  500. return sk;
  501. }
  502. STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
  503. {
  504. int i, idx, cnt;
  505. STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
  506. X509_CRL *x;
  507. X509_OBJECT *obj, *xobj = X509_OBJECT_new();
  508. /* Always do lookup to possibly add new CRLs to cache */
  509. if (sk == NULL
  510. || xobj == NULL
  511. || ctx->ctx == NULL
  512. || !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
  513. X509_OBJECT_free(xobj);
  514. sk_X509_CRL_free(sk);
  515. return NULL;
  516. }
  517. X509_OBJECT_free(xobj);
  518. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  519. idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
  520. if (idx < 0) {
  521. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  522. sk_X509_CRL_free(sk);
  523. return NULL;
  524. }
  525. for (i = 0; i < cnt; i++, idx++) {
  526. obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
  527. x = obj->data.crl;
  528. X509_CRL_up_ref(x);
  529. if (!sk_X509_CRL_push(sk, x)) {
  530. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  531. X509_CRL_free(x);
  532. sk_X509_CRL_pop_free(sk, X509_CRL_free);
  533. return NULL;
  534. }
  535. }
  536. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  537. return sk;
  538. }
  539. X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
  540. X509_OBJECT *x)
  541. {
  542. int idx, i, num;
  543. X509_OBJECT *obj;
  544. idx = sk_X509_OBJECT_find(h, x);
  545. if (idx < 0)
  546. return NULL;
  547. if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
  548. return sk_X509_OBJECT_value(h, idx);
  549. for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) {
  550. obj = sk_X509_OBJECT_value(h, i);
  551. if (x509_object_cmp((const X509_OBJECT **)&obj,
  552. (const X509_OBJECT **)&x))
  553. return NULL;
  554. if (x->type == X509_LU_X509) {
  555. if (!X509_cmp(obj->data.x509, x->data.x509))
  556. return obj;
  557. } else if (x->type == X509_LU_CRL) {
  558. if (!X509_CRL_match(obj->data.crl, x->data.crl))
  559. return obj;
  560. } else
  561. return obj;
  562. }
  563. return NULL;
  564. }
  565. /*-
  566. * Try to get issuer certificate from store. Due to limitations
  567. * of the API this can only retrieve a single certificate matching
  568. * a given subject name. However it will fill the cache with all
  569. * matching certificates, so we can examine the cache for all
  570. * matches.
  571. *
  572. * Return values are:
  573. * 1 lookup successful.
  574. * 0 certificate not found.
  575. * -1 some other error.
  576. */
  577. int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  578. {
  579. X509_NAME *xn;
  580. X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
  581. int i, ok, idx, ret;
  582. if (obj == NULL)
  583. return -1;
  584. *issuer = NULL;
  585. xn = X509_get_issuer_name(x);
  586. ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
  587. if (ok != 1) {
  588. X509_OBJECT_free(obj);
  589. return 0;
  590. }
  591. /* If certificate matches all OK */
  592. if (ctx->check_issued(ctx, x, obj->data.x509)) {
  593. if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
  594. *issuer = obj->data.x509;
  595. X509_up_ref(*issuer);
  596. X509_OBJECT_free(obj);
  597. return 1;
  598. }
  599. }
  600. X509_OBJECT_free(obj);
  601. if (ctx->ctx == NULL)
  602. return 0;
  603. /* Else find index of first cert accepted by 'check_issued' */
  604. ret = 0;
  605. CRYPTO_THREAD_write_lock(ctx->ctx->lock);
  606. idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
  607. if (idx != -1) { /* should be true as we've had at least one
  608. * match */
  609. /* Look through all matching certs for suitable issuer */
  610. for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
  611. pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
  612. /* See if we've run past the matches */
  613. if (pobj->type != X509_LU_X509)
  614. break;
  615. if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
  616. break;
  617. if (ctx->check_issued(ctx, x, pobj->data.x509)) {
  618. *issuer = pobj->data.x509;
  619. ret = 1;
  620. /*
  621. * If times check, exit with match,
  622. * otherwise keep looking. Leave last
  623. * match in issuer so we return nearest
  624. * match if no certificate time is OK.
  625. */
  626. if (x509_check_cert_time(ctx, *issuer, -1))
  627. break;
  628. }
  629. }
  630. }
  631. CRYPTO_THREAD_unlock(ctx->ctx->lock);
  632. if (*issuer)
  633. X509_up_ref(*issuer);
  634. return ret;
  635. }
  636. int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
  637. {
  638. return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  639. }
  640. int X509_STORE_set_depth(X509_STORE *ctx, int depth)
  641. {
  642. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  643. return 1;
  644. }
  645. int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
  646. {
  647. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  648. }
  649. int X509_STORE_set_trust(X509_STORE *ctx, int trust)
  650. {
  651. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  652. }
  653. int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
  654. {
  655. return X509_VERIFY_PARAM_set1(ctx->param, param);
  656. }
  657. X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
  658. {
  659. return ctx->param;
  660. }
  661. void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
  662. {
  663. ctx->verify = verify;
  664. }
  665. X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx)
  666. {
  667. return ctx->verify;
  668. }
  669. void X509_STORE_set_verify_cb(X509_STORE *ctx,
  670. X509_STORE_CTX_verify_cb verify_cb)
  671. {
  672. ctx->verify_cb = verify_cb;
  673. }
  674. X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx)
  675. {
  676. return ctx->verify_cb;
  677. }
  678. void X509_STORE_set_get_issuer(X509_STORE *ctx,
  679. X509_STORE_CTX_get_issuer_fn get_issuer)
  680. {
  681. ctx->get_issuer = get_issuer;
  682. }
  683. X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx)
  684. {
  685. return ctx->get_issuer;
  686. }
  687. void X509_STORE_set_check_issued(X509_STORE *ctx,
  688. X509_STORE_CTX_check_issued_fn check_issued)
  689. {
  690. ctx->check_issued = check_issued;
  691. }
  692. X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx)
  693. {
  694. return ctx->check_issued;
  695. }
  696. void X509_STORE_set_check_revocation(X509_STORE *ctx,
  697. X509_STORE_CTX_check_revocation_fn check_revocation)
  698. {
  699. ctx->check_revocation = check_revocation;
  700. }
  701. X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx)
  702. {
  703. return ctx->check_revocation;
  704. }
  705. void X509_STORE_set_get_crl(X509_STORE *ctx,
  706. X509_STORE_CTX_get_crl_fn get_crl)
  707. {
  708. ctx->get_crl = get_crl;
  709. }
  710. X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx)
  711. {
  712. return ctx->get_crl;
  713. }
  714. void X509_STORE_set_check_crl(X509_STORE *ctx,
  715. X509_STORE_CTX_check_crl_fn check_crl)
  716. {
  717. ctx->check_crl = check_crl;
  718. }
  719. X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx)
  720. {
  721. return ctx->check_crl;
  722. }
  723. void X509_STORE_set_cert_crl(X509_STORE *ctx,
  724. X509_STORE_CTX_cert_crl_fn cert_crl)
  725. {
  726. ctx->cert_crl = cert_crl;
  727. }
  728. X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx)
  729. {
  730. return ctx->cert_crl;
  731. }
  732. void X509_STORE_set_check_policy(X509_STORE *ctx,
  733. X509_STORE_CTX_check_policy_fn check_policy)
  734. {
  735. ctx->check_policy = check_policy;
  736. }
  737. X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx)
  738. {
  739. return ctx->check_policy;
  740. }
  741. void X509_STORE_set_lookup_certs(X509_STORE *ctx,
  742. X509_STORE_CTX_lookup_certs_fn lookup_certs)
  743. {
  744. ctx->lookup_certs = lookup_certs;
  745. }
  746. X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx)
  747. {
  748. return ctx->lookup_certs;
  749. }
  750. void X509_STORE_set_lookup_crls(X509_STORE *ctx,
  751. X509_STORE_CTX_lookup_crls_fn lookup_crls)
  752. {
  753. ctx->lookup_crls = lookup_crls;
  754. }
  755. X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx)
  756. {
  757. return ctx->lookup_crls;
  758. }
  759. void X509_STORE_set_cleanup(X509_STORE *ctx,
  760. X509_STORE_CTX_cleanup_fn ctx_cleanup)
  761. {
  762. ctx->cleanup = ctx_cleanup;
  763. }
  764. X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx)
  765. {
  766. return ctx->cleanup;
  767. }
  768. int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
  769. {
  770. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  771. }
  772. void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
  773. {
  774. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  775. }
  776. X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)
  777. {
  778. return ctx->ctx;
  779. }