x509_lu.c 23 KB

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