DecryptOpenSSLPass.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.util.Arrays;

  22. import javax.crypto.spec.SecretKeySpec;

  23. import org.openspcoop2.utils.UtilsException;
  24. import org.openspcoop2.utils.certificate.SymmetricKeyUtils;
  25. import org.openspcoop2.utils.io.Base64Utilities;
  26. import org.openspcoop2.utils.io.HexBinaryUtilities;

  27. /**
  28.  * Encrypt
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class DecryptOpenSSLPass extends AbstractCipher {

  35.     /**
  36.       *
  37.       * Openssl encrypts data using the following steps:
  38.       * 1. bytes = cipherText
  39.       * 2. salt = bytes[8,16)
  40.       * 3. key = messageDigest("sha256", password+salt)
  41.       * 4. iv = messageDigest(key+password+salt)[0,16)
  42.       * 5. plainText = decrypt("aes256cbc", key, iv, bytes[16..end])
  43.     */

  44.     public static CipherInfo buildCipherInfo(byte[] cipherBytes, String password, String digestAlgoParam, OpenSSLEncryptionMode mode) throws UtilsException {
  45.        
  46.         CipherInfo cipherInfo = new CipherInfo();
  47.        
  48.         cipherInfo.setSalt(buildSalt(cipherBytes));
  49.        
  50.         cipherInfo.setEncodedKey(EncryptOpenSSLPass.buildSecretKey(password, cipherInfo.getSalt(), digestAlgoParam, mode));
  51.         cipherInfo.setKey(new SecretKeySpec(cipherInfo.getEncodedKey(), SymmetricKeyUtils.ALGO_AES));
  52.        
  53.         cipherInfo.setIv(EncryptOpenSSLPass.buildIV(password, cipherInfo.getSalt(), cipherInfo.getEncodedKey(), digestAlgoParam));
  54.         cipherInfo.setIvParameterSpec(EncryptOpenSSLPass.convertTo(cipherInfo.getIv()));
  55.        
  56.         return cipherInfo;
  57.     }
  58.     static byte[] buildSalt(byte[] cipherBytes) throws UtilsException {
  59.         try {                
  60.             return Arrays.copyOfRange(cipherBytes, 8, 16);
  61.         }catch(Exception e) {
  62.             throw new UtilsException(e.getMessage(),e);
  63.         }
  64.     }
  65.    
  66.     public static byte[] extractCipherBytes(byte[] data) {
  67.         return Arrays.copyOfRange(
  68.                 data, 16, data.length);
  69.     }
  70.    
  71.    
  72.     private OpenSSLEncryptionMode mode;
  73.     private String password;
  74.    
  75.     public DecryptOpenSSLPass(String password) {
  76.         this(password, null);
  77.     }
  78.     public DecryptOpenSSLPass(String password, OpenSSLEncryptionMode modeParam) {
  79.         super(javax.crypto.Cipher.DECRYPT_MODE);
  80.         this.mode = modeParam!=null ? modeParam : OpenSSLEncryptionMode.AES_256_CBC;
  81.         this.password = password;
  82.     }


  83.     public byte[] decrypt(byte[] data) throws UtilsException{
  84.         CipherInfo cipherInfo = buildCipherInfo(data, this.password, null, this.mode);
  85.         this.key = cipherInfo.getKey();
  86.         this.ivParameterSpec = cipherInfo.getIvParameterSpec();
  87.         byte[] cipherBytes = extractCipherBytes(data);
  88.         return super.process(cipherBytes, EncryptOpenSSLPass.getAlgorithm(this.mode));
  89.     }
  90.    
  91.     public byte[] decryptBase64(byte[] data) throws UtilsException{
  92.         return this.decrypt(Base64Utilities.decode(data));
  93.     }
  94.    
  95.     public byte[] decryptBase64(String data) throws UtilsException{
  96.         return this.decrypt(Base64Utilities.decode(data));
  97.     }
  98.    
  99.     public byte[] decryptHexBinary(char[] data) throws UtilsException{
  100.         return this.decrypt(HexBinaryUtilities.decode(data));
  101.     }
  102.    
  103.     public byte[] decryptHexBinary(String data) throws UtilsException{
  104.         return this.decrypt(HexBinaryUtilities.decode(data));
  105.     }
  106.    
  107.    
  108.     @Override
  109.     public void initIV(String algorithm) throws UtilsException{
  110.         // NOP
  111.         // Non deve fare nulla questa chiamata, viene gestita dalla funzione sopra l'IV
  112.     }
  113. }