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.util.Base64;
38 import javax.crypto.BadPaddingException;
39 import javax.crypto.Cipher;
40 import javax.crypto.IllegalBlockSizeException;
41 import javax.crypto.NoSuchPaddingException;
42 import javax.crypto.SecretKey;
43 import javax.crypto.spec.IvParameterSpec;
44 import javax.crypto.spec.SecretKeySpec;
63 if (licenseResponse == null || licenseResponse.getBoostLicense() == null) {
82 String decryptedJsonResponse;
85 licenseResponse.getEncryptedJson(),
86 licenseResponse.getIv(),
87 licenseResponse.getEncryptedKey(),
88 licenseResponse.getVersion()
90 }
catch (IOException | GeneralSecurityException ex) {
95 if (!
"AUTOPSY".equalsIgnoreCase(decryptedLicense.
getProduct())) {
100 return decryptedLicense;
104 if (!
"1.0".equals(version)) {
108 byte[] encryptedKeyBytes = Base64.getDecoder().decode(encryptedKey);
109 byte[] keyBytes =
decryptKey(encryptedKeyBytes);
110 SecretKey key =
new SecretKeySpec(keyBytes, 0, keyBytes.length,
"AES");
112 byte[] ivBytes = Base64.getDecoder().decode(ivBase64);
113 IvParameterSpec iv =
new IvParameterSpec(ivBytes);
115 byte[] encryptedLicenseJsonBytes = Base64.getDecoder().decode(encryptedJson);
117 String algorithm =
"AES/CBC/PKCS5Padding";
118 Cipher cipher = Cipher.getInstance(algorithm);
119 cipher.init(Cipher.DECRYPT_MODE, key, iv);
120 byte[] licenseJsonBytes = cipher.doFinal(encryptedLicenseJsonBytes);
122 return new String(licenseJsonBytes, StandardCharsets.UTF_8);
125 private PublicKey
getPublicKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
127 String publicKeyString =
"""
128 -----BEGIN PUBLIC KEY-----
129 MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwIKulLyaLQ2WeO0gIW2G
130 3jQqny3Y/7VUevBKulAEywaUbvECvZ4zGsnaMyACjXxMNkA1xU2WeSMP/WqC03wz
131 4d71liUeAqOYKMdGHXFN2qswWz/ufK6An0pTEqYaoiUfcwSBVo2ZTUcMQexScKaS
132 ghmaWqBHBYx+lBkVMcLG2PtLDRZbqgJvJr2QCzMSVUpEGGQEWs7YolIq46KCgqsq
133 pTdfrdqd59x6oRhTLegswzxwLyouvrKbRqKR2ZRbVvlGtUnnnlLDuhEfd0flMxuv
134 W98Siw6dWe1K3x45nDu5py2G9Q9fZS8/2KHUC6QcLLstLIoPnZjCl9Lcur1U6s9N
135 f5aLI9mwMfmSJsoVOuwx2/MC98uHvPoPbG4ZjiT0aaGg4JccTGD6pssDA35zPhkk
136 1l6wktEYtyF2A7zjzuFxioQz8fHBzIbHPCxzu4S2gh3qOVFf7c9COmX9MsnB70o2
137 EZ1rxlFIJ7937IGJNwWOQuiMKTpEeT6BwTdQNZQPqCUGvZ5eEjhrm57yCF4zuyrt
138 AR8DG7ahK2YAarADHRyxTuxH1qY7E5/CTQKYk9tIYsV4O05CKj7B8rBMtjVNjb4b
139 d7JwPW43Z3J6jo/gLlVdGSPg8vQDNVLl6sdDM4Pm1eJEzgR2JlqXDCRDUGNNsXH2
140 qt9Ru8ykX7PAfF2Q3/qg1jkCAwEAAQ==
141 -----END PUBLIC KEY-----
144 publicKeyString = publicKeyString.replaceAll(
"-----BEGIN PUBLIC KEY-----",
"").replaceAll(
"-----END PUBLIC KEY-----",
"").replaceAll(
"\\s",
"");
145 byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
147 KeySpec keySpec =
new X509EncodedKeySpec(publicKeyBytes);
148 KeyFactory keyFactory = KeyFactory.getInstance(
"RSA");
149 PublicKey publicKey = keyFactory.generatePublic(keySpec);
154 private byte[]
decryptKey(byte[] encryptedKeyBytes)
throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
158 Cipher decryptCipher = Cipher.getInstance(
"RSA");
159 decryptCipher.init(Cipher.DECRYPT_MODE, publicKey);
161 byte[] decryptedBytes = decryptCipher.doFinal(encryptedKeyBytes);
163 return decryptedBytes;
173 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)