DecryptOpenSSLPassPBKDF2.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 org.openspcoop2.utils.UtilsException;
  22. import org.openspcoop2.utils.io.Base64Utilities;
  23. import org.openspcoop2.utils.io.HexBinaryUtilities;

  24. /**
  25.  * Encrypt
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class DecryptOpenSSLPassPBKDF2 extends AbstractCipher {

  32.     /**
  33.       *
  34.       * Openssl encrypts data using the following steps:
  35.       * 1. bytes = cipherText
  36.       * 2. salt = bytes[8,16)
  37.       * 3. key = PBKDF2(password+salt)
  38.       * 4. iv = derivated with PBKDF2
  39.       * 5. plainText = decrypt("aes256cbc", key, iv, bytes[16..end])
  40.     */

  41.     public static CipherInfo buildCipherInfo(byte[] cipherBytes, String password, Integer iterationCount, OpenSSLEncryptionMode mode) throws UtilsException {
  42.        
  43.         CipherInfo cipherInfo = new CipherInfo();
  44.        
  45.         cipherInfo.setSalt(DecryptOpenSSLPass.buildSalt(cipherBytes));
  46.        
  47.         EncryptOpenSSLPassPBKDF2.buildSecretKeyAndIV(password, cipherInfo.getSalt(), iterationCount, mode, cipherInfo);
  48.        
  49.         return cipherInfo;
  50.     }
  51.    
  52.     private OpenSSLEncryptionMode mode;
  53.     private String password;
  54.     private Integer iterationCount;
  55.    
  56.     public DecryptOpenSSLPassPBKDF2(String password) {
  57.         this(password, null, null);
  58.     }
  59.     public DecryptOpenSSLPassPBKDF2(String password, Integer iterationCount) {
  60.         this(password, iterationCount, null);
  61.     }
  62.     public DecryptOpenSSLPassPBKDF2(String password, OpenSSLEncryptionMode modeParam) {
  63.         this(password, null, modeParam);
  64.     }
  65.     public DecryptOpenSSLPassPBKDF2(String password, Integer iterationCount, OpenSSLEncryptionMode modeParam) {
  66.         super(javax.crypto.Cipher.DECRYPT_MODE);
  67.         this.mode = modeParam!=null ? modeParam : OpenSSLEncryptionMode.AES_256_CBC;
  68.         this.password = password;
  69.         this.iterationCount = iterationCount;
  70.     }


  71.     public byte[] decrypt(byte[] data) throws UtilsException{
  72.         CipherInfo cipherInfo = buildCipherInfo(data, this.password, this.iterationCount, this.mode);
  73.         this.key = cipherInfo.getKey();
  74.         this.ivParameterSpec = cipherInfo.getIvParameterSpec();
  75.         byte[] cipherBytes = DecryptOpenSSLPass.extractCipherBytes(data);
  76.         return super.process(cipherBytes, EncryptOpenSSLPass.getAlgorithm(this.mode));
  77.     }
  78.    
  79.     public byte[] decryptBase64(byte[] data) throws UtilsException{
  80.         return this.decrypt(Base64Utilities.decode(data));
  81.     }
  82.    
  83.     public byte[] decryptBase64(String data) throws UtilsException{
  84.         return this.decrypt(Base64Utilities.decode(data));
  85.     }
  86.    
  87.     public byte[] decryptHexBinary(char[] data) throws UtilsException{
  88.         return this.decrypt(HexBinaryUtilities.decode(data));
  89.     }
  90.    
  91.     public byte[] decryptHexBinary(String data) throws UtilsException{
  92.         return this.decrypt(HexBinaryUtilities.decode(data));
  93.     }
  94.    
  95.    
  96.     @Override
  97.     public void initIV(String algorithm) throws UtilsException{
  98.         // NOP
  99.         // Non deve fare nulla questa chiamata, viene gestita dalla funzione sopra l'IV
  100.     }
  101. }