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.
 
 
 
 
 
 

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