You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2408 line
70 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <string.h>
  57. #include <time.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/err.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/lhash.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/thread.h>
  66. #include <openssl/x509.h>
  67. #include <openssl/x509v3.h>
  68. #include "vpm_int.h"
  69. #include "../internal.h"
  70. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  71. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  72. /* CRL score values */
  73. /* No unhandled critical extensions */
  74. #define CRL_SCORE_NOCRITICAL 0x100
  75. /* certificate is within CRL scope */
  76. #define CRL_SCORE_SCOPE 0x080
  77. /* CRL times valid */
  78. #define CRL_SCORE_TIME 0x040
  79. /* Issuer name matches certificate */
  80. #define CRL_SCORE_ISSUER_NAME 0x020
  81. /* If this score or above CRL is probably valid */
  82. #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
  83. /* CRL issuer is certificate issuer */
  84. #define CRL_SCORE_ISSUER_CERT 0x018
  85. /* CRL issuer is on certificate path */
  86. #define CRL_SCORE_SAME_PATH 0x008
  87. /* CRL issuer matches CRL AKID */
  88. #define CRL_SCORE_AKID 0x004
  89. /* Have a delta CRL with valid times */
  90. #define CRL_SCORE_TIME_DELTA 0x002
  91. static int null_callback(int ok, X509_STORE_CTX *e);
  92. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  93. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  94. static int check_chain_extensions(X509_STORE_CTX *ctx);
  95. static int check_name_constraints(X509_STORE_CTX *ctx);
  96. static int check_id(X509_STORE_CTX *ctx);
  97. static int check_trust(X509_STORE_CTX *ctx);
  98. static int check_revocation(X509_STORE_CTX *ctx);
  99. static int check_cert(X509_STORE_CTX *ctx);
  100. static int check_policy(X509_STORE_CTX *ctx);
  101. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  102. unsigned int *preasons, X509_CRL *crl, X509 *x);
  103. static int get_crl_delta(X509_STORE_CTX *ctx,
  104. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
  105. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
  106. int *pcrl_score, X509_CRL *base,
  107. STACK_OF(X509_CRL) *crls);
  108. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
  109. int *pcrl_score);
  110. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  111. unsigned int *preasons);
  112. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
  113. static int check_crl_chain(X509_STORE_CTX *ctx,
  114. STACK_OF(X509) *cert_path,
  115. STACK_OF(X509) *crl_path);
  116. static int internal_verify(X509_STORE_CTX *ctx);
  117. static int null_callback(int ok, X509_STORE_CTX *e)
  118. {
  119. return ok;
  120. }
  121. #if 0
  122. static int x509_subject_cmp(X509 **a, X509 **b)
  123. {
  124. return X509_subject_name_cmp(*a, *b);
  125. }
  126. #endif
  127. /* Return 1 is a certificate is self signed */
  128. static int cert_self_signed(X509 *x)
  129. {
  130. X509_check_purpose(x, -1, 0);
  131. if (x->ex_flags & EXFLAG_SS)
  132. return 1;
  133. else
  134. return 0;
  135. }
  136. /* Given a certificate try and find an exact match in the store */
  137. static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
  138. {
  139. STACK_OF(X509) *certs;
  140. X509 *xtmp = NULL;
  141. size_t i;
  142. /* Lookup all certs with matching subject name */
  143. certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
  144. if (certs == NULL)
  145. return NULL;
  146. /* Look for exact match */
  147. for (i = 0; i < sk_X509_num(certs); i++) {
  148. xtmp = sk_X509_value(certs, i);
  149. if (!X509_cmp(xtmp, x))
  150. break;
  151. }
  152. if (i < sk_X509_num(certs))
  153. X509_up_ref(xtmp);
  154. else
  155. xtmp = NULL;
  156. sk_X509_pop_free(certs, X509_free);
  157. return xtmp;
  158. }
  159. int X509_verify_cert(X509_STORE_CTX *ctx)
  160. {
  161. X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
  162. int bad_chain = 0;
  163. X509_VERIFY_PARAM *param = ctx->param;
  164. int depth, i, ok = 0;
  165. int num, j, retry, trust;
  166. int (*cb) (int xok, X509_STORE_CTX *xctx);
  167. STACK_OF(X509) *sktmp = NULL;
  168. if (ctx->cert == NULL) {
  169. OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  170. return -1;
  171. }
  172. if (ctx->chain != NULL) {
  173. /*
  174. * This X509_STORE_CTX has already been used to verify a cert. We
  175. * cannot do another one.
  176. */
  177. OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  178. return -1;
  179. }
  180. cb = ctx->verify_cb;
  181. /*
  182. * first we make sure the chain we are going to build is present and that
  183. * the first entry is in place
  184. */
  185. ctx->chain = sk_X509_new_null();
  186. if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) {
  187. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  188. goto end;
  189. }
  190. X509_up_ref(ctx->cert);
  191. ctx->last_untrusted = 1;
  192. /* We use a temporary STACK so we can chop and hack at it */
  193. if (ctx->untrusted != NULL
  194. && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
  195. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  196. goto end;
  197. }
  198. num = sk_X509_num(ctx->chain);
  199. x = sk_X509_value(ctx->chain, num - 1);
  200. depth = param->depth;
  201. for (;;) {
  202. /* If we have enough, we break */
  203. if (depth < num)
  204. break; /* FIXME: If this happens, we should take
  205. * note of it and, if appropriate, use the
  206. * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
  207. * later. */
  208. /* If we are self signed, we break */
  209. if (cert_self_signed(x))
  210. break;
  211. /*
  212. * If asked see if we can find issuer in trusted store first
  213. */
  214. if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
  215. ok = ctx->get_issuer(&xtmp, ctx, x);
  216. if (ok < 0)
  217. goto end;
  218. /*
  219. * If successful for now free up cert so it will be picked up
  220. * again later.
  221. */
  222. if (ok > 0) {
  223. X509_free(xtmp);
  224. break;
  225. }
  226. }
  227. /* If we were passed a cert chain, use it first */
  228. if (ctx->untrusted != NULL) {
  229. xtmp = find_issuer(ctx, sktmp, x);
  230. if (xtmp != NULL) {
  231. if (!sk_X509_push(ctx->chain, xtmp)) {
  232. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  233. ok = 0;
  234. goto end;
  235. }
  236. X509_up_ref(xtmp);
  237. (void)sk_X509_delete_ptr(sktmp, xtmp);
  238. ctx->last_untrusted++;
  239. x = xtmp;
  240. num++;
  241. /*
  242. * reparse the full chain for the next one
  243. */
  244. continue;
  245. }
  246. }
  247. break;
  248. }
  249. /* Remember how many untrusted certs we have */
  250. j = num;
  251. /*
  252. * at this point, chain should contain a list of untrusted certificates.
  253. * We now need to add at least one trusted one, if possible, otherwise we
  254. * complain.
  255. */
  256. do {
  257. /*
  258. * Examine last certificate in chain and see if it is self signed.
  259. */
  260. i = sk_X509_num(ctx->chain);
  261. x = sk_X509_value(ctx->chain, i - 1);
  262. if (cert_self_signed(x)) {
  263. /* we have a self signed certificate */
  264. if (sk_X509_num(ctx->chain) == 1) {
  265. /*
  266. * We have a single self signed certificate: see if we can
  267. * find it in the store. We must have an exact match to avoid
  268. * possible impersonation.
  269. */
  270. ok = ctx->get_issuer(&xtmp, ctx, x);
  271. if ((ok <= 0) || X509_cmp(x, xtmp)) {
  272. ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  273. ctx->current_cert = x;
  274. ctx->error_depth = i - 1;
  275. if (ok == 1)
  276. X509_free(xtmp);
  277. bad_chain = 1;
  278. ok = cb(0, ctx);
  279. if (!ok)
  280. goto end;
  281. } else {
  282. /*
  283. * We have a match: replace certificate with store
  284. * version so we get any trust settings.
  285. */
  286. X509_free(x);
  287. x = xtmp;
  288. (void)sk_X509_set(ctx->chain, i - 1, x);
  289. ctx->last_untrusted = 0;
  290. }
  291. } else {
  292. /*
  293. * extract and save self signed certificate for later use
  294. */
  295. chain_ss = sk_X509_pop(ctx->chain);
  296. ctx->last_untrusted--;
  297. num--;
  298. j--;
  299. x = sk_X509_value(ctx->chain, num - 1);
  300. }
  301. }
  302. /* We now lookup certs from the certificate store */
  303. for (;;) {
  304. /* If we have enough, we break */
  305. if (depth < num)
  306. break;
  307. /* If we are self signed, we break */
  308. if (cert_self_signed(x))
  309. break;
  310. ok = ctx->get_issuer(&xtmp, ctx, x);
  311. if (ok < 0)
  312. goto end;
  313. if (ok == 0)
  314. break;
  315. x = xtmp;
  316. if (!sk_X509_push(ctx->chain, x)) {
  317. X509_free(xtmp);
  318. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  319. ok = 0;
  320. goto end;
  321. }
  322. num++;
  323. }
  324. /* we now have our chain, lets check it... */
  325. trust = check_trust(ctx);
  326. /* If explicitly rejected error */
  327. if (trust == X509_TRUST_REJECTED) {
  328. ok = 0;
  329. goto end;
  330. }
  331. /*
  332. * If it's not explicitly trusted then check if there is an alternative
  333. * chain that could be used. We only do this if we haven't already
  334. * checked via TRUSTED_FIRST and the user hasn't switched off alternate
  335. * chain checking
  336. */
  337. retry = 0;
  338. if (trust != X509_TRUST_TRUSTED
  339. && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
  340. && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
  341. while (j-- > 1) {
  342. xtmp2 = sk_X509_value(ctx->chain, j - 1);
  343. ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
  344. if (ok < 0)
  345. goto end;
  346. /* Check if we found an alternate chain */
  347. if (ok > 0) {
  348. /*
  349. * Free up the found cert we'll add it again later
  350. */
  351. X509_free(xtmp);
  352. /*
  353. * Dump all the certs above this point - we've found an
  354. * alternate chain
  355. */
  356. while (num > j) {
  357. xtmp = sk_X509_pop(ctx->chain);
  358. X509_free(xtmp);
  359. num--;
  360. }
  361. ctx->last_untrusted = sk_X509_num(ctx->chain);
  362. retry = 1;
  363. break;
  364. }
  365. }
  366. }
  367. } while (retry);
  368. /*
  369. * If not explicitly trusted then indicate error unless it's a single
  370. * self signed certificate in which case we've indicated an error already
  371. * and set bad_chain == 1
  372. */
  373. if (trust != X509_TRUST_TRUSTED && !bad_chain) {
  374. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
  375. if (ctx->last_untrusted >= num)
  376. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  377. else
  378. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  379. ctx->current_cert = x;
  380. } else {
  381. sk_X509_push(ctx->chain, chain_ss);
  382. num++;
  383. ctx->last_untrusted = num;
  384. ctx->current_cert = chain_ss;
  385. ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  386. chain_ss = NULL;
  387. }
  388. ctx->error_depth = num - 1;
  389. bad_chain = 1;
  390. ok = cb(0, ctx);
  391. if (!ok)
  392. goto end;
  393. }
  394. /* We have the chain complete: now we need to check its purpose */
  395. ok = check_chain_extensions(ctx);
  396. if (!ok)
  397. goto end;
  398. /* Check name constraints */
  399. ok = check_name_constraints(ctx);
  400. if (!ok)
  401. goto end;
  402. ok = check_id(ctx);
  403. if (!ok)
  404. goto end;
  405. /*
  406. * Check revocation status: we do this after copying parameters because
  407. * they may be needed for CRL signature verification.
  408. */
  409. ok = ctx->check_revocation(ctx);
  410. if (!ok)
  411. goto end;
  412. int err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
  413. ctx->param->flags);
  414. if (err != X509_V_OK) {
  415. ctx->error = err;
  416. ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
  417. ok = cb(0, ctx);
  418. if (!ok)
  419. goto end;
  420. }
  421. /* At this point, we have a chain and need to verify it */
  422. if (ctx->verify != NULL)
  423. ok = ctx->verify(ctx);
  424. else
  425. ok = internal_verify(ctx);
  426. if (!ok)
  427. goto end;
  428. /* If we get this far evaluate policies */
  429. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  430. ok = ctx->check_policy(ctx);
  431. end:
  432. if (sktmp != NULL)
  433. sk_X509_free(sktmp);
  434. if (chain_ss != NULL)
  435. X509_free(chain_ss);
  436. return ok;
  437. }
  438. /*
  439. * Given a STACK_OF(X509) find the issuer of cert (if any)
  440. */
  441. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  442. {
  443. size_t i;
  444. X509 *issuer;
  445. for (i = 0; i < sk_X509_num(sk); i++) {
  446. issuer = sk_X509_value(sk, i);
  447. if (ctx->check_issued(ctx, x, issuer))
  448. return issuer;
  449. }
  450. return NULL;
  451. }
  452. /* Given a possible certificate and issuer check them */
  453. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  454. {
  455. int ret;
  456. ret = X509_check_issued(issuer, x);
  457. if (ret == X509_V_OK)
  458. return 1;
  459. /* If we haven't asked for issuer errors don't set ctx */
  460. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  461. return 0;
  462. ctx->error = ret;
  463. ctx->current_cert = x;
  464. ctx->current_issuer = issuer;
  465. return ctx->verify_cb(0, ctx);
  466. }
  467. /* Alternative lookup method: look from a STACK stored in other_ctx */
  468. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  469. {
  470. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  471. if (*issuer) {
  472. X509_up_ref(*issuer);
  473. return 1;
  474. } else
  475. return 0;
  476. }
  477. /*
  478. * Check a certificate chains extensions for consistency with the supplied
  479. * purpose
  480. */
  481. static int check_chain_extensions(X509_STORE_CTX *ctx)
  482. {
  483. int i, ok = 0, must_be_ca, plen = 0;
  484. X509 *x;
  485. int (*cb) (int xok, X509_STORE_CTX *xctx);
  486. int proxy_path_length = 0;
  487. int purpose;
  488. int allow_proxy_certs;
  489. cb = ctx->verify_cb;
  490. /*
  491. * must_be_ca can have 1 of 3 values: -1: we accept both CA and non-CA
  492. * certificates, to allow direct use of self-signed certificates (which
  493. * are marked as CA). 0: we only accept non-CA certificates. This is
  494. * currently not used, but the possibility is present for future
  495. * extensions. 1: we only accept CA certificates. This is currently used
  496. * for all certificates in the chain except the leaf certificate.
  497. */
  498. must_be_ca = -1;
  499. /* CRL path validation */
  500. if (ctx->parent) {
  501. allow_proxy_certs = 0;
  502. purpose = X509_PURPOSE_CRL_SIGN;
  503. } else {
  504. allow_proxy_certs =
  505. ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  506. /*
  507. * A hack to keep people who don't want to modify their software
  508. * happy
  509. */
  510. if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
  511. allow_proxy_certs = 1;
  512. purpose = ctx->param->purpose;
  513. }
  514. /* Check all untrusted certificates */
  515. for (i = 0; i < ctx->last_untrusted; i++) {
  516. int ret;
  517. x = sk_X509_value(ctx->chain, i);
  518. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  519. && (x->ex_flags & EXFLAG_CRITICAL)) {
  520. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  521. ctx->error_depth = i;
  522. ctx->current_cert = x;
  523. ok = cb(0, ctx);
  524. if (!ok)
  525. goto end;
  526. }
  527. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
  528. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  529. ctx->error_depth = i;
  530. ctx->current_cert = x;
  531. ok = cb(0, ctx);
  532. if (!ok)
  533. goto end;
  534. }
  535. ret = X509_check_ca(x);
  536. switch (must_be_ca) {
  537. case -1:
  538. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  539. && (ret != 1) && (ret != 0)) {
  540. ret = 0;
  541. ctx->error = X509_V_ERR_INVALID_CA;
  542. } else
  543. ret = 1;
  544. break;
  545. case 0:
  546. if (ret != 0) {
  547. ret = 0;
  548. ctx->error = X509_V_ERR_INVALID_NON_CA;
  549. } else
  550. ret = 1;
  551. break;
  552. default:
  553. if ((ret == 0)
  554. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  555. && (ret != 1))) {
  556. ret = 0;
  557. ctx->error = X509_V_ERR_INVALID_CA;
  558. } else
  559. ret = 1;
  560. break;
  561. }
  562. if (ret == 0) {
  563. ctx->error_depth = i;
  564. ctx->current_cert = x;
  565. ok = cb(0, ctx);
  566. if (!ok)
  567. goto end;
  568. }
  569. if (ctx->param->purpose > 0) {
  570. ret = X509_check_purpose(x, purpose, must_be_ca > 0);
  571. if ((ret == 0)
  572. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  573. && (ret != 1))) {
  574. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  575. ctx->error_depth = i;
  576. ctx->current_cert = x;
  577. ok = cb(0, ctx);
  578. if (!ok)
  579. goto end;
  580. }
  581. }
  582. /* Check pathlen if not self issued */
  583. if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
  584. && (x->ex_pathlen != -1)
  585. && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
  586. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  587. ctx->error_depth = i;
  588. ctx->current_cert = x;
  589. ok = cb(0, ctx);
  590. if (!ok)
  591. goto end;
  592. }
  593. /* Increment path length if not self issued */
  594. if (!(x->ex_flags & EXFLAG_SI))
  595. plen++;
  596. /*
  597. * If this certificate is a proxy certificate, the next certificate
  598. * must be another proxy certificate or a EE certificate. If not,
  599. * the next certificate must be a CA certificate.
  600. */
  601. if (x->ex_flags & EXFLAG_PROXY) {
  602. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
  603. ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  604. ctx->error_depth = i;
  605. ctx->current_cert = x;
  606. ok = cb(0, ctx);
  607. if (!ok)
  608. goto end;
  609. }
  610. proxy_path_length++;
  611. must_be_ca = 0;
  612. } else
  613. must_be_ca = 1;
  614. }
  615. ok = 1;
  616. end:
  617. return ok;
  618. }
  619. static int check_name_constraints(X509_STORE_CTX *ctx)
  620. {
  621. X509 *x;
  622. int i, j, rv;
  623. /* Check name constraints for all certificates */
  624. for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
  625. x = sk_X509_value(ctx->chain, i);
  626. /* Ignore self issued certs unless last in chain */
  627. if (i && (x->ex_flags & EXFLAG_SI))
  628. continue;
  629. /*
  630. * Check against constraints for all certificates higher in chain
  631. * including trust anchor. Trust anchor not strictly speaking needed
  632. * but if it includes constraints it is to be assumed it expects them
  633. * to be obeyed.
  634. */
  635. for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
  636. NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
  637. if (nc) {
  638. rv = NAME_CONSTRAINTS_check(x, nc);
  639. if (rv != X509_V_OK) {
  640. ctx->error = rv;
  641. ctx->error_depth = i;
  642. ctx->current_cert = x;
  643. if (!ctx->verify_cb(0, ctx))
  644. return 0;
  645. }
  646. }
  647. }
  648. }
  649. return 1;
  650. }
  651. static int check_id_error(X509_STORE_CTX *ctx, int errcode)
  652. {
  653. ctx->error = errcode;
  654. ctx->current_cert = ctx->cert;
  655. ctx->error_depth = 0;
  656. return ctx->verify_cb(0, ctx);
  657. }
  658. static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
  659. {
  660. size_t i;
  661. size_t n = sk_OPENSSL_STRING_num(id->hosts);
  662. char *name;
  663. if (id->peername != NULL) {
  664. OPENSSL_free(id->peername);
  665. id->peername = NULL;
  666. }
  667. for (i = 0; i < n; ++i) {
  668. name = sk_OPENSSL_STRING_value(id->hosts, i);
  669. if (X509_check_host(x, name, strlen(name), id->hostflags,
  670. &id->peername) > 0)
  671. return 1;
  672. }
  673. return n == 0;
  674. }
  675. static int check_id(X509_STORE_CTX *ctx)
  676. {
  677. X509_VERIFY_PARAM *vpm = ctx->param;
  678. X509_VERIFY_PARAM_ID *id = vpm->id;
  679. X509 *x = ctx->cert;
  680. if (id->hosts && check_hosts(x, id) <= 0) {
  681. if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
  682. return 0;
  683. }
  684. if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) {
  685. if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
  686. return 0;
  687. }
  688. if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
  689. if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
  690. return 0;
  691. }
  692. return 1;
  693. }
  694. static int check_trust(X509_STORE_CTX *ctx)
  695. {
  696. size_t i;
  697. int ok;
  698. X509 *x = NULL;
  699. int (*cb) (int xok, X509_STORE_CTX *xctx);
  700. cb = ctx->verify_cb;
  701. /* Check all trusted certificates in chain */
  702. for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
  703. x = sk_X509_value(ctx->chain, i);
  704. ok = X509_check_trust(x, ctx->param->trust, 0);
  705. /* If explicitly trusted return trusted */
  706. if (ok == X509_TRUST_TRUSTED)
  707. return X509_TRUST_TRUSTED;
  708. /*
  709. * If explicitly rejected notify callback and reject if not
  710. * overridden.
  711. */
  712. if (ok == X509_TRUST_REJECTED) {
  713. ctx->error_depth = i;
  714. ctx->current_cert = x;
  715. ctx->error = X509_V_ERR_CERT_REJECTED;
  716. ok = cb(0, ctx);
  717. if (!ok)
  718. return X509_TRUST_REJECTED;
  719. }
  720. }
  721. /*
  722. * If we accept partial chains and have at least one trusted certificate
  723. * return success.
  724. */
  725. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  726. X509 *mx;
  727. if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain))
  728. return X509_TRUST_TRUSTED;
  729. x = sk_X509_value(ctx->chain, 0);
  730. mx = lookup_cert_match(ctx, x);
  731. if (mx) {
  732. (void)sk_X509_set(ctx->chain, 0, mx);
  733. X509_free(x);
  734. ctx->last_untrusted = 0;
  735. return X509_TRUST_TRUSTED;
  736. }
  737. }
  738. /*
  739. * If no trusted certs in chain at all return untrusted and allow
  740. * standard (no issuer cert) etc errors to be indicated.
  741. */
  742. return X509_TRUST_UNTRUSTED;
  743. }
  744. static int check_revocation(X509_STORE_CTX *ctx)
  745. {
  746. int i, last, ok;
  747. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  748. return 1;
  749. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  750. last = sk_X509_num(ctx->chain) - 1;
  751. else {
  752. /* If checking CRL paths this isn't the EE certificate */
  753. if (ctx->parent)
  754. return 1;
  755. last = 0;
  756. }
  757. for (i = 0; i <= last; i++) {
  758. ctx->error_depth = i;
  759. ok = check_cert(ctx);
  760. if (!ok)
  761. return ok;
  762. }
  763. return 1;
  764. }
  765. static int check_cert(X509_STORE_CTX *ctx)
  766. {
  767. X509_CRL *crl = NULL, *dcrl = NULL;
  768. X509 *x;
  769. int ok = 0, cnum;
  770. unsigned int last_reasons;
  771. cnum = ctx->error_depth;
  772. x = sk_X509_value(ctx->chain, cnum);
  773. ctx->current_cert = x;
  774. ctx->current_issuer = NULL;
  775. ctx->current_crl_score = 0;
  776. ctx->current_reasons = 0;
  777. while (ctx->current_reasons != CRLDP_ALL_REASONS) {
  778. last_reasons = ctx->current_reasons;
  779. /* Try to retrieve relevant CRL */
  780. if (ctx->get_crl)
  781. ok = ctx->get_crl(ctx, &crl, x);
  782. else
  783. ok = get_crl_delta(ctx, &crl, &dcrl, x);
  784. /*
  785. * If error looking up CRL, nothing we can do except notify callback
  786. */
  787. if (!ok) {
  788. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  789. ok = ctx->verify_cb(0, ctx);
  790. goto err;
  791. }
  792. ctx->current_crl = crl;
  793. ok = ctx->check_crl(ctx, crl);
  794. if (!ok)
  795. goto err;
  796. if (dcrl) {
  797. ok = ctx->check_crl(ctx, dcrl);
  798. if (!ok)
  799. goto err;
  800. ok = ctx->cert_crl(ctx, dcrl, x);
  801. if (!ok)
  802. goto err;
  803. } else
  804. ok = 1;
  805. /* Don't look in full CRL if delta reason is removefromCRL */
  806. if (ok != 2) {
  807. ok = ctx->cert_crl(ctx, crl, x);
  808. if (!ok)
  809. goto err;
  810. }
  811. X509_CRL_free(crl);
  812. X509_CRL_free(dcrl);
  813. crl = NULL;
  814. dcrl = NULL;
  815. /*
  816. * If reasons not updated we wont get anywhere by another iteration,
  817. * so exit loop.
  818. */
  819. if (last_reasons == ctx->current_reasons) {
  820. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  821. ok = ctx->verify_cb(0, ctx);
  822. goto err;
  823. }
  824. }
  825. err:
  826. X509_CRL_free(crl);
  827. X509_CRL_free(dcrl);
  828. ctx->current_crl = NULL;
  829. return ok;
  830. }
  831. /* Check CRL times against values in X509_STORE_CTX */
  832. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  833. {
  834. time_t *ptime;
  835. int i;
  836. if (notify)
  837. ctx->current_crl = crl;
  838. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  839. ptime = &ctx->param->check_time;
  840. else
  841. ptime = NULL;
  842. i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  843. if (i == 0) {
  844. if (!notify)
  845. return 0;
  846. ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  847. if (!ctx->verify_cb(0, ctx))
  848. return 0;
  849. }
  850. if (i > 0) {
  851. if (!notify)
  852. return 0;
  853. ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
  854. if (!ctx->verify_cb(0, ctx))
  855. return 0;
  856. }
  857. if (X509_CRL_get_nextUpdate(crl)) {
  858. i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  859. if (i == 0) {
  860. if (!notify)
  861. return 0;
  862. ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  863. if (!ctx->verify_cb(0, ctx))
  864. return 0;
  865. }
  866. /* Ignore expiry of base CRL is delta is valid */
  867. if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
  868. if (!notify)
  869. return 0;
  870. ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
  871. if (!ctx->verify_cb(0, ctx))
  872. return 0;
  873. }
  874. }
  875. if (notify)
  876. ctx->current_crl = NULL;
  877. return 1;
  878. }
  879. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
  880. X509 **pissuer, int *pscore, unsigned int *preasons,
  881. STACK_OF(X509_CRL) *crls)
  882. {
  883. int crl_score, best_score = *pscore;
  884. size_t i;
  885. unsigned int reasons, best_reasons = 0;
  886. X509 *x = ctx->current_cert;
  887. X509_CRL *crl, *best_crl = NULL;
  888. X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
  889. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  890. crl = sk_X509_CRL_value(crls, i);
  891. reasons = *preasons;
  892. crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
  893. if (crl_score > best_score) {
  894. best_crl = crl;
  895. best_crl_issuer = crl_issuer;
  896. best_score = crl_score;
  897. best_reasons = reasons;
  898. }
  899. }
  900. if (best_crl) {
  901. if (*pcrl)
  902. X509_CRL_free(*pcrl);
  903. *pcrl = best_crl;
  904. *pissuer = best_crl_issuer;
  905. *pscore = best_score;
  906. *preasons = best_reasons;
  907. X509_CRL_up_ref(best_crl);
  908. if (*pdcrl) {
  909. X509_CRL_free(*pdcrl);
  910. *pdcrl = NULL;
  911. }
  912. get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
  913. }
  914. if (best_score >= CRL_SCORE_VALID)
  915. return 1;
  916. return 0;
  917. }
  918. /*
  919. * Compare two CRL extensions for delta checking purposes. They should be
  920. * both present or both absent. If both present all fields must be identical.
  921. */
  922. static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
  923. {
  924. ASN1_OCTET_STRING *exta, *extb;
  925. int i;
  926. i = X509_CRL_get_ext_by_NID(a, nid, -1);
  927. if (i >= 0) {
  928. /* Can't have multiple occurrences */
  929. if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
  930. return 0;
  931. exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
  932. } else
  933. exta = NULL;
  934. i = X509_CRL_get_ext_by_NID(b, nid, -1);
  935. if (i >= 0) {
  936. if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
  937. return 0;
  938. extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
  939. } else
  940. extb = NULL;
  941. if (!exta && !extb)
  942. return 1;
  943. if (!exta || !extb)
  944. return 0;
  945. if (ASN1_OCTET_STRING_cmp(exta, extb))
  946. return 0;
  947. return 1;
  948. }
  949. /* See if a base and delta are compatible */
  950. static int check_delta_base(X509_CRL *delta, X509_CRL *base)
  951. {
  952. /* Delta CRL must be a delta */
  953. if (!delta->base_crl_number)
  954. return 0;
  955. /* Base must have a CRL number */
  956. if (!base->crl_number)
  957. return 0;
  958. /* Issuer names must match */
  959. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
  960. return 0;
  961. /* AKID and IDP must match */
  962. if (!crl_extension_match(delta, base, NID_authority_key_identifier))
  963. return 0;
  964. if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
  965. return 0;
  966. /* Delta CRL base number must not exceed Full CRL number. */
  967. if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
  968. return 0;
  969. /* Delta CRL number must exceed full CRL number */
  970. if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
  971. return 1;
  972. return 0;
  973. }
  974. /*
  975. * For a given base CRL find a delta... maybe extend to delta scoring or
  976. * retrieve a chain of deltas...
  977. */
  978. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
  979. X509_CRL *base, STACK_OF(X509_CRL) *crls)
  980. {
  981. X509_CRL *delta;
  982. size_t i;
  983. if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
  984. return;
  985. if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
  986. return;
  987. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  988. delta = sk_X509_CRL_value(crls, i);
  989. if (check_delta_base(delta, base)) {
  990. if (check_crl_time(ctx, delta, 0))
  991. *pscore |= CRL_SCORE_TIME_DELTA;
  992. X509_CRL_up_ref(delta);
  993. *dcrl = delta;
  994. return;
  995. }
  996. }
  997. *dcrl = NULL;
  998. }
  999. /*
  1000. * For a given CRL return how suitable it is for the supplied certificate
  1001. * 'x'. The return value is a mask of several criteria. If the issuer is not
  1002. * the certificate issuer this is returned in *pissuer. The reasons mask is
  1003. * also used to determine if the CRL is suitable: if no new reasons the CRL
  1004. * is rejected, otherwise reasons is updated.
  1005. */
  1006. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  1007. unsigned int *preasons, X509_CRL *crl, X509 *x)
  1008. {
  1009. int crl_score = 0;
  1010. unsigned int tmp_reasons = *preasons, crl_reasons;
  1011. /* First see if we can reject CRL straight away */
  1012. /* Invalid IDP cannot be processed */
  1013. if (crl->idp_flags & IDP_INVALID)
  1014. return 0;
  1015. /* Reason codes or indirect CRLs need extended CRL support */
  1016. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
  1017. if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
  1018. return 0;
  1019. } else if (crl->idp_flags & IDP_REASONS) {
  1020. /* If no new reasons reject */
  1021. if (!(crl->idp_reasons & ~tmp_reasons))
  1022. return 0;
  1023. }
  1024. /* Don't process deltas at this stage */
  1025. else if (crl->base_crl_number)
  1026. return 0;
  1027. /* If issuer name doesn't match certificate need indirect CRL */
  1028. if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
  1029. if (!(crl->idp_flags & IDP_INDIRECT))
  1030. return 0;
  1031. } else
  1032. crl_score |= CRL_SCORE_ISSUER_NAME;
  1033. if (!(crl->flags & EXFLAG_CRITICAL))
  1034. crl_score |= CRL_SCORE_NOCRITICAL;
  1035. /* Check expiry */
  1036. if (check_crl_time(ctx, crl, 0))
  1037. crl_score |= CRL_SCORE_TIME;
  1038. /* Check authority key ID and locate certificate issuer */
  1039. crl_akid_check(ctx, crl, pissuer, &crl_score);
  1040. /* If we can't locate certificate issuer at this point forget it */
  1041. if (!(crl_score & CRL_SCORE_AKID))
  1042. return 0;
  1043. /* Check cert for matching CRL distribution points */
  1044. if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
  1045. /* If no new reasons reject */
  1046. if (!(crl_reasons & ~tmp_reasons))
  1047. return 0;
  1048. tmp_reasons |= crl_reasons;
  1049. crl_score |= CRL_SCORE_SCOPE;
  1050. }
  1051. *preasons = tmp_reasons;
  1052. return crl_score;
  1053. }
  1054. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  1055. X509 **pissuer, int *pcrl_score)
  1056. {
  1057. X509 *crl_issuer = NULL;
  1058. X509_NAME *cnm = X509_CRL_get_issuer(crl);
  1059. int cidx = ctx->error_depth;
  1060. size_t i;
  1061. if ((size_t)cidx != sk_X509_num(ctx->chain) - 1)
  1062. cidx++;
  1063. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1064. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1065. if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
  1066. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
  1067. *pissuer = crl_issuer;
  1068. return;
  1069. }
  1070. }
  1071. for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
  1072. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1073. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1074. continue;
  1075. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1076. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
  1077. *pissuer = crl_issuer;
  1078. return;
  1079. }
  1080. }
  1081. /* Anything else needs extended CRL support */
  1082. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  1083. return;
  1084. /*
  1085. * Otherwise the CRL issuer is not on the path. Look for it in the set of
  1086. * untrusted certificates.
  1087. */
  1088. for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
  1089. crl_issuer = sk_X509_value(ctx->untrusted, i);
  1090. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1091. continue;
  1092. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1093. *pissuer = crl_issuer;
  1094. *pcrl_score |= CRL_SCORE_AKID;
  1095. return;
  1096. }
  1097. }
  1098. }
  1099. /*
  1100. * Check the path of a CRL issuer certificate. This creates a new
  1101. * X509_STORE_CTX and populates it with most of the parameters from the
  1102. * parent. This could be optimised somewhat since a lot of path checking will
  1103. * be duplicated by the parent, but this will rarely be used in practice.
  1104. */
  1105. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
  1106. {
  1107. X509_STORE_CTX crl_ctx;
  1108. int ret;
  1109. /* Don't allow recursive CRL path validation */
  1110. if (ctx->parent)
  1111. return 0;
  1112. if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
  1113. return -1;
  1114. crl_ctx.crls = ctx->crls;
  1115. /* Copy verify params across */
  1116. X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
  1117. crl_ctx.parent = ctx;
  1118. crl_ctx.verify_cb = ctx->verify_cb;
  1119. /* Verify CRL issuer */
  1120. ret = X509_verify_cert(&crl_ctx);
  1121. if (ret <= 0)
  1122. goto err;
  1123. /* Check chain is acceptable */
  1124. ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  1125. err:
  1126. X509_STORE_CTX_cleanup(&crl_ctx);
  1127. return ret;
  1128. }
  1129. /*
  1130. * RFC3280 says nothing about the relationship between CRL path and
  1131. * certificate path, which could lead to situations where a certificate could
  1132. * be revoked or validated by a CA not authorised to do so. RFC5280 is more
  1133. * strict and states that the two paths must end in the same trust anchor,
  1134. * though some discussions remain... until this is resolved we use the
  1135. * RFC5280 version
  1136. */
  1137. static int check_crl_chain(X509_STORE_CTX *ctx,
  1138. STACK_OF(X509) *cert_path,
  1139. STACK_OF(X509) *crl_path)
  1140. {
  1141. X509 *cert_ta, *crl_ta;
  1142. cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
  1143. crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
  1144. if (!X509_cmp(cert_ta, crl_ta))
  1145. return 1;
  1146. return 0;
  1147. }
  1148. /*
  1149. * Check for match between two dist point names: three separate cases. 1.
  1150. * Both are relative names and compare X509_NAME types. 2. One full, one
  1151. * relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
  1152. * compare two GENERAL_NAMES. 4. One is NULL: automatic match.
  1153. */
  1154. static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
  1155. {
  1156. X509_NAME *nm = NULL;
  1157. GENERAL_NAMES *gens = NULL;
  1158. GENERAL_NAME *gena, *genb;
  1159. size_t i, j;
  1160. if (!a || !b)
  1161. return 1;
  1162. if (a->type == 1) {
  1163. if (!a->dpname)
  1164. return 0;
  1165. /* Case 1: two X509_NAME */
  1166. if (b->type == 1) {
  1167. if (!b->dpname)
  1168. return 0;
  1169. if (!X509_NAME_cmp(a->dpname, b->dpname))
  1170. return 1;
  1171. else
  1172. return 0;
  1173. }
  1174. /* Case 2: set name and GENERAL_NAMES appropriately */
  1175. nm = a->dpname;
  1176. gens = b->name.fullname;
  1177. } else if (b->type == 1) {
  1178. if (!b->dpname)
  1179. return 0;
  1180. /* Case 2: set name and GENERAL_NAMES appropriately */
  1181. gens = a->name.fullname;
  1182. nm = b->dpname;
  1183. }
  1184. /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
  1185. if (nm) {
  1186. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  1187. gena = sk_GENERAL_NAME_value(gens, i);
  1188. if (gena->type != GEN_DIRNAME)
  1189. continue;
  1190. if (!X509_NAME_cmp(nm, gena->d.directoryName))
  1191. return 1;
  1192. }
  1193. return 0;
  1194. }
  1195. /* Else case 3: two GENERAL_NAMES */
  1196. for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
  1197. gena = sk_GENERAL_NAME_value(a->name.fullname, i);
  1198. for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
  1199. genb = sk_GENERAL_NAME_value(b->name.fullname, j);
  1200. if (!GENERAL_NAME_cmp(gena, genb))
  1201. return 1;
  1202. }
  1203. }
  1204. return 0;
  1205. }
  1206. static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
  1207. {
  1208. size_t i;
  1209. X509_NAME *nm = X509_CRL_get_issuer(crl);
  1210. /* If no CRLissuer return is successful iff don't need a match */
  1211. if (!dp->CRLissuer)
  1212. return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
  1213. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  1214. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  1215. if (gen->type != GEN_DIRNAME)
  1216. continue;
  1217. if (!X509_NAME_cmp(gen->d.directoryName, nm))
  1218. return 1;
  1219. }
  1220. return 0;
  1221. }
  1222. /* Check CRLDP and IDP */
  1223. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  1224. unsigned int *preasons)
  1225. {
  1226. size_t i;
  1227. if (crl->idp_flags & IDP_ONLYATTR)
  1228. return 0;
  1229. if (x->ex_flags & EXFLAG_CA) {
  1230. if (crl->idp_flags & IDP_ONLYUSER)
  1231. return 0;
  1232. } else {
  1233. if (crl->idp_flags & IDP_ONLYCA)
  1234. return 0;
  1235. }
  1236. *preasons = crl->idp_reasons;
  1237. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
  1238. DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
  1239. if (crldp_check_crlissuer(dp, crl, crl_score)) {
  1240. if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
  1241. *preasons &= dp->dp_reasons;
  1242. return 1;
  1243. }
  1244. }
  1245. }
  1246. if ((!crl->idp || !crl->idp->distpoint)
  1247. && (crl_score & CRL_SCORE_ISSUER_NAME))
  1248. return 1;
  1249. return 0;
  1250. }
  1251. /*
  1252. * Retrieve CRL corresponding to current certificate. If deltas enabled try
  1253. * to find a delta CRL too
  1254. */
  1255. static int get_crl_delta(X509_STORE_CTX *ctx,
  1256. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
  1257. {
  1258. int ok;
  1259. X509 *issuer = NULL;
  1260. int crl_score = 0;
  1261. unsigned int reasons;
  1262. X509_CRL *crl = NULL, *dcrl = NULL;
  1263. STACK_OF(X509_CRL) *skcrl;
  1264. X509_NAME *nm = X509_get_issuer_name(x);
  1265. reasons = ctx->current_reasons;
  1266. ok = get_crl_sk(ctx, &crl, &dcrl,
  1267. &issuer, &crl_score, &reasons, ctx->crls);
  1268. if (ok)
  1269. goto done;
  1270. /* Lookup CRLs from store */
  1271. skcrl = ctx->lookup_crls(ctx, nm);
  1272. /* If no CRLs found and a near match from get_crl_sk use that */
  1273. if (!skcrl && crl)
  1274. goto done;
  1275. get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
  1276. sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
  1277. done:
  1278. /* If we got any kind of CRL use it and return success */
  1279. if (crl) {
  1280. ctx->current_issuer = issuer;
  1281. ctx->current_crl_score = crl_score;
  1282. ctx->current_reasons = reasons;
  1283. *pcrl = crl;
  1284. *pdcrl = dcrl;
  1285. return 1;
  1286. }
  1287. return 0;
  1288. }
  1289. /* Check CRL validity */
  1290. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  1291. {
  1292. X509 *issuer = NULL;
  1293. EVP_PKEY *ikey = NULL;
  1294. int ok = 0, chnum, cnum;
  1295. cnum = ctx->error_depth;
  1296. chnum = sk_X509_num(ctx->chain) - 1;
  1297. /* if we have an alternative CRL issuer cert use that */
  1298. if (ctx->current_issuer)
  1299. issuer = ctx->current_issuer;
  1300. /*
  1301. * Else find CRL issuer: if not last certificate then issuer is next
  1302. * certificate in chain.
  1303. */
  1304. else if (cnum < chnum)
  1305. issuer = sk_X509_value(ctx->chain, cnum + 1);
  1306. else {
  1307. issuer = sk_X509_value(ctx->chain, chnum);
  1308. /* If not self signed, can't check signature */
  1309. if (!ctx->check_issued(ctx, issuer, issuer)) {
  1310. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  1311. ok = ctx->verify_cb(0, ctx);
  1312. if (!ok)
  1313. goto err;
  1314. }
  1315. }
  1316. if (issuer) {
  1317. /*
  1318. * Skip most tests for deltas because they have already been done
  1319. */
  1320. if (!crl->base_crl_number) {
  1321. /* Check for cRLSign bit if keyUsage present */
  1322. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  1323. !(issuer->ex_kusage & KU_CRL_SIGN)) {
  1324. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  1325. ok = ctx->verify_cb(0, ctx);
  1326. if (!ok)
  1327. goto err;
  1328. }
  1329. if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
  1330. ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
  1331. ok = ctx->verify_cb(0, ctx);
  1332. if (!ok)
  1333. goto err;
  1334. }
  1335. if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
  1336. if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
  1337. ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
  1338. ok = ctx->verify_cb(0, ctx);
  1339. if (!ok)
  1340. goto err;
  1341. }
  1342. }
  1343. if (crl->idp_flags & IDP_INVALID) {
  1344. ctx->error = X509_V_ERR_INVALID_EXTENSION;
  1345. ok = ctx->verify_cb(0, ctx);
  1346. if (!ok)
  1347. goto err;
  1348. }
  1349. }
  1350. if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
  1351. ok = check_crl_time(ctx, crl, 1);
  1352. if (!ok)
  1353. goto err;
  1354. }
  1355. /* Attempt to get issuer certificate public key */
  1356. ikey = X509_get_pubkey(issuer);
  1357. if (!ikey) {
  1358. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1359. ok = ctx->verify_cb(0, ctx);
  1360. if (!ok)
  1361. goto err;
  1362. } else {
  1363. int rv;
  1364. rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
  1365. if (rv != X509_V_OK) {
  1366. ctx->error = rv;
  1367. ok = ctx->verify_cb(0, ctx);
  1368. if (!ok)
  1369. goto err;
  1370. }
  1371. /* Verify CRL signature */
  1372. if (X509_CRL_verify(crl, ikey) <= 0) {
  1373. ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
  1374. ok = ctx->verify_cb(0, ctx);
  1375. if (!ok)
  1376. goto err;
  1377. }
  1378. }
  1379. }
  1380. ok = 1;
  1381. err:
  1382. EVP_PKEY_free(ikey);
  1383. return ok;
  1384. }
  1385. /* Check certificate against CRL */
  1386. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  1387. {
  1388. int ok;
  1389. X509_REVOKED *rev;
  1390. /*
  1391. * The rules changed for this... previously if a CRL contained unhandled
  1392. * critical extensions it could still be used to indicate a certificate
  1393. * was revoked. This has since been changed since critical extension can
  1394. * change the meaning of CRL entries.
  1395. */
  1396. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  1397. && (crl->flags & EXFLAG_CRITICAL)) {
  1398. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  1399. ok = ctx->verify_cb(0, ctx);
  1400. if (!ok)
  1401. return 0;
  1402. }
  1403. /*
  1404. * Look for serial number of certificate in CRL If found make sure reason
  1405. * is not removeFromCRL.
  1406. */
  1407. if (X509_CRL_get0_by_cert(crl, &rev, x)) {
  1408. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  1409. return 2;
  1410. ctx->error = X509_V_ERR_CERT_REVOKED;
  1411. ok = ctx->verify_cb(0, ctx);
  1412. if (!ok)
  1413. return 0;
  1414. }
  1415. return 1;
  1416. }
  1417. static int check_policy(X509_STORE_CTX *ctx)
  1418. {
  1419. int ret;
  1420. if (ctx->parent)
  1421. return 1;
  1422. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  1423. ctx->param->policies, ctx->param->flags);
  1424. if (ret == 0) {
  1425. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1426. return 0;
  1427. }
  1428. /* Invalid or inconsistent extensions */
  1429. if (ret == -1) {
  1430. /*
  1431. * Locate certificates with bad extensions and notify callback.
  1432. */
  1433. X509 *x;
  1434. size_t i;
  1435. for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  1436. x = sk_X509_value(ctx->chain, i);
  1437. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  1438. continue;
  1439. ctx->current_cert = x;
  1440. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  1441. if (!ctx->verify_cb(0, ctx))
  1442. return 0;
  1443. }
  1444. return 1;
  1445. }
  1446. if (ret == -2) {
  1447. ctx->current_cert = NULL;
  1448. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  1449. return ctx->verify_cb(0, ctx);
  1450. }
  1451. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
  1452. ctx->current_cert = NULL;
  1453. ctx->error = X509_V_OK;
  1454. if (!ctx->verify_cb(2, ctx))
  1455. return 0;
  1456. }
  1457. return 1;
  1458. }
  1459. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  1460. {
  1461. time_t *ptime;
  1462. int i;
  1463. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  1464. ptime = &ctx->param->check_time;
  1465. else
  1466. ptime = NULL;
  1467. i = X509_cmp_time(X509_get_notBefore(x), ptime);
  1468. if (i == 0) {
  1469. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  1470. ctx->current_cert = x;
  1471. if (!ctx->verify_cb(0, ctx))
  1472. return 0;
  1473. }
  1474. if (i > 0) {
  1475. ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
  1476. ctx->current_cert = x;
  1477. if (!ctx->verify_cb(0, ctx))
  1478. return 0;
  1479. }
  1480. i = X509_cmp_time(X509_get_notAfter(x), ptime);
  1481. if (i == 0) {
  1482. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  1483. ctx->current_cert = x;
  1484. if (!ctx->verify_cb(0, ctx))
  1485. return 0;
  1486. }
  1487. if (i < 0) {
  1488. ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
  1489. ctx->current_cert = x;
  1490. if (!ctx->verify_cb(0, ctx))
  1491. return 0;
  1492. }
  1493. return 1;
  1494. }
  1495. static int internal_verify(X509_STORE_CTX *ctx)
  1496. {
  1497. int ok = 0, n;
  1498. X509 *xs, *xi;
  1499. EVP_PKEY *pkey = NULL;
  1500. int (*cb) (int xok, X509_STORE_CTX *xctx);
  1501. cb = ctx->verify_cb;
  1502. n = sk_X509_num(ctx->chain);
  1503. ctx->error_depth = n - 1;
  1504. n--;
  1505. xi = sk_X509_value(ctx->chain, n);
  1506. if (ctx->check_issued(ctx, xi, xi))
  1507. xs = xi;
  1508. else {
  1509. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  1510. xs = xi;
  1511. goto check_cert;
  1512. }
  1513. if (n <= 0) {
  1514. ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  1515. ctx->current_cert = xi;
  1516. ok = cb(0, ctx);
  1517. goto end;
  1518. } else {
  1519. n--;
  1520. ctx->error_depth = n;
  1521. xs = sk_X509_value(ctx->chain, n);
  1522. }
  1523. }
  1524. /* ctx->error=0; not needed */
  1525. while (n >= 0) {
  1526. ctx->error_depth = n;
  1527. /*
  1528. * Skip signature check for self signed certificates unless
  1529. * explicitly asked for. It doesn't add any security and just wastes
  1530. * time.
  1531. */
  1532. if (!xs->valid
  1533. && (xs != xi
  1534. || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
  1535. if ((pkey = X509_get_pubkey(xi)) == NULL) {
  1536. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1537. ctx->current_cert = xi;
  1538. ok = (*cb) (0, ctx);
  1539. if (!ok)
  1540. goto end;
  1541. } else if (X509_verify(xs, pkey) <= 0) {
  1542. ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
  1543. ctx->current_cert = xs;
  1544. ok = (*cb) (0, ctx);
  1545. if (!ok) {
  1546. EVP_PKEY_free(pkey);
  1547. goto end;
  1548. }
  1549. }
  1550. EVP_PKEY_free(pkey);
  1551. pkey = NULL;
  1552. }
  1553. xs->valid = 1;
  1554. check_cert:
  1555. ok = check_cert_time(ctx, xs);
  1556. if (!ok)
  1557. goto end;
  1558. /* The last error (if any) is still in the error value */
  1559. ctx->current_issuer = xi;
  1560. ctx->current_cert = xs;
  1561. ok = (*cb) (1, ctx);
  1562. if (!ok)
  1563. goto end;
  1564. n--;
  1565. if (n >= 0) {
  1566. xi = xs;
  1567. xs = sk_X509_value(ctx->chain, n);
  1568. }
  1569. }
  1570. ok = 1;
  1571. end:
  1572. return ok;
  1573. }
  1574. int X509_cmp_current_time(const ASN1_TIME *ctm)
  1575. {
  1576. return X509_cmp_time(ctm, NULL);
  1577. }
  1578. int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
  1579. {
  1580. char *str;
  1581. ASN1_TIME atm;
  1582. long offset;
  1583. char buff1[24], buff2[24], *p;
  1584. int i, j, remaining;
  1585. p = buff1;
  1586. remaining = ctm->length;
  1587. str = (char *)ctm->data;
  1588. /*
  1589. * Note that the following (historical) code allows much more slack in
  1590. * the time format than RFC5280. In RFC5280, the representation is fixed:
  1591. * UTCTime: YYMMDDHHMMSSZ GeneralizedTime: YYYYMMDDHHMMSSZ
  1592. */
  1593. if (ctm->type == V_ASN1_UTCTIME) {
  1594. /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
  1595. int min_length = sizeof("YYMMDDHHMMZ") - 1;
  1596. int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
  1597. if (remaining < min_length || remaining > max_length)
  1598. return 0;
  1599. memcpy(p, str, 10);
  1600. p += 10;
  1601. str += 10;
  1602. remaining -= 10;
  1603. } else {
  1604. /*
  1605. * YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm
  1606. */
  1607. int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
  1608. int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
  1609. if (remaining < min_length || remaining > max_length)
  1610. return 0;
  1611. memcpy(p, str, 12);
  1612. p += 12;
  1613. str += 12;
  1614. remaining -= 12;
  1615. }
  1616. if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
  1617. *(p++) = '0';
  1618. *(p++) = '0';
  1619. } else {
  1620. /* SS (seconds) */
  1621. if (remaining < 2)
  1622. return 0;
  1623. *(p++) = *(str++);
  1624. *(p++) = *(str++);
  1625. remaining -= 2;
  1626. /*
  1627. * Skip any (up to three) fractional seconds... TODO(emilia): in
  1628. * RFC5280, fractional seconds are forbidden. Can we just kill them
  1629. * altogether?
  1630. */
  1631. if (remaining && *str == '.') {
  1632. str++;
  1633. remaining--;
  1634. for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
  1635. if (*str < '0' || *str > '9')
  1636. break;
  1637. }
  1638. }
  1639. }
  1640. *(p++) = 'Z';
  1641. *(p++) = '\0';
  1642. /* We now need either a terminating 'Z' or an offset. */
  1643. if (!remaining)
  1644. return 0;
  1645. if (*str == 'Z') {
  1646. if (remaining != 1)
  1647. return 0;
  1648. offset = 0;
  1649. } else {
  1650. /* (+-)HHMM */
  1651. if ((*str != '+') && (*str != '-'))
  1652. return 0;
  1653. /*
  1654. * Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280.
  1655. */
  1656. if (remaining != 5)
  1657. return 0;
  1658. if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
  1659. str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
  1660. return 0;
  1661. offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
  1662. offset += (str[3] - '0') * 10 + (str[4] - '0');
  1663. if (*str == '-')
  1664. offset = -offset;
  1665. }
  1666. atm.type = ctm->type;
  1667. atm.flags = 0;
  1668. atm.length = sizeof(buff2);
  1669. atm.data = (unsigned char *)buff2;
  1670. if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
  1671. return 0;
  1672. if (ctm->type == V_ASN1_UTCTIME) {
  1673. i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
  1674. if (i < 50)
  1675. i += 100; /* cf. RFC 2459 */
  1676. j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
  1677. if (j < 50)
  1678. j += 100;
  1679. if (i < j)
  1680. return -1;
  1681. if (i > j)
  1682. return 1;
  1683. }
  1684. i = strcmp(buff1, buff2);
  1685. if (i == 0) /* wait a second then return younger :-) */
  1686. return -1;
  1687. else
  1688. return i;
  1689. }
  1690. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1691. {
  1692. return X509_time_adj(s, adj, NULL);
  1693. }
  1694. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
  1695. {
  1696. return X509_time_adj_ex(s, 0, offset_sec, in_tm);
  1697. }
  1698. ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
  1699. int offset_day, long offset_sec, time_t *in_tm)
  1700. {
  1701. time_t t = 0;
  1702. if (in_tm)
  1703. t = *in_tm;
  1704. else
  1705. time(&t);
  1706. if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
  1707. if (s->type == V_ASN1_UTCTIME)
  1708. return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
  1709. if (s->type == V_ASN1_GENERALIZEDTIME)
  1710. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
  1711. }
  1712. return ASN1_TIME_adj(s, t, offset_day, offset_sec);
  1713. }
  1714. /* Make a delta CRL as the diff between two full CRLs */
  1715. X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
  1716. EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
  1717. {
  1718. X509_CRL *crl = NULL;
  1719. int i;
  1720. size_t j;
  1721. STACK_OF(X509_REVOKED) *revs = NULL;
  1722. /* CRLs can't be delta already */
  1723. if (base->base_crl_number || newer->base_crl_number) {
  1724. OPENSSL_PUT_ERROR(X509, X509_R_CRL_ALREADY_DELTA);
  1725. return NULL;
  1726. }
  1727. /* Base and new CRL must have a CRL number */
  1728. if (!base->crl_number || !newer->crl_number) {
  1729. OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_NUMBER);
  1730. return NULL;
  1731. }
  1732. /* Issuer names must match */
  1733. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
  1734. OPENSSL_PUT_ERROR(X509, X509_R_ISSUER_MISMATCH);
  1735. return NULL;
  1736. }
  1737. /* AKID and IDP must match */
  1738. if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
  1739. OPENSSL_PUT_ERROR(X509, X509_R_AKID_MISMATCH);
  1740. return NULL;
  1741. }
  1742. if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
  1743. OPENSSL_PUT_ERROR(X509, X509_R_IDP_MISMATCH);
  1744. return NULL;
  1745. }
  1746. /* Newer CRL number must exceed full CRL number */
  1747. if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
  1748. OPENSSL_PUT_ERROR(X509, X509_R_NEWER_CRL_NOT_NEWER);
  1749. return NULL;
  1750. }
  1751. /* CRLs must verify */
  1752. if (skey && (X509_CRL_verify(base, skey) <= 0 ||
  1753. X509_CRL_verify(newer, skey) <= 0)) {
  1754. OPENSSL_PUT_ERROR(X509, X509_R_CRL_VERIFY_FAILURE);
  1755. return NULL;
  1756. }
  1757. /* Create new CRL */
  1758. crl = X509_CRL_new();
  1759. if (!crl || !X509_CRL_set_version(crl, 1))
  1760. goto memerr;
  1761. /* Set issuer name */
  1762. if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
  1763. goto memerr;
  1764. if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
  1765. goto memerr;
  1766. if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
  1767. goto memerr;
  1768. /* Set base CRL number: must be critical */
  1769. if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
  1770. goto memerr;
  1771. /*
  1772. * Copy extensions across from newest CRL to delta: this will set CRL
  1773. * number to correct value too.
  1774. */
  1775. for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
  1776. X509_EXTENSION *ext;
  1777. ext = X509_CRL_get_ext(newer, i);
  1778. if (!X509_CRL_add_ext(crl, ext, -1))
  1779. goto memerr;
  1780. }
  1781. /* Go through revoked entries, copying as needed */
  1782. revs = X509_CRL_get_REVOKED(newer);
  1783. for (j = 0; j < sk_X509_REVOKED_num(revs); j++) {
  1784. X509_REVOKED *rvn, *rvtmp;
  1785. rvn = sk_X509_REVOKED_value(revs, j);
  1786. /*
  1787. * Add only if not also in base. TODO: need something cleverer here
  1788. * for some more complex CRLs covering multiple CAs.
  1789. */
  1790. if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
  1791. rvtmp = X509_REVOKED_dup(rvn);
  1792. if (!rvtmp)
  1793. goto memerr;
  1794. if (!X509_CRL_add0_revoked(crl, rvtmp)) {
  1795. X509_REVOKED_free(rvtmp);
  1796. goto memerr;
  1797. }
  1798. }
  1799. }
  1800. /* TODO: optionally prune deleted entries */
  1801. if (skey && md && !X509_CRL_sign(crl, skey, md))
  1802. goto memerr;
  1803. return crl;
  1804. memerr:
  1805. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1806. if (crl)
  1807. X509_CRL_free(crl);
  1808. return NULL;
  1809. }
  1810. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
  1811. CRYPTO_EX_unused * unused,
  1812. CRYPTO_EX_dup *dup_func,
  1813. CRYPTO_EX_free *free_func)
  1814. {
  1815. /*
  1816. * This function is (usually) called only once, by
  1817. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
  1818. */
  1819. int index;
  1820. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  1821. dup_func, free_func)) {
  1822. return -1;
  1823. }
  1824. return index;
  1825. }
  1826. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1827. {
  1828. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1829. }
  1830. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1831. {
  1832. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1833. }
  1834. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1835. {
  1836. return ctx->error;
  1837. }
  1838. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1839. {
  1840. ctx->error = err;
  1841. }
  1842. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1843. {
  1844. return ctx->error_depth;
  1845. }
  1846. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1847. {
  1848. return ctx->current_cert;
  1849. }
  1850. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1851. {
  1852. return ctx->chain;
  1853. }
  1854. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1855. {
  1856. if (!ctx->chain)
  1857. return NULL;
  1858. return X509_chain_up_ref(ctx->chain);
  1859. }
  1860. X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
  1861. {
  1862. return ctx->current_issuer;
  1863. }
  1864. X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
  1865. {
  1866. return ctx->current_crl;
  1867. }
  1868. X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
  1869. {
  1870. return ctx->parent;
  1871. }
  1872. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1873. {
  1874. ctx->cert = x;
  1875. }
  1876. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1877. {
  1878. ctx->untrusted = sk;
  1879. }
  1880. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1881. {
  1882. ctx->crls = sk;
  1883. }
  1884. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1885. {
  1886. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1887. }
  1888. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1889. {
  1890. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1891. }
  1892. /*
  1893. * This function is used to set the X509_STORE_CTX purpose and trust values.
  1894. * This is intended to be used when another structure has its own trust and
  1895. * purpose values which (if set) will be inherited by the ctx. If they aren't
  1896. * set then we will usually have a default purpose in mind which should then
  1897. * be used to set the trust value. An example of this is SSL use: an SSL
  1898. * structure will have its own purpose and trust settings which the
  1899. * application can set: if they aren't set then we use the default of SSL
  1900. * client/server.
  1901. */
  1902. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1903. int purpose, int trust)
  1904. {
  1905. int idx;
  1906. /* If purpose not set use default */
  1907. if (!purpose)
  1908. purpose = def_purpose;
  1909. /* If we have a purpose then check it is valid */
  1910. if (purpose) {
  1911. X509_PURPOSE *ptmp;
  1912. idx = X509_PURPOSE_get_by_id(purpose);
  1913. if (idx == -1) {
  1914. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
  1915. return 0;
  1916. }
  1917. ptmp = X509_PURPOSE_get0(idx);
  1918. if (ptmp->trust == X509_TRUST_DEFAULT) {
  1919. idx = X509_PURPOSE_get_by_id(def_purpose);
  1920. if (idx == -1) {
  1921. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
  1922. return 0;
  1923. }
  1924. ptmp = X509_PURPOSE_get0(idx);
  1925. }
  1926. /* If trust not set then get from purpose default */
  1927. if (!trust)
  1928. trust = ptmp->trust;
  1929. }
  1930. if (trust) {
  1931. idx = X509_TRUST_get_by_id(trust);
  1932. if (idx == -1) {
  1933. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
  1934. return 0;
  1935. }
  1936. }
  1937. if (purpose && !ctx->param->purpose)
  1938. ctx->param->purpose = purpose;
  1939. if (trust && !ctx->param->trust)
  1940. ctx->param->trust = trust;
  1941. return 1;
  1942. }
  1943. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1944. {
  1945. X509_STORE_CTX *ctx;
  1946. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1947. if (!ctx) {
  1948. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1949. return NULL;
  1950. }
  1951. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1952. return ctx;
  1953. }
  1954. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1955. {
  1956. if (ctx == NULL) {
  1957. return;
  1958. }
  1959. X509_STORE_CTX_cleanup(ctx);
  1960. OPENSSL_free(ctx);
  1961. }
  1962. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1963. STACK_OF(X509) *chain)
  1964. {
  1965. int ret = 1;
  1966. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1967. ctx->ctx = store;
  1968. ctx->cert = x509;
  1969. ctx->untrusted = chain;
  1970. CRYPTO_new_ex_data(&ctx->ex_data);
  1971. ctx->param = X509_VERIFY_PARAM_new();
  1972. if (!ctx->param)
  1973. goto err;
  1974. /*
  1975. * Inherit callbacks and flags from X509_STORE if not set use defaults.
  1976. */
  1977. if (store)
  1978. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  1979. else
  1980. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
  1981. if (store) {
  1982. ctx->verify_cb = store->verify_cb;
  1983. ctx->cleanup = store->cleanup;
  1984. } else
  1985. ctx->cleanup = 0;
  1986. if (ret)
  1987. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  1988. X509_VERIFY_PARAM_lookup("default"));
  1989. if (ret == 0)
  1990. goto err;
  1991. if (store && store->check_issued)
  1992. ctx->check_issued = store->check_issued;
  1993. else
  1994. ctx->check_issued = check_issued;
  1995. if (store && store->get_issuer)
  1996. ctx->get_issuer = store->get_issuer;
  1997. else
  1998. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  1999. if (store && store->verify_cb)
  2000. ctx->verify_cb = store->verify_cb;
  2001. else
  2002. ctx->verify_cb = null_callback;
  2003. if (store && store->verify)
  2004. ctx->verify = store->verify;
  2005. else
  2006. ctx->verify = internal_verify;
  2007. if (store && store->check_revocation)
  2008. ctx->check_revocation = store->check_revocation;
  2009. else
  2010. ctx->check_revocation = check_revocation;
  2011. if (store && store->get_crl)
  2012. ctx->get_crl = store->get_crl;
  2013. else
  2014. ctx->get_crl = NULL;
  2015. if (store && store->check_crl)
  2016. ctx->check_crl = store->check_crl;
  2017. else
  2018. ctx->check_crl = check_crl;
  2019. if (store && store->cert_crl)
  2020. ctx->cert_crl = store->cert_crl;
  2021. else
  2022. ctx->cert_crl = cert_crl;
  2023. if (store && store->lookup_certs)
  2024. ctx->lookup_certs = store->lookup_certs;
  2025. else
  2026. ctx->lookup_certs = X509_STORE_get1_certs;
  2027. if (store && store->lookup_crls)
  2028. ctx->lookup_crls = store->lookup_crls;
  2029. else
  2030. ctx->lookup_crls = X509_STORE_get1_crls;
  2031. ctx->check_policy = check_policy;
  2032. return 1;
  2033. err:
  2034. CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data);
  2035. if (ctx->param != NULL) {
  2036. X509_VERIFY_PARAM_free(ctx->param);
  2037. }
  2038. memset(ctx, 0, sizeof(X509_STORE_CTX));
  2039. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  2040. return 0;
  2041. }
  2042. /*
  2043. * Set alternative lookup method: just a STACK of trusted certificates. This
  2044. * avoids X509_STORE nastiness where it isn't needed.
  2045. */
  2046. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2047. {
  2048. ctx->other_ctx = sk;
  2049. ctx->get_issuer = get_issuer_sk;
  2050. }
  2051. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  2052. {
  2053. /* We need to be idempotent because, unfortunately, |X509_STORE_CTX_free|
  2054. * also calls this function. */
  2055. if (ctx->cleanup != NULL) {
  2056. ctx->cleanup(ctx);
  2057. ctx->cleanup = NULL;
  2058. }
  2059. if (ctx->param != NULL) {
  2060. if (ctx->parent == NULL)
  2061. X509_VERIFY_PARAM_free(ctx->param);
  2062. ctx->param = NULL;
  2063. }
  2064. if (ctx->tree != NULL) {
  2065. X509_policy_tree_free(ctx->tree);
  2066. ctx->tree = NULL;
  2067. }
  2068. if (ctx->chain != NULL) {
  2069. sk_X509_pop_free(ctx->chain, X509_free);
  2070. ctx->chain = NULL;
  2071. }
  2072. CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
  2073. memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
  2074. }
  2075. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  2076. {
  2077. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2078. }
  2079. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  2080. {
  2081. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  2082. }
  2083. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
  2084. time_t t)
  2085. {
  2086. X509_VERIFY_PARAM_set_time(ctx->param, t);
  2087. }
  2088. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  2089. int (*verify_cb) (int, X509_STORE_CTX *))
  2090. {
  2091. ctx->verify_cb = verify_cb;
  2092. }
  2093. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  2094. {
  2095. return ctx->tree;
  2096. }
  2097. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  2098. {
  2099. return ctx->explicit_policy;
  2100. }
  2101. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  2102. {
  2103. const X509_VERIFY_PARAM *param;
  2104. param = X509_VERIFY_PARAM_lookup(name);
  2105. if (!param)
  2106. return 0;
  2107. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  2108. }
  2109. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  2110. {
  2111. return ctx->param;
  2112. }
  2113. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  2114. {
  2115. if (ctx->param)
  2116. X509_VERIFY_PARAM_free(ctx->param);
  2117. ctx->param = param;
  2118. }
  2119. IMPLEMENT_ASN1_SET_OF(X509)
  2120. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)