Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2425 Zeilen
71 KiB

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