TrustValidatorDecorator.java

  1. /*
  2.  * Java Trust Project.
  3.  * Copyright (C) 2011 FedICT.
  4.  * Copyright (C) 2014-2023 e-Contract.be BV.
  5.  *
  6.  * This is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU Lesser General Public License version
  8.  * 3.0 as published by the Free Software Foundation.
  9.  *
  10.  * This software is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this software; if not, see
  17.  * http://www.gnu.org/licenses/.
  18.  */

  19. package be.fedict.trust;

  20. import be.fedict.trust.crl.CachedCrlRepository;
  21. import be.fedict.trust.crl.CrlRepository;
  22. import be.fedict.trust.crl.CrlTrustLinker;
  23. import be.fedict.trust.crl.OnlineCrlRepository;
  24. import be.fedict.trust.ext.CriticalExtensionTrustLinker;
  25. import be.fedict.trust.linker.AlwaysTrustTrustLinker;
  26. import be.fedict.trust.linker.FallbackTrustLinker;
  27. import be.fedict.trust.linker.PublicKeyTrustLinker;
  28. import be.fedict.trust.linker.TrustLinker;
  29. import be.fedict.trust.ocsp.OcspTrustLinker;
  30. import be.fedict.trust.ocsp.OnlineOcspRepository;

  31. /**
  32.  * Trust Validator Decorator. This class helps to configure trust validators.
  33.  *
  34.  * @author Frank Cornelis
  35.  *
  36.  */
  37. public class TrustValidatorDecorator {

  38.     private final NetworkConfig networkConfig;

  39.     /**
  40.      * Main constructor.
  41.      *
  42.      * @param networkConfig the network configuration to be used. Can be
  43.      *                      <code>null</code> .
  44.      */
  45.     public TrustValidatorDecorator(NetworkConfig networkConfig) {
  46.         this.networkConfig = networkConfig;
  47.     }

  48.     /**
  49.      * Convenience constructor.
  50.      */
  51.     public TrustValidatorDecorator() {
  52.         this(null);
  53.     }

  54.     /**
  55.      * Adds a default trust linker configuration to a given trust validator.
  56.      *
  57.      * @param trustValidator      the trust validator to be configured.
  58.      * @param externalTrustLinker optional additional trust linker.
  59.      */
  60.     public void addDefaultTrustLinkerConfig(TrustValidator trustValidator, TrustLinker externalTrustLinker) {
  61.         addDefaultTrustLinkerConfig(trustValidator, externalTrustLinker, false);
  62.     }

  63.     /**
  64.      * Adds a default trust linker configuration to a given trust validator.
  65.      *
  66.      * @param trustValidator      the trust validator to be configured.
  67.      * @param externalTrustLinker optional additional trust linker.
  68.      * @param noOcsp              set to <code>true</code> to avoid OCSP validation.
  69.      */
  70.     public void addDefaultTrustLinkerConfig(TrustValidator trustValidator, TrustLinker externalTrustLinker,
  71.             boolean noOcsp) {
  72.         addDefaultTrustLinkerConfig(trustValidator, externalTrustLinker, noOcsp, null);
  73.     }

  74.     /**
  75.      * Adds a default trust linker configuration to a given trust validator.
  76.      *
  77.      * @param trustValidator      the trust validator to be configured.
  78.      * @param externalTrustLinker optional additional trust linker.
  79.      * @param noOcsp              set to <code>true</code> to avoid OCSP validation.
  80.      * @param crlRepository       the optional CRL repository to use.
  81.      */
  82.     public void addDefaultTrustLinkerConfig(TrustValidator trustValidator, TrustLinker externalTrustLinker,
  83.             boolean noOcsp, CrlRepository crlRepository) {
  84.         trustValidator.addTrustLinker(new PublicKeyTrustLinker());
  85.         trustValidator.addTrustLinker(new CriticalExtensionTrustLinker());

  86.         OnlineOcspRepository ocspRepository = new OnlineOcspRepository(this.networkConfig);

  87.         if (null == crlRepository) {
  88.             OnlineCrlRepository onlineCrlRepository = new OnlineCrlRepository(this.networkConfig);
  89.             crlRepository = new CachedCrlRepository(onlineCrlRepository);
  90.         }

  91.         FallbackTrustLinker fallbackTrustLinker = new FallbackTrustLinker();
  92.         if (null != externalTrustLinker) {
  93.             fallbackTrustLinker.addTrustLinker(externalTrustLinker);
  94.         }
  95.         if (false == noOcsp) {
  96.             fallbackTrustLinker.addTrustLinker(new OcspTrustLinker(ocspRepository));
  97.         }
  98.         fallbackTrustLinker.addTrustLinker(new CrlTrustLinker(crlRepository));

  99.         trustValidator.addTrustLinker(fallbackTrustLinker);
  100.     }

  101.     /**
  102.      * Adds a default trust linker configuration to a given trust validator.
  103.      *
  104.      * @param trustValidator the trust validator to be configured.
  105.      */
  106.     public void addDefaultTrustLinkerConfig(TrustValidator trustValidator) {
  107.         addDefaultTrustLinkerConfig(trustValidator, null);
  108.     }

  109.     /**
  110.      * Adds a trust linker configuration to be used to validate already expired
  111.      * certificates. Please notice that this configuration will not perform any
  112.      * verification on the revocation status of the certificates.
  113.      *
  114.      * @param trustValidator the trust validator to be configured.
  115.      */
  116.     public void addTrustLinkerConfigWithoutRevocationStatus(TrustValidator trustValidator) {
  117.         trustValidator.addTrustLinker(new PublicKeyTrustLinker(true));
  118.         trustValidator.addTrustLinker(new AlwaysTrustTrustLinker());
  119.         trustValidator.addTrustLinker(new CriticalExtensionTrustLinker());
  120.     }
  121. }