/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.WhirlycacheFactory
 *  com.l7tech.gateway.common.security.RevocationCheckPolicy
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.util.Functions$Unary
 *  com.whirlycott.cache.Cache
 */
package com.l7tech.server.security.cert;

import com.l7tech.common.io.WhirlycacheFactory;
import com.l7tech.gateway.common.security.RevocationCheckPolicy;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.server.security.cert.CertValidationProcessor;
import com.l7tech.server.security.cert.CrlCache;
import com.l7tech.server.security.cert.OCSPCache;
import com.l7tech.server.security.cert.RevocationChecker;
import com.l7tech.server.security.cert.o;
import com.l7tech.server.security.cert.t;
import com.l7tech.server.security.cert.u;
import com.l7tech.server.security.cert.v;
import com.l7tech.util.Functions;
import com.whirlycott.cache.Cache;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RevocationCheckerFactory {
    private static final Logger a;
    private final CertValidationProcessor b;
    private final CrlCache c;
    private final OCSPCache d;
    private final ReadWriteLock e;
    private final Cache f;
    private final Map<Goid, Set<t>> g;
    private final Map<Goid, Set<t>> h;
    private static final String[] i;

    public RevocationCheckerFactory(CertValidationProcessor certValidationProcessor, CrlCache crlCache, OCSPCache oCSPCache) {
        this.b = certValidationProcessor;
        this.c = crlCache;
        this.d = oCSPCache;
        this.e = new ReentrantReadWriteLock();
        this.g = new HashMap<Goid, Set<t>>();
        this.h = new HashMap<Goid, Set<t>>();
        this.f = WhirlycacheFactory.createCache((String)i[0], (int)1000, (int)66, (String)WhirlycacheFactory.POLICY_LRU);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     */
    public RevocationChecker getRevocationChecker(X509Certificate x509Certificate) {
        u u2;
        block22: {
            u2 = null;
            try {
                RevocationCheckPolicy revocationCheckPolicy;
                t t2 = new t(x509Certificate);
                this.e.readLock().lock();
                try {
                    u2 = (u)this.f.retrieve((Object)t2);
                }
                finally {
                    this.e.readLock().unlock();
                }
                if (u2 != null) break block22;
                TrustedCert trustedCert = this.b.getTrustedCertEntry(x509Certificate);
                try {
                    revocationCheckPolicy = trustedCert == null ? this.b.getDefaultPolicy() : this.b.getRevocationCheckPolicy(trustedCert);
                }
                catch (CertificateException certificateException) {
                    throw RevocationCheckerFactory.a(certificateException);
                }
                RevocationCheckPolicy revocationCheckPolicy2 = revocationCheckPolicy;
                u2 = this.a(t2, revocationCheckPolicy2);
                this.e.writeLock().lock();
                try {
                    this.f.store((Object)t2, (Object)u2);
                    if (trustedCert != null) {
                        this.a(this.g, trustedCert.getGoid(), u2);
                    }
                    if (revocationCheckPolicy2 == null) break block22;
                    try {
                        block23: {
                            if (RevocationCheckPolicy.DEFAULT_GOID.equals((Object)revocationCheckPolicy2.getGoid())) break block22;
                            break block23;
                            catch (CertificateException certificateException) {
                                throw RevocationCheckerFactory.a(certificateException);
                            }
                        }
                        this.a(this.h, revocationCheckPolicy2.getGoid(), u2);
                    }
                    catch (CertificateException certificateException) {
                        throw RevocationCheckerFactory.a(certificateException);
                    }
                }
                finally {
                    this.e.writeLock().unlock();
                }
            }
            catch (CertificateException certificateException) {
                Object object;
                try {
                    object = x509Certificate == null ? i[3] : x509Certificate.getSubjectDN();
                }
                catch (CertificateException certificateException2) {
                    throw RevocationCheckerFactory.a(certificateException2);
                }
                Object object2 = object;
                String[] stringArray = i;
                a.log(Level.WARNING, stringArray[7] + String.valueOf(object2) + stringArray[11], certificateException);
            }
            catch (FindException findException) {
                Object object;
                try {
                    object = x509Certificate == null ? i[6] : x509Certificate.getSubjectDN();
                }
                catch (CertificateException certificateException) {
                    throw RevocationCheckerFactory.a(certificateException);
                }
                Object object3 = object;
                String[] stringArray = i;
                a.log(Level.WARNING, stringArray[9] + String.valueOf(object3) + stringArray[10], findException);
            }
        }
        if (u2 == null) {
            u2 = new v();
        }
        return u2;
    }

    public void invalidateRevocationCheckPolicy(Goid goid) {
        this.a(goid, null, this.h);
    }

    public void invalidateTrustedCert(Goid goid) {
        this.a(goid, new o(this), this.g);
    }

    private void a(Map<Goid, Set<t>> map, Goid goid, u u2) {
        Set<t> set = map.get(goid);
        if (set == null) {
            set = new HashSet<t>();
            map.put(goid, set);
        }
        set.add(u2.a);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void a(Goid goid, Functions.Unary<X509Certificate, Goid> unary, Map<Goid, Set<t>> map) {
        HashSet<t> hashSet;
        HashSet<Goid> hashSet2;
        block20: {
            block19: {
                hashSet2 = new HashSet<Goid>();
                hashSet = new HashSet<t>();
                Set<t> set = null;
                this.e.readLock().lock();
                try {
                    set = map.get(goid);
                }
                finally {
                    this.e.readLock().unlock();
                }
                try {
                    if (set == null) break block19;
                    hashSet2.add(goid);
                    hashSet.addAll(set);
                    break block20;
                }
                catch (Exception exception) {
                    throw RevocationCheckerFactory.a(exception);
                }
            }
            if (unary != null) {
                try {
                    X509Certificate x509Certificate = (X509Certificate)unary.call((Object)goid);
                    try {
                        if (x509Certificate != null) {
                            hashSet.add(new t(x509Certificate));
                        }
                    }
                    catch (Exception exception) {
                        throw RevocationCheckerFactory.a(exception);
                    }
                }
                catch (Exception exception) {
                    String[] stringArray = i;
                    a.log(Level.WARNING, stringArray[2] + String.valueOf(goid) + stringArray[8], exception);
                }
            }
        }
        try {
            if (a.isLoggable(Level.FINE)) {
                a.log(Level.FINE, i[4], hashSet);
            }
        }
        catch (Exception exception) {
            throw RevocationCheckerFactory.a(exception);
        }
        this.e.writeLock().lock();
        try {
            map.keySet().removeAll(hashSet2);
            for (t t2 : hashSet) {
                this.f.remove((Object)t2);
            }
        }
        finally {
            this.e.writeLock().unlock();
        }
    }

    private X509Certificate[] a(List<Goid> list) throws FindException, CertificateException {
        ArrayList<X509Certificate> arrayList = new ArrayList<X509Certificate>();
        for (Goid goid : list) {
            TrustedCert trustedCert = this.b.getTrustedCertByOid(goid);
            try {
                if (trustedCert == null) continue;
                arrayList.add(trustedCert.getCertificate());
            }
            catch (FindException findException) {
                throw RevocationCheckerFactory.a((Exception)((Object)findException));
            }
        }
        return arrayList.toArray(new X509Certificate[arrayList.size()]);
    }

    /*
     * Exception decompiling
     */
    private u a(t var1_1, RevocationCheckPolicy var2_2) throws FindException, CertificateException {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [2[TRYBLOCK]], but top level block is 3[SWITCH]
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:435)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:484)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1055)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[12];
                var3_1 = 0;
                var2_2 = "7y]\u00071^z\fsE8=Sg\u0006eh\t1Wk=,{E\u0007 V`\u0002<Y\r$Pm\u0004hB\u0007<\u001fm\ryH\u00037M.\u0012u_\u0000rV`\u0013}G\u00016\u001f[7P\u000b\u00183Kz\u0000nEHu\u0018uUa\fO|# nY\u0007 \u001fh\frO\u0001<X.\u0011n^\u001b&ZjE\u007fN\u001a&Vh\f\u007fJ\u001c7\u001f)\u0004\u000bsE\r=&pN\t V`\u0002<H\t1Wk\u0001<Y\r$Pm\u0004hB\u0007<\u001fm\ryH\u00037M}EzD\u001ar\\k\u0017hB\u000e;\\o\u0011y\u000b\u00037F}Eg\u001b\u0015|2,{E\u0007 V`\u0002<^\u00069Qa\u0012r\u000b\u001a7Ia\u0006}_\u0001=Q.\u0006tN\u000b9V`\u0002<_\u0011\"Z.B;PX/\u0018)K\u0004\u000bsE\r50rJ\n>Z.\u0011s\u000b\u000b Zo\u0011y\u000b\u001a7Ia\u0006}_\u0001=Q.\u0006tN\u000b9Z|EzD\u001ar\\k\u0017hB\u000e;\\o\u0011y\u000bO\u0013B<M\u0007 \u001fg\u000bjJ\u0004;[o\u0011uD\u0006|50rJ\n>Z.\u0011s\u000b\u000b Zo\u0011y\u000b\u001a7Ia\u0006}_\u0001=Q.\u0006tN\u000b9Z|EzD\u001ar\\k\u0017hB\u000e;\\o\u0011y\u000bO";
                var4_3 = "7y]\u00071^z\fsE8=Sg\u0006eh\t1Wk=,{E\u0007 V`\u0002<Y\r$Pm\u0004hB\u0007<\u001fm\ryH\u00037M.\u0012u_\u0000rV`\u0013}G\u00016\u001f[7P\u000b\u00183Kz\u0000nEHu\u0018uUa\fO|# nY\u0007 \u001fh\frO\u0001<X.\u0011n^\u001b&ZjE\u007fN\u001a&Vh\f\u007fJ\u001c7\u001f)\u0004\u000bsE\r=&pN\t V`\u0002<H\t1Wk\u0001<Y\r$Pm\u0004hB\u0007<\u001fm\ryH\u00037M}EzD\u001ar\\k\u0017hB\u000e;\\o\u0011y\u000b\u00037F}Eg\u001b\u0015|2,{E\u0007 V`\u0002<^\u00069Qa\u0012r\u000b\u001a7Ia\u0006}_\u0001=Q.\u0006tN\u000b9V`\u0002<_\u0011\"Z.B;PX/\u0018)K\u0004\u000bsE\r50rJ\n>Z.\u0011s\u000b\u000b Zo\u0011y\u000b\u001a7Ia\u0006}_\u0001=Q.\u0006tN\u000b9Z|EzD\u001ar\\k\u0017hB\u000e;\\o\u0011y\u000bO\u0013B<M\u0007 \u001fg\u000bjJ\u0004;[o\u0011uD\u0006|50rJ\n>Z.\u0011s\u000b\u000b Zo\u0011y\u000b\u001a7Ia\u0006}_\u0001=Q.\u0006tN\u000b9Z|EzD\u001ar\\k\u0017hB\u000e;\\o\u0011y\u000bO".length();
                var1_4 = 21;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = RevocationCheckerFactory.a(v0, RevocationCheckerFactory.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = ".^\u0002.^";
                    var4_3 = ".^\u0002.^".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 109;
                        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++] = RevocationCheckerFactory.a(v0, RevocationCheckerFactory.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;
            }
        }
        RevocationCheckerFactory.i = var5;
        RevocationCheckerFactory.a = Logger.getLogger(RevocationCheckerFactory.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] ^ 0xF);
        }
        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 -> 100;
                    case 1 -> 29;
                    case 2 -> 42;
                    case 3 -> 105;
                    case 4 -> 83;
                    case 5 -> 62;
                    default -> 15;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

