SSLHelper.java 8.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.codec.binary.Base64
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.bedrock.internal;

import com.adobe.aemds.bedrock.CoreConfigService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SSLHelper {
    private static final byte[] NEW_LINE = System.getProperty("line.separator", "\n").getBytes();
    private Logger log = LoggerFactory.getLogger(SSLHelper.class);
    private KeyStore keyStore;
    private CoreConfigService coreConfigService;
    private char[] password;

    public void initialize() throws IOException, GeneralSecurityException {
        this.keyStore = this.getKeyStore();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void createCertificateForOpenSSL() throws Exception {
        FileOutputStream fosKey = null;
        try {
            int idx;
            Certificate cert = this.getCertificate();
            byte[] bCert = cert.getEncoded();
            File fCert = new File(this.getServerNativeDir(), "SSLCert.pem");
            FileOutputStream fosCert = new FileOutputStream(fCert);
            fosCert.write("-----BEGIN CERTIFICATE-----".getBytes());
            fosCert.write(NEW_LINE);
            byte[] baCert = Base64.encodeBase64((byte[])bCert);
            fosCert.write(baCert);
            fosCert.write(NEW_LINE);
            fosCert.write("-----END CERTIFICATE-----".getBytes());
            fosCert.write(NEW_LINE);
            fosCert.close();
            File fKey = new File(this.getServerNativeDir(), "SSLKey.pem");
            fosKey = new FileOutputStream(fKey);
            PrivateKey pk = this.getPrivateKey();
            byte[] rawkey = pk.getEncoded();
            byte[] key = new byte[rawkey.length - 26];
            System.arraycopy(rawkey, 26, key, 0, rawkey.length - 26);
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            SecureRandom rand = new SecureRandom();
            byte[] seed = new byte[8];
            rand.nextBytes(seed);
            IvParameterSpec iv = new IvParameterSpec(seed);
            byte[] abyte0 = new byte[24];
            byte[] pwd = new String(this.getPassword()).getBytes();
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");
            messagedigest.update(pwd);
            messagedigest.update(iv.getIV());
            byte[] abyte2 = messagedigest.digest();
            System.arraycopy(abyte2, 0, abyte0, 0, 16);
            messagedigest.update(abyte2);
            messagedigest.update(pwd);
            messagedigest.update(iv.getIV());
            abyte2 = messagedigest.digest();
            System.arraycopy(abyte2, 0, abyte0, 16, 8);
            SecretKeySpec sKey = new SecretKeySpec(abyte0, "DESede");
            cipher.init(1, (Key)sKey, iv);
            byte[] baKey = cipher.doFinal(key);
            fosKey.write("-----BEGIN RSA PRIVATE KEY-----".getBytes());
            fosKey.write(NEW_LINE);
            fosKey.write("Proc-Type: 4,ENCRYPTED".getBytes());
            fosKey.write(NEW_LINE);
            fosKey.write("DEK-Info: DES-EDE3-CBC,".getBytes());
            fosKey.write(SSLHelper.toHex(iv.getIV()).getBytes());
            fosKey.write(NEW_LINE);
            fosKey.write(NEW_LINE);
            byte[] pemKey = SSLHelper.base64(baKey);
            for (idx = 0; idx < pemKey.length - 64; idx += 64) {
                fosKey.write(pemKey, idx, 64);
                fosKey.write(NEW_LINE);
            }
            fosKey.write(pemKey, idx, pemKey.length - idx);
            fosKey.write(NEW_LINE);
            fosKey.write("-----END RSA PRIVATE KEY-----".getBytes());
            fosKey.write(NEW_LINE);
            fosKey.write("-----BEGIN CERTIFICATE-----".getBytes());
            fosKey.write(NEW_LINE);
            fosKey.write(baCert);
            fosKey.write(NEW_LINE);
            fosKey.write("-----END CERTIFICATE-----".getBytes());
            fosKey.write(NEW_LINE);
            Object var24_23 = null;
        }
        catch (Throwable var23_25) {
            Object var24_24 = null;
            IOUtils.closeQuietly((OutputStream)fosKey);
            throw var23_25;
        }
        IOUtils.closeQuietly((OutputStream)fosKey);
        {
        }
    }

    private PrivateKey getPrivateKey() throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
        return (PrivateKey)this.keyStore.getKey("ads-credentials", this.getPassword());
    }

    private Certificate getCertificate() throws KeyStoreException {
        return this.keyStore.getCertificate("ads-credentials");
    }

    private KeyStore getKeyStore() throws IOException, GeneralSecurityException {
        File svcFile = new File(this.getServerNativeDir());
        File keyStoreFile = new File(svcFile, "ads-ssl.jks");
        KeyStore keyStore = KeyStore.getInstance("JKS");
        FileInputStream fis = FileUtils.openInputStream((File)keyStoreFile);
        keyStore.load(fis, this.getPassword());
        return keyStore;
    }

    private char[] getPassword() {
        return this.password;
    }

    private String getServerNativeDir() {
        return this.coreConfigService.getServerNativeDir();
    }

    private static String toHex(byte[] b) {
        char[] hex = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] buf = new char[b.length * 2];
        int j = 0;
        for (int i = 0; i < b.length; ++i) {
            byte k = b[i];
            buf[j++] = hex[k >>> 4 & 15];
            buf[j++] = hex[k & 15];
        }
        return new String(buf);
    }

    private static byte[] base64(byte[] dataIn) {
        int idxIn;
        if (dataIn == null) {
            return null;
        }
        byte[] dataOut = new byte[(dataIn.length + 2) / 3 * 4];
        int idxOut = 0;
        for (idxIn = 0; idxIn < dataIn.length - 2; idxIn += 3) {
            dataOut[idxOut++] = (byte)(dataIn[idxIn] >>> 2 & 63);
            dataOut[idxOut++] = (byte)(dataIn[idxIn + 1] >>> 4 & 15 | dataIn[idxIn] << 4 & 63);
            dataOut[idxOut++] = (byte)(dataIn[idxIn + 2] >>> 6 & 3 | dataIn[idxIn + 1] << 2 & 63);
            dataOut[idxOut++] = (byte)(dataIn[idxIn + 2] & 63);
        }
        if (idxIn < dataIn.length) {
            dataOut[idxOut++] = (byte)(dataIn[idxIn] >>> 2 & 63);
            if (idxIn < dataIn.length - 1) {
                dataOut[idxOut++] = (byte)(dataIn[idxIn + 1] >>> 4 & 15 | dataIn[idxIn] << 4 & 63);
                dataOut[idxOut++] = (byte)(dataIn[idxIn + 1] << 2 & 63);
            } else {
                dataOut[idxOut++] = (byte)(dataIn[idxIn] << 4 & 63);
            }
        }
        for (idxIn = 0; idxIn < idxOut; ++idxIn) {
            dataOut[idxIn] = dataOut[idxIn] < 26 ? (byte)(dataOut[idxIn] + 65) : (dataOut[idxIn] < 52 ? (byte)(dataOut[idxIn] + 97 - 26) : (dataOut[idxIn] < 62 ? (byte)(dataOut[idxIn] + 48 - 52) : (dataOut[idxIn] < 63 ? 43 : 47)));
        }
        while (idxIn < dataOut.length) {
            dataOut[idxIn] = 61;
            ++idxIn;
        }
        return dataOut;
    }

    public void setCoreConfigService(CoreConfigService coreConfigService) {
        this.coreConfigService = coreConfigService;
    }

    public void setPassword(char[] password) {
        this.password = password;
    }
}