EncryptWrapKey.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 javax.crypto.KeyGenerator;
  25. import javax.crypto.SecretKey;

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

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

  39.     private java.security.Key key;
  40.     private java.security.cert.Certificate certificate;
  41.    
  42.     private byte[] wrappedKey;
  43.     private byte[] iv;

  44.     public EncryptWrapKey(Certificate certificate) {
  45.         this.certificate = certificate;
  46.     }

  47.     public EncryptWrapKey(Key key) {
  48.         this.key = key;
  49.     }

  50.     public EncryptWrapKey(KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException {
  51.         this.key = keystore.getPrivateKey(alias, passwordPrivateKey);
  52.     }

  53.     public EncryptWrapKey(KeyStore keystore, String alias) throws UtilsException {
  54.         this.certificate = keystore.getCertificate(alias);
  55.     }

  56.     public EncryptWrapKey(KeyStore keystore) throws UtilsException {
  57.         this.certificate = keystore.getCertificate();
  58.     }
  59.    
  60.    
  61.     protected byte[] process(String data, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  62.         try{
  63.             return this.process(data.getBytes(charsetName), wrappedKeyAlgorithm, contentAlgorithm);
  64.         }catch(Exception e){
  65.             throw new UtilsException(e.getMessage(),e);
  66.         }
  67.     }
  68.    
  69.     protected byte[] process(byte[] data, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  70.         try{
  71.             int symmetricKeySize = 256;
  72.             if(wrappedKeyAlgorithm.contains("128")) {
  73.                 symmetricKeySize = 128;
  74.             }
  75.             else if(wrappedKeyAlgorithm.contains("192")) {
  76.                 symmetricKeySize = 192;
  77.             }
  78.            
  79.             // Genero chiave simmetrica
  80.             KeyGenerator keyGen = KeyGenerator.getInstance(SymmetricKeyUtils.ALGO_AES);
  81.             keyGen.init(symmetricKeySize);
  82.             SecretKey secretKey = keyGen.generateKey();
  83.            
  84.             // operazione di wrapping con la chiave asincrona
  85.             Cipher cipher = Cipher.getInstance(wrappedKeyAlgorithm);
  86.             if(this.certificate!=null) {
  87.                 cipher.init(Cipher.WRAP_MODE, this.certificate);
  88.             }
  89.             else {
  90.                 cipher.init(Cipher.WRAP_MODE, this.key);
  91.             }

  92.             // Esegue l'operazione di wrapping della chiave simmetrica con la chiave pubblica RSA
  93.             this.wrappedKey = cipher.wrap(secretKey);
  94.            
  95.             // Cifro
  96.             Encrypt encrypt = new Encrypt(secretKey);
  97.             encrypt.initIV(contentAlgorithm);
  98.             this.iv = encrypt.getIV();
  99.            
  100.             return encrypt.encrypt(data, contentAlgorithm);
  101.            
  102.         }catch(Exception e){
  103.             throw new UtilsException(e.getMessage(),e);
  104.         }
  105.     }
  106.    
  107.     public String getWrappedKeyBase64() {
  108.         return Base64Utilities.encodeAsString(this.wrappedKey);
  109.     }
  110.     public String getWrappedKeyHexBinary() throws UtilsException {
  111.         return HexBinaryUtilities.encodeAsString(this.wrappedKey);
  112.     }
  113.     public byte[] getWrappedKey() {
  114.         return this.wrappedKey;
  115.     }
  116.    
  117.     public byte[] getIV() {
  118.         return this.iv;
  119.     }
  120.     public byte[] getIVBase64() {
  121.         return Base64Utilities.encode(this.iv);
  122.     }
  123.     public String getIVBase64AsString() {
  124.         return Base64Utilities.encodeAsString(this.iv);
  125.     }
  126.     public char[] getIVHexBinary() throws UtilsException{
  127.         return HexBinaryUtilities.encode(this.iv);
  128.     }  
  129.     public String getIVHexBinaryAsString() throws UtilsException{
  130.         return HexBinaryUtilities.encodeAsString(this.iv);
  131.     }
  132.    
  133.    
  134.     public byte[] encrypt(String data, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  135.         return this.process(data, charsetName, wrappedKeyAlgorithm, contentAlgorithm);
  136.     }
  137.     public byte[] encrypt(byte[] data, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  138.         return this.process(data, wrappedKeyAlgorithm, contentAlgorithm);
  139.     }
  140.    
  141.     public byte[] encryptBase64(String data, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  142.         return Base64Utilities.encode(this.process(data, charsetName, wrappedKeyAlgorithm, contentAlgorithm));
  143.     }
  144.     public byte[] encryptBase64(byte[] data, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  145.         return Base64Utilities.encode(this.process(data, wrappedKeyAlgorithm, contentAlgorithm));
  146.     }
  147.    
  148.     public String encryptBase64AsString(String data, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  149.         return Base64Utilities.encodeAsString(this.process(data, charsetName, wrappedKeyAlgorithm, contentAlgorithm));
  150.     }
  151.     public String encryptBase64AsString(byte[] data, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  152.         return Base64Utilities.encodeAsString(this.process(data, wrappedKeyAlgorithm, contentAlgorithm));
  153.     }
  154.    
  155.     public char[] encryptHexBinary(String data, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  156.         return HexBinaryUtilities.encode(this.process(data, charsetName, wrappedKeyAlgorithm, contentAlgorithm));
  157.     }
  158.     public char[] encryptHexBinary(byte[] data, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  159.         return HexBinaryUtilities.encode(this.process(data, wrappedKeyAlgorithm, contentAlgorithm));
  160.     }
  161.    
  162.     public String encryptHexBinaryAsString(String data, String charsetName, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  163.         return HexBinaryUtilities.encodeAsString(this.process(data, charsetName, wrappedKeyAlgorithm, contentAlgorithm));
  164.     }
  165.     public String encryptHexBinaryAsString(byte[] data, String wrappedKeyAlgorithm, String contentAlgorithm) throws UtilsException{
  166.         return HexBinaryUtilities.encodeAsString(this.process(data, wrappedKeyAlgorithm, contentAlgorithm));
  167.     }
  168. }