x509_lu.c 25 KB

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