MerlinTruststore.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.KeyStore;
  23. import java.security.cert.Certificate;
  24. import java.util.Properties;

  25. import org.openspcoop2.core.commons.DBUtils;
  26. import org.openspcoop2.security.SecurityException;
  27. import org.openspcoop2.utils.certificate.KeystoreType;
  28. import org.openspcoop2.utils.certificate.hsm.HSMManager;

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

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;
  41.    
  42.     private transient org.openspcoop2.utils.certificate.KeyStore ks = null;
  43.     private byte[] ksBytes;
  44.     private String tipoStore = null;
  45.     private String pathStore = null;
  46.     private String passwordStore = null;
  47.    
  48.     private boolean hsm;
  49.    
  50.     @Override
  51.     public String toString() {
  52.         StringBuilder bf = new StringBuilder();
  53.         bf.append("TrustStore (").append(this.tipoStore).append(") ").append(this.pathStore);
  54.         return bf.toString();
  55.     }
  56.    
  57.     public MerlinTruststore(String propertyFilePath) throws SecurityException{
  58.        
  59.         Properties propStore = StoreUtils.readProperties("PropertyFilePath", propertyFilePath);
  60.         initMerlinTruststoreEngine(propStore);
  61.                
  62.     }
  63.     public MerlinTruststore(Properties propStore) throws SecurityException{
  64.        
  65.         initMerlinTruststoreEngine(propStore);
  66.        
  67.     }
  68.    
  69.     private void initMerlinTruststoreEngine(Properties propStore) throws SecurityException{
  70.        
  71.         try{
  72.             if(propStore==null){
  73.                 throw new SecurityException("Properties per lo Store non indicato");
  74.             }
  75.            
  76.             this.tipoStore = propStore.getProperty(KeystoreConstants.PROPERTY_KEYSTORE_TYPE);
  77.             if(this.tipoStore!=null){
  78.                 this.tipoStore = this.tipoStore.trim();
  79.             }else{
  80.                 this.tipoStore = KeyStore.getDefaultType();
  81.             }
  82.            
  83.             this.pathStore = propStore.getProperty(KeystoreConstants.PROPERTY_KEYSTORE_PATH);
  84.            
  85.             this.passwordStore = propStore.getProperty(KeystoreConstants.PROPERTY_KEYSTORE_PASSWORD);
  86.            
  87.             init();
  88.            
  89.         }catch(Exception e){
  90.             throw new SecurityException(e.getMessage(),e);
  91.         }
  92.        
  93.     }
  94.        
  95.     public MerlinTruststore(String pathStore,String tipoStore,String passwordStore) throws SecurityException{
  96.        
  97.         this.pathStore = pathStore;
  98.         this.tipoStore = tipoStore;
  99.         this.passwordStore = passwordStore;
  100.        
  101.         init();
  102.        
  103.     }
  104.    
  105.     public MerlinTruststore(byte[]bytesKeystore,String tipoStore,String passwordStore) throws SecurityException{
  106.        
  107.         this.ksBytes = bytesKeystore;
  108.         this.tipoStore = tipoStore;
  109.         this.passwordStore = passwordStore;
  110.        
  111.         init();
  112.        
  113.     }
  114.    
  115.     private void init() throws SecurityException{
  116.         try{
  117.             if(this.tipoStore==null){
  118.                 throw new SecurityException("Tipo dello Store non indicato");
  119.             }
  120.             if(this.passwordStore==null){
  121.                 boolean required = true;
  122.                 if(KeystoreType.JKS.isType(this.tipoStore)) {
  123.                     required = DBUtils.isTruststoreJksPasswordRequired();
  124.                 }
  125.                 else if(KeystoreType.PKCS12.isType(this.tipoStore)) {
  126.                     required = DBUtils.isTruststorePkcs12PasswordRequired();
  127.                 }
  128.                 if(required) {
  129.                     throw new SecurityException("Password dello Store non indicata");
  130.                 }
  131.             }
  132.            
  133.             HSMManager hsmManager = HSMManager.getInstance();
  134.             if(hsmManager!=null) {
  135.                 this.hsm = hsmManager.existsKeystoreType(this.tipoStore);
  136.             }
  137.            
  138.             if(!this.hsm) {
  139.            
  140.                 if(this.ksBytes==null && this.pathStore==null){
  141.                     throw new SecurityException("Path per lo Store non indicato");
  142.                 }
  143.                
  144.                 if(this.ksBytes==null) {
  145.                     this.ksBytes = StoreUtils.readContent("Path", this.pathStore);
  146.                 }
  147.                
  148.             }

  149.             this.initKS();
  150.            
  151.         }catch(Exception e){
  152.             throw new SecurityException(e.getMessage(),e);
  153.         }
  154.     }
  155.    
  156.     public boolean isHsm() {
  157.         return this.hsm;
  158.     }
  159.    
  160.     private void checkInit() throws SecurityException{
  161.         if(this.ks==null) {
  162.             this.initKS();
  163.         }
  164.     }
  165.     private synchronized void initKS() throws SecurityException{
  166.         if(this.ks==null) {
  167.             try {
  168.                 if(this.hsm) {
  169.                     this.ks = HSMManager.getInstance().getKeystore(this.tipoStore);
  170.                 }
  171.                 else {
  172.                     this.ks = new org.openspcoop2.utils.certificate.KeyStore(this.ksBytes, this.tipoStore, this.passwordStore);
  173.                    
  174.                     // non utilizzabile in hsm, si ottiene errore: java.lang.UnsupportedOperationException: trusted certificates may only be set by token initialization application
  175.                     // at jdk.crypto.cryptoki/sun.security.pkcs11.P11KeyStore.engineSetEntry(P11KeyStore.java:1022)
  176.                     FixTrustAnchorsNotEmpty.addCertificate(this.ks.getKeystore());
  177.                 }
  178.             }
  179.             catch(Exception e){
  180.                 throw new SecurityException(e.getMessage(),e);
  181.             }
  182.         }
  183.     }
  184.    
  185.    
  186.     public Certificate getCertificate(String alias) throws SecurityException {
  187.         if(alias==null) {
  188.             throw new SecurityException("Alias non fornito");
  189.         }
  190.         this.checkInit(); // per ripristino da Serializable
  191.         try{
  192.             return this.ks.getCertificate(alias);
  193.         }catch(Exception e){
  194.             throw new SecurityException(e.getMessage(),e);
  195.         }
  196.     }
  197.    
  198.     public org.openspcoop2.utils.certificate.KeyStore getTrustStore() throws SecurityException {
  199.         this.checkInit(); // per ripristino da Serializable
  200.         try{
  201.             return this.ks;
  202.         }catch(Exception e){
  203.             throw new SecurityException(e.getMessage(),e);
  204.         }
  205.     }

  206.     public String getTipoStore() {
  207.         return this.tipoStore;
  208.     }

  209.     public String getPathStore() {
  210.         return this.pathStore;
  211.     }

  212.     public String getPasswordStore() {
  213.         return this.passwordStore;
  214.     }
  215. }