DecryptWrapKey.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.security;

  21. import java.security.Key;
  22. import java.security.cert.Certificate;

  23. import javax.crypto.Cipher;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.certificate.KeyStore;
  26. import org.openspcoop2.utils.certificate.SymmetricKeyUtils;
  27. import org.openspcoop2.utils.io.Base64Utilities;
  28. import org.openspcoop2.utils.io.HexBinaryUtilities;

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

  37.     private java.security.Key key;
  38.     private java.security.cert.Certificate certificate;
  39.    
  40.     public DecryptWrapKey(Certificate certificate) {
  41.         this.certificate = certificate;
  42.     }

  43.     public DecryptWrapKey(Key key) {
  44.         this.key = key;
  45.     }

  46.     public DecryptWrapKey(KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException {
  47.         this.key = keystore.getPrivateKey(alias, passwordPrivateKey);
  48.     }

  49.     public DecryptWrapKey(KeyStore keystore, String alias) throws UtilsException {
  50.         this.certificate = keystore.getCertificate(alias);
  51.     }

  52.     public DecryptWrapKey(KeyStore keystore) throws UtilsException {
  53.         this.certificate = keystore.getCertificate();
  54.     }
  55.    
  56.    
  57.     protected byte[] process(String data, String charsetName,
  58.             byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  59.         try{
  60.             return this.process(data.getBytes(charsetName),
  61.                     wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm);
  62.         }catch(Exception e){
  63.             throw new UtilsException(e.getMessage(),e);
  64.         }
  65.     }
  66.    
  67.     protected byte[] process(byte[] data,
  68.             byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  69.         try{            
  70.             // operazione di wrapping con la chiave asincrona
  71.             Cipher cipher = Cipher.getInstance(wrappedKeyAlgorithm);
  72.             if(this.certificate!=null) {
  73.                 cipher.init(Cipher.UNWRAP_MODE, this.certificate);
  74.             }
  75.             else {
  76.                 cipher.init(Cipher.UNWRAP_MODE, this.key);
  77.             }

  78.             // Esegue l'operazione di wrapping della chiave simmetrica con la chiave pubblica RSA
  79.             Key secretKey = cipher.unwrap(wrappedKey, SymmetricKeyUtils.ALGO_AES, Cipher.SECRET_KEY);
  80.            
  81.             // Decifro
  82.             Decrypt decrypt = new Decrypt(secretKey, iv);
  83.            
  84.             return decrypt.decrypt(data, contentAlgorithm);
  85.            
  86.         }catch(Exception e){
  87.             throw new UtilsException(e.getMessage(),e);
  88.         }
  89.     }
  90.    
  91.    
  92.    
  93.    
  94.     public byte[] decrypt(String data, byte[] wrappedKey, byte[] iv, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  95.         return this.process(data, charsetName, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm);
  96.     }
  97.     public byte[] decrypt(byte[] data, byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  98.         return this.process(data, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm);
  99.     }
  100.    
  101.     public byte[] decryptBase64(String data, byte[] wrappedKey, byte[] iv, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  102.         return Base64Utilities.encode(this.process(data, charsetName, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  103.     }
  104.     public byte[] decryptBase64(byte[] data, byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  105.         return Base64Utilities.encode(this.process(data, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  106.     }
  107.    
  108.     public String decryptBase64AsString(String data, byte[] wrappedKey, byte[] iv, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  109.         return Base64Utilities.encodeAsString(this.process(data, charsetName, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  110.     }
  111.     public String decryptBase64AsString(byte[] data, byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  112.         return Base64Utilities.encodeAsString(this.process(data, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  113.     }
  114.    
  115.     public char[] decryptHexBinary(String data, byte[] wrappedKey, byte[] iv, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  116.         return HexBinaryUtilities.encode(this.process(data, charsetName, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  117.     }
  118.     public char[] decryptHexBinary(byte[] data, byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  119.         return HexBinaryUtilities.encode(this.process(data, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  120.     }
  121.    
  122.     public String decryptHexBinaryAsString(String data, byte[] wrappedKey, byte[] iv, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  123.         return HexBinaryUtilities.encodeAsString(this.process(data, charsetName, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  124.     }
  125.     public String decryptHexBinaryAsString(byte[] data, byte[] wrappedKey, byte[] iv, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  126.         return HexBinaryUtilities.encodeAsString(this.process(data, wrappedKey, iv, wrappedKeyAlgorithm, contentAlgorithm));
  127.     }
  128. }