GestoreOCSPResource.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.security.keystore.cache;

  21. import java.util.List;
  22. import java.util.Map;

  23. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  24. import org.openspcoop2.security.keystore.ExternalResource;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.certificate.CRLCertstore;
  27. import org.openspcoop2.utils.certificate.KeyStore;
  28. import org.openspcoop2.utils.certificate.ocsp.IOCSPResourceReader;
  29. import org.openspcoop2.utils.certificate.ocsp.OCSPConfig;
  30. import org.openspcoop2.utils.transport.http.ExternalResourceConfig;

  31. /**
  32.  * GestoreOCSPResource
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class GestoreOCSPResource implements IOCSPResourceReader {

  39.     private OCSPConfig config;
  40.     private RequestInfo requestInfo;
  41.    
  42.     public GestoreOCSPResource(RequestInfo requestInfo) {
  43.         this.requestInfo = requestInfo;
  44.     }
  45.    
  46.     @Override
  47.     public void initConfig(OCSPConfig config) throws UtilsException {
  48.         this.config = config;
  49.     }

  50.     private static final String OCSP_CONFIG_NULL = "OCSPConfig is null";
  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.             try {
  59.                 return GestoreKeystoreCache.getMerlinTruststore(this.requestInfo,
  60.                         this.config.getAlternativeTrustStoreCAPath(),
  61.                         this.config.getAlternativeTrustStoreCAType(),
  62.                         this.config.getAlternativeTrustStoreCAPassword()).getTrustStore();
  63.             }catch(Exception t) {
  64.                 throw new UtilsException(t.getMessage(),t);
  65.             }
  66.         }
  67.         return null;
  68.     }

  69.     @Override
  70.     public void readExternalResource(String resource, Map<String, byte[]> holderResource) throws UtilsException {
  71.         if(this.config==null) {
  72.             throw new UtilsException(OCSP_CONFIG_NULL);
  73.         }
  74.         try {
  75.             ExternalResourceConfig externalConfig = new ExternalResourceConfig();
  76.            
  77.             externalConfig.setHostnameVerifier(this.config.isExternalResourcesHostnameVerifier());
  78.             externalConfig.setTrustAllCerts(this.config.isExternalResourcesTrustAllCerts());
  79.             if(this.config.getExternalResourcesTrustStorePath()!=null) {
  80.                 try {
  81.                     externalConfig.setTrustStore(GestoreKeystoreCache.getMerlinTruststore(this.requestInfo,
  82.                             this.config.getExternalResourcesTrustStorePath(),
  83.                             this.config.getExternalResourcesTrustStoreType(),
  84.                             this.config.getExternalResourcesTrustStorePassword()).getTrustStore().getKeystore());
  85.                 }catch(Exception t) {
  86.                     throw new UtilsException(t.getMessage(),t);
  87.                 }
  88.             }
  89.            
  90.             if(this.config.getExternalResourcesKeyStorePath()!=null) {
  91.                 try {
  92.                     externalConfig.setKeyStore(GestoreKeystoreCache.getMerlinKeystore(this.requestInfo,
  93.                             this.config.getExternalResourcesKeyStorePath(),
  94.                             this.config.getExternalResourcesKeyStoreType(),
  95.                             this.config.getExternalResourcesKeyStorePassword()).getKeyStore().getKeystore());
  96.                 }catch(Exception t) {
  97.                     throw new UtilsException(t.getMessage(),t);
  98.                 }
  99.                 externalConfig.setKeyAlias(this.config.getExternalResourcesKeyAlias());
  100.                 externalConfig.setKeyPassword(this.config.getExternalResourcesKeyPassword());
  101.             }
  102.            
  103.             if(this.config.getForwardProxyUrl()!=null) {
  104.                 externalConfig.setForwardProxyUrl(this.config.getForwardProxyUrl());
  105.                 externalConfig.setForwardProxyHeader(this.config.getForwardProxyHeader());
  106.                 externalConfig.setForwardProxyQueryParameter(this.config.getForwardProxyQueryParameter());
  107.                 externalConfig.setForwardProxyBase64(this.config.isForwardProxyBase64());
  108.             }
  109.            
  110.             externalConfig.setConnectTimeout(this.config.getConnectTimeout());
  111.             externalConfig.setReadTimeout(this.config.getReadTimeout());
  112.            
  113.             ExternalResource externalResource = GestoreKeystoreCache.getExternalResource(this.requestInfo, resource, externalConfig);
  114.             if(externalResource!=null && externalResource.getId()!=null) {
  115.                 holderResource.put(externalResource.getId(), externalResource.getResource());
  116.             }
  117.         }catch(Exception t) {
  118.             throw new UtilsException(t.getMessage(),t);
  119.         }
  120.     }

  121.     @Override
  122.     public CRLCertstore readCRL(List<String> crl, Map<String, byte[]> localResources) throws UtilsException {
  123.         if(this.config==null) {
  124.             throw new UtilsException(OCSP_CONFIG_NULL);
  125.         }
  126.         try {
  127.             return GestoreKeystoreCache.getCRLCertstore(this.requestInfo, CRLCertstore.convertToCrlPaths(crl), localResources).getWrappedCRLCertStore();
  128.         }catch(Exception t) {
  129.             throw new UtilsException(t.getMessage(),t);
  130.         }
  131.     }

  132.     @Override
  133.     public KeyStore getSignerTrustStore() throws UtilsException {
  134.         if(this.config==null) {
  135.             throw new UtilsException(OCSP_CONFIG_NULL);
  136.         }
  137.         if(this.config.getTrustStoreSignerPath()!=null) {
  138.             try {
  139.                 return GestoreKeystoreCache.getMerlinTruststore(this.requestInfo,
  140.                         this.config.getTrustStoreSignerPath(),
  141.                         this.config.getTrustStoreSignerType(),
  142.                         this.config.getTrustStoreSignerPassword()).getTrustStore();
  143.             }catch(Exception t) {
  144.                 throw new UtilsException(t.getMessage(),t);
  145.             }
  146.         }
  147.         return null;
  148.     }

  149.     @Override
  150.     public KeyStore getHttpsTrustStore() throws UtilsException {
  151.         if(this.config==null) {
  152.             throw new UtilsException(OCSP_CONFIG_NULL);
  153.         }
  154.         if(this.config.getExternalResourcesTrustStorePath()!=null) {
  155.             try {
  156.                 return GestoreKeystoreCache.getMerlinTruststore(this.requestInfo,
  157.                         this.config.getExternalResourcesTrustStorePath(),
  158.                         this.config.getExternalResourcesTrustStoreType(),
  159.                         this.config.getExternalResourcesTrustStorePassword()).getTrustStore();
  160.             }catch(Exception t) {
  161.                 throw new UtilsException(t.getMessage(),t);
  162.             }
  163.         }
  164.         return null;
  165.     }
  166.    
  167.     @Override
  168.     public KeyStore getHttpsKeyStore() throws UtilsException {
  169.         if(this.config==null) {
  170.             throw new UtilsException(OCSP_CONFIG_NULL);
  171.         }
  172.         if(this.config.getExternalResourcesKeyStorePath()!=null) {
  173.             try {
  174.                 return GestoreKeystoreCache.getMerlinKeystore(this.requestInfo,
  175.                         this.config.getExternalResourcesKeyStorePath(),
  176.                         this.config.getExternalResourcesKeyStoreType(),
  177.                         this.config.getExternalResourcesKeyStorePassword()).getKeyStore();
  178.             }catch(Exception t) {
  179.                 throw new UtilsException(t.getMessage(),t);
  180.             }
  181.         }
  182.         return null;
  183.     }

  184.     @Override
  185.     public KeyStore getCrlAlternativeTrustStore() throws UtilsException{
  186.         if(this.config==null) {
  187.             throw new UtilsException(OCSP_CONFIG_NULL);
  188.         }
  189.         if(this.config.getAlternativeTrustStoreCRLPath()!=null) {
  190.             try {
  191.                 return GestoreKeystoreCache.getMerlinTruststore(this.requestInfo,
  192.                         this.config.getAlternativeTrustStoreCRLPath(),
  193.                         this.config.getAlternativeTrustStoreCRLType(),
  194.                         this.config.getAlternativeTrustStoreCRLPassword()).getTrustStore();
  195.             }catch(Exception t) {
  196.                 throw new UtilsException(t.getMessage(),t);
  197.             }
  198.         }
  199.         return null;
  200.     }
  201. }