EncryptOpenSSLPass.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.io.ByteArrayOutputStream;
  22. import java.security.MessageDigest;
  23. import java.util.Arrays;

  24. import javax.crypto.spec.IvParameterSpec;
  25. import javax.crypto.spec.SecretKeySpec;

  26. import org.apache.commons.lang.StringUtils;
  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. import org.openspcoop2.utils.random.RandomUtilities;

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

  40.      /**
  41.       *
  42.       * Openssl encrypts data using the following steps:
  43.       * 1. salt = 8-byte cryptographically-strong random number
  44.       * 2. key = messageDigest("sha256", password+salt)
  45.       * 3. iv = messageDigest(key+password+salt)[0,16)
  46.       * 4. cipherTextRaw = encrypt("aes256cbc", key, iv, textPlain)
  47.       * 5. cipherText = "Salted__"+salt+cipherTextRaw
  48.     */
  49.     public static CipherInfo buildCipherInfo(String password, String digestAlgoParam, OpenSSLEncryptionMode mode) throws UtilsException {
  50.        
  51.         CipherInfo cipherInfo = new CipherInfo();
  52.        
  53.         cipherInfo.setSalt(buildSalt());
  54.        
  55.         cipherInfo.setEncodedKey(buildSecretKey(password, cipherInfo.getSalt(), digestAlgoParam, mode));
  56.         cipherInfo.setKey(new SecretKeySpec(cipherInfo.getEncodedKey(), SymmetricKeyUtils.ALGO_AES));
  57.        
  58.         cipherInfo.setIv(buildIV(password, cipherInfo.getSalt(), cipherInfo.getEncodedKey(), digestAlgoParam));
  59.         cipherInfo.setIvParameterSpec(convertTo(cipherInfo.getIv()));
  60.        
  61.         return cipherInfo;
  62.     }
  63.     static byte[] buildSalt() throws UtilsException {
  64.         try {                
  65.             // Create salt
  66.             byte[] salt = new byte[8];
  67.             RandomUtilities.getSecureRandom().nextBytes(salt);
  68.             return salt;
  69.         }catch(Exception e) {
  70.             throw new UtilsException(e.getMessage(),e);
  71.         }
  72.     }
  73.     static byte[] buildSecretKey(String password, byte[] salt, String digestAlgoParam, OpenSSLEncryptionMode modeParam) throws UtilsException {
  74.         try {
  75.             // Create key
  76.             byte[] secretKeyClear = password.getBytes();
  77.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  78.             bout.write(secretKeyClear);
  79.             bout.write(salt);
  80.             bout.flush();
  81.             bout.close();
  82.             byte[]passAndSalt = bout.toByteArray();
  83.            
  84.             String digestAlgo = digestAlgoParam;
  85.             if(digestAlgoParam==null || StringUtils.isEmpty(digestAlgoParam)) {
  86.                 digestAlgo = "SHA-256";
  87.             }
  88.             MessageDigest md = MessageDigest.getInstance(digestAlgo);
  89.             byte[] key = md.digest(passAndSalt);
  90.             OpenSSLEncryptionMode mode = modeParam!=null ? modeParam : OpenSSLEncryptionMode.AES_256_CBC;
  91.             switch (mode) {
  92.             case AES_128_CBC:
  93.                 key = Arrays.copyOf(key, 16); // AES-128 richiede una chiave di 128 bit (16 byte).
  94.                 break;
  95.             case AES_192_CBC:
  96.                 key = Arrays.copyOf(key, 24); // AES-192 richiede una chiave di 192 bit (24 byte).
  97.                 break;
  98.             case AES_256_CBC:
  99.                 key = Arrays.copyOf(key, 32); // AES-256 richiede una chiave di 256 bit (32 byte).
  100.                 break;
  101.             }
  102.             return key;
  103.         }catch(Exception e) {
  104.             throw new UtilsException(e.getMessage(),e);
  105.         }
  106.     }
  107.     static byte[] buildIV(String password, byte[] salt, byte[]encodedKey, String digestAlgoParam) throws UtilsException {
  108.         try {
  109.             String digestAlgo = digestAlgoParam;
  110.             if(digestAlgoParam==null || StringUtils.isEmpty(digestAlgoParam)) {
  111.                 digestAlgo = "SHA-256";
  112.             }
  113.             MessageDigest md = MessageDigest.getInstance(digestAlgo);
  114.            
  115.             // Derive IV
  116.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  117.             bout.write(encodedKey);
  118.             bout.write(password.getBytes());
  119.             bout.write(salt);
  120.             bout.flush();
  121.             bout.close();
  122.             byte[] keyAndPassAndSalt = bout.toByteArray();
  123.                      
  124.             return Arrays.copyOfRange( md.digest(keyAndPassAndSalt), 0, 16);
  125.         }catch(Exception e) {
  126.             throw new UtilsException(e.getMessage(),e);
  127.         }
  128.     }
  129.     static IvParameterSpec convertTo(byte[] iv) {
  130.         return new IvParameterSpec(iv);
  131.     }

  132.     public static byte[] formatOutput(byte [] salt, byte [] cipherText) throws UtilsException {
  133.         try {
  134.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  135.             bos.writeBytes("Salted__".getBytes());
  136.             bos.writeBytes(salt);
  137.             bos.writeBytes(cipherText);
  138.             bos.flush();
  139.             bos.close();
  140.             return bos.toByteArray();
  141.         }catch(Exception e) {
  142.             throw new UtilsException(e.getMessage(),e);
  143.         }
  144.     }
  145.    
  146.     private static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5Padding";
  147.    
  148.    
  149.     private CipherInfo cipherInfo;
  150.     private OpenSSLEncryptionMode mode;
  151.    
  152.     public EncryptOpenSSLPass(String password) throws UtilsException {
  153.         this(password, null);
  154.     }
  155.     public EncryptOpenSSLPass(String password, OpenSSLEncryptionMode modeParam) throws UtilsException {
  156.         super(javax.crypto.Cipher.ENCRYPT_MODE);
  157.         this.mode = modeParam!=null ? modeParam : OpenSSLEncryptionMode.AES_256_CBC;
  158.         this.cipherInfo = buildCipherInfo(password, null, this.mode);
  159.         this.key = this.cipherInfo.getKey();
  160.         this.ivParameterSpec = this.cipherInfo.getIvParameterSpec();
  161.     }
  162.    
  163.    
  164.     static String getAlgorithm(OpenSSLEncryptionMode mode) throws UtilsException {
  165.         switch (mode) {
  166.         case AES_128_CBC:
  167.         case AES_192_CBC:
  168.         case AES_256_CBC:
  169.             return AES_CBC_PKCS5PADDING;
  170.         }
  171.         throw new UtilsException("Unsupported mode");
  172.     }
  173.    
  174.     public byte[] encrypt(String data, String charsetName) throws UtilsException{
  175.         return formatOutput(this.cipherInfo.getSalt(), super.process(data, charsetName, getAlgorithm(this.mode)));
  176.     }
  177.     public byte[] encrypt(byte[] data) throws UtilsException{
  178.         return formatOutput(this.cipherInfo.getSalt(), super.process(data, getAlgorithm(this.mode)));
  179.     }
  180.    
  181.     public byte[] encryptBase64(String data, String charsetName) throws UtilsException{
  182.         return Base64Utilities.encode(this.encrypt(data, charsetName));
  183.     }
  184.     public byte[] encryptBase64(byte[] data) throws UtilsException{
  185.         return Base64Utilities.encode(this.encrypt(data));
  186.     }
  187.    
  188.     public String encryptBase64AsString(String data, String charsetName) throws UtilsException{
  189.         return Base64Utilities.encodeAsString(this.encrypt(data, charsetName));
  190.     }
  191.     public String encryptBase64AsString(byte[] data) throws UtilsException{
  192.         return Base64Utilities.encodeAsString(this.encrypt(data));
  193.     }
  194.    
  195.     public char[] encryptHexBinary(String data, String charsetName) throws UtilsException{
  196.         return HexBinaryUtilities.encode(this.encrypt(data, charsetName));
  197.     }
  198.     public char[] encryptHexBinary(byte[] data) throws UtilsException{
  199.         return HexBinaryUtilities.encode(this.encrypt(data));
  200.     }
  201.    
  202.     public String encryptHexBinaryAsString(String data, String charsetName) throws UtilsException{
  203.         return HexBinaryUtilities.encodeAsString(this.encrypt(data, charsetName));
  204.     }
  205.     public String encryptHexBinaryAsString(byte[] data) throws UtilsException{
  206.         return HexBinaryUtilities.encodeAsString(this.encrypt(data));
  207.     }
  208.    
  209.     @Override
  210.     public void initIV(String algorithm) throws UtilsException{
  211.         // NOP
  212.         // Non deve fare nulla questa chiamata, viene gestita dalla funzione sopra l'IV
  213.     }
  214. }