/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.security.prov.JceProvider
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Functions$UnaryVoid
 *  com.l7tech.util.MasterPasswordManager
 *  com.l7tech.util.ResourceUtils
 *  org.springframework.orm.hibernate5.HibernateCallback
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.security.keystore;

import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.security.prov.JceProvider;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.security.keystore.KeystoreFile;
import com.l7tech.server.security.keystore.KeystoreFileManager;
import com.l7tech.server.security.keystore.e;
import com.l7tech.server.security.keystore.f;
import com.l7tech.server.util.PropertiesDecryptor;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Functions;
import com.l7tech.util.MasterPasswordManager;
import com.l7tech.util.ResourceUtils;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;
import java.util.logging.Logger;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
public class KeystoreFileManagerImpl
extends HibernateEntityManager<KeystoreFile, EntityHeader>
implements KeystoreFileManager {
    protected static final Logger logger;
    private static final String m;
    private static final String n;
    private static final String o;
    private MasterPasswordManager p;
    private static final String[] q;

    public KeystoreFileManagerImpl(MasterPasswordManager masterPasswordManager) {
        this.p = masterPasswordManager;
    }

    public Class<KeystoreFile> getImpClass() {
        return KeystoreFile.class;
    }

    @Override
    public EntityType getEntityType() {
        return EntityType.SSG_KEY_ENTRY;
    }

    @Override
    public KeystoreFile mutateKeystoreFile(Goid goid, Functions.UnaryVoid<KeystoreFile> unaryVoid) throws UpdateException {
        try {
            return (KeystoreFile)((Object)this.getHibernateTemplate().execute((HibernateCallback)new e(this, goid, unaryVoid)));
        }
        catch (Exception exception) {
            throw new UpdateException((Throwable)exception);
        }
    }

    /*
     * Loose catch block
     */
    @Override
    public void initializeHsmKeystorePasswordFromFile() throws UpdateException {
        block22: {
            try {
                if (!this.isHsmAvailable()) {
                    return;
                }
            }
            catch (Exception exception) {
                throw KeystoreFileManagerImpl.a(exception);
            }
            String[] stringArray = q;
            File file = new File(stringArray[9]);
            File file2 = new File(file, stringArray[4]);
            try {
                String string;
                block21: {
                    Properties properties = this.a(file2);
                    string = properties.getProperty(stringArray[11]);
                    try {
                        if (string != null) break block21;
                        logger.info(stringArray[3]);
                        break block22;
                    }
                    catch (Exception exception) {
                        throw KeystoreFileManagerImpl.a(exception);
                    }
                }
                KeystoreFile keystoreFile = null;
                Collection collection = this.findAll();
                for (KeystoreFile keystoreFile2 : collection) {
                    block23: {
                        block24: {
                            if (keystoreFile2.getFormat() == null) continue;
                            if (!keystoreFile2.getFormat().startsWith(q[10])) continue;
                            break block24;
                            catch (Exception exception) {
                                throw KeystoreFileManagerImpl.a(exception);
                            }
                        }
                        try {
                            block25: {
                                if (keystoreFile == null) break block23;
                                break block25;
                                catch (Exception exception) {
                                    throw KeystoreFileManagerImpl.a(exception);
                                }
                            }
                            throw new UpdateException(q[6]);
                        }
                        catch (Exception exception) {
                            throw KeystoreFileManagerImpl.a(exception);
                        }
                    }
                    keystoreFile = keystoreFile2;
                }
                try {
                    if (keystoreFile == null) {
                        throw new UpdateException(q[7]);
                    }
                }
                catch (Exception exception) {
                    throw KeystoreFileManagerImpl.a(exception);
                }
                this.a(keystoreFile.getGoid(), string.toCharArray());
                if (!file2.exists()) break block22;
                try {
                    block26: {
                        if (file2.delete()) break block22;
                        break block26;
                        catch (Exception exception) {
                            throw KeystoreFileManagerImpl.a(exception);
                        }
                    }
                    logger.warning(q[2] + file2.getAbsolutePath() + q[8]);
                }
                catch (Exception exception) {
                    throw KeystoreFileManagerImpl.a(exception);
                }
            }
            catch (Exception exception) {
                throw new UpdateException((Throwable)exception);
            }
        }
    }

    private void a(Goid goid, char[] cArray) throws UpdateException {
        try {
            MasterPasswordManager masterPasswordManager = (MasterPasswordManager)this.applicationContext.getBean(q[5]);
            this.getHibernateTemplate().execute((HibernateCallback)new f(this, goid, masterPasswordManager, cArray));
        }
        catch (Exception exception) {
            throw new UpdateException((Throwable)exception);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private Properties a(File file) {
        Properties properties = new Properties();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            properties.load(fileInputStream);
        }
        catch (IOException iOException) {
            try {
                String[] stringArray = q;
                logger.info(stringArray[0] + file.getAbsolutePath() + stringArray[12] + ExceptionUtils.getMessage((Throwable)iOException));
            }
            catch (Throwable throwable) {
                ResourceUtils.closeQuietly(fileInputStream);
                throw throwable;
            }
            ResourceUtils.closeQuietly((Closeable)fileInputStream);
        }
        ResourceUtils.closeQuietly((Closeable)fileInputStream);
        PropertiesDecryptor propertiesDecryptor = new PropertiesDecryptor(this.p);
        propertiesDecryptor.setPasswordProperties(new String[]{q[13]});
        propertiesDecryptor.decryptEncryptedPasswords(properties);
        return properties;
    }

    public boolean isHsmAvailable() {
        return JceProvider.PKCS11_ENGINE.equals(JceProvider.getEngineClass());
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[15];
                var3_1 = 0;
                var2_2 = "\u0003V\u001dm~\u000b\r/MHs\u007fJ\u0007`M\u0000d:c0\r\u0019\u0001os_C&P\u0004d4\u000b+\u0013tHj\u007fR\u00104V\u001ad:M\n,\\HvsG\u000f`W\u0007u:I\u0006`P\u0006hnB\u0002)U\u0001{\u007fOC7P\u001ci:JC.\\\u001f!jJ\u00103N\u0007s~\u000bD\u0013(J\u0005^sE\n4\u0017\u0018su[\u00062M\u0001di%\u0004\\\u0004dnNC&X\u0001m\u007fOC&V\u001a!Ih\"`q;L:B\r)MHgsG\u0006`\u001e=\u000eVHIIfC)W\u0001u:M\n,\\Hnh\u000b\u0013!J\u001bvuY\u0007`_\u0007ttOM`l\u001bhtLC4Q\r!~J\u0017![\tr\u007f\u000b\u0015!U\u001dd4\u0013(J\u0005^sE\n4\u0017\u0018su[\u00062M\u0001di\u0014$[8`iX\u0014/K\fDtH\u00119I\u001chuE8\u0004X\u001c`xJ\u0010%\u0019\u000bnt_\u0002)W\u001b!w^\u000f4P\u0018m\u007f\u000b\u0006.M\u001ah\u007fXC&V\u001a!Ih\"`q;L:@\u00069J\u001cnhNM6\u0003V\u001dm~\u000b\r/MHgsE\u0007`X\u0006!Ih\"`q;L:@\u00069J\u001cnhNC2V\u001f!sEC4Q\r!~J\u0017![\tr\u007f\u0002g\u0017)oV\u0018u5x\u0006#L\u001adI[\u0002.\u0016/`nN\u0014!@GouO\u0006o]\rg{^\u000f4\u0016\u001e`h\u0004\b(J\u0005/iH\u0002n\u0010(J\u0005/iH\u0002nI\tri\\\f2]\u0002g\u0019";
                var4_3 = "\u0003V\u001dm~\u000b\r/MHs\u007fJ\u0007`M\u0000d:c0\r\u0019\u0001os_C&P\u0004d4\u000b+\u0013tHj\u007fR\u00104V\u001ad:M\n,\\HvsG\u000f`W\u0007u:I\u0006`P\u0006hnB\u0002)U\u0001{\u007fOC7P\u001ci:JC.\\\u001f!jJ\u00103N\u0007s~\u000bD\u0013(J\u0005^sE\n4\u0017\u0018su[\u00062M\u0001di%\u0004\\\u0004dnNC&X\u0001m\u007fOC&V\u001a!Ih\"`q;L:B\r)MHgsG\u0006`\u001e=\u000eVHIIfC)W\u0001u:M\n,\\Hnh\u000b\u0013!J\u001bvuY\u0007`_\u0007ttOM`l\u001bhtLC4Q\r!~J\u0017![\tr\u007f\u000b\u0015!U\u001dd4\u0013(J\u0005^sE\n4\u0017\u0018su[\u00062M\u0001di\u0014$[8`iX\u0014/K\fDtH\u00119I\u001chuE8\u0004X\u001c`xJ\u0010%\u0019\u000bnt_\u0002)W\u001b!w^\u000f4P\u0018m\u007f\u000b\u0006.M\u001ah\u007fXC&V\u001a!Ih\"`q;L:@\u00069J\u001cnhNM6\u0003V\u001dm~\u000b\r/MHgsE\u0007`X\u0006!Ih\"`q;L:@\u00069J\u001cnhNC2V\u001f!sEC4Q\r!~J\u0017![\tr\u007f\u0002g\u0017)oV\u0018u5x\u0006#L\u001adI[\u0002.\u0016/`nN\u0014!@GouO\u0006o]\rg{^\u000f4\u0016\u001e`h\u0004\b(J\u0005/iH\u0002n\u0010(J\u0005/iH\u0002nI\tri\\\f2]\u0002g\u0019".length();
                var1_4 = 98;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = KeystoreFileManagerImpl.b(v0, KeystoreFileManagerImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u000fm\"\bNo%In.UN{+\u0015z)Hq?R\u0012_!\u0004k=Cn|%\t1\bGIi3\u0006g`HRh!Hz*@\\y(\u001319GO#";
                    var4_3 = "\u000fm\"\bNo%In.UN{+\u0015z)Hq?R\u0012_!\u0004k=Cn|%\t1\bGIi3\u0006g`HRh!Hz*@\\y(\u001319GO#".length();
                    var1_4 = 16;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 9;
                        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++] = KeystoreFileManagerImpl.b(v0, KeystoreFileManagerImpl.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;
            }
        }
        KeystoreFileManagerImpl.q = var5;
        KeystoreFileManagerImpl.m = KeystoreFileManagerImpl.q[14];
        KeystoreFileManagerImpl.o = KeystoreFileManagerImpl.q[13];
        KeystoreFileManagerImpl.n = KeystoreFileManagerImpl.q[1];
        KeystoreFileManagerImpl.logger = Logger.getLogger(KeystoreFileManagerImpl.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] ^ 0x4D);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 110;
                    case 1 -> 23;
                    case 2 -> 70;
                    case 3 -> 47;
                    case 4 -> 52;
                    case 5 -> 5;
                    default -> 77;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

