HSMKeystore.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.certificate.hsm;

  21. import java.io.File;
  22. import java.io.Serializable;
  23. import java.security.Provider;
  24. import java.security.Security;
  25. import java.util.Properties;

  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.certificate.KeyStore;
  28. import org.slf4j.Logger;

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

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = -3572589461109860459L;

  41.     private transient Boolean uniqueProviderInstance;
  42.     private transient Provider providerInstance;
  43.        
  44.     private String id;

  45.     private String provider;
  46.     private boolean providerAdd = false;
  47.     private String configFile;
  48.     private String config;
  49.     private String pin;
  50.     private String keystoreTypeLabel;
  51.     private String keystoreType;
  52.     private boolean usableAsTrustStore = false;
  53.     private boolean usableAsSecretKeyStore = false;
  54.    
  55.     protected HSMKeystore(String id, Properties p, Logger log, boolean accessKeystore) throws UtilsException {
  56.         this.id = id;
  57.        
  58.         if(p==null || p.isEmpty()) {
  59.             log.error("Properties is null");
  60.         }
  61.        
  62.         String prefix = "Property '"+HSMCostanti.PROPERTY_PREFIX+id;
  63.         String uncorrect = "' uncorrect: ";
  64.        
  65.         if(p==null || p.isEmpty()) {
  66.             throw new UtilsException("Properties '"+HSMCostanti.PROPERTY_PREFIX+id+".*' undefined");
  67.         }
  68.        
  69.         this.provider = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_PROVIDER, accessKeystore);  
  70.        
  71.         String tmp = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_PROVIDER_ADD, false);
  72.         if(tmp!=null) {
  73.             try {
  74.                 this.providerAdd = Boolean.parseBoolean(tmp);
  75.             }catch(Exception e) {
  76.                 throw new UtilsException(prefix+"."+HSMCostanti.PROPERTY_SUFFIX_PROVIDER_ADD+uncorrect+e.getMessage(),e);
  77.             }
  78.         }
  79.        
  80.         this.configFile = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_PROVIDER_CONFIG_FILE, false);
  81.         this.config = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_PROVIDER_CONFIG, false);
  82.        
  83.         this.pin = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_PIN, accessKeystore);
  84.        
  85.         this.keystoreType = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_KEYSTORE_TYPE, accessKeystore);  
  86.         this.keystoreTypeLabel = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_KEYSTORE_TYPE_LABEL, true);
  87.        
  88.         tmp = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_USABLE_AS_TRUST_STORE, false);
  89.         if(tmp!=null) {
  90.             try {
  91.                 this.usableAsTrustStore = Boolean.parseBoolean(tmp);
  92.             }catch(Exception e) {
  93.                 throw new UtilsException(prefix+"."+HSMCostanti.PROPERTY_SUFFIX_USABLE_AS_TRUST_STORE+uncorrect+e.getMessage(),e);
  94.             }
  95.         }
  96.        
  97.         tmp = getProperty(id, p, HSMCostanti.PROPERTY_SUFFIX_USABLE_AS_SECRET_KEY_STORE, false);
  98.         if(tmp!=null) {
  99.             try {
  100.                 this.usableAsSecretKeyStore = Boolean.parseBoolean(tmp);
  101.             }catch(Exception e) {
  102.                 throw new UtilsException(prefix+"."+HSMCostanti.PROPERTY_SUFFIX_USABLE_AS_SECRET_KEY_STORE+uncorrect+e.getMessage(),e);
  103.             }
  104.         }
  105.     }

  106.     private static String getProperty(String id, Properties p, String name, boolean required) throws UtilsException {
  107.         String tmp = p.getProperty(name);
  108.         if(tmp!=null) {
  109.             return tmp.trim();
  110.         }
  111.         else {
  112.             if(required) {
  113.                 throw new UtilsException("Property '"+HSMCostanti.PROPERTY_PREFIX+id+"."+name+"' notFound");
  114.             }
  115.             return null;
  116.         }
  117.     }
  118.    
  119.     public String getId() {
  120.         return this.id;
  121.     }
  122.    
  123.     public String getProvider() {
  124.         return this.provider;
  125.     }

  126.     public boolean isProviderAdd() {
  127.         return this.providerAdd;
  128.     }

  129.     public String getConfigFile() {
  130.         return this.configFile;
  131.     }

  132.     public String getConfig() {
  133.         return this.config;
  134.     }

  135.     public String getKeystoreTypeLabel() {
  136.         return this.keystoreTypeLabel;
  137.     }

  138.     public String getKeystoreType() {
  139.         return this.keystoreType;
  140.     }  
  141.    
  142.     public String getPrefixForLog() {
  143.         return "[Keystore '"+this.getId()+"' type:"+this.getKeystoreTypeLabel()+"] ";
  144.     }
  145.    
  146.     // ******* Keystore engine ********
  147.    
  148.     public void init(Logger log, boolean uniqueProviderInstance) throws UtilsException {
  149.         if(this.uniqueProviderInstance==null) {
  150.             initInstance(log, uniqueProviderInstance);
  151.         }
  152.     }
  153.     private synchronized void initInstance(Logger log, boolean uniqueProviderInstance) throws UtilsException {
  154.         if(this.uniqueProviderInstance==null) {
  155.             Provider providerNew = newProviderInstance();
  156.             if(this.isProviderAdd()) {
  157.                 Provider providerCheck = null;
  158.                 try {
  159.                     providerCheck = Security.getProvider(providerNew.getName());
  160.                 }catch(Throwable t) {
  161.                     // ignore
  162.                 }
  163.                 if(providerCheck==null) {
  164.                     Security.addProvider(providerNew);
  165.                     String d = "Registered provider: "+providerNew.getName();
  166.                     log.info(d);
  167.                 }
  168.                 else {
  169.                     String d = "Loaded provider (not registered, already exists): "+providerNew.getName();
  170.                     log.info(d);
  171.                 }
  172.             }
  173.             else {
  174.                 String d = "Loaded provider: "+providerNew.getName();
  175.                 log.info(d);
  176.             }
  177.            
  178.             this.uniqueProviderInstance = uniqueProviderInstance;
  179.             if(this.uniqueProviderInstance!=null && this.uniqueProviderInstance.booleanValue()) {
  180.                 this.providerInstance = providerNew;
  181.             }
  182.         }
  183.     }
  184.     private Provider newProviderInstance() throws UtilsException{
  185.         Provider providerNew = Security.getProvider(this.getProvider());
  186.         String prefix = this.getPrefixForLog();
  187.         if(this.getConfigFile()!=null) {
  188.             File f = new File(this.getConfigFile());
  189.             if(!f.exists()) {
  190.                 throw new UtilsException(prefix+"Configuration file '"+f.getAbsolutePath()+"' not exists");
  191.             }
  192.             else {
  193.                 if(!f.canRead()) {
  194.                     throw new UtilsException(prefix+"Configuration file '"+f.getAbsolutePath()+"' cannot read");
  195.                 }
  196.             }
  197.             providerNew = providerNew.configure(this.getConfigFile());
  198.         }
  199.         else if(this.getConfig()!=null) {
  200.             providerNew = providerNew.configure(this.getConfig());
  201.         }
  202.         return providerNew;
  203.     }
  204.    
  205.     public KeyStore getInstance() throws UtilsException {
  206.         String prefix = this.getPrefixForLog();
  207.        
  208.         Provider providerInstanceGet = null;
  209.         if(this.uniqueProviderInstance==null) {
  210.             throw new UtilsException(prefix+"Provider not initialized");
  211.         }
  212.         if(this.uniqueProviderInstance.booleanValue()) {
  213.             if(this.providerInstance==null) {
  214.                 throw new UtilsException(prefix+"Provider not initialized");
  215.             }
  216.             providerInstanceGet = this.providerInstance;
  217.         }
  218.         else {
  219.             providerInstanceGet = newProviderInstance();
  220.         }
  221.        
  222.         java.security.KeyStore hsmKeyStore = null;
  223.         try {
  224.             hsmKeyStore = java.security.KeyStore.getInstance(this.keystoreType, providerInstanceGet);
  225.             hsmKeyStore.load(null, this.pin.toCharArray());
  226.         }catch(Throwable t) {
  227.             throw new UtilsException(prefix+t.getMessage(),t);
  228.         }
  229.         return new KeyStore(hsmKeyStore, true);
  230.     }
  231.    
  232.     public boolean isUsableAsTrustStore() {
  233.         return this.usableAsTrustStore;
  234.     }
  235.     public boolean isUsableAsSecretKeyStore() {
  236.         return this.usableAsSecretKeyStore;
  237.     }
  238. }