MultiKeystore.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.security.Key;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Properties;

  28. import org.openspcoop2.security.SecurityException;
  29. import org.openspcoop2.utils.LoggerWrapperFactory;
  30. import org.openspcoop2.utils.certificate.KeyStore;
  31. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;

  32. /**
  33.  * MultiKeystore
  34.  *
  35.  * @author Andrea Poli (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class MultiKeystore implements Serializable  {

  40.     /**
  41.      *
  42.      */
  43.     private static final long serialVersionUID = 1L;
  44.    
  45.     private static final String ALIASES = "aliases";
  46.     private static final String KEYSTORE_TYPE = ".keystore.type";
  47.     private static final String KEYSTORE_PATH = ".keystore.path";
  48.     private static final String KEYSTORE_PASSWORD = ".keystore.password";
  49.     private static final String KEY_ALIAS = ".key.alias";
  50.     private static final String KEY_PASSWORD = ".key.password";
  51.     private static final String KEY_VALUE = ".key.value";
  52.     private static final String KEY_ALGORITHM = ".key.algorithm";
  53.    
  54.     private List<String> aliasesList = new ArrayList<>();
  55.     private Map<String, Serializable> keystores = new HashMap<>();
  56.     private Map<String, String> mappingAliasToKeystorePath = new HashMap<>();
  57.     private Map<String, String> mappingAliasToKeystorePassword = new HashMap<>();
  58.     private Map<String, String> mappingAliasToKeystoreType = new HashMap<>();
  59.     private Map<String, String> mappingAliasToKeyAlias = new HashMap<>();
  60.     private Map<String, String> mappingAliasToKeyPassword = new HashMap<>();
  61.    
  62.     @Override
  63.     public String toString() {
  64.         StringBuilder bf = new StringBuilder();
  65.         bf.append("MultiKeystore ").append(this.aliasesList);
  66.         return bf.toString();
  67.     }
  68.    
  69.     public MultiKeystore(String propertyFilePath) throws SecurityException{
  70.         this(propertyFilePath, null);
  71.     }
  72.     public MultiKeystore(String propertyFilePath, BYOKRequestParams requestParams) throws SecurityException{
  73.        
  74.         try{
  75.             Properties multiProperties = StoreUtils.readProperties("MultiProperties", propertyFilePath);
  76.            
  77.             String keyAliases = getProperty(multiProperties, MultiKeystore.ALIASES);
  78.             String [] tmp = keyAliases.split(",");
  79.             for (int i = 0; i < tmp.length; i++) {
  80.                 if(!this.aliasesList.contains(tmp[i].trim()))
  81.                     this.aliasesList.add(tmp[i].trim());
  82.             }
  83.            
  84.             for (String alias : this.aliasesList) {
  85.                 String keyAlias = getSafeKeyAlias(alias, multiProperties);
  86.                 String keyPassword = getProperty(multiProperties, alias+MultiKeystore.KEY_PASSWORD);
  87.                
  88.                 String keyValue = multiProperties.getProperty(alias+MultiKeystore.KEY_VALUE);
  89.                
  90.                 if(keyValue!=null){
  91.                     // Configurazione con keyValue fornita direttamente
  92.                     String keyAlgorithm = getProperty(multiProperties, alias+MultiKeystore.KEY_ALGORITHM);
  93.                     addSymmetricKeystore(alias, keyAlias, keyValue, keyAlgorithm, requestParams);
  94.                 }
  95.                 else{
  96.                     // Configurazione con MerlinKeystore
  97.                     String keystoreType = getProperty(multiProperties, alias+MultiKeystore.KEYSTORE_TYPE);
  98.                     String keystorePath = getProperty(multiProperties, alias+MultiKeystore.KEYSTORE_PATH);
  99.                     String keystorePassword = getProperty(multiProperties, alias+MultiKeystore.KEYSTORE_PASSWORD);
  100.                     addMerlinKeystore(alias, keyAlias, keystoreType, keystorePath, keystorePassword, keyPassword, requestParams);
  101.                 }
  102.                
  103.                 this.mappingAliasToKeyAlias.put(alias, keyAlias);
  104.                 this.mappingAliasToKeyPassword.put(alias, keyPassword);
  105.             }
  106.            
  107.         }catch(Exception e){
  108.             throw new SecurityException(e.getMessage(),e);
  109.         }
  110.     }
  111.    
  112.     private String getSafeKeyAlias(String alias, Properties multiProperties) {
  113.         String keyAlias = null;
  114.         try{
  115.             keyAlias = getProperty(multiProperties, alias+MultiKeystore.KEY_ALIAS);
  116.         }catch(Exception e){
  117.             keyAlias = alias;
  118.         }
  119.         return keyAlias;
  120.     }
  121.    
  122.     private void addSymmetricKeystore(String alias, String keyAlias, String keyValue, String keyAlgorithm, BYOKRequestParams requestParams) {
  123.         try {
  124.             this.keystores.put(alias, new SymmetricKeystore(keyAlias, keyValue, keyAlgorithm, requestParams));
  125.         }catch(Exception e) {
  126.             String idKeystore = "!!! Errore durante il caricamento del SymmetricKeystore !!! [keyAlias:"+keyAlias+"] ";
  127.             LoggerWrapperFactory.getLogger(MultiKeystore.class).error(idKeystore+e.getMessage(),e);
  128.         }
  129.     }
  130.     private void addMerlinKeystore(String alias, String keyAlias, String keystoreType, String keystorePath, String keystorePassword, String keyPassword, BYOKRequestParams requestParams) {
  131.         try {
  132.             this.keystores.put(alias, new MerlinKeystore(keystorePath, keystoreType, keystorePassword, keyPassword, requestParams));
  133.             this.mappingAliasToKeystoreType.put(alias, keystoreType);
  134.             this.mappingAliasToKeystorePath.put(alias, keystorePath);
  135.             this.mappingAliasToKeystorePassword.put(alias, keystorePassword);
  136.         }catch(Exception e) {
  137.             String idKeystore = "!!! Errore durante il caricamento del MerlinKeystore !!! [keyAlias:"+keyAlias+"] ";
  138.             LoggerWrapperFactory.getLogger(MultiKeystore.class).error(idKeystore+e.getMessage(),e);
  139.         }
  140.     }
  141.    
  142.     private String getProperty(Properties multiProperties,String property)throws SecurityException{
  143.         if(!multiProperties.containsKey(property)){
  144.             throw new SecurityException("Proprieta' ["+property+"] non trovata nel MultiProperties file");
  145.         }
  146.         else{
  147.             return multiProperties.getProperty(property).trim();
  148.         }
  149.     }
  150.    
  151.     private String getErrorAlias(String alias) {
  152.         return "Alias["+alias+"] non trovato nella configurazione MultiKeystore";
  153.     }
  154.    
  155.     public Key getKey(String alias) throws SecurityException {
  156.         try{
  157.             if(!this.aliasesList.contains(alias)){
  158.                 throw new SecurityException(getErrorAlias(alias));
  159.             }
  160.            
  161.             Object keystore = this.keystores.get(alias);
  162.             if(keystore==null) {
  163.                 throw new SecurityException("Non esiste un keystore per l'alias["+alias+"]; verificare che non sia avvenuti errori durante l'inizializzazione (MultiKeystore)");
  164.             }
  165.             if(keystore instanceof MerlinKeystore){
  166.                 return ((MerlinKeystore)keystore).getKey(this.mappingAliasToKeyAlias.get(alias));
  167.             }
  168.             else if(keystore instanceof SymmetricKeystore){
  169.                 return ((SymmetricKeystore)keystore).getKey();
  170.             }
  171.             else{
  172.                 throw new SecurityException("Tipo di Keystore non supportato: "+keystore.getClass().getName());
  173.             }
  174.         }catch(Exception e){
  175.             throw new SecurityException(e.getMessage(),e);
  176.         }
  177.     }

  178.     public boolean existsAlias(String alias){
  179.         return this.aliasesList.contains(alias);
  180.     }
  181.    
  182.     public KeyStore getKeyStore(String alias) throws SecurityException {
  183.         try{
  184.             if(!this.aliasesList.contains(alias)){
  185.                 throw new SecurityException(getErrorAlias(alias));
  186.             }
  187.            
  188.             Object keystore = this.keystores.get(alias);
  189.             if(keystore==null) {
  190.                 throw new SecurityException("Non esiste un keystore per l'alias["+alias+"]; verificare che non sia avvenuti errori durante l'inizializzazione (MultiKeystore)");
  191.             }
  192.             if(keystore instanceof MerlinKeystore){
  193.                 return ((MerlinKeystore)keystore).getKeyStore();
  194.             }
  195.             else if(keystore instanceof SymmetricKeystore){
  196.                 return ((SymmetricKeystore)keystore).getKeyStore();
  197.             }
  198.             else{
  199.                 throw new SecurityException("Tipo di Keystore non supportato: "+keystore.getClass().getName());
  200.             }
  201.            
  202.         }catch(Exception e){
  203.             throw new SecurityException(e.getMessage(),e);
  204.         }
  205.     }

  206.     public String getKeyAlias(String alias) throws SecurityException {
  207.         try{
  208.             if(!this.aliasesList.contains(alias)){
  209.                 throw new SecurityException(getErrorAlias(alias));
  210.             }
  211.            
  212.             return this.mappingAliasToKeyAlias.get(alias);
  213.         }catch(Exception e){
  214.             throw new SecurityException(e.getMessage(),e);
  215.         }
  216.     }
  217.    
  218.     public String getKeyPassword(String alias) throws SecurityException {
  219.         try{
  220.             if(!this.aliasesList.contains(alias)){
  221.                 throw new SecurityException(getErrorAlias(alias));
  222.             }
  223.            
  224.             return this.mappingAliasToKeyPassword.get(alias);
  225.         }catch(Exception e){
  226.             throw new SecurityException(e.getMessage(),e);
  227.         }
  228.     }
  229.    
  230.     public String getKeystorePath(String alias) throws SecurityException {
  231.         try{
  232.             if(!this.aliasesList.contains(alias)){
  233.                 throw new SecurityException(getErrorAlias(alias));
  234.             }
  235.            
  236.             return this.mappingAliasToKeystorePath.get(alias);
  237.         }catch(Exception e){
  238.             throw new SecurityException(e.getMessage(),e);
  239.         }
  240.     }
  241.    
  242.     public String getKeystorePassword(String alias) throws SecurityException {
  243.         try{
  244.             if(!this.aliasesList.contains(alias)){
  245.                 throw new SecurityException(getErrorAlias(alias));
  246.             }
  247.            
  248.             return this.mappingAliasToKeystorePassword.get(alias);
  249.         }catch(Exception e){
  250.             throw new SecurityException(e.getMessage(),e);
  251.         }
  252.     }
  253.    
  254.     public String getKeystoreType(String alias) throws SecurityException {
  255.         try{
  256.             if(!this.aliasesList.contains(alias)){
  257.                 throw new SecurityException(getErrorAlias(alias));
  258.             }
  259.            
  260.             return this.mappingAliasToKeystoreType.get(alias);
  261.         }catch(Exception e){
  262.             throw new SecurityException(e.getMessage(),e);
  263.         }
  264.     }
  265.    
  266.     public String getInternalConfigAlias(String keyAlias) throws SecurityException {
  267.         try{
  268.             if(!this.mappingAliasToKeyAlias.containsValue(keyAlias)){
  269.                 throw new SecurityException(getErrorAlias(keyAlias));
  270.             }
  271.             for (Map.Entry<String,String> entry : this.mappingAliasToKeyAlias.entrySet()) {
  272.                 if(entry.getValue().equals(keyAlias)) {
  273.                     return entry.getKey();
  274.                 }
  275.             }
  276.            
  277.             throw new SecurityException(getErrorAlias(keyAlias));
  278.         }catch(Exception e){
  279.             throw new SecurityException(e.getMessage(),e);
  280.         }
  281.     }
  282.    
  283. }