19 package com.basistech.df.cybertriage.autopsy.ctapi.util;
 
   25 import com.fasterxml.jackson.core.JsonProcessingException;
 
   26 import com.fasterxml.jackson.databind.ObjectMapper;
 
   27 import java.io.IOException;
 
   28 import java.nio.charset.StandardCharsets;
 
   29 import java.security.GeneralSecurityException;
 
   30 import java.security.InvalidKeyException;
 
   31 import java.security.KeyFactory;
 
   32 import java.security.NoSuchAlgorithmException;
 
   33 import java.security.PublicKey;
 
   34 import java.security.spec.InvalidKeySpecException;
 
   35 import java.security.spec.KeySpec;
 
   36 import java.security.spec.X509EncodedKeySpec;
 
   37 import java.text.MessageFormat;
 
   38 import java.util.Base64;
 
   39 import javax.crypto.BadPaddingException;
 
   40 import javax.crypto.Cipher;
 
   41 import javax.crypto.IllegalBlockSizeException;
 
   42 import javax.crypto.NoSuchPaddingException;
 
   43 import javax.crypto.SecretKey;
 
   44 import javax.crypto.spec.IvParameterSpec;
 
   45 import javax.crypto.spec.SecretKeySpec;
 
   46 import org.apache.commons.lang3.ObjectUtils;
 
   65         if (licenseResponse == null) {
 
   83         if (licenseResponse == null) {
 
   87         String decryptedJsonResponse;
 
   90                     licenseResponse.getEncryptedJson(),
 
   91                     licenseResponse.getIv(),
 
   92                     licenseResponse.getEncryptedKey(),
 
   93                     licenseResponse.getVersion()
 
   95         } 
catch (IOException | GeneralSecurityException ex) {
 
  100         if (!
"AUTOPSY".equalsIgnoreCase(decryptedLicense.
getProduct())) {
 
  105         return decryptedLicense;
 
  109         if (ObjectUtils.anyNull(encryptedJson, ivBase64, encryptedKey, version)) {
 
  111                     "encryptedJson: {0}, iv: {1}, encryptedKey: {2}, version: {3} must all be non-null",
 
  112                     encryptedJson, ivBase64, encryptedKey, version));
 
  115         if (!
"1.0".equals(version)) {
 
  119         byte[] encryptedKeyBytes = Base64.getDecoder().decode(encryptedKey);
 
  120         byte[] keyBytes = 
decryptKey(encryptedKeyBytes);
 
  121         SecretKey key = 
new SecretKeySpec(keyBytes, 0, keyBytes.length, 
"AES");
 
  123         byte[] ivBytes = Base64.getDecoder().decode(ivBase64);
 
  124         IvParameterSpec iv = 
new IvParameterSpec(ivBytes);
 
  126         byte[] encryptedLicenseJsonBytes = Base64.getDecoder().decode(encryptedJson);
 
  128         String algorithm = 
"AES/CBC/PKCS5Padding";
 
  129         Cipher cipher = Cipher.getInstance(algorithm);
 
  130         cipher.init(Cipher.DECRYPT_MODE, key, iv);
 
  131         byte[] licenseJsonBytes = cipher.doFinal(encryptedLicenseJsonBytes);
 
  133         return new String(licenseJsonBytes, StandardCharsets.UTF_8);
 
  136     private PublicKey 
getPublicKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
 
  138         String publicKeyString = 
""" 
  139                                  -----BEGIN PUBLIC KEY----- 
  140                                  MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwIKulLyaLQ2WeO0gIW2G 
  141                                  3jQqny3Y/7VUevBKulAEywaUbvECvZ4zGsnaMyACjXxMNkA1xU2WeSMP/WqC03wz 
  142                                  4d71liUeAqOYKMdGHXFN2qswWz/ufK6An0pTEqYaoiUfcwSBVo2ZTUcMQexScKaS 
  143                                  ghmaWqBHBYx+lBkVMcLG2PtLDRZbqgJvJr2QCzMSVUpEGGQEWs7YolIq46KCgqsq 
  144                                  pTdfrdqd59x6oRhTLegswzxwLyouvrKbRqKR2ZRbVvlGtUnnnlLDuhEfd0flMxuv 
  145                                  W98Siw6dWe1K3x45nDu5py2G9Q9fZS8/2KHUC6QcLLstLIoPnZjCl9Lcur1U6s9N 
  146                                  f5aLI9mwMfmSJsoVOuwx2/MC98uHvPoPbG4ZjiT0aaGg4JccTGD6pssDA35zPhkk 
  147                                  1l6wktEYtyF2A7zjzuFxioQz8fHBzIbHPCxzu4S2gh3qOVFf7c9COmX9MsnB70o2 
  148                                  EZ1rxlFIJ7937IGJNwWOQuiMKTpEeT6BwTdQNZQPqCUGvZ5eEjhrm57yCF4zuyrt 
  149                                  AR8DG7ahK2YAarADHRyxTuxH1qY7E5/CTQKYk9tIYsV4O05CKj7B8rBMtjVNjb4b 
  150                                  d7JwPW43Z3J6jo/gLlVdGSPg8vQDNVLl6sdDM4Pm1eJEzgR2JlqXDCRDUGNNsXH2 
  151                                  qt9Ru8ykX7PAfF2Q3/qg1jkCAwEAAQ== 
  152                                  -----END PUBLIC KEY----- 
  155         publicKeyString = publicKeyString.replaceAll(
"-----BEGIN PUBLIC KEY-----", 
"").replaceAll(
"-----END PUBLIC KEY-----", 
"").replaceAll(
"\\s", 
"");
 
  156         byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
 
  158         KeySpec keySpec = 
new X509EncodedKeySpec(publicKeyBytes);
 
  159         KeyFactory keyFactory = KeyFactory.getInstance(
"RSA");
 
  160         PublicKey publicKey = keyFactory.generatePublic(keySpec);
 
  165     private byte[] 
decryptKey(byte[] encryptedKeyBytes) 
throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
 
  169         Cipher decryptCipher = Cipher.getInstance(
"RSA");
 
  170         decryptCipher.init(Cipher.DECRYPT_MODE, publicKey);
 
  172         byte[] decryptedBytes = decryptCipher.doFinal(encryptedKeyBytes);
 
  174         return decryptedBytes;
 
  184             super(message, cause);
 
byte[] decryptKey(byte[] encryptedKeyBytes)
static ObjectMapperUtil getInstance()
static final LicenseDecryptorUtil instance
DecryptedLicenseResponse parseLicenseJSON(BoostLicenseResponse licenseResponse)
ObjectMapper getDefaultObjectMapper()
String decryptLicenseString(String encryptedJson, String ivBase64, String encryptedKey, String version)
static LicenseDecryptorUtil getInstance()
LicenseInfo createLicenseInfo(LicenseResponse licenseResponse)
final ObjectMapper objectMapper
InvalidLicenseException(String message, Throwable cause)
InvalidLicenseException(String message)