Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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