選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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