OCSPResourceReader.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.certificate.ocsp;

  21. import java.io.File;
  22. import java.io.InputStream;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.utils.Utilities;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.certificate.CRLCertstore;
  28. import org.openspcoop2.utils.certificate.KeyStore;
  29. import org.openspcoop2.utils.transport.http.ExternalResourceConfig;
  30. import org.openspcoop2.utils.transport.http.ExternalResourceUtils;

  31. /**
  32.  * OCSPResourceReader
  33.  * NOTA: implementazione di esempio senza cache
  34.  *
  35.  * @author Andrea Poli (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class OCSPResourceReader implements IOCSPResourceReader {

  40.     private OCSPConfig config;
  41.    
  42.     public OCSPResourceReader() {
  43.         // public
  44.     }
  45.    
  46.     private static final String OCSP_CONFIG_NULL = "OCSPConfig is null";
  47.    
  48.     @Override
  49.     public void initConfig(OCSPConfig config) throws UtilsException {
  50.         this.config = config;
  51.     }

  52.     @Override
  53.     public KeyStore getIssuerAlternativeTrustStore() throws UtilsException {
  54.         if(this.config==null) {
  55.             throw new UtilsException(OCSP_CONFIG_NULL);
  56.         }
  57.         if(this.config.getAlternativeTrustStoreCAPath()!=null) {
  58.             return newKeyStore(this.config.getAlternativeTrustStoreCAPath(),
  59.                     this.config.getAlternativeTrustStoreCAType(),
  60.                     this.config.getAlternativeTrustStoreCAPassword());
  61.         }
  62.         return null;
  63.     }

  64.     @Override
  65.     public void readExternalResource(String resource, Map<String, byte[]> holderResource) throws UtilsException {
  66.         if(this.config==null) {
  67.             throw new UtilsException(OCSP_CONFIG_NULL);
  68.         }
  69.         try {
  70.             ExternalResourceConfig externalConfig = new ExternalResourceConfig();
  71.            
  72.             externalConfig.setHostnameVerifier(this.config.isExternalResourcesHostnameVerifier());
  73.             externalConfig.setTrustAllCerts(this.config.isExternalResourcesTrustAllCerts());
  74.             if(this.config.getExternalResourcesTrustStorePath()!=null) {
  75.                 KeyStore ks = newKeyStore(this.config.getExternalResourcesTrustStorePath(),
  76.                         this.config.getExternalResourcesTrustStoreType(),
  77.                         this.config.getExternalResourcesTrustStorePassword());
  78.                 externalConfig.setTrustStore(ks.getKeystore());
  79.             }
  80.            
  81.             if(this.config.getExternalResourcesKeyStorePath()!=null) {
  82.                 KeyStore ks = newKeyStore(this.config.getExternalResourcesKeyStorePath(),
  83.                         this.config.getExternalResourcesKeyStoreType(),
  84.                         this.config.getExternalResourcesKeyStorePassword());
  85.                 externalConfig.setKeyStore(ks.getKeystore());
  86.                 externalConfig.setKeyAlias(this.config.getExternalResourcesKeyAlias());
  87.                 externalConfig.setKeyPassword(this.config.getExternalResourcesKeyPassword());
  88.             }
  89.            
  90.             externalConfig.setBasicUsername(this.config.getExternalResourcesUsername());
  91.             externalConfig.setBasicPassword(this.config.getExternalResourcesPassword());
  92.            
  93.             if(this.config.getForwardProxyUrl()!=null) {
  94.                 externalConfig.setForwardProxyUrl(this.config.getForwardProxyUrl());
  95.                 externalConfig.setForwardProxyHeader(this.config.getForwardProxyHeader());
  96.                 externalConfig.setForwardProxyQueryParameter(this.config.getForwardProxyQueryParameter());
  97.                 externalConfig.setForwardProxyBase64(this.config.isForwardProxyBase64());
  98.             }
  99.            
  100.             externalConfig.setConnectTimeout(this.config.getConnectTimeout());
  101.             externalConfig.setReadTimeout(this.config.getReadTimeout());
  102.            
  103.             byte [] r = ExternalResourceUtils.readResource(resource, externalConfig);
  104.             if(r!=null) {
  105.                 holderResource.put(resource, r);
  106.             }
  107.         }catch(Exception t) {
  108.             throw new UtilsException(t.getMessage(),t);
  109.         }
  110.     }

  111.     @Override
  112.     public CRLCertstore readCRL(List<String> crl, Map<String, byte[]> localResources) throws UtilsException {
  113.         if(this.config==null) {
  114.             throw new UtilsException(OCSP_CONFIG_NULL);
  115.         }
  116.         try {
  117.             return new CRLCertstore(crl, localResources);
  118.         }catch(Exception t) {
  119.             throw new UtilsException(t.getMessage(),t);
  120.         }
  121.     }

  122.     @Override
  123.     public KeyStore getSignerTrustStore() throws UtilsException {
  124.         if(this.config==null) {
  125.             throw new UtilsException(OCSP_CONFIG_NULL);
  126.         }
  127.         if(this.config.getTrustStoreSignerPath()!=null) {
  128.             return newKeyStore(this.config.getTrustStoreSignerPath(),
  129.                     this.config.getTrustStoreSignerType(),
  130.                     this.config.getTrustStoreSignerPassword());
  131.         }
  132.         return null;
  133.     }

  134.     @Override
  135.     public KeyStore getHttpsTrustStore() throws UtilsException {
  136.         if(this.config==null) {
  137.             throw new UtilsException(OCSP_CONFIG_NULL);
  138.         }
  139.         if(this.config.getExternalResourcesTrustStorePath()!=null) {
  140.             return newKeyStore(this.config.getExternalResourcesTrustStorePath(),
  141.                     this.config.getExternalResourcesTrustStoreType(),
  142.                     this.config.getExternalResourcesTrustStorePassword());
  143.         }
  144.         return null;
  145.     }
  146.    
  147.     @Override
  148.     public KeyStore getHttpsKeyStore() throws UtilsException {
  149.         if(this.config==null) {
  150.             throw new UtilsException(OCSP_CONFIG_NULL);
  151.         }
  152.         if(this.config.getExternalResourcesKeyStorePath()!=null) {
  153.             return newKeyStore(this.config.getExternalResourcesKeyStorePath(),
  154.                     this.config.getExternalResourcesKeyStoreType(),
  155.                     this.config.getExternalResourcesKeyStorePassword());
  156.         }
  157.         return null;
  158.     }

  159.     @Override
  160.     public KeyStore getCrlAlternativeTrustStore() throws UtilsException{
  161.         if(this.config==null) {
  162.             throw new UtilsException(OCSP_CONFIG_NULL);
  163.         }
  164.         if(this.config.getAlternativeTrustStoreCRLPath()!=null) {
  165.             return newKeyStore(this.config.getAlternativeTrustStoreCRLPath(),
  166.                     this.config.getAlternativeTrustStoreCRLType(),
  167.                     this.config.getAlternativeTrustStoreCRLPassword());
  168.         }
  169.         return null;
  170.     }
  171.    
  172.     private static KeyStore newKeyStore(String path, String type, String password) throws UtilsException {
  173.         File f = new File(path);
  174.         if(!f.exists()) {
  175.             try(InputStream is = OCSPResourceReader.class.getResourceAsStream(path)){
  176.                 if(is!=null) {
  177.                     byte[] content = Utilities.getAsByteArray(is);
  178.                     return new KeyStore(content, type,  password);
  179.                 }
  180.             }
  181.             catch(Exception t) {
  182.                 throw new UtilsException(t.getMessage(),t);
  183.             }
  184.         }
  185.         return new KeyStore(path, type, password);
  186.     }

  187. }