SSLConfig.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.transport.http;

  21. import java.io.Serializable;
  22. import java.security.KeyStore;
  23. import java.security.cert.CertStore;
  24. import java.util.Map;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.utils.LoggerBuffer;
  27. import org.slf4j.Logger;


  28. /**
  29.  * SSLConfig
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class SSLConfig implements Serializable  {

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;
  40.    
  41.     // AUTENTICAZIONE SERVER:
  42.     // TrustAllCerts
  43.     private boolean trustAllCerts = false;
  44.     // TrustStore
  45.     private transient KeyStore trustStore;
  46.     private transient boolean trustStoreHsm;
  47.     // Path del trustStore che contiene il certificato del server.
  48.     private String trustStoreLocation;
  49.     // Password del trustStore che contiene il certificato del server.
  50.     private String trustStorePassword;
  51.     // the standard name of the requested trust management algorithm
  52.     private String trustManagementAlgorithm;
  53.     // tipo del truststore
  54.     private String trustStoreType;
  55.     // CRLs
  56.     private String trustStoreCRLsLocation;
  57.     // CertStore
  58.     private transient CertStore trustStoreCRLs;
  59.     // OCSP Policy
  60.     private String trustStoreOCSPPolicy;
  61.    
  62.    
  63.     // AUTENTICAZIONE CLIENT:
  64.     // KeyStore
  65.     private transient KeyStore keyStore;
  66.     private transient boolean keyStoreHsm;
  67.     // Path del keyStore che contiene il certificato del client e la chiave privata del client.
  68.     private String keyStoreLocation;
  69.     // Password del keyStore che contiene il certificato del client
  70.     private String keyStorePassword;
  71.     // Alias della chiave privata
  72.     private String keyAlias;
  73.     // Password della chiave privata
  74.     private String keyPassword;
  75.     // the standard name of the requested key management algorithm
  76.     private String keyManagementAlgorithm;
  77.     // tipo del keystore
  78.     private String keyStoreType;
  79.     // BYOK Policy
  80.     private String keyStoreBYOKPolicy;

  81.     // HostName verifier
  82.     private boolean hostnameVerifier = true;
  83.     // Eventuale classe da utilizzare per effettuare hostnameVerifier al posto di quella di default
  84.     private String classNameHostnameVerifier;
  85.    
  86.     // TipologiaSSL
  87.     private String sslType= null;
  88.    
  89.     // Use Secure Random
  90.     private boolean secureRandomSet = false;
  91.     private boolean secureRandom = false;
  92.     private String secureRandomAlgorithm = null;
  93.    
  94.     // Utilities
  95.     private StringBuilder sbError;
  96.     private StringBuilder sbDebug;
  97.     private transient Logger logger;
  98.    
  99.     // Dynamic Map
  100.     private Map<String,Object> dynamicMap;
  101.    
  102.     @Override
  103.     public String toString() {
  104.         return this.toString(false);
  105.     }
  106.     public String toString(boolean includePassword) {
  107.         StringBuilder sb = new StringBuilder();
  108.        
  109.         sb.append("sslType=").append(this.sslType);
  110.         sb.append(" ");
  111.        
  112.         sb.append("secureRandom=").append(this.secureRandom);
  113.         sb.append(" ");
  114.         if(this.secureRandomAlgorithm!=null) {
  115.             sb.append("secureRandomAlgorithm=").append(this.secureRandomAlgorithm);
  116.             sb.append(" ");
  117.         }
  118.        
  119.         sb.append("hostnameVerifier=").append(this.hostnameVerifier);
  120.         sb.append(" ");
  121.         if(this.classNameHostnameVerifier!=null) {
  122.             sb.append("classNameHostnameVerifier=").append(this.classNameHostnameVerifier);
  123.             sb.append(" ");
  124.         }
  125.        
  126.         sb.append("trustAllCerts=").append(this.trustAllCerts);
  127.         sb.append(" ");
  128.         if(!this.trustAllCerts) {
  129.             sb.append("trustStoreLocation=").append(this.trustStoreLocation);
  130.             sb.append(" ");
  131.             sb.append("trustStoreType=").append(this.trustStoreType);
  132.             sb.append(" ");
  133.             sb.append("trustStorePassword=").append(includePassword? this.trustStorePassword : hidePassword(this.trustStorePassword) );
  134.             sb.append(" ");
  135.             sb.append("trustManagementAlgorithm=").append(this.trustManagementAlgorithm);
  136.             sb.append(" ");
  137.             sb.append("trustStoreCRLsLocation=").append(this.trustStoreCRLsLocation);
  138.             sb.append(" ");
  139.             sb.append("trustStoreOCSPPolicy=").append(this.trustStoreOCSPPolicy);
  140.             sb.append(" ");
  141.         }
  142.         else if(this.trustStoreOCSPPolicy!=null && StringUtils.isNotEmpty(this.trustStoreOCSPPolicy)){
  143.             sb.append("trustStoreCRLsLocation=").append(this.trustStoreCRLsLocation);
  144.             sb.append(" ");
  145.             sb.append("trustStoreOCSPPolicy=").append(this.trustStoreOCSPPolicy);
  146.             sb.append(" ");
  147.         }
  148.        
  149.         if(this.keyStoreLocation!=null) {
  150.             sb.append("keyStoreLocation=").append(this.keyStoreLocation);
  151.             sb.append(" ");
  152.             sb.append("keyStoreType=").append(this.keyStoreType);
  153.             sb.append(" ");
  154.             sb.append("keyStorePassword=").append(includePassword? this.keyStorePassword : hidePassword(this.keyStorePassword) );
  155.             sb.append(" ");
  156.             sb.append("keyStoreBYOKPolicy=").append(this.keyStoreBYOKPolicy);
  157.             sb.append(" ");
  158.             sb.append("keyAlias=").append(this.keyAlias);
  159.             sb.append(" ");
  160.             sb.append("keyPassword=").append(includePassword? this.keyPassword : hidePassword(this.keyPassword) );
  161.             sb.append(" ");
  162.             sb.append("keyManagementAlgorithm=").append(this.keyManagementAlgorithm);
  163.             sb.append(" ");
  164.         }
  165.         else {
  166.             sb.append("keyStore=disabled");
  167.             sb.append(" ");
  168.         }
  169.        
  170.         return sb.toString();
  171.     }
  172.     private String hidePassword(String value) {
  173.         return (value!=null) ? "***" : "unset" ;
  174.     }

  175.     public boolean isTrustAllCerts() {
  176.         return this.trustAllCerts;
  177.     }

  178.     public void setTrustAllCerts(boolean trustAllCerts) {
  179.         this.trustAllCerts = trustAllCerts;
  180.     }
  181.    
  182.     public KeyStore getTrustStore() {
  183.         return this.trustStore;
  184.     }
  185.     public boolean isTrustStoreHsm() {
  186.         return this.trustStoreHsm;
  187.     }

  188.     public void setTrustStore(KeyStore trustStore) {
  189.         this.setTrustStore(trustStore, false);
  190.     }
  191.     public void setTrustStore(KeyStore trustStore, boolean hsm) {
  192.         this.trustStore = trustStore;
  193.         this.trustStoreHsm = hsm;
  194.     }

  195.     public KeyStore getKeyStore() {
  196.         return this.keyStore;
  197.     }
  198.     public boolean isKeyStoreHsm() {
  199.         return this.keyStoreHsm;
  200.     }

  201.     public void setKeyStore(KeyStore keyStore) {
  202.         this.setKeyStore(keyStore, false);
  203.     }
  204.     public void setKeyStore(KeyStore keyStore, boolean hsm) {
  205.         this.keyStore = keyStore;
  206.         this.keyStoreHsm = hsm;
  207.     }
  208.    
  209.     public String getTrustStoreLocation() {
  210.         return this.trustStoreLocation;
  211.     }

  212.     public void setTrustStoreLocation(String trustStoreLocation) {
  213.         this.trustStoreLocation = trustStoreLocation;
  214.     }

  215.     public String getTrustStorePassword() {
  216.         return this.trustStorePassword;
  217.     }

  218.     public void setTrustStorePassword(String trustStorePassword) {
  219.         this.trustStorePassword = trustStorePassword;
  220.     }

  221.     public String getTrustManagementAlgorithm() {
  222.         return this.trustManagementAlgorithm;
  223.     }

  224.     public void setTrustManagementAlgorithm(String trustManagementAlgorithm) {
  225.         this.trustManagementAlgorithm = trustManagementAlgorithm;
  226.     }

  227.     public String getTrustStoreType() {
  228.         return this.trustStoreType;
  229.     }

  230.     public void setTrustStoreType(String trustStoreType) {
  231.         this.trustStoreType = trustStoreType;
  232.     }
  233.    
  234.     public String getTrustStoreCRLsLocation() {
  235.         return this.trustStoreCRLsLocation;
  236.     }

  237.     public void setTrustStoreCRLsLocation(String trustStoreCRLsLocation) {
  238.         this.trustStoreCRLsLocation = trustStoreCRLsLocation;
  239.     }

  240.     public CertStore getTrustStoreCRLs() {
  241.         return this.trustStoreCRLs;
  242.     }

  243.     public void setTrustStoreCRLs(CertStore trustStoreCRLs) {
  244.         this.trustStoreCRLs = trustStoreCRLs;
  245.     }

  246.     public String getTrustStoreOCSPPolicy() {
  247.         return this.trustStoreOCSPPolicy;
  248.     }
  249.     public void setTrustStoreOCSPPolicy(String trustStoreOCSPPolicy) {
  250.         this.trustStoreOCSPPolicy = trustStoreOCSPPolicy;
  251.     }
  252.    
  253.     public String getKeyStoreLocation() {
  254.         return this.keyStoreLocation;
  255.     }

  256.     public void setKeyStoreLocation(String keyStoreLocation) {
  257.         this.keyStoreLocation = keyStoreLocation;
  258.     }

  259.     public String getKeyStorePassword() {
  260.         return this.keyStorePassword;
  261.     }

  262.     public void setKeyStorePassword(String keyStorePassword) {
  263.         this.keyStorePassword = keyStorePassword;
  264.     }

  265.     public String getKeyAlias() {
  266.         return this.keyAlias;
  267.     }

  268.     public void setKeyAlias(String keyAlias) {
  269.         this.keyAlias = keyAlias;
  270.     }
  271.    
  272.     public String getKeyPassword() {
  273.         return this.keyPassword;
  274.     }

  275.     public void setKeyPassword(String keyPassword) {
  276.         this.keyPassword = keyPassword;
  277.     }

  278.     public String getKeyManagementAlgorithm() {
  279.         return this.keyManagementAlgorithm;
  280.     }

  281.     public void setKeyManagementAlgorithm(String keyManagementAlgorithm) {
  282.         this.keyManagementAlgorithm = keyManagementAlgorithm;
  283.     }

  284.     public String getKeyStoreType() {
  285.         return this.keyStoreType;
  286.     }

  287.     public void setKeyStoreType(String keyStoreType) {
  288.         this.keyStoreType = keyStoreType;
  289.     }

  290.     public String getKeyStoreBYOKPolicy() {
  291.         return this.keyStoreBYOKPolicy;
  292.     }
  293.    
  294.     public void setKeyStoreBYOKPolicy(String keyStoreBYOKPolicy) {
  295.         this.keyStoreBYOKPolicy = keyStoreBYOKPolicy;
  296.     }
  297.    
  298.     public boolean isHostnameVerifier() {
  299.         return this.hostnameVerifier;
  300.     }

  301.     public void setHostnameVerifier(boolean hostnameVerifier) {
  302.         this.hostnameVerifier = hostnameVerifier;
  303.     }

  304.     public String getClassNameHostnameVerifier() {
  305.         return this.classNameHostnameVerifier;
  306.     }

  307.     public void setClassNameHostnameVerifier(String classNameHostnameVerifier) {
  308.         this.classNameHostnameVerifier = classNameHostnameVerifier;
  309.     }

  310.     public String getSslType() {
  311.         return this.sslType;
  312.     }

  313.     public void setSslType(String sslType) {
  314.         this.sslType = sslType;
  315.     }
  316.    
  317.     public boolean isSecureRandom() {
  318.         return this.secureRandom;
  319.     }

  320.     public void setSecureRandom(boolean secureRandom) {
  321.         this.secureRandom = secureRandom;
  322.         this.secureRandomSet = true;
  323.     }

  324.     public boolean isSecureRandomSet() {
  325.         return this.secureRandomSet;
  326.     }

  327.     public String getSecureRandomAlgorithm() {
  328.         return this.secureRandomAlgorithm;
  329.     }

  330.     public void setSecureRandomAlgorithm(String secureRandomAlgorithm) {
  331.         this.secureRandomAlgorithm = secureRandomAlgorithm;
  332.     }
  333.    
  334.     public StringBuilder getSbError() {
  335.         return this.sbError;
  336.     }

  337.     public void setSbError(StringBuilder sbError) {
  338.         this.sbError = sbError;
  339.     }

  340.     public StringBuilder getSbDebug() {
  341.         return this.sbDebug;
  342.     }

  343.     public void setSbDebug(StringBuilder sbDebug) {
  344.         this.sbDebug = sbDebug;
  345.     }
  346.    
  347.     public void setLogger(Logger logger) {
  348.         this.logger = logger;
  349.     }
  350.    
  351.     public LoggerBuffer getLoggerBuffer() {
  352.         LoggerBuffer lb = new LoggerBuffer();
  353.         lb.setSbDebug(this.sbDebug);
  354.         lb.setSbError(this.sbError);
  355.         lb.setLogErrorInDebug(true);
  356.         return lb;
  357.     }
  358.     public LoggerBuffer getLog4jBuffer() {
  359.         LoggerBuffer lb = new LoggerBuffer();
  360.         lb.setLogError(this.logger);
  361.         if(this.sbDebug!=null) {
  362.             lb.setLogDebug(this.logger);
  363.         }
  364.         return lb;
  365.     }
  366.    
  367.     public Map<String, Object> getDynamicMap() {
  368.         return this.dynamicMap;
  369.     }
  370.     public void setDynamicMap(Map<String, Object> dynamicMap) {
  371.         this.dynamicMap = dynamicMap;
  372.     }
  373. }