SSLSocketFactory.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;

  21. import java.io.Serializable;
  22. import java.util.HashMap;

  23. import javax.net.ssl.SSLContext;

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  26. import org.openspcoop2.security.SecurityException;
  27. import org.openspcoop2.security.keystore.cache.GestoreKeystoreCache;
  28. import org.openspcoop2.security.keystore.cache.GestoreOCSPResource;
  29. import org.openspcoop2.security.keystore.cache.GestoreOCSPValidator;
  30. import org.openspcoop2.utils.certificate.KeyStore;
  31. import org.openspcoop2.utils.certificate.byok.BYOKProvider;
  32. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;
  33. import org.openspcoop2.utils.transport.http.IBYOKUnwrapManager;
  34. import org.openspcoop2.utils.transport.http.IOCSPValidator;
  35. import org.openspcoop2.utils.transport.http.SSLConfig;
  36. import org.openspcoop2.utils.transport.http.SSLUtilities;

  37. /**
  38.  * SSLSocketFactory
  39.  *
  40.  * @author Andrea Poli (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class SSLSocketFactory implements Serializable {

  45.     /**
  46.      *
  47.      */
  48.     private static final long serialVersionUID = 1L;
  49.    
  50.     private SSLConfig sslConfig;
  51.     private transient javax.net.ssl.SSLSocketFactory sslSocketFactoryObject;
  52.    

  53.     @Override
  54.     public String toString() {
  55.         StringBuilder bf = new StringBuilder();
  56.         bf.append("SSLContextFactory (").append(this.sslConfig).append(") ");
  57.         return bf.toString();
  58.     }
  59.    
  60.     public SSLSocketFactory(RequestInfo requestInfo, SSLConfig sslConfig) throws SecurityException{
  61.         this.sslConfig = sslConfig;
  62.         this.initFactory(requestInfo);
  63.     }
  64.    
  65.     private void checkInit(RequestInfo requestInfo) throws SecurityException{
  66.         if(this.sslSocketFactoryObject==null) {
  67.             this.initFactory(requestInfo);
  68.         }
  69.     }
  70.     private String getErrorMessage(String location, Exception e, boolean keystore) {
  71.         return "Lettura "+(keystore?"keystore":"truststore")+" '"+location+"' dalla cache fallita: "+e.getMessage();
  72.     }
  73.     private synchronized void initFactory(RequestInfo requestInfo) throws SecurityException{
  74.         if(this.sslSocketFactoryObject==null) {
  75.             try{
  76.                 // Gestione https
  77.                 if(this.sslConfig!=null){
  78.                    
  79.                     // provo a leggere i keystore dalla cache
  80.                     IBYOKUnwrapManager byokManager = null;
  81.                     if(this.sslConfig.getKeyStoreLocation()!=null) {
  82.                         BYOKRequestParams byokParams = null;
  83.                         if(BYOKProvider.isPolicyDefined(this.sslConfig.getKeyStoreBYOKPolicy())){
  84.                             byokParams = BYOKProvider.getBYOKRequestParamsByUnwrapBYOKPolicy(this.sslConfig.getKeyStoreBYOKPolicy(),
  85.                                     this.sslConfig.getDynamicMap()!=null ? this.sslConfig.getDynamicMap() : new HashMap<>() );
  86.                         }
  87.                         try {
  88.                             KeyStore keystore = GestoreKeystoreCache.getMerlinKeystore(requestInfo, this.sslConfig.getKeyStoreLocation(),
  89.                                     this.sslConfig.getKeyStoreType(), this.sslConfig.getKeyStorePassword(),
  90.                                     byokParams).getKeyStore();
  91.                             this.sslConfig.setKeyStore(keystore.getKeystore(), keystore.isKeystoreHsm());
  92.                         }catch(Exception e) {
  93.                             String msgError = getErrorMessage(this.sslConfig.getKeyStoreLocation(),e,true);
  94.                             this.sslConfig.getLoggerBuffer().error(msgError, e);
  95.                         }
  96.                         if(this.sslConfig.getKeyStore()==null && byokParams!=null) {
  97.                             // operazione precedente non riuscita
  98.                             byokManager = new BYOKUnwrapManager(this.sslConfig.getKeyStoreBYOKPolicy(), byokParams);
  99.                         }
  100.                     }
  101.                     if(this.sslConfig.getTrustStoreLocation()!=null) {
  102.                         try {
  103.                             KeyStore truststore = GestoreKeystoreCache.getMerlinTruststore(requestInfo, this.sslConfig.getTrustStoreLocation(),
  104.                                     this.sslConfig.getTrustStoreType(), this.sslConfig.getTrustStorePassword()).getTrustStore();
  105.                             this.sslConfig.setTrustStore(truststore.getKeystore(), truststore.isKeystoreHsm());
  106.                         }catch(Exception e) {
  107.                             String msgError = getErrorMessage(this.sslConfig.getTrustStoreLocation(),e,false);
  108.                             this.sslConfig.getLoggerBuffer().error(msgError, e);
  109.                         }
  110.                     }
  111.                    
  112.                     IOCSPValidator ocspValidator = null;
  113.                     boolean crlByOcsp = false;
  114.                     if(this.sslConfig.getTrustStoreOCSPPolicy()!=null){
  115.                         String policyType = this.sslConfig.getTrustStoreOCSPPolicy();
  116.                         if(policyType!=null && StringUtils.isNotEmpty(policyType)) {
  117.                             GestoreOCSPResource ocspResourceReader = new GestoreOCSPResource(requestInfo);
  118.                             String crlInputConfig = this.sslConfig.getTrustStoreCRLsLocation();
  119.                             ocspValidator = new GestoreOCSPValidator(requestInfo, this.sslConfig.getLog4jBuffer(), crlInputConfig, policyType, ocspResourceReader);
  120.                             if(ocspValidator!=null) {
  121.                                 GestoreOCSPValidator gOcspValidator = (GestoreOCSPValidator) ocspValidator;
  122.                                 if(gOcspValidator.getOcspConfig()!=null) {
  123.                                     crlByOcsp = gOcspValidator.getOcspConfig().isCrl();
  124.                                 }
  125.                             }
  126.                         }
  127.                     }
  128.                    
  129.                     if(this.sslConfig.getTrustStoreCRLsLocation()!=null && !crlByOcsp) {
  130.                         try {
  131.                             this.sslConfig.setTrustStoreCRLs(GestoreKeystoreCache.getCRLCertstore(requestInfo, this.sslConfig.getTrustStoreCRLsLocation()).getCertStore());
  132.                         }catch(Exception e) {
  133.                             String msgError = "Lettura CRLs '"+this.sslConfig.getTrustStoreLocation()+"' dalla cache fallita: "+e.getMessage();
  134.                             this.sslConfig.getLoggerBuffer().error(msgError, e);
  135.                         }
  136.                     }
  137.                    
  138.                     StringBuilder bfSSLConfig = new StringBuilder();
  139.                     SSLContext sslContext = SSLUtilities.generateSSLContext(this.sslConfig, ocspValidator, byokManager, bfSSLConfig);
  140.                     this.sslSocketFactoryObject = sslContext.getSocketFactory();
  141.                    
  142.                     if(this.sslConfig.getLoggerBuffer()!=null) {
  143.                         String msgDebug = bfSSLConfig.toString();
  144.                         this.sslConfig.getLoggerBuffer().debug(msgDebug);      
  145.                     }
  146.                 }

  147.             }catch(Exception e){
  148.                 throw new SecurityException(e.getMessage(),e);
  149.             }
  150.         }
  151.     }
  152.    
  153.     public javax.net.ssl.SSLSocketFactory getSslSocketFactory(RequestInfo requestInfo) throws SecurityException {
  154.         this.checkInit(requestInfo); // per ripristino da Serializable
  155.         return this.sslSocketFactoryObject;
  156.     }  


  157. }