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

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