ConnettoreHTTPSProperties.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.core.commons;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.Serializable;
  24. import java.security.KeyStore;
  25. import java.util.Enumeration;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.Properties;

  29. import javax.net.ssl.KeyManagerFactory;
  30. import javax.net.ssl.TrustManagerFactory;

  31. import org.openspcoop2.core.constants.CostantiConnettori;
  32. import org.openspcoop2.utils.certificate.KeystoreType;
  33. import org.openspcoop2.utils.transport.http.SSLConfig;

  34. /**
  35.  * Classe utilizzata per interprare le proprieta' https
  36.  *
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class ConnettoreHTTPSProperties extends SSLConfig implements Serializable  {

  43.     /**
  44.      *
  45.      */
  46.     private static final long serialVersionUID = 1L;

  47.     private static final String VALORE_DEFINITO_PER_PROPRIETA = "Valore definito per la proprietà '";
  48.     private static final String VALORE_NON_DEFINITO_PER_PROPRIETA = "Valore non definito per la proprietà '";
  49.     private static final String NON_VALIDO_TRUE_FALSE = "' non valido, valori accettati true/false";
  50.     private static final String DEFINITO_TRAMITE_PROPRIETA = "' nonostante sia stato definito un trustStore attraverso la proprietà '";
  51.    
  52.     public static ConnettoreHTTPSProperties readProperties(java.util.Map<String,String> properties) throws CoreException{
  53.         ConnettoreHTTPSProperties propertiesHTTPS = new ConnettoreHTTPSProperties();
  54.        
  55.         // AUTENTICAZIONE SERVER
  56.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_ALL_CERTS)!=null){
  57.             String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_ALL_CERTS).trim();
  58.             try{
  59.                 propertiesHTTPS.setTrustAllCerts(Boolean.parseBoolean(tmp));
  60.             }catch(Exception e){
  61.                 throw new CoreException(VALORE_DEFINITO_PER_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_TRUST_ALL_CERTS+NON_VALIDO_TRUE_FALSE);
  62.             }
  63.         }
  64.         if(!propertiesHTTPS.isTrustAllCerts() && properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_LOCATION)!=null){
  65.             readTrustStoreConfig(properties, propertiesHTTPS);
  66.         }
  67.         else if(propertiesHTTPS.isTrustAllCerts()){
  68.             // potrei accettare qualsiasi certificato ma validarlo rispetto a OCSP
  69.             readTrustStoreAllConfig(properties, propertiesHTTPS);
  70.         }
  71.        
  72.         // AUTENTICAZIONE CLIENT
  73.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_LOCATION)!=null){
  74.             readKeyStoreConfig(properties, propertiesHTTPS);
  75.         }  
  76.        
  77.         // HostName verifier
  78.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_HOSTNAME_VERIFIER)!=null){
  79.             String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_HOSTNAME_VERIFIER).trim();
  80.             try{
  81.                 propertiesHTTPS.setHostnameVerifier(Boolean.parseBoolean(tmp));
  82.             }catch(Exception e){
  83.                 throw new CoreException(VALORE_DEFINITO_PER_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_HOSTNAME_VERIFIER+NON_VALIDO_TRUE_FALSE);
  84.             }
  85.         }
  86.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_CLASSNAME_HOSTNAME_VERIFIER)!=null){
  87.             String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_CLASSNAME_HOSTNAME_VERIFIER).trim();
  88.             propertiesHTTPS.setClassNameHostnameVerifier(tmp);
  89.         }
  90.        
  91.         // TipologiaSSL: SSL, SSLv3, TLS, TLSv1
  92.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_SSL_TYPE)!=null){
  93.             String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_SSL_TYPE).trim();
  94.             propertiesHTTPS.setSslType(tmp);
  95.         }else{
  96.             propertiesHTTPS.setSslType(CostantiConnettori.CONNETTORE_HTTPS_SSL_TYPE_DEFAULT_VALUE);
  97.         }
  98.        
  99.         // SecureRandom
  100.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_SECURE_RANDOM)!=null){
  101.             readSecureRandomConfig(properties, propertiesHTTPS);
  102.         }
  103.        
  104.         return propertiesHTTPS;
  105.     }
  106.     private static void readTrustStoreConfig(java.util.Map<String,String> properties, ConnettoreHTTPSProperties propertiesHTTPS) throws CoreException {
  107.         String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_LOCATION).trim();
  108.         propertiesHTTPS.setTrustStoreLocation(tmp);
  109.        
  110.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_TYPE)!=null){
  111.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_TYPE).trim();
  112.             propertiesHTTPS.setTrustStoreType(tmp);
  113.         }else{
  114.             propertiesHTTPS.setTrustStoreType(KeyStore.getDefaultType()); // JKS
  115.         }
  116.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_PASSWORD)!=null){
  117.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_PASSWORD).trim();
  118.             propertiesHTTPS.setTrustStorePassword(tmp);
  119.         }
  120.         else {
  121.             boolean throwException = true;
  122.             if(
  123.                     (KeystoreType.JKS.isType(propertiesHTTPS.getTrustStoreType()) && !DBUtils.isTruststoreJksPasswordRequired())
  124.                     ||
  125.                     (KeystoreType.PKCS12.isType(propertiesHTTPS.getTrustStoreType()) && !DBUtils.isTruststorePkcs12PasswordRequired())
  126.             ) {
  127.                 throwException = false;
  128.             }
  129.             if(throwException) {
  130.                 throw new CoreException(VALORE_NON_DEFINITO_PER_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_PASSWORD+
  131.                         DEFINITO_TRAMITE_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_LOCATION+"'");
  132.             }
  133.         }
  134.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_MANAGEMENT_ALGORITHM)!=null){
  135.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_MANAGEMENT_ALGORITHM).trim();
  136.             propertiesHTTPS.setTrustManagementAlgorithm(tmp);
  137.         }else{
  138.             propertiesHTTPS.setTrustManagementAlgorithm(TrustManagerFactory.getDefaultAlgorithm());
  139.         }
  140.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_CRLS)!=null){
  141.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_CRLS).trim();
  142.             propertiesHTTPS.setTrustStoreCRLsLocation(tmp);
  143.         }
  144.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_OCSP_POLICY)!=null){
  145.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_OCSP_POLICY).trim();
  146.             propertiesHTTPS.setTrustStoreOCSPPolicy(tmp);
  147.         }
  148.     }
  149.     private static void readTrustStoreAllConfig(java.util.Map<String,String> properties, ConnettoreHTTPSProperties propertiesHTTPS) {
  150.         // potrei accettare qualsiasi certificato ma validarlo rispetto a OCSP
  151.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_OCSP_POLICY)!=null){
  152.             String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_OCSP_POLICY).trim();
  153.             propertiesHTTPS.setTrustStoreOCSPPolicy(tmp);
  154.            
  155.             if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_CRLS)!=null){
  156.                 tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_CRLS).trim();
  157.                 propertiesHTTPS.setTrustStoreCRLsLocation(tmp);
  158.             }
  159.         }
  160.     }
  161.     private static void readKeyStoreConfig(java.util.Map<String,String> properties, ConnettoreHTTPSProperties propertiesHTTPS) throws CoreException {
  162.         String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_LOCATION).trim();
  163.         propertiesHTTPS.setKeyStoreLocation(tmp);
  164.        
  165.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_TYPE)!=null){
  166.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_TYPE).trim();
  167.             propertiesHTTPS.setKeyStoreType(tmp);
  168.         }else{
  169.             propertiesHTTPS.setKeyStoreType(KeyStore.getDefaultType()); // JKS
  170.         }
  171.        
  172.         readKeyStorePasswordConfig(properties, propertiesHTTPS);
  173.        
  174.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_ALIAS)!=null){
  175.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_ALIAS).trim();
  176.             propertiesHTTPS.setKeyAlias(tmp);
  177.         }
  178.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_MANAGEMENT_ALGORITHM)!=null){
  179.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_MANAGEMENT_ALGORITHM).trim();
  180.             propertiesHTTPS.setKeyManagementAlgorithm(tmp);
  181.         }else{
  182.             propertiesHTTPS.setKeyManagementAlgorithm(KeyManagerFactory.getDefaultAlgorithm());
  183.         }
  184.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_BYOK_POLICY)!=null){
  185.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_BYOK_POLICY).trim();
  186.             propertiesHTTPS.setKeyStoreBYOKPolicy(tmp);
  187.         }
  188.     }
  189.     private static void readKeyStorePasswordConfig(java.util.Map<String,String> properties, ConnettoreHTTPSProperties propertiesHTTPS) throws CoreException {
  190.         String tmp = null;
  191.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_PASSWORD)!=null){
  192.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_PASSWORD).trim();
  193.             propertiesHTTPS.setKeyStorePassword(tmp);
  194.         }
  195.         else{
  196.             boolean throwException = true;
  197.             if(
  198.                     (KeystoreType.JKS.isType(propertiesHTTPS.getKeyStoreType()) && !DBUtils.isKeystoreJksPasswordRequired())
  199.                     ||
  200.                     (KeystoreType.PKCS12.isType(propertiesHTTPS.getKeyStoreType()) && !DBUtils.isKeystorePkcs12PasswordRequired())
  201.             ) {
  202.                 throwException = false;
  203.             }
  204.             if(throwException) {
  205.                 throw new CoreException(VALORE_NON_DEFINITO_PER_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_PASSWORD
  206.                         +DEFINITO_TRAMITE_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_LOCATION+"'");
  207.             }
  208.         }
  209.         if(properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_PASSWORD)!=null){
  210.             tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_KEY_PASSWORD).trim();
  211.             propertiesHTTPS.setKeyPassword(tmp);
  212.         }else{
  213.             boolean throwException = true;
  214.             if(
  215.                     (KeystoreType.JKS.isType(propertiesHTTPS.getKeyStoreType()) && !DBUtils.isKeystoreJksKeyPasswordRequired())
  216.                     ||
  217.                     (KeystoreType.PKCS12.isType(propertiesHTTPS.getKeyStoreType()) && !DBUtils.isKeystorePkcs12KeyPasswordRequired())
  218.             ) {
  219.                 throwException = false;
  220.             }
  221.             if(throwException) {
  222.                 throw new CoreException(VALORE_NON_DEFINITO_PER_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_KEY_PASSWORD
  223.                         +DEFINITO_TRAMITE_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_LOCATION+"'");
  224.             }
  225.         }
  226.     }
  227.     private static void readSecureRandomConfig(java.util.Map<String,String> properties, ConnettoreHTTPSProperties propertiesHTTPS) throws CoreException {
  228.         String tmp = properties.get(CostantiConnettori.CONNETTORE_HTTPS_SECURE_RANDOM).trim();
  229.         try{
  230.             propertiesHTTPS.setSecureRandom(Boolean.parseBoolean(tmp));
  231.         }catch(Exception e){
  232.             throw new CoreException(VALORE_DEFINITO_PER_PROPRIETA+CostantiConnettori.CONNETTORE_HTTPS_SECURE_RANDOM+NON_VALIDO_TRUE_FALSE);
  233.         }
  234.         if(propertiesHTTPS.isSecureRandom() &&
  235.             properties.get(CostantiConnettori.CONNETTORE_HTTPS_SECURE_RANDOM_ALGORITHM)!=null){
  236.             propertiesHTTPS.setSecureRandomAlgorithm(properties.get(CostantiConnettori.CONNETTORE_HTTPS_SECURE_RANDOM_ALGORITHM).trim());
  237.         }
  238.     }
  239.    
  240.     public static ConnettoreHTTPSProperties readPropertyFile(String file, boolean sslConfigRequired) throws CoreException{
  241.         return readPropertyFile(new File(file), sslConfigRequired);
  242.     }
  243.     public static ConnettoreHTTPSProperties readPropertyFile(File file, boolean sslConfigRequired) throws CoreException{
  244.        
  245.         String prefix = "Config file ["+file.getAbsolutePath()+"] ";
  246.         if(!file.exists()) {
  247.             if(sslConfigRequired) {
  248.                 throw new CoreException(prefix+"not exists");
  249.             }
  250.             return null;
  251.         }
  252.         if(file.isDirectory()) {
  253.             throw new CoreException(prefix+"is directory");
  254.         }
  255.         if(!file.canRead()) {
  256.             throw new CoreException(prefix+"cannot read");
  257.         }
  258.        
  259.         Properties p = new Properties();
  260.         try (FileInputStream fin = new FileInputStream(file)){
  261.             p.load(fin);
  262.         }
  263.         catch(Exception e) {
  264.             throw new CoreException(e.getMessage(),e);
  265.         }
  266.         Map<String, String> pMap = new HashMap<>();
  267.         Enumeration<?> enP = p.keys();
  268.         while (enP.hasMoreElements()) {
  269.             String key = (String) enP.nextElement();
  270.             pMap.put(key, p.getProperty(key));
  271.         }
  272.         return readProperties(pMap);
  273.     }
  274. }