EncryptOpenSSLPassPBKDF2.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.spec.KeySpec;

  22. import javax.crypto.SecretKey;
  23. import javax.crypto.SecretKeyFactory;
  24. import javax.crypto.spec.IvParameterSpec;
  25. import javax.crypto.spec.PBEKeySpec;
  26. import javax.crypto.spec.SecretKeySpec;

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

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

  39.      /**
  40.       *
  41.       * Openssl encrypts data using the following steps:
  42.       * 1. salt = 8-byte cryptographically-strong random number
  43.       * 2. key = PBKDF2(password+salt)
  44.       * 3. iv = derivated with PBKDF2
  45.       * 4. cipherTextRaw = encrypt("aes256cbc", key, iv, textPlain)
  46.       * 5. cipherText = "Salted__"+salt+cipherTextRaw
  47.     */
  48.     public static CipherInfo buildCipherInfo(String password, Integer iterationCount, OpenSSLEncryptionMode mode) throws UtilsException {
  49.        
  50.         CipherInfo cipherInfo = new CipherInfo();
  51.        
  52.         cipherInfo.setSalt(EncryptOpenSSLPass.buildSalt());
  53.        
  54.         buildSecretKeyAndIV(password, cipherInfo.getSalt(), iterationCount, mode, cipherInfo);
  55.        
  56.         return cipherInfo;
  57.     }
  58.     static void buildSecretKeyAndIV(String password, byte[] salt, Integer iterationCountParam, OpenSSLEncryptionMode modeParam, CipherInfo cipherInfo) throws UtilsException {
  59.         try {
  60.             int keylen = -1;
  61.             int ivlen = 16;
  62.             OpenSSLEncryptionMode mode = modeParam!=null ? modeParam : OpenSSLEncryptionMode.AES_256_CBC;
  63.             switch (mode) {
  64.             case AES_128_CBC:
  65.                 keylen = 16; // AES-128 richiede una chiave di 128 bit (16 byte).
  66.                 break;
  67.             case AES_192_CBC:
  68.                 keylen = 24;// AES-192 richiede una chiave di 192 bit (24 byte).
  69.                 break;
  70.             case AES_256_CBC:
  71.                 keylen = 32; // AES-256 richiede una chiave di 256 bit (32 byte).
  72.                 break;
  73.             }
  74.            
  75.             SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
  76.             int iterationCount = iterationCountParam == null || iterationCountParam.intValue()<=0 ? 10000 : iterationCountParam.intValue();
  77.             KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, (keylen+ivlen)*8);
  78.             SecretKey tmp = factory.generateSecret(spec);
  79.             byte[] keyandIV = tmp.getEncoded();
  80.            
  81.             SecretKey secretKey = new SecretKeySpec(keyandIV,0,keylen,SymmetricKeyUtils.ALGO_AES);
  82.             cipherInfo.setKey(secretKey);
  83.             cipherInfo.setEncodedKey(secretKey.getEncoded());
  84.            
  85.             // Derive IV using PBKDF2
  86.             IvParameterSpec iv = convertTo(keyandIV,keylen,ivlen);
  87.             cipherInfo.setIv(iv.getIV());
  88.             cipherInfo.setIvParameterSpec(iv);
  89.            
  90.         }catch(Exception e) {
  91.             throw new UtilsException(e.getMessage(),e);
  92.         }
  93.     }
  94.     static IvParameterSpec convertTo(byte[] keyandIV, int keylen, int ivlen) {
  95.         return new IvParameterSpec(keyandIV,keylen,ivlen);
  96.     }
  97.    

  98.     private CipherInfo cipherInfo;
  99.     private OpenSSLEncryptionMode mode;
  100.    
  101.     public EncryptOpenSSLPassPBKDF2(String password) throws UtilsException {
  102.         this(password, null, null);
  103.     }
  104.     public EncryptOpenSSLPassPBKDF2(String password, Integer iterationCount) throws UtilsException {
  105.         this(password, iterationCount, null);
  106.     }
  107.     public EncryptOpenSSLPassPBKDF2(String password, OpenSSLEncryptionMode modeParam) throws UtilsException {
  108.         this(password, null, modeParam);
  109.     }
  110.     public EncryptOpenSSLPassPBKDF2(String password, Integer iterationCount, OpenSSLEncryptionMode modeParam) throws UtilsException {
  111.         super(javax.crypto.Cipher.ENCRYPT_MODE);
  112.         this.mode = modeParam!=null ? modeParam : OpenSSLEncryptionMode.AES_256_CBC;
  113.         this.cipherInfo = buildCipherInfo(password, iterationCount, this.mode);
  114.         this.key = this.cipherInfo.getKey();
  115.         this.ivParameterSpec = this.cipherInfo.getIvParameterSpec();
  116.     }
  117.    
  118.    
  119.     public byte[] encrypt(String data, String charsetName) throws UtilsException{
  120.         return EncryptOpenSSLPass.formatOutput(this.cipherInfo.getSalt(), super.process(data, charsetName, EncryptOpenSSLPass.getAlgorithm(this.mode)));
  121.     }
  122.     public byte[] encrypt(byte[] data) throws UtilsException{
  123.         return EncryptOpenSSLPass.formatOutput(this.cipherInfo.getSalt(), super.process(data, EncryptOpenSSLPass.getAlgorithm(this.mode)));
  124.     }
  125.    
  126.     public byte[] encryptBase64(String data, String charsetName) throws UtilsException{
  127.         return Base64Utilities.encode(this.encrypt(data, charsetName));
  128.     }
  129.     public byte[] encryptBase64(byte[] data) throws UtilsException{
  130.         return Base64Utilities.encode(this.encrypt(data));
  131.     }
  132.    
  133.     public String encryptBase64AsString(String data, String charsetName) throws UtilsException{
  134.         return Base64Utilities.encodeAsString(this.encrypt(data, charsetName));
  135.     }
  136.     public String encryptBase64AsString(byte[] data) throws UtilsException{
  137.         return Base64Utilities.encodeAsString(this.encrypt(data));
  138.     }
  139.    
  140.     public char[] encryptHexBinary(String data, String charsetName) throws UtilsException{
  141.         return HexBinaryUtilities.encode(this.encrypt(data, charsetName));
  142.     }
  143.     public char[] encryptHexBinary(byte[] data) throws UtilsException{
  144.         return HexBinaryUtilities.encode(this.encrypt(data));
  145.     }
  146.    
  147.     public String encryptHexBinaryAsString(String data, String charsetName) throws UtilsException{
  148.         return HexBinaryUtilities.encodeAsString(this.encrypt(data, charsetName));
  149.     }
  150.     public String encryptHexBinaryAsString(byte[] data) throws UtilsException{
  151.         return HexBinaryUtilities.encodeAsString(this.encrypt(data));
  152.     }
  153.    
  154.     @Override
  155.     public void initIV(String algorithm) throws UtilsException{
  156.         // NOP
  157.         // Non deve fare nulla questa chiamata, viene gestita dalla funzione sopra l'IV
  158.     }
  159. }