/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.WhirlycacheFactory
 *  com.l7tech.gateway.common.security.keystore.SsgKeyEntry
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.ObjectNotFoundException
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.cert.TrustedCertManager
 *  com.l7tech.security.cert.X509Entity
 *  com.l7tech.security.token.KerberosSigningSecurityToken
 *  com.l7tech.security.xml.SecurityTokenResolver
 *  com.l7tech.security.xml.SignerInfo
 *  com.l7tech.security.xml.SimpleSecurityTokenResolver
 *  com.l7tech.util.Background
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.SoapConstants
 *  com.whirlycott.cache.Cache
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server;

import com.l7tech.common.io.WhirlycacheFactory;
import com.l7tech.gateway.common.security.keystore.SsgKeyEntry;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.ObjectNotFoundException;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.security.cert.TrustedCertManager;
import com.l7tech.security.cert.X509Entity;
import com.l7tech.security.token.KerberosSigningSecurityToken;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.SignerInfo;
import com.l7tech.security.xml.SimpleSecurityTokenResolver;
import com.l7tech.server.b8;
import com.l7tech.server.ba;
import com.l7tech.server.event.EntityClassEvent;
import com.l7tech.server.security.keystore.KeystoreFile;
import com.l7tech.server.security.keystore.SsgKeyFinder;
import com.l7tech.server.security.keystore.SsgKeyStoreManager;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.Background;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.SoapConstants;
import com.whirlycott.cache.Cache;
import java.math.BigInteger;
import java.security.KeyStoreException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.x500.X500Principal;
import org.springframework.context.ApplicationEvent;

