x509_lu.c 27 KB

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