/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.security.prov.JceProvider
 *  com.l7tech.security.prov.ProviderUtil
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Functions$Nullary
 *  com.l7tech.util.HexUtils
 *  org.bouncycastle.jcajce.PKCS12Key
 *  org.springframework.dao.DataAccessException
 *  org.springframework.orm.hibernate5.HibernateCallback
 *  org.springframework.orm.hibernate5.support.HibernateDaoSupport
 *  org.springframework.transaction.PlatformTransactionManager
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 *  org.springframework.transaction.support.TransactionCallback
 *  org.springframework.transaction.support.TransactionTemplate
 */
package com.l7tech.server.security.sharedkey;

import com.l7tech.objectmodel.FindException;
import com.l7tech.security.prov.JceProvider;
import com.l7tech.security.prov.ProviderUtil;
import com.l7tech.server.security.sharedkey.SharedKeyManager;
import com.l7tech.server.security.sharedkey.SharedKeyRecord;
import com.l7tech.server.security.sharedkey.a;
import com.l7tech.server.security.sharedkey.b;
import com.l7tech.server.security.sharedkey.c;
import com.l7tech.server.security.sharedkey.d;
import com.l7tech.server.security.sharedkey.e;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Functions;
import com.l7tech.util.HexUtils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.spec.InvalidParameterSpecException;
import java.util.Collection;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.jcajce.PKCS12Key;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
public class SharedKeyManagerImpl
extends HibernateDaoSupport
implements SharedKeyManager {
    private final Logger a = Logger.getLogger(SharedKeyManagerImpl.class.getName());
    public static final String CLUSTER_WIDE_IDENTIFIER;
    public static final String CIPHER;
    private static final int b = 1024;
    private final PlatformTransactionManager c;
    private SecretKey d;
    private SecretKey e;
    private static final SecureRandom f;
    private static final String[] g;

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public SharedKeyManagerImpl(char[] cArray, PlatformTransactionManager platformTransactionManager) {
        if (cArray == null) throw new IllegalArgumentException(g[10]);
        try {
            if (cArray.length == 0) {
                throw new IllegalArgumentException(g[10]);
            }
        }
        catch (GeneralSecurityException generalSecurityException) {
            throw SharedKeyManagerImpl.a(generalSecurityException);
        }
        try {
            if (platformTransactionManager == null) {
                throw new IllegalArgumentException(g[16]);
            }
        }
        catch (GeneralSecurityException generalSecurityException) {
            throw SharedKeyManagerImpl.a(generalSecurityException);
        }
        try {
            String[] stringArray = g;
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(stringArray[15]);
            this.d = secretKeyFactory.generateSecret(new PBEKeySpec(cArray));
            this.e = new PKCS12Key(cArray);
        }
        catch (GeneralSecurityException generalSecurityException) {
            throw new RuntimeException(g[7] + ExceptionUtils.getMessage((Throwable)generalSecurityException), generalSecurityException);
        }
        this.c = platformTransactionManager;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    SharedKeyManagerImpl(char[] cArray) {
        if (cArray == null) throw new IllegalArgumentException(g[22]);
        try {
            if (cArray.length == 0) {
                throw new IllegalArgumentException(g[22]);
            }
        }
        catch (GeneralSecurityException generalSecurityException) {
            throw SharedKeyManagerImpl.a(generalSecurityException);
        }
        try {
            String[] stringArray = g;
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(stringArray[6]);
            this.d = secretKeyFactory.generateSecret(new PBEKeySpec(cArray));
            this.e = new PKCS12Key(cArray);
        }
        catch (GeneralSecurityException generalSecurityException) {
            throw new RuntimeException(g[12] + ExceptionUtils.getMessage((Throwable)generalSecurityException), generalSecurityException);
        }
        this.c = null;
    }

    @Override
    public byte[] getSharedKey() throws FindException {
        block4: {
            Collection<SharedKeyRecord> collection = this.selectSharedKeyRecordsByEncodingId();
            try {
                if (collection == null || collection.size() <= 0) break block4;
            }
            catch (Exception exception) {
                throw SharedKeyManagerImpl.a(exception);
            }
            SharedKeyRecord sharedKeyRecord = (SharedKeyRecord)collection.toArray()[0];
            try {
                String[] stringArray = g;
                this.a.fine(stringArray[24]);
                return this.a(sharedKeyRecord.getB64edKey());
            }
            catch (Exception exception) {
                throw new FindException(g[27] + ExceptionUtils.getMessage((Throwable)exception), (Throwable)exception);
            }
        }
        return this.a();
    }

    @Override
    public String getEncrytpedSharedKey(String string) throws FindException {
        String[] stringArray = g;
        String string2 = stringArray[25] + SharedKeyRecord.class.getName() + stringArray[21];
        Collection collection = (Collection)this.getHibernateTemplate().execute((HibernateCallback)new d(this, string2, string));
        try {
            if (collection.isEmpty()) {
                throw new FindException(stringArray[9] + string + stringArray[2]);
            }
        }
        catch (FindException findException) {
            throw SharedKeyManagerImpl.a((Exception)((Object)findException));
        }
        try {
            if (collection.size() > 1) {
                throw new FindException(g[5] + string + g[4]);
            }
        }
        catch (FindException findException) {
            throw SharedKeyManagerImpl.a((Exception)((Object)findException));
        }
        return ((SharedKeyRecord)collection.iterator().next()).getB64edKey();
    }

    @Override
    public void saveAndEncryptSharedKey(byte[] byArray) throws FindException {
        String[] stringArray;
        SharedKeyRecord sharedKeyRecord = new SharedKeyRecord();
        try {
            stringArray = g;
            sharedKeyRecord.setEncodingID(stringArray[23]);
            sharedKeyRecord.setB64edKey(this.a(byArray));
        }
        catch (Exception exception) {
            String[] stringArray2 = g;
            throw new FindException(stringArray2[14] + ExceptionUtils.getMessage((Throwable)exception), (Throwable)exception);
        }
        stringArray = g;
        this.a.info(stringArray[1]);
        try {
            this.a(new a(this, sharedKeyRecord));
        }
        catch (DataAccessException dataAccessException) {
            throw new FindException(g[18] + ExceptionUtils.getMessage((Throwable)dataAccessException), (Throwable)dataAccessException);
        }
    }

    private byte[] a() throws FindException {
        String[] stringArray = g;
        this.a.info(stringArray[0]);
        byte[] byArray = this.b();
        this.saveAndEncryptSharedKey(byArray);
        this.a.info(stringArray[8]);
        return byArray;
    }

    private <T> T a(Functions.Nullary<T> nullary) {
        if (this.c != null) {
            TransactionTemplate transactionTemplate = new TransactionTemplate(this.c);
            transactionTemplate.setPropagationBehavior(3);
            return (T)transactionTemplate.execute((TransactionCallback)new b(this, nullary));
        }
        return (T)nullary.call();
    }

    protected void saveSharedKeyRecord(SharedKeyRecord sharedKeyRecord) throws DataAccessException {
        this.getHibernateTemplate().execute((HibernateCallback)new c(this, sharedKeyRecord));
    }

    protected Collection<SharedKeyRecord> selectSharedKeyRecordsByEncodingId() {
        String[] stringArray = g;
        String string = stringArray[19] + SharedKeyRecord.class.getName() + stringArray[26];
        return (Collection)this.getHibernateTemplate().execute((HibernateCallback)new e(this, string));
    }

    private String a(byte[] byArray) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException, InvalidAlgorithmParameterException {
        byte[] byArray2;
        byte[] byArray3;
        Cipher cipher;
        int n2;
        block5: {
            Cipher cipher2;
            Provider provider;
            block4: {
                n2 = 0;
                provider = JceProvider.getInstance().getProviderFor(JceProvider.SERVICE_PBE_WITH_SHA1_AND_DESEDE);
                try {
                    if (provider != null) break block4;
                    cipher2 = Cipher.getInstance(g[6]);
                    break block5;
                }
                catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                    throw SharedKeyManagerImpl.a(noSuchAlgorithmException);
                }
            }
            String[] stringArray = g;
            cipher2 = cipher = Cipher.getInstance(stringArray[6], provider);
        }
        if (ProviderUtil.BCFIPS_PROVIDER_NAME.equalsIgnoreCase(cipher.getProvider().getName())) {
            byArray3 = this.b();
            n2 = 1024;
            cipher.init(1, (Key)this.e, new PBEParameterSpec(byArray3, n2));
            byArray2 = cipher.doFinal(byArray);
        } else {
            cipher.init(1, this.d);
            byArray2 = cipher.doFinal(byArray);
            PBEParameterSpec pBEParameterSpec = cipher.getParameters().getParameterSpec(PBEParameterSpec.class);
            byArray3 = pBEParameterSpec.getSalt();
            n2 = pBEParameterSpec.getIterationCount();
        }
        String string = HexUtils.encodeBase64((byte[])byArray2, (boolean)true);
        int n3 = n2;
        String string2 = HexUtils.encodeBase64((byte[])byArray3, (boolean)true);
        return g[11] + string2 + "$" + n3 + "$" + string + "$";
    }

    private byte[] a(String string) throws IOException, GeneralSecurityException {
        Cipher cipher;
        byte[] byArray;
        block10: {
            int n2;
            byte[] byArray2;
            block9: {
                Cipher cipher2;
                String[] stringArray = g;
                Pattern pattern = Pattern.compile(stringArray[17]);
                Matcher matcher = pattern.matcher(string);
                try {
                    if (!matcher.matches()) {
                        throw new IOException(stringArray[13] + string);
                    }
                }
                catch (NumberFormatException numberFormatException) {
                    throw SharedKeyManagerImpl.a(numberFormatException);
                }
                String string2 = matcher.group(1);
                String string3 = matcher.group(2);
                String string4 = matcher.group(3);
                try {
                    byArray2 = HexUtils.decodeBase64((String)string2);
                    n2 = Integer.parseInt(string3);
                    byArray = HexUtils.decodeBase64((String)string4);
                }
                catch (NumberFormatException numberFormatException) {
                    stringArray = g;
                    throw new IOException(stringArray[3] + string, numberFormatException);
                }
                Provider provider = JceProvider.getInstance().getProviderFor(JceProvider.SERVICE_PBE_WITH_SHA1_AND_DESEDE);
                try {
                    cipher2 = provider == null ? Cipher.getInstance(g[6]) : Cipher.getInstance(g[6], provider);
                }
                catch (NumberFormatException numberFormatException) {
                    throw SharedKeyManagerImpl.a(numberFormatException);
                }
                cipher = cipher2;
                try {
                    if (!ProviderUtil.BCFIPS_PROVIDER_NAME.equalsIgnoreCase(cipher.getProvider().getName())) break block9;
                    cipher.init(2, (Key)this.e, new PBEParameterSpec(byArray2, n2));
                    break block10;
                }
                catch (NumberFormatException numberFormatException) {
                    throw SharedKeyManagerImpl.a(numberFormatException);
                }
            }
            cipher.init(2, (Key)this.d, new PBEParameterSpec(byArray2, n2));
        }
        return cipher.doFinal(byArray);
    }

    private byte[] b() {
        byte[] byArray = new byte[64];
        f.nextBytes(byArray);
        return byArray;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[28];
                var3_1 = 0;
                var2_2 = ".RCi\u0011o\u000e\u0016_[;\u0010dK\u000e\u001aLt\u0000+W\u0018N\u0002~\fb]\t\u0016\u0002z\u0000\u007fK\u0010JVr\u001al\u000e\tU\u0002x\u0006nO\t_\u0002t\u001an\u0012.[Tr\u001al\u000e\u000eRCi\u0011o\u000e\u0016_[5\u0002Z\u0014\u001b4TTz\u0018bJ]IJz\u0006nJ]QGbTmA\u000fWCoN+\u0002Z\u0014+0ONo\u001d{B\u0018\u001aQs\u0015yK\u0019\u001aI~\rx\u000e\u001bUWu\u0010+H\u0012H\u0002r\u0010n@\tSDr\u0011y\u000eZ\u0014-xgL\u001d\u007fF.rc*5eJ9\u007fq~\u0010n<(TCy\u0018n\u000e\tU\u0002r\u001abZ\u0014[Nr\u000en\u000e\u0019_Ai\r{Z\u0014UL;\u001fnW]\\MiThB\bIV~\u0006+]\u0015[P~\u0010+E\u0018C\u0018;\"\u0013_U;\u0007cO\u000f_F;\u001fnW]ICm\u0011o\u0002]HGo\u0001y@\u0014TE;\u001d\u007f%.RCi\u0011o\u000e\u0016_[;\u001adZ]\\Mn\u001ao\u000e\u001bUP;\u001doK\u0013NK}\u001dn\\]\u001d\u001d\u001eVWh\u0000n\\-[Qh\u0004c\\\u001cIG;\u001dx\u000e\u000f_Sn\u001dyK\u0019\u0006Yj`^E/<(TCy\u0018n\u000e\tU\u0002r\u001abZ\u0014[Nr\u000en\u000e\u0019_Ai\r{Z\u0014UL;\u001fnW]\\MiThB\bIV~\u0006+]\u0015[P~\u0010+E\u0018C\u0018;\u001b4TTz\u0018bJ]IJz\u0006nJ]QGbTmA\u000fWCoN+\">UWw\u0010+@\u0012N\u0002~\u001ah\\\u0004JV;\u001anY]IJz\u0006nJ]QGbN+\u0014-xgL\u001d\u007fF.rc*5eJ9\u007fq~\u0010n\u001e\tHCu\u0007jM\tSMu9j@\u001c]GiTb]]HGj\u0001b\\\u0018^##f\u0006K6N\u001f!\u001e\n@*/sV\u0013~?\\WJV\u0013~?\\PpYg\t2(/\n\u0018(TCy\u0018n\u000e\tU\u0002h\u0015}K]TGlT`K\u0004\u0000\u0002\u001b]\\Pt\u0019+]\u0015[P~\u0010TE\u0018CQ;\u001de\u000e\u001eVCh\u0007+\u0010XyNn\u0007\u007fK\u000fmK\u007f\u0011[l8\u001f+]MJ~\u0006n\u000e\u000eRCi\u0011oq\u0016_[hZn@\u001eUFr\u001alg9\u001a\u001f;NbJ\u0018TVr\u0012bK\u000f\u001d\u001eVWh\u0000n\\-[Qh\u0004c\\\u001cIG;\u001dx\u000e\u000f_Sn\u001dyK\u0019\u0010XyNn\u0007\u007fK\u000fmK\u007f\u0011[l8\u001f*.RCi\u0011o\u000e\u0016_[;\u0012d[\u0013^\u000e;\u0015\u007fZ\u0018WRo\u001deI]NM;\u0010nM\u000fCRoTbZ\u001b]\\Pt\u0019+]\u0015[P~\u0010TE\u0018CQ;\u001de\u000e\u001eVCh\u0007+";
                var4_3 = ".RCi\u0011o\u000e\u0016_[;\u0010dK\u000e\u001aLt\u0000+W\u0018N\u0002~\fb]\t\u0016\u0002z\u0000\u007fK\u0010JVr\u001al\u000e\tU\u0002x\u0006nO\t_\u0002t\u001an\u0012.[Tr\u001al\u000e\u000eRCi\u0011o\u000e\u0016_[5\u0002Z\u0014\u001b4TTz\u0018bJ]IJz\u0006nJ]QGbTmA\u000fWCoN+\u0002Z\u0014+0ONo\u001d{B\u0018\u001aQs\u0015yK\u0019\u001aI~\rx\u000e\u001bUWu\u0010+H\u0012H\u0002r\u0010n@\tSDr\u0011y\u000eZ\u0014-xgL\u001d\u007fF.rc*5eJ9\u007fq~\u0010n<(TCy\u0018n\u000e\tU\u0002r\u001abZ\u0014[Nr\u000en\u000e\u0019_Ai\r{Z\u0014UL;\u001fnW]\\MiThB\bIV~\u0006+]\u0015[P~\u0010+E\u0018C\u0018;\"\u0013_U;\u0007cO\u000f_F;\u001fnW]ICm\u0011o\u0002]HGo\u0001y@\u0014TE;\u001d\u007f%.RCi\u0011o\u000e\u0016_[;\u001adZ]\\Mn\u001ao\u000e\u001bUP;\u001doK\u0013NK}\u001dn\\]\u001d\u001d\u001eVWh\u0000n\\-[Qh\u0004c\\\u001cIG;\u001dx\u000e\u000f_Sn\u001dyK\u0019\u0006Yj`^E/<(TCy\u0018n\u000e\tU\u0002r\u001abZ\u0014[Nr\u000en\u000e\u0019_Ai\r{Z\u0014UL;\u001fnW]\\MiThB\bIV~\u0006+]\u0015[P~\u0010+E\u0018C\u0018;\u001b4TTz\u0018bJ]IJz\u0006nJ]QGbTmA\u000fWCoN+\">UWw\u0010+@\u0012N\u0002~\u001ah\\\u0004JV;\u001anY]IJz\u0006nJ]QGbN+\u0014-xgL\u001d\u007fF.rc*5eJ9\u007fq~\u0010n\u001e\tHCu\u0007jM\tSMu9j@\u001c]GiTb]]HGj\u0001b\\\u0018^##f\u0006K6N\u001f!\u001e\n@*/sV\u0013~?\\WJV\u0013~?\\PpYg\t2(/\n\u0018(TCy\u0018n\u000e\tU\u0002h\u0015}K]TGlT`K\u0004\u0000\u0002\u001b]\\Pt\u0019+]\u0015[P~\u0010TE\u0018CQ;\u001de\u000e\u001eVCh\u0007+\u0010XyNn\u0007\u007fK\u000fmK\u007f\u0011[l8\u001f+]MJ~\u0006n\u000e\u000eRCi\u0011oq\u0016_[hZn@\u001eUFr\u001alg9\u001a\u001f;NbJ\u0018TVr\u0012bK\u000f\u001d\u001eVWh\u0000n\\-[Qh\u0004c\\\u001cIG;\u001dx\u000e\u000f_Sn\u001dyK\u0019\u0010XyNn\u0007\u007fK\u000fmK\u007f\u0011[l8\u001f*.RCi\u0011o\u000e\u0016_[;\u0012d[\u0013^\u000e;\u0015\u007fZ\u0018WRo\u001deI]NM;\u0010nM\u000fCRoTbZ\u001b]\\Pt\u0019+]\u0015[P~\u0010TE\u0018CQ;\u001de\u000e\u001eVCh\u0007+".length();
                var1_4 = 55;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = SharedKeyManagerImpl.a(v0, SharedKeyManagerImpl.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "%52\u0006~\u0016vv*;\u0011i\u0017\tn'#\u0010\"\u00168f->\nb\u0014\u001fAbgC6\u00168f->\nb\u0014\u001fA\u001ef-/\u000fhS8j6z\u0007i\u0010$|2.C\u007f\u001b7w'>Cg\u0016/?b";
                    var4_3 = "%52\u0006~\u0016vv*;\u0011i\u0017\tn'#\u0010\"\u00168f->\nb\u0014\u001fAbgC6\u00168f->\nb\u0014\u001fA\u001ef-/\u000fhS8j6z\u0007i\u0010$|2.C\u007f\u001b7w'>Cg\u0016/?b".length();
                    var1_4 = 43;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 71;
                        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++] = SharedKeyManagerImpl.a(v0, SharedKeyManagerImpl.b(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;
            }
        }
        SharedKeyManagerImpl.g = var5;
        SharedKeyManagerImpl.CIPHER = SharedKeyManagerImpl.g[6];
        SharedKeyManagerImpl.CLUSTER_WIDE_IDENTIFIER = SharedKeyManagerImpl.g[20];
        SharedKeyManagerImpl.f = new SecureRandom();
    }

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

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x11);
        }
        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 -> 66;
                    case 1 -> 5;
                    case 2 -> 29;
                    case 3 -> 36;
                    case 4 -> 75;
                    case 5 -> 52;
                    default -> 17;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