public class TrustedCertificateResolver
implements SecurityTokenResolver,
PostStartupApplicationListener {
    private static final Logger a;
    private final TrustedCertManager b;
    private final Cache c;
    private final AtomicBoolean d = new AtomicBoolean();
    private final SsgKeyStoreManager e;
    private volatile SimpleSecurityTokenResolver f;
    private static final String[] g;

    public TrustedCertificateResolver(TrustedCertManager trustedCertManager, Config config, SsgKeyStoreManager ssgKeyStoreManager) {
        int n2;
        boolean bl2;
        AtomicBoolean atomicBoolean;
        this.b = trustedCertManager;
        this.e = ssgKeyStoreManager;
        int n3 = 10181;
        int n4 = 1000;
        String[] stringArray = g;
        int n5 = config.getIntProperty(stringArray[10], 1000);
        try {
            atomicBoolean = this.d;
            bl2 = n5 > 0;
        }
        catch (RuntimeException runtimeException) {
            throw TrustedCertificateResolver.a(runtimeException);
        }
        try {
            atomicBoolean.set(bl2);
            n2 = this.d.get() ? n5 : 1000;
        }
        catch (RuntimeException runtimeException) {
            throw TrustedCertificateResolver.a(runtimeException);
        }
        int n6 = n2;
        stringArray = g;
        this.c = WhirlycacheFactory.createCache((String)stringArray[6], (int)n6, (int)127, (String)WhirlycacheFactory.POLICY_LRU);
        a.info(stringArray[7] + n6);
        Background.scheduleRepeated((TimerTask)new b8(this, config), (long)10181L, (long)10181L);
    }

    public X509Certificate lookup(String string) {
        try {
            block5: {
                SignerInfo signerInfo = this.lookupPrivateKeyByX509Thumbprint(string);
                if (signerInfo != null) {
                    return signerInfo.getCertificateChain()[0];
                }
                List list = this.b.findByThumbprint(string);
                try {
                    if (list == null || list.size() < 1) break block5;
                }
                catch (FindException findException) {
                    throw TrustedCertificateResolver.a((Exception)((Object)findException));
                }
                return ((X509Entity)list.get(0)).getCertificate();
            }
            return TrustedCertificateResolver.a(this.lookupPrivateKeyByX509Thumbprint(string));
        }
        catch (FindException findException) {
            throw new RuntimeException(findException);
        }
    }

    public X509Certificate lookupBySki(String string) {
        try {
            block5: {
                SignerInfo signerInfo = this.lookupPrivateKeyBySki(string);
                if (signerInfo != null) {
                    return signerInfo.getCertificateChain()[0];
                }
                List list = this.b.findBySki(string);
                try {
                    if (list == null || list.size() < 1) break block5;
                }
                catch (FindException findException) {
                    throw TrustedCertificateResolver.a((Exception)((Object)findException));
                }
                return ((X509Entity)list.get(0)).getCertificate();
            }
            return TrustedCertificateResolver.a(this.lookupPrivateKeyBySki(string));
        }
        catch (FindException findException) {
            throw new RuntimeException(findException);
        }
    }

    public X509Certificate lookupByKeyName(String string) {
        try {
            block5: {
                Collection collection = this.b.findBySubjectDn(string);
                try {
                    if (collection == null || collection.isEmpty()) break block5;
                }
                catch (FindException findException) {
                    throw TrustedCertificateResolver.a((Exception)((Object)findException));
                }
                return ((TrustedCert)collection.iterator().next()).getCertificate();
            }
            SignerInfo signerInfo = this.lookupPrivateKeyByKeyName(string);
            if (signerInfo != null) {
                return signerInfo.getCertificateChain()[0];
            }
            return null;
        }
        catch (FindException findException) {
            throw new RuntimeException(findException);
        }
    }

    public X509Certificate lookupByIssuerAndSerial(X500Principal x500Principal, BigInteger bigInteger) {
        try {
            block4: {
                List list = this.b.findByIssuerAndSerial(x500Principal, bigInteger);
                try {
                    if (list == null || list.size() < 1) break block4;
                }
                catch (FindException findException) {
                    throw TrustedCertificateResolver.a((Exception)((Object)findException));
                }
                return ((X509Entity)list.get(0)).getCertificate();
            }
            return TrustedCertificateResolver.a(this.lookupPrivateKeyByIssuerAndSerial(x500Principal, bigInteger));
        }
        catch (FindException findException) {
            throw new RuntimeException(findException);
        }
    }

    private SecurityTokenResolver a() {
        Object object;
        SimpleSecurityTokenResolver simpleSecurityTokenResolver = this.f;
        try {
            object = simpleSecurityTokenResolver != null ? simpleSecurityTokenResolver : this.b();
        }
        catch (RuntimeException runtimeException) {
            throw TrustedCertificateResolver.a(runtimeException);
        }
        return object;
    }

    private synchronized SecurityTokenResolver b() {
        List<SsgKeyFinder> list;
        ArrayList<SsgKeyEntry> arrayList = new ArrayList<SsgKeyEntry>();
        try {
            list = this.e.findAll();
        }
        catch (FindException findException) {
            String[] stringArray = g;
            String string = stringArray[0] + ExceptionUtils.getMessage((Throwable)findException);
            throw new RuntimeException(string, findException);
        }
        catch (KeyStoreException keyStoreException) {
            String[] stringArray = g;
            String string = stringArray[9] + ExceptionUtils.getMessage((Throwable)keyStoreException);
            throw new RuntimeException(string, keyStoreException);
        }
        for (SsgKeyFinder ssgKeyFinder : list) {
            String[] stringArray;
            List<String> list2;
            try {
                list2 = ssgKeyFinder.getAliases();
            }
            catch (KeyStoreException keyStoreException) {
                stringArray = g;
                a.log(Level.WARNING, stringArray[5] + ssgKeyFinder.getName() + stringArray[8] + ExceptionUtils.getMessage((Throwable)keyStoreException), keyStoreException);
                continue;
            }
            for (String string : list2) {
                try {
                    SsgKeyEntry ssgKeyEntry = ssgKeyFinder.getCertificateChain(string);
                    try {
                        if (!ssgKeyEntry.isPrivateKeyAvailable()) continue;
                        arrayList.add(ssgKeyEntry);
                    }
                    catch (FindException findException) {
                        throw TrustedCertificateResolver.a((Exception)((Object)findException));
                    }
                }
                catch (ObjectNotFoundException objectNotFoundException) {
                    String string2 = ExceptionUtils.getMessage((Throwable)objectNotFoundException);
                    String string3 = ssgKeyFinder.getName();
                    String string4 = string;
                    stringArray = g;
                    a.log(Level.WARNING, stringArray[2] + string4 + stringArray[3] + string3 + stringArray[11] + string2, ExceptionUtils.getDebugException((Throwable)objectNotFoundException));
                }
                catch (KeyStoreException keyStoreException) {
                    String string5 = ExceptionUtils.getMessage((Throwable)keyStoreException);
                    String string6 = ssgKeyFinder.getName();
                    String string7 = string;
                    stringArray = g;
                    a.log(Level.WARNING, stringArray[1] + string7 + stringArray[4] + string6 + stringArray[11] + string5, keyStoreException);
                }
            }
        }
        this.f = new SimpleSecurityTokenResolver(null, arrayList.toArray(new SignerInfo[arrayList.size()]));
        return this.f;
    }

    private static X509Certificate a(SignerInfo signerInfo) {
        X509Certificate x509Certificate;
        try {
            x509Certificate = signerInfo == null ? null : signerInfo.getCertificate();
        }
        catch (RuntimeException runtimeException) {
            throw TrustedCertificateResolver.a(runtimeException);
        }
        return x509Certificate;
    }

    public SignerInfo lookupPrivateKeyByCert(X509Certificate x509Certificate) {
        return this.a().lookupPrivateKeyByCert(x509Certificate);
    }

    public SignerInfo lookupPrivateKeyByX509Thumbprint(String string) {
        return this.a().lookupPrivateKeyByX509Thumbprint(string);
    }

    public SignerInfo lookupPrivateKeyBySki(String string) {
        return this.a().lookupPrivateKeyBySki(string);
    }

    public SignerInfo lookupPrivateKeyByKeyName(String string) {
        return this.a().lookupPrivateKeyByKeyName(string);
    }

    public SignerInfo lookupPrivateKeyByIssuerAndSerial(X500Principal x500Principal, BigInteger bigInteger) {
        return this.a().lookupPrivateKeyByIssuerAndSerial(x500Principal, bigInteger);
    }

    public byte[] getSecretKeyByEncryptedKeySha1(String string) {
        return this.getSecretKeyByTokenIdentifier(SoapConstants.VALUETYPE_ENCRYPTED_KEY_SHA1, string);
    }

    public void putSecretKeyByEncryptedKeySha1(String string, byte[] byArray) {
        this.putSecretKeyByTokenIdentifier(SoapConstants.VALUETYPE_ENCRYPTED_KEY_SHA1, string, byArray);
    }

    public byte[] getSecretKeyByTokenIdentifier(String string, String string2) {
        try {
            if (!this.d.get()) {
                return null;
            }
        }
        catch (RuntimeException runtimeException) {
            throw TrustedCertificateResolver.a(runtimeException);
        }
        return (byte[])this.c.retrieve((Object)new ba(string, string2));
    }

    public void putSecretKeyByTokenIdentifier(String string, String string2, byte[] byArray) {
        try {
            if (this.d.get()) {
                this.c.store((Object)new ba(string, string2), (Object)byArray);
            }
        }
        catch (RuntimeException runtimeException) {
            throw TrustedCertificateResolver.a(runtimeException);
        }
    }

    public KerberosSigningSecurityToken getKerberosTokenBySha1(String string) {
        return null;
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof EntityClassEvent) {
            EntityClassEvent entityClassEvent = (EntityClassEvent)applicationEvent;
            try {
                if (KeystoreFile.class.equals(entityClassEvent.getEntityClass())) {
                    this.f = null;
                }
            }
            catch (RuntimeException runtimeException) {
                throw TrustedCertificateResolver.a(runtimeException);
            }
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[12];
                var3_1 = 0;
                var2_2 = "3J+O}\\>\u0012KjLrZ{\u0015WjFt@>\u0015P%_t\u0003>#3J+O}\\>\u0012KjLrZ{\u0015Wj]cPh\u0007P/\rz\\gFE&DpJ>#3J+O}\\>\u0012KjLrZ{\u0015Wj]cPh\u0007P/\rz\\gFE&DpJ>\u000eFM$\rz\\gFW>Bc\\>\u000eFM$\rz\\gFW>Bc\\>\u001b3J+O}\\>\u0012KjLrZ{\u0015WjFt@>\u0015P%_t\u0019\u0013#T\"H|\\l\u0007HjFt@>\u0005E)Et+/J#YxXr\u000f^#Cv\u0019{\u0016L/@tK\u007f\n\u0004!Hh\u0019}\u0007G\"H1Nw\u0012Lj^xC{F\u0002\\\u0004\u001c3J+O}\\>\u0012KjLrZ{\u0015WjFt@>\u0015P%_t\u0003>";
                var4_3 = "3J+O}\\>\u0012KjLrZ{\u0015WjFt@>\u0015P%_t\u0003>#3J+O}\\>\u0012KjLrZ{\u0015Wj]cPh\u0007P/\rz\\gFE&DpJ>#3J+O}\\>\u0012KjLrZ{\u0015Wj]cPh\u0007P/\rz\\gFE&DpJ>\u000eFM$\rz\\gFW>Bc\\>\u000eFM$\rz\\gFW>Bc\\>\u001b3J+O}\\>\u0012KjLrZ{\u0015WjFt@>\u0015P%_t\u0019\u0013#T\"H|\\l\u0007HjFt@>\u0005E)Et+/J#YxXr\u000f^#Cv\u0019{\u0016L/@tK\u007f\n\u0004!Hh\u0019}\u0007G\"H1Nw\u0012Lj^xC{F\u0002\\\u0004\u001c3J+O}\\>\u0012KjLrZ{\u0015WjFt@>\u0015P%_t\u0003>".length();
                var1_4 = 28;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = TrustedCertificateResolver.a(v0, TrustedCertificateResolver.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0014C5_kK{\u0010_\u0016_\u007fch\tp<YnKL\u001fG/Sc]\u0002K\u0013";
                    var4_3 = "\u0014C5_kK{\u0010_\u0016_\u007fch\tp<YnKL\u001fG/Sc]\u0002K\u0013".length();
                    var1_4 = 27;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 19;
                        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++] = TrustedCertificateResolver.a(v0, TrustedCertificateResolver.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;
            }
        }
        TrustedCertificateResolver.g = var5;
        TrustedCertificateResolver.a = Logger.getLogger(TrustedCertificateResolver.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] ^ 0x1A);
        }
        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 -> 98;
                    case 1 -> 32;
                    case 2 -> 78;
                    case 3 -> 41;
                    case 4 -> 21;
                    case 5 -> 61;
                    default -> 26;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

