ModITruststoreConfig.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.protocol.modipa.utils;

  21. import java.util.List;

  22. import org.openspcoop2.core.commons.DBUtils;
  23. import org.openspcoop2.core.constants.CostantiDB;
  24. import org.openspcoop2.core.id.IDSoggetto;
  25. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  26. import org.openspcoop2.core.registry.ProtocolProperty;
  27. import org.openspcoop2.protocol.modipa.config.ModIProperties;
  28. import org.openspcoop2.protocol.modipa.constants.ModIConsoleCostanti;
  29. import org.openspcoop2.protocol.modipa.constants.ModICostanti;
  30. import org.openspcoop2.protocol.sdk.ProtocolException;
  31. import org.openspcoop2.protocol.sdk.properties.ProtocolPropertiesUtils;
  32. import org.openspcoop2.utils.certificate.KeystoreType;
  33. import org.openspcoop2.utils.certificate.hsm.HSMUtils;
  34. import org.openspcoop2.utils.transport.http.HttpUtilities;

  35. /**
  36.  * ModIKeystoreConfig
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class ModITruststoreConfig {

  43.     private String securityMessageTruststoreType = null;
  44.     private boolean securityMessageTruststoreHSM = false;
  45.     private boolean securityMessageTruststoreJWK = false;
  46.     private boolean securityMessageTruststoreRemote = false;
  47.     private String securityMessageTruststorePath = null;
  48.     private String securityMessageTruststorePassword = null;
  49.     private String securityMessageTruststoreCRLs = null;
  50.     private String securityMessageTruststoreOCSPPolicy = null;
  51.    
  52.     public ModITruststoreConfig(boolean fruizione, IDSoggetto soggettoFruitore, AccordoServizioParteSpecifica asps, boolean ssl) throws ProtocolException {
  53.        
  54.         try {
  55.             List<ProtocolProperty> listProtocolProperties = ModIPropertiesUtils.getProtocolProperties(fruizione, soggettoFruitore, asps);
  56.            
  57.             String prefix = ModIConsoleCostanti.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_MODE_LABEL;
  58.             if(ssl) {
  59.                 prefix = ModIConsoleCostanti.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_MODE_LABEL;
  60.             }
  61.            
  62.             String mode = ProtocolPropertiesUtils.getRequiredStringValuePropertyRegistry(listProtocolProperties,
  63.                     ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_MODE : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_MODE);
  64.             boolean ridefinisci =  ModICostanti.MODIPA_PROFILO_RIDEFINISCI.equals(mode);
  65.             boolean undefined =  ModICostanti.MODIPA_PROFILO_UNDEFINED.equals(mode);
  66.            
  67.             ModIProperties modIproperties = ModIProperties.getInstance();
  68.            
  69.             if(ridefinisci) {
  70.                
  71.                 this.securityMessageTruststoreType = ProtocolPropertiesUtils.getRequiredStringValuePropertyRegistry(listProtocolProperties,
  72.                         ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_TYPE : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_TYPE);
  73.                 this.securityMessageTruststoreHSM = HSMUtils.isKeystoreHSM(this.securityMessageTruststoreType);
  74.                
  75.                 if(!this.securityMessageTruststoreHSM) {
  76.                     this.securityMessageTruststoreJWK = CostantiDB.KEYSTORE_TYPE_JWK.equals(this.securityMessageTruststoreType);
  77.                     if(!this.securityMessageTruststoreJWK) {
  78.                         this.securityMessageTruststoreRemote = modIproperties.isRemoteStore(this.securityMessageTruststoreType);
  79.                     }
  80.                 }
  81.                
  82.                 if(this.securityMessageTruststoreHSM) {
  83.                    
  84.                     this.securityMessageTruststorePath = HSMUtils.KEYSTORE_HSM_PREFIX+this.securityMessageTruststoreType;
  85.                    
  86.                     this.securityMessageTruststorePassword = HSMUtils.KEYSTORE_HSM_STORE_PASSWORD_UNDEFINED;
  87.                            
  88.                 }
  89.                 else {
  90.                     if(!this.securityMessageTruststoreRemote) {
  91.                         this.securityMessageTruststorePath = ProtocolPropertiesUtils.getRequiredStringValuePropertyRegistry(listProtocolProperties,
  92.                                 ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PATH : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PATH);
  93.                         try {
  94.                             HttpUtilities.validateUri(this.securityMessageTruststorePath, true);
  95.                         }catch(Exception e) {
  96.                             throw new ProtocolException(prefix+" ["+this.securityMessageTruststorePath+"] "+e.getMessage(),e);
  97.                         }
  98.                     }
  99.                    
  100.                     if(!this.securityMessageTruststoreJWK && !this.securityMessageTruststoreRemote) {
  101.                         boolean required = true;
  102.                         if(
  103.                                 (KeystoreType.JKS.isType(this.securityMessageTruststoreType) && !DBUtils.isTruststoreJksPasswordRequired())
  104.                                 ||
  105.                                 (KeystoreType.PKCS12.isType(this.securityMessageTruststoreType) && !DBUtils.isTruststorePkcs12PasswordRequired())
  106.                         ) {
  107.                             required = false;
  108.                         }
  109.                         if(required) {
  110.                             this.securityMessageTruststorePassword = ProtocolPropertiesUtils.getRequiredStringValuePropertyRegistry(listProtocolProperties,
  111.                                     ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PASSWORD : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PASSWORD);
  112.                         }
  113.                         else {
  114.                             this.securityMessageTruststorePassword = ProtocolPropertiesUtils.getOptionalStringValuePropertyRegistry(listProtocolProperties,
  115.                                     ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PASSWORD : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PASSWORD);
  116.                         }
  117.                     }
  118.                 }

  119.                 if(!this.securityMessageTruststoreJWK && !this.securityMessageTruststoreRemote) {
  120.                     this.securityMessageTruststoreCRLs = ProtocolPropertiesUtils.getOptionalStringValuePropertyRegistry(listProtocolProperties,
  121.                             ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_CRLS : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_CRLS);
  122.                    
  123.                     this.securityMessageTruststoreOCSPPolicy = ProtocolPropertiesUtils.getOptionalStringValuePropertyRegistry(listProtocolProperties,
  124.                             ssl ? ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_OCSP_POLICY : ModICostanti.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_OCSP_POLICY);
  125.                 }
  126.                
  127.             }
  128.             else if(undefined) {
  129.                 if(!ssl) {
  130.                     throw new ProtocolException(prefix+" non definito");
  131.                 }
  132.             }
  133.             else {
  134.                
  135.                 this.securityMessageTruststoreType = ssl ? modIproperties.getSicurezzaMessaggioSslTrustStoreTipo() : modIproperties.getSicurezzaMessaggioCertificatiTrustStoreTipo();
  136.                 if(this.securityMessageTruststoreType!=null) {
  137.                    
  138.                     this.securityMessageTruststoreHSM = HSMUtils.isKeystoreHSM(this.securityMessageTruststoreType);
  139.                    
  140.                     if(this.securityMessageTruststoreHSM) {
  141.                        
  142.                         this.securityMessageTruststorePath = HSMUtils.KEYSTORE_HSM_PREFIX+this.securityMessageTruststoreType;
  143.                        
  144.                         this.securityMessageTruststorePassword = HSMUtils.KEYSTORE_HSM_STORE_PASSWORD_UNDEFINED;
  145.                        
  146.                     }
  147.                     else {
  148.                        
  149.                         this.securityMessageTruststorePath = ssl ? modIproperties.getSicurezzaMessaggioSslTrustStorePath() : modIproperties.getSicurezzaMessaggioCertificatiTrustStorePath();
  150.                         try {
  151.                             HttpUtilities.validateUri(this.securityMessageTruststorePath, true);
  152.                         }catch(Exception e) {
  153.                             throw new ProtocolException(prefix+" ["+this.securityMessageTruststorePath+"] "+e.getMessage(),e);
  154.                         }
  155.                        
  156.                         this.securityMessageTruststorePassword = ssl ? modIproperties.getSicurezzaMessaggioSslTrustStorePassword() : modIproperties.getSicurezzaMessaggioCertificatiTrustStorePassword();
  157.                     }
  158.                
  159.                     this.securityMessageTruststoreCRLs = ssl ? modIproperties.getSicurezzaMessaggioSslTrustStoreCrls() : modIproperties.getSicurezzaMessaggioCertificatiTrustStoreCrls();
  160.                
  161.                     this.securityMessageTruststoreOCSPPolicy = ssl ? modIproperties.getSicurezzaMessaggioSslTrustStoreOcspPolicy() : modIproperties.getSicurezzaMessaggioCertificatiTrustStoreOcspPolicy();
  162.                 }
  163.                
  164.             }
  165.            
  166.             if(!ssl) {
  167.                 if(this.securityMessageTruststoreType==null) {
  168.                     throw new ProtocolException(prefix+" non definito");
  169.                 }
  170.                 if(this.securityMessageTruststorePath==null && !this.securityMessageTruststoreRemote) {
  171.                     throw new ProtocolException(prefix+" (path) non definito");
  172.                 }
  173.             }
  174.            
  175.         }catch(Exception e) {
  176.             throw new ProtocolException(e);
  177.         }
  178.        
  179.     }
  180.    
  181.    
  182.     public String getSecurityMessageTruststorePath() {
  183.         return this.securityMessageTruststorePath;
  184.     }

  185.     public String getSecurityMessageTruststoreType() {
  186.         return this.securityMessageTruststoreType;
  187.     }
  188.    
  189.     public boolean isSecurityMessageTruststoreHSM() {
  190.         return this.securityMessageTruststoreHSM;
  191.     }
  192.    
  193.     public boolean isSecurityMessageTruststoreJWK() {
  194.         return this.securityMessageTruststoreJWK;
  195.     }

  196.     public boolean isSecurityMessageTruststoreRemote() {
  197.         return this.securityMessageTruststoreRemote;
  198.     }

  199.     public String getSecurityMessageTruststorePassword() {
  200.         return this.securityMessageTruststorePassword;
  201.     }

  202.     public String getSecurityMessageTruststoreCRLs() {
  203.         return this.securityMessageTruststoreCRLs;
  204.     }
  205.    
  206.     public String getSecurityMessageTruststoreOCSPPolicy() {
  207.         return this.securityMessageTruststoreOCSPPolicy;
  208.     }
  209.    
  210. }