/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.keystore.SsgKeyEntry
 *  com.l7tech.gateway.common.transport.jms.JmsConnection
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.cert.TrustedCertManager
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.GoidUpgradeMapper
 *  com.l7tech.util.HexUtils
 */
package com.l7tech.server.transport.jms;

import com.l7tech.gateway.common.security.keystore.SsgKeyEntry;
import com.l7tech.gateway.common.transport.jms.JmsConnection;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.security.cert.TrustedCertManager;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.transport.jms.a;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.GoidUpgradeMapper;
import com.l7tech.util.HexUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JmsPropertyMapper {
    private static final Logger a;
    private final TrustedCertManager b;
    private final DefaultKey c;
    private final Random d;
    private final Map<String, a> e = new HashMap<String, a>();
    private static final String[] f;

    public JmsPropertyMapper(TrustedCertManager trustedCertManager, DefaultKey defaultKey) {
        this.b = trustedCertManager;
        this.c = defaultKey;
        this.d = new Random(System.currentTimeMillis());
    }

    /*
     * Could not resolve type clashes
     * Loose catch block
     */
    public boolean substitutePropertyValues(Map<Object, Object> map) {
        String[] stringArray;
        Object object;
        Object object2;
        boolean bl2 = false;
        try {
            for (Object object3 : map.entrySet()) {
                Object object4;
                block25: {
                    block24: {
                        object4 = object3.getValue().toString().split("\t");
                        if (JmsConnection.VALUE_KEYSTORE.equals(object4[0])) break block24;
                        try {
                            block31: {
                                if (JmsConnection.VALUE_KEYSTORE_BYTES.equals(object4[0])) break block24;
                                break block31;
                                catch (NumberFormatException numberFormatException) {
                                    throw JmsPropertyMapper.a(numberFormatException);
                                }
                            }
                            if (!JmsConnection.VALUE_KEYSTORE_PASSWORD.equals(object4[0])) break block25;
                        }
                        catch (NumberFormatException numberFormatException) {
                            throw JmsPropertyMapper.a(numberFormatException);
                        }
                    }
                    try {
                        block29: {
                            a a10;
                            block30: {
                                block28: {
                                    block27: {
                                        block26: {
                                            object2 = PersistentEntity.DEFAULT_GOID;
                                            object = null;
                                            try {
                                                if (((Object)object4).length != 1) break block26;
                                                break block27;
                                            }
                                            catch (NumberFormatException numberFormatException) {
                                                throw JmsPropertyMapper.a(numberFormatException);
                                            }
                                        }
                                        if (((Object)object4).length == 3) {
                                            object2 = GoidUpgradeMapper.mapId((EntityType)EntityType.SSG_KEYSTORE, (String)object4[1]);
                                            object = object4[2];
                                        } else {
                                            stringArray = f;
                                            a.log(Level.WARNING, stringArray[8] + (String)object4[0] + stringArray[0] + String.valueOf(object3.getValue()) + "\"");
                                        }
                                    }
                                    a10 = this.a((Goid)object2, (String)object, map);
                                    try {
                                        if (!JmsConnection.VALUE_KEYSTORE.equals(object4[0])) break block28;
                                        object3.setValue(a10.b);
                                        break block29;
                                    }
                                    catch (NumberFormatException numberFormatException) {
                                        throw JmsPropertyMapper.a(numberFormatException);
                                    }
                                }
                                try {
                                    if (!JmsConnection.VALUE_KEYSTORE_BYTES.equals(object4[0])) break block30;
                                    object3.setValue(a10.c);
                                    break block29;
                                }
                                catch (NumberFormatException numberFormatException) {
                                    throw JmsPropertyMapper.a(numberFormatException);
                                }
                            }
                            try {
                                if (JmsConnection.VALUE_KEYSTORE_PASSWORD.equals(object4[0])) {
                                    object3.setValue(a10.a);
                                }
                            }
                            catch (NumberFormatException numberFormatException) {
                                throw JmsPropertyMapper.a(numberFormatException);
                            }
                        }
                        bl2 = true;
                    }
                    catch (NumberFormatException numberFormatException) {
                        stringArray = f;
                        a.log(Level.WARNING, stringArray[9], numberFormatException);
                    }
                    continue;
                }
                if (JmsConnection.VALUE_TRUSTED_LIST.equals(object4[0])) {
                    object3.setValue(this.b());
                    bl2 = true;
                    continue;
                }
                if (JmsConnection.VALUE_BOOLEAN_TRUE.equals(object4[0])) {
                    object3.setValue(Boolean.TRUE);
                    bl2 = true;
                    continue;
                }
                if (!JmsConnection.VALUE_BOOLEAN_FALSE.equals(object4[0])) continue;
                object3.setValue(Boolean.FALSE);
                bl2 = true;
            }
        }
        catch (KeyStoreException keyStoreException) {
            stringArray = f;
            a.log(Level.WARNING, stringArray[1] + ExceptionUtils.getMessage((Throwable)keyStoreException), ExceptionUtils.getDebugException((Throwable)keyStoreException));
        }
        if (a.isLoggable(Level.FINE)) {
            StringBuffer stringBuffer = new StringBuffer(512);
            for (Object object4 : map.entrySet()) {
                object2 = object4.getKey();
                object = object4.getValue();
                stringBuffer.append(object2);
                stringBuffer.append('=');
                stringBuffer.append(object);
                stringBuffer.append('\n');
            }
            a.log(Level.FINE, f[2] + stringBuffer.toString());
        }
        return bl2;
    }

    private String a() {
        byte[] byArray = new byte[16];
        this.d.nextBytes(byArray);
        return HexUtils.encodeBase64((byte[])byArray);
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private List b() {
        Vector<X509Certificate> vector = new Vector<X509Certificate>();
        try {
            Collection collection = this.b.findAll();
            for (TrustedCert trustedCert : collection) {
                try {
                    if (!trustedCert.isTrustedForSigningServerCerts()) continue;
                    vector.add(trustedCert.getCertificate());
                }
                catch (Exception exception) {
                    throw JmsPropertyMapper.a(exception);
                    return vector;
                }
            }
        }
        catch (Exception exception) {
            a.log(Level.WARNING, f[5]);
        }
        return vector;
    }

    private a a(Goid goid, String string, Map map) throws KeyStoreException {
        String string2 = String.valueOf(goid) + "\t" + string;
        a a10 = this.e.get(string2);
        if (a10 == null) {
            try {
                String[] stringArray;
                SsgKeyEntry ssgKeyEntry = this.c.lookupKeyByKeyAlias(string, goid);
                Certificate[] certificateArray = ssgKeyEntry.getCertificateChain();
                PrivateKey privateKey = ssgKeyEntry.getPrivateKey();
                try {
                    if (!f[3].equals(privateKey.getFormat())) {
                        a.warning(f[10]);
                    }
                }
                catch (IOException iOException) {
                    throw JmsPropertyMapper.a(iOException);
                }
                String string3 = (String)map.get(JmsConnection.PROP_KEYSTORE_ALIAS);
                if (string3 == null) {
                    string3 = string;
                }
                if (string3 == null) {
                    stringArray = f;
                    string3 = stringArray[6];
                }
                String string4 = this.a();
                stringArray = f;
                KeyStore keyStore = KeyStore.getInstance(stringArray[11]);
                keyStore.load(null, null);
                keyStore.setKeyEntry(string3, privateKey, string4.toCharArray(), certificateArray);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                keyStore.store(byteArrayOutputStream, string4.toCharArray());
                a10 = new a();
                a10.c = byteArrayOutputStream.toByteArray();
                a10.b = keyStore;
                a10.a = string4;
            }
            catch (IOException iOException) {
                String[] stringArray = f;
                throw new KeyStoreException(stringArray[7] + ExceptionUtils.getMessage((Throwable)iOException), iOException);
            }
            catch (FindException findException) {
                String[] stringArray = f;
                throw new KeyStoreException(stringArray[4] + ExceptionUtils.getMessage((Throwable)findException), findException);
            }
            catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                String[] stringArray = f;
                throw new KeyStoreException(stringArray[4] + ExceptionUtils.getMessage((Throwable)noSuchAlgorithmException), noSuchAlgorithmException);
            }
            catch (CertificateException certificateException) {
                String[] stringArray = f;
                throw new KeyStoreException(stringArray[4] + ExceptionUtils.getMessage((Throwable)certificateException), certificateException);
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw new KeyStoreException(f[4] + ExceptionUtils.getMessage((Throwable)unrecoverableKeyException), unrecoverableKeyException);
            }
            this.e.put(string2, a10);
        }
        return a10;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[12];
                var3_1 = 0;
                var2_2 = "Z6\u0011\u001f\u0012Lw\u00076\u0015\nM\t\u0010\r'\u0003\u0004X\u001fB\u0003b99\u0012LD\u0007 NAk\u0011D\u00035\u0011\u0004\f\u001bU\u001fb\u0011\u0011E\u0011CX`^]i\u001eS\t7\u001e\tI\u0002U\u0002xP_\u001d#0\u0002\u0012^P@\u0014-\u0013\u0018_\u0003Y\b%P\u0016I\t\u0010\u00156\u001f\u000fI^\nF\u0017+#\u0000\rI\u0014\u0010,\u000f#]\\\u0002_\u0016'\u0002\tE\u0015C\\H\u00066\t3.\u000fH\u0019#0\u0002\u0012^PS\u0014'\u0011\tE\u001eWF)\u0015\u0004_\u0004_\u0014'J]$#0\u0002\u0012^PR\u0013+\u001c\u0019E\u001eWF6\u0002\b_\u0004U\u0002b\u0013\u0018^\u0004Y\u0000+\u0013\u001cX\u0015CH\u0003\f/\u0003\u0019#0\u0002\u0012^PS\u0014'\u0011\tE\u001eWF)\u0015\u0004_\u0004_\u0014'J]G3,\u0015\u0005\\\u0015S\u0012'\u0014]J\u001fB\u000b#\u0004]C\u0016\u0010\u00157\u0012\u000eX\u0019D\u00136\u0011\u001f@\u0015\u0010\u00160\u001f\rI\u0002D\u001fb\u0006\u001c@\u0005UHb5\u0005\\\u0015S\u0012'\u0014]J\u001fB\u000b#\u0004]E\u0003\u0010D$3,\u0011\u001f@\u0015\u0010\u0012-P\rM\u0002C\u0003b7\u001cX\u0015G\u0007;P\u0016I\tC\u0012-\u0002\u0018\f9tH";
                var4_3 = "Z6\u0011\u001f\u0012Lw\u00076\u0015\nM\t\u0010\r'\u0003\u0004X\u001fB\u0003b99\u0012LD\u0007 NAk\u0011D\u00035\u0011\u0004\f\u001bU\u001fb\u0011\u0011E\u0011CX`^]i\u001eS\t7\u001e\tI\u0002U\u0002xP_\u001d#0\u0002\u0012^P@\u0014-\u0013\u0018_\u0003Y\b%P\u0016I\t\u0010\u00156\u001f\u000fI^\nF\u0017+#\u0000\rI\u0014\u0010,\u000f#]\\\u0002_\u0016'\u0002\tE\u0015C\\H\u00066\t3.\u000fH\u0019#0\u0002\u0012^PS\u0014'\u0011\tE\u001eWF)\u0015\u0004_\u0004_\u0014'J]$#0\u0002\u0012^PR\u0013+\u001c\u0019E\u001eWF6\u0002\b_\u0004U\u0002b\u0013\u0018^\u0004Y\u0000+\u0013\u001cX\u0015CH\u0003\f/\u0003\u0019#0\u0002\u0012^PS\u0014'\u0011\tE\u001eWF)\u0015\u0004_\u0004_\u0014'J]G3,\u0015\u0005\\\u0015S\u0012'\u0014]J\u001fB\u000b#\u0004]C\u0016\u0010\u00157\u0012\u000eX\u0019D\u00136\u0011\u001f@\u0015\u0010\u00160\u001f\rI\u0002D\u001fb\u0006\u001c@\u0005UHb5\u0005\\\u0015S\u0012'\u0014]J\u001fB\u000b#\u0004]E\u0003\u0010D$3,\u0011\u001f@\u0015\u0010\u0012-P\rM\u0002C\u0003b7\u001cX\u0015G\u0007;P\u0016I\tC\u0012-\u0002\u0018\f9tH".length();
                var1_4 = 67;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 72;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = JmsPropertyMapper.a(v0, JmsPropertyMapper.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0017\u00118*l%tg\b4%-4\u007f$\f55c61.\u0010q2b%1\u0017(\u0012\u000f.i1jNq,b\"b.\u0001=9-9p5\u0007&=\u007f41,\u0006(/y>c\"\\q|X\"tg\f7|y9x4C:9tqf.\u00179|G\u001cBg\u0013#3{8u\"\u0011q5~qd)\u000f87h=hg\u0017>|z>c,C0/-4i7\u00062(h5\u0006\u0017(\u0012\u000f<c";
                    var4_3 = "\u0017\u00118*l%tg\b4%-4\u007f$\f55c61.\u0010q2b%1\u0017(\u0012\u000f.i1jNq,b\"b.\u0001=9-9p5\u0007&=\u007f41,\u0006(/y>c\"\\q|X\"tg\f7|y9x4C:9tqf.\u00179|G\u001cBg\u0013#3{8u\"\u0011q5~qd)\u000f87h=hg\u0017>|z>c,C0/-4i7\u00062(h5\u0006\u0017(\u0012\u000f<c".length();
                    var1_4 = 132;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 105;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = JmsPropertyMapper.a(v0, JmsPropertyMapper.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        JmsPropertyMapper.f = var5;
        JmsPropertyMapper.a = Logger.getLogger(JmsPropertyMapper.class.getName());
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x78);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 46;
                    case 1 -> 10;
                    case 2 -> 56;
                    case 3 -> 53;
                    case 4 -> 100;
                    case 5 -> 56;
                    default -> 120;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

