SecurityUtils.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.message.utils;

  21. import java.security.KeyStore;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.core.config.MessageSecurityFlowParameter;
  26. import org.openspcoop2.core.config.PortaApplicativa;
  27. import org.openspcoop2.core.config.PortaDelegata;
  28. import org.openspcoop2.core.constants.CostantiProprieta;
  29. import org.openspcoop2.security.keystore.MerlinProvider;
  30. import org.openspcoop2.security.message.constants.SecurityConstants;
  31. import org.openspcoop2.utils.certificate.KeystoreParams;

  32. /**    
  33.  * SecurityUtils
  34.  *
  35.  * @author Poli Andrea (poli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class SecurityUtils {
  40.    
  41.     private SecurityUtils() {}

  42.     public static List<KeystoreParams> readRequestKeystoreParams(PortaApplicativa porta) {
  43.         List<KeystoreParams> listKP = new ArrayList<>();
  44.         if(porta!=null && porta.getMessageSecurity()!=null && porta.getMessageSecurity().getRequestFlow()!=null &&
  45.                 porta.getMessageSecurity().getRequestFlow().sizeParameterList()>0) {
  46.             readStoreParams(listKP, porta.getMessageSecurity().getRequestFlow().getParameterList());
  47.         }
  48.         return listKP;
  49.     }
  50.    
  51.     public static List<KeystoreParams> readResponseKeystoreParams(PortaApplicativa porta) {
  52.         List<KeystoreParams> listKP = new ArrayList<>();
  53.         if(porta!=null && porta.getMessageSecurity()!=null && porta.getMessageSecurity().getResponseFlow()!=null &&
  54.                 porta.getMessageSecurity().getResponseFlow().sizeParameterList()>0) {
  55.             readStoreParams(listKP, porta.getMessageSecurity().getResponseFlow().getParameterList());
  56.         }
  57.         return listKP;
  58.     }
  59.    
  60.     public static List<KeystoreParams> readRequestKeystoreParams(PortaDelegata porta) {
  61.         List<KeystoreParams> listKP = new ArrayList<>();
  62.         if(porta!=null && porta.getMessageSecurity()!=null && porta.getMessageSecurity().getRequestFlow()!=null &&
  63.                 porta.getMessageSecurity().getRequestFlow().sizeParameterList()>0) {
  64.             readStoreParams(listKP, porta.getMessageSecurity().getRequestFlow().getParameterList());
  65.         }
  66.         return listKP;
  67.     }
  68.    
  69.     public static List<KeystoreParams> readResponseKeystoreParams(PortaDelegata porta) {
  70.         List<KeystoreParams> listKP = new ArrayList<>();
  71.         if(porta!=null && porta.getMessageSecurity()!=null && porta.getMessageSecurity().getResponseFlow()!=null &&
  72.                 porta.getMessageSecurity().getResponseFlow().sizeParameterList()>0) {
  73.             readStoreParams(listKP, porta.getMessageSecurity().getResponseFlow().getParameterList());
  74.         }
  75.         return listKP;
  76.     }
  77.    
  78.     private static void readStoreParams(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list) {
  79.        
  80.         readKestoreParamsJose(listKP, list);
  81.         readTruststoreParamsJoseTls(listKP, list);
  82.         readTruststoreParamsJoseUseHeaders(listKP, list);
  83.         readKeystoreParamsJoseUseHeaders(listKP, list);
  84.        
  85.         readKeystoreParamsMerlin(listKP, list);
  86.        
  87.     }
  88.    
  89.    
  90.     // JOSE
  91.    
  92.     private static void readKestoreParamsJose(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list) {
  93.         String pathJose = readProperty(list, SecurityConstants.JOSE_KEYSTORE_FILE);
  94.         if(pathJose!=null && StringUtils.isNotEmpty(pathJose)) {
  95.             // jose
  96.             KeystoreParams kp = new KeystoreParams();
  97.             kp.setPath(pathJose);
  98.             kp.setType(readProperty(list, SecurityConstants.JOSE_KEYSTORE_TYPE));
  99.             kp.setPassword(readProperty(list, SecurityConstants.JOSE_KEYSTORE_PSWD));
  100.            
  101.             kp.setCrls(readProperty(list, SecurityConstants.JOSE_KEYSTORE_CRL));
  102.             kp.setOcspPolicy(readProperty(list, SecurityConstants.JOSE_KEYSTORE_OCSP_POLICY));
  103.            
  104.             kp.setByokPolicy(readProperty(list, SecurityConstants.JOSE_KEYSTORE_BYOK_POLICY));
  105.            
  106.             kp.setKeyAlias(readProperty(list, SecurityConstants.JOSE_KEYSTORE_KEY_ALIAS));
  107.             kp.setKeyPassword(readProperty(list, SecurityConstants.JOSE_KEYSTORE_KEY_PSWD));
  108.            
  109.             kp.setKeyPairPublicKeyPath(readProperty(list, SecurityConstants.JOSE_KEYSTORE_PUBLIC_KEY));
  110.             kp.setKeyPairAlgorithm(readProperty(list, SecurityConstants.JOSE_KEYSTORE_KEY_ALGORITHM));
  111.            
  112.             listKP.add(kp);
  113.         }
  114.     }
  115.    
  116.     private static void readTruststoreParamsJoseTls(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list) {
  117.         String pathJose = readProperty(list, SecurityConstants.JOSE_TRUSTSTORE_SSL_FILE);
  118.         if(pathJose!=null && StringUtils.isNotEmpty(pathJose)) {
  119.             // jose
  120.             KeystoreParams kp = new KeystoreParams();
  121.             kp.setPath(pathJose);
  122.             kp.setType(readProperty(list, SecurityConstants.JOSE_TRUSTSTORE_SSL_TYPE));
  123.             kp.setPassword(readProperty(list, SecurityConstants.JOSE_TRUSTSTORE_SSL_PSWD));
  124.            
  125.             kp.setCrls(readProperty(list, SecurityConstants.JOSE_TRUSTSTORE_SSL_CRL));
  126.             kp.setOcspPolicy(readProperty(list, SecurityConstants.JOSE_TRUSTSTORE_SSL_OCSP));
  127.            
  128.             kp.setDescription("TLS TrustStore");
  129.            
  130.             listKP.add(kp);
  131.         }
  132.     }
  133.    
  134.     private static void readTruststoreParamsJoseUseHeaders(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list) {
  135.         String pathJose = readProperty(list, SecurityConstants.JOSE_USE_HEADERS_TRUSTSTORE_FILE);
  136.         if(pathJose!=null && StringUtils.isNotEmpty(pathJose)) {
  137.             // jose
  138.             KeystoreParams kp = new KeystoreParams();
  139.             kp.setPath(pathJose);
  140.             kp.setType(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_TRUSTSTORE_TYPE));
  141.             kp.setPassword(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_TRUSTSTORE_PASSWORD));
  142.            
  143.             kp.setCrls(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_TRUSTSTORE_CRL));
  144.             kp.setOcspPolicy(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_TRUSTSTORE_OCSP));
  145.            
  146.             kp.setKeyPairAlgorithm(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_TRUSTSTORE_KEY_PAIR_ALGORITHM));
  147.            
  148.             kp.setDescription("TrustStore Certificati X.509");
  149.            
  150.             listKP.add(kp);
  151.         }    
  152.     }
  153.    
  154.     private static void readKeystoreParamsJoseUseHeaders(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list) {
  155.         String pathJose = readProperty(list, SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_FILE);
  156.         if(pathJose!=null && StringUtils.isNotEmpty(pathJose)) {
  157.            
  158.             for (int i = 1; i < 11; i++) {
  159.            
  160.                 String user = readProperty(list, SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_MAP_ALIAS_PW+i+SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_MAP_ALIAS_PW_SUFFIX_ALIAS);
  161.                 if(user!=null) {
  162.                     // jose
  163.                     KeystoreParams kp = new KeystoreParams();
  164.                     kp.setPath(pathJose);
  165.                     kp.setType(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_TYPE));
  166.                     kp.setPassword(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_PASSWORD));
  167.                    
  168.                     kp.setKeyAlias(user);
  169.                     kp.setKeyPassword(readProperty(list, SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_MAP_ALIAS_PW+i+SecurityConstants.JOSE_USE_HEADERS_KEYSTORE_MAP_ALIAS_PW_SUFFIX_PW));
  170.                    
  171.                     kp.setDescription("KeyStore Certificati X.509");
  172.                    
  173.                     listKP.add(kp);
  174.                 }
  175.                
  176.             }
  177.            
  178.         }    
  179.     }
  180.    
  181.    
  182.     // MERLIN
  183.    
  184.     private static void readKeystoreParamsMerlin(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list) {
  185.        
  186.         String prefix = readPrefix(list);
  187.         if(prefix==null || StringUtils.isEmpty(prefix)) {
  188.             return;
  189.         }
  190.        
  191.         String pathKeystore = readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.KEYSTORE_FILE);
  192.         if(pathKeystore==null || StringUtils.isEmpty(pathKeystore)) {
  193.             pathKeystore = readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.OLD_KEYSTORE_FILE);
  194.         }
  195.        
  196.         if(pathKeystore!=null && StringUtils.isNotEmpty(pathKeystore)) {
  197.             readKeystoreParamsMerlin(listKP, list, prefix, pathKeystore);
  198.         }    
  199.     }
  200.     private static void readKeystoreParamsMerlin(List<KeystoreParams> listKP, List<MessageSecurityFlowParameter> list, String prefix, String pathKeystore) {
  201.         KeystoreParams kp = new KeystoreParams();
  202.         kp.setPath(pathKeystore);
  203.        
  204.         String keyStoreType = readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.KEYSTORE_TYPE);
  205.         if (keyStoreType == null) {
  206.             keyStoreType = KeyStore.getDefaultType();
  207.         }
  208.         kp.setType(keyStoreType);
  209.         kp.setPassword(readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.KEYSTORE_PASSWORD));
  210.        
  211.         String crlLocations = readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.X509_CRL_FILE);
  212.         if (crlLocations == null || StringUtils.isEmpty(crlLocations)) {
  213.             crlLocations = readProperty(list, SecurityConstants.SIGNATURE_CRL);
  214.         }
  215.         kp.setCrls(crlLocations);
  216.         kp.setOcspPolicy(readProperty(list, SecurityConstants.SIGNATURE_OCSP));
  217.        
  218.         String keyStoreByokPolicy = readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.KEYSTORE_FILE+MerlinProvider.SUFFIX_BYOK);
  219.         if (keyStoreByokPolicy == null || StringUtils.isEmpty(keyStoreByokPolicy)) {
  220.             keyStoreByokPolicy = readProperty(list, prefix+org.apache.wss4j.common.crypto.Merlin.OLD_KEYSTORE_FILE+MerlinProvider.SUFFIX_BYOK);
  221.         }
  222.         kp.setByokPolicy(keyStoreByokPolicy);
  223.        
  224.         listKP.add(kp);
  225.        
  226.         readKeystoreAliasParamsMerlin(list, kp);
  227.     }
  228.     private static void readKeystoreAliasParamsMerlin(List<MessageSecurityFlowParameter> list, KeystoreParams kp) {
  229.         if(readKeystoreSignatureAliasParamsMerlin(list,  kp)) {
  230.             return;
  231.         }
  232.         if(readKeystoreEncryptAliasParamsMerlin(list, kp)) {
  233.             return;
  234.         }
  235.         if(readKeystoreUserAliasParamsMerlin(list, kp)) {
  236.             /**return;*/
  237.         }
  238.     }
  239.     private static boolean readKeystoreSignatureAliasParamsMerlin(List<MessageSecurityFlowParameter> list, KeystoreParams kp) {
  240.         String alias = readProperty(list, SecurityConstants.SIGNATURE_USER);
  241.         if (alias != null && StringUtils.isNotEmpty(alias)) {
  242.             kp.setKeyAlias(alias);
  243.             kp.setKeyPassword(readProperty(list, SecurityConstants.SIGNATURE_PASSWORD));
  244.             return true;
  245.         }
  246.         return false;
  247.     }
  248.     private static boolean readKeystoreEncryptAliasParamsMerlin(List<MessageSecurityFlowParameter> list, KeystoreParams kp) {  
  249.         String alias = readProperty(list, SecurityConstants.ENCRYPTION_USER);
  250.         if (alias != null && StringUtils.isNotEmpty(alias)) {
  251.             kp.setKeyAlias(alias);
  252.             String pwd = readProperty(list, SecurityConstants.ENCRYPTION_PASSWORD);
  253.             if (pwd == null || StringUtils.isEmpty(pwd)) {
  254.                 pwd = readProperty(list, SecurityConstants.DECRYPTION_PASSWORD);
  255.             }
  256.             kp.setKeyPassword(pwd);
  257.             return true;
  258.         }
  259.        
  260.         alias = readProperty(list, SecurityConstants.DECRYPTION_USER);
  261.         if (alias != null && StringUtils.isNotEmpty(alias)) {
  262.             kp.setKeyAlias(alias);
  263.             String pwd = readProperty(list, SecurityConstants.DECRYPTION_PASSWORD);
  264.             if (pwd == null || StringUtils.isEmpty(pwd)) {
  265.                 pwd = readProperty(list, SecurityConstants.ENCRYPTION_PASSWORD);
  266.             }
  267.             kp.setKeyPassword(pwd);
  268.             return true;
  269.         }
  270.         return false;
  271.     }
  272.     private static boolean readKeystoreUserAliasParamsMerlin(List<MessageSecurityFlowParameter> list, KeystoreParams kp) {  
  273.        
  274.         String alias = readProperty(list, SecurityConstants.USER);
  275.         if (alias != null && StringUtils.isNotEmpty(alias)) {
  276.             kp.setKeyAlias(alias);
  277.             String pwd = readProperty(list, SecurityConstants.SIGNATURE_PASSWORD);
  278.             if (pwd == null || StringUtils.isEmpty(pwd)) {
  279.                 pwd = readProperty(list, SecurityConstants.ENCRYPTION_PASSWORD);
  280.             }
  281.             if (pwd == null || StringUtils.isEmpty(pwd)) {
  282.                 pwd = readProperty(list, SecurityConstants.DECRYPTION_PASSWORD);
  283.             }
  284.             kp.setKeyPassword(pwd);
  285.             return true;
  286.         }
  287.         return false;
  288.     }
  289.    
  290.     private static String readPrefix(List<MessageSecurityFlowParameter> list) {
  291.         if(list==null || list.isEmpty()) {
  292.             return null;
  293.         }
  294.         String prefix = null;
  295.         for (MessageSecurityFlowParameter p : list) {
  296.             String propKey = p.getNome();
  297.             if (startsWith(propKey,org.apache.wss4j.common.crypto.Merlin.PREFIX)) {
  298.                 prefix = org.apache.wss4j.common.crypto.Merlin.PREFIX;
  299.                 return prefix;
  300.             } else if (startsWith(propKey,org.apache.wss4j.common.crypto.Merlin.OLD_PREFIX)) {
  301.                 prefix = org.apache.wss4j.common.crypto.Merlin.OLD_PREFIX;
  302.                 return prefix;
  303.             }
  304.         }
  305.         return prefix;
  306.     }
  307.     private static boolean startsWith(String propKey, String check) {
  308.         return propKey.startsWith(check)
  309.                 ||
  310.                 (propKey.contains(CostantiProprieta.KEY_PROPERTIES_CUSTOM_SEPARATOR) && isStartsWithConfidentialPropertyCustomSeparator(propKey,check))
  311.                 ||
  312.                 (propKey.contains(CostantiProprieta.KEY_PROPERTIES_DEFAULT_SEPARATOR) && isStartsWithConfidentialPropertyDefaultSeparator(propKey,check));
  313.     }
  314.    
  315.    
  316.     // UTILITY
  317.    
  318.     private static String readProperty(List<MessageSecurityFlowParameter> list, String name) {
  319.         if(list!=null && !list.isEmpty()) {
  320.             for (MessageSecurityFlowParameter messageSecurityFlowParameter : list) {
  321.                 if(isProperty(messageSecurityFlowParameter, name)) {
  322.                     if(messageSecurityFlowParameter.getValore()!=null && StringUtils.isNotEmpty(messageSecurityFlowParameter.getValore())) {
  323.                         return messageSecurityFlowParameter.getValore();
  324.                     }
  325.                     return null;
  326.                 }
  327.             }
  328.         }
  329.         return null;
  330.     }
  331.     private static boolean isProperty(MessageSecurityFlowParameter messageSecurityFlowParameter, String name) {
  332.         return (messageSecurityFlowParameter.getNome()!=null &&
  333.                 (
  334.                         messageSecurityFlowParameter.getNome().equals(name)
  335.                         ||
  336.                         (messageSecurityFlowParameter.getNome().contains(CostantiProprieta.KEY_PROPERTIES_CUSTOM_SEPARATOR) && isConfidentialPropertyCustomSeparator(messageSecurityFlowParameter.getNome(),name))
  337.                         ||
  338.                         (messageSecurityFlowParameter.getNome().contains(CostantiProprieta.KEY_PROPERTIES_DEFAULT_SEPARATOR) && isConfidentialPropertyDefaultSeparator(messageSecurityFlowParameter.getNome(),name))
  339.                 )
  340.         );
  341.     }  
  342.     private static boolean isConfidentialPropertyCustomSeparator(String nome, String check) {
  343.         String [] tmp = nome.split(CostantiProprieta.KEY_PROPERTIES_CUSTOM_SEPARATOR);
  344.         return (tmp!=null && tmp.length>1 && tmp[1]!=null &&
  345.             tmp[1].equals(check));
  346.     }
  347.     private static boolean isConfidentialPropertyDefaultSeparator(String nome, String check) {
  348.         String [] tmp = nome.split(CostantiProprieta.KEY_PROPERTIES_DEFAULT_SEPARATOR);
  349.         return (tmp!=null && tmp.length>1 && tmp[1]!=null &&
  350.             tmp[1].equals(check)) ;
  351.     }
  352.     private static boolean isStartsWithConfidentialPropertyCustomSeparator(String nome, String check) {
  353.         String [] tmp = nome.split(CostantiProprieta.KEY_PROPERTIES_CUSTOM_SEPARATOR);
  354.         return (tmp!=null && tmp.length>1 && tmp[1]!=null &&
  355.             tmp[1].startsWith(check));
  356.     }
  357.     private static boolean isStartsWithConfidentialPropertyDefaultSeparator(String nome, String check) {
  358.         String [] tmp = nome.split(CostantiProprieta.KEY_PROPERTIES_DEFAULT_SEPARATOR);
  359.         return (tmp!=null && tmp.length>1 && tmp[1]!=null &&
  360.             tmp[1].startsWith(check)) ;
  361.     }
  362. }