Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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