SymmetricKeystore.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 javax.crypto.spec.SecretKeySpec;

  24. import org.openspcoop2.security.Constants;
  25. import org.openspcoop2.security.SecurityException;
  26. import org.openspcoop2.utils.certificate.KeyStore;
  27. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;

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

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;
  40.        
  41.     private SecretKeySpec key = null;
  42.     private transient KeyStore keyStore = null;
  43.     private String pwKey = null;
  44.     private String alias = null;
  45.    
  46.     @Override
  47.     public String toString() {
  48.         StringBuilder bf = new StringBuilder();
  49.         bf.append("SymmetricKeystore (").append(this.alias).append(") ");
  50.         return bf.toString();
  51.     }
  52.    
  53.     public SymmetricKeystore(String alias,String key,String algoritmo) throws SecurityException{
  54.         this(alias, key, algoritmo, null);
  55.     }
  56.     public SymmetricKeystore(String alias,String key,String algoritmo, BYOKRequestParams requestParams) throws SecurityException{
  57.         try{
  58.             String algorithm = null;
  59.             if(Constants.TRIPLE_DES.equals(algoritmo)){
  60.                 algorithm = "DESede";
  61.             }
  62.             else{
  63.                 algorithm = "DES"; // default
  64.             }

  65.             byte[] keyBytes = key.getBytes();
  66.            
  67.             if(requestParams!=null) {
  68.                 keyBytes = StoreUtils.unwrapBYOK(keyBytes, requestParams);  
  69.             }
  70.            
  71.             this.key = new SecretKeySpec(keyBytes,algorithm);
  72.            
  73.             this.alias = alias;
  74.             this.pwKey = "PW_CUSTOM";
  75.            
  76.             this.initKS();
  77.            
  78.         }catch(Exception e){
  79.             throw new SecurityException(e.getMessage(),e);
  80.         }
  81.     }
  82.    
  83.     private void checkInit() throws SecurityException{
  84.         if(this.keyStore==null) {
  85.             this.initKS();
  86.         }
  87.     }
  88.     private synchronized void initKS() throws SecurityException{
  89.         if(this.keyStore==null) {
  90.             try {
  91.                 java.security.KeyStore keyStoreJCEKS = java.security.KeyStore.getInstance("JCEKS");
  92.                 keyStoreJCEKS.load(null);
  93.                 keyStoreJCEKS.setKeyEntry(this.alias, this.key,this.pwKey.toCharArray(), null);
  94.                 this.keyStore = new KeyStore(keyStoreJCEKS);
  95.                 FixTrustAnchorsNotEmpty.addCertificate(this.keyStore.getKeystore());            
  96.             }
  97.             catch(Exception e){
  98.                 throw new SecurityException(e.getMessage(),e);
  99.             }
  100.         }
  101.     }
  102.    
  103.    
  104.     public Key getKey() {
  105.         return this.key;
  106.     }

  107.     public KeyStore getKeyStore() throws SecurityException {
  108.         this.checkInit(); // per ripristino da Serializable
  109.         return this.keyStore;
  110.     }

  111.     public String getPasswordKey() {
  112.         return this.pwKey;
  113.     }

  114. }