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.
 
 
 
 
 
 

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