/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.CertUtils
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.security.cert.CertVerifier
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.cert.TrustedCert$TrustedFor
 *  com.l7tech.security.cert.TrustedCertManager$UnknownCertificateException
 */
package com.l7tech.server.identity.cert;

import com.l7tech.common.io.CertUtils;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.security.cert.CertVerifier;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.security.cert.TrustedCertManager;
import com.l7tech.server.identity.cert.TrustedCertCache;
import com.l7tech.server.identity.cert.TrustedCertServices;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TrustedCertServicesImpl
implements TrustedCertServices {
    private static final Logger a;
    private final TrustedCertCache b;
    private static final String[] c;

    public TrustedCertServicesImpl(TrustedCertCache trustedCertCache) {
        this.b = trustedCertCache;
    }

    @Override
    public void checkSslTrust(X509Certificate[] x509CertificateArray, Set<Goid> set) throws CertificateException {
        String string = CertUtils.getIssuerDN((X509Certificate)x509CertificateArray[0]);
        try {
            if (this.a(x509CertificateArray, set)) {
                return;
            }
            this.a(x509CertificateArray, string, set);
        }
        catch (Exception exception) {
            try {
                if (exception instanceof CertificateException) {
                    throw (CertificateException)exception;
                }
            }
            catch (Exception exception2) {
                throw TrustedCertServicesImpl.a(exception2);
            }
            a.log(Level.WARNING, exception.getMessage(), exception);
            throw new CertificateException(exception.getMessage(), exception);
        }
    }

    @Override
    public Collection<TrustedCert> getCertsBySubjectDnFiltered(String string, boolean bl2, Set<TrustedCert.TrustedFor> set, Set<Goid> set2) throws FindException {
        Collection<TrustedCert> collection = this.b.findBySubjectDn(string);
        ArrayList<TrustedCert> arrayList = new ArrayList<TrustedCert>();
        for (TrustedCert trustedCert : collection) {
            try {
                try {
                    if (bl2 && !CertUtils.isValid((X509Certificate)trustedCert.getCertificate())) {
                        continue;
                    }
                }
                catch (FindException findException) {
                    throw TrustedCertServicesImpl.a((Exception)((Object)findException));
                }
            }
            catch (FindException findException) {
                throw TrustedCertServicesImpl.a((Exception)((Object)findException));
            }
            try {
                try {
                    if (set != null && !trustedCert.isTrustedForAll(set)) {
                        continue;
                    }
                }
                catch (FindException findException) {
                    throw TrustedCertServicesImpl.a((Exception)((Object)findException));
                }
            }
            catch (FindException findException) {
                throw TrustedCertServicesImpl.a((Exception)((Object)findException));
            }
            try {
                try {
                    if (set2 != null && !set2.contains(trustedCert.getGoid())) {
                        continue;
                    }
                }
                catch (FindException findException) {
                    throw TrustedCertServicesImpl.a((Exception)((Object)findException));
                }
            }
            catch (FindException findException) {
                throw TrustedCertServicesImpl.a((Exception)((Object)findException));
            }
            trustedCert.setReadOnly();
            arrayList.add(trustedCert);
        }
        return arrayList;
    }

    @Override
    public Collection<TrustedCert> getAllCertsByTrustFlags(Set<TrustedCert.TrustedFor> set) throws FindException {
        HashSet<TrustedCert> hashSet = new HashSet<TrustedCert>();
        for (TrustedCert.TrustedFor trustedFor : set) {
            hashSet.addAll(this.b.findByTrustFlag(trustedFor));
        }
        return Collections.unmodifiableCollection(hashSet);
    }

    private void a(X509Certificate[] x509CertificateArray, String string, Set<Goid> set) throws FindException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        Collection<TrustedCert> collection = this.getCertsBySubjectDnFiltered(string, true, EnumSet.of(TrustedCert.TrustedFor.SIGNING_SERVER_CERTS), set);
        if (collection.isEmpty()) {
            String string2 = CertUtils.getSubjectDN((X509Certificate)x509CertificateArray[0]);
            try {
                if (this.b.findBySubjectDn(string2).isEmpty()) {
                    throw new TrustedCertManager.UnknownCertificateException(c[4] + string + "'");
                }
            }
            catch (FindException findException) {
                throw TrustedCertServicesImpl.a((Exception)((Object)findException));
            }
            String[] stringArray = c;
            throw new CertificateException(stringArray[5] + string2 + stringArray[2]);
        }
        for (TrustedCert trustedCert : collection) {
            X509Certificate x509Certificate = trustedCert.getCertificate();
            try {
                if (!CertVerifier.isVerified((X509Certificate)x509CertificateArray[0], (X509Certificate)x509Certificate)) continue;
                return;
            }
            catch (FindException findException) {
                throw TrustedCertServicesImpl.a((Exception)((Object)findException));
            }
        }
        String[] stringArray = c;
        throw new CertificateException(stringArray[1] + string + stringArray[6]);
    }

    private boolean a(X509Certificate[] x509CertificateArray, Set<Goid> set) throws CertificateException {
        try {
            String string = CertUtils.getSubjectDN((X509Certificate)x509CertificateArray[0]);
            Collection<TrustedCert> collection = this.getCertsBySubjectDnFiltered(string, true, EnumSet.of(TrustedCert.TrustedFor.SSL), set);
            for (TrustedCert trustedCert : collection) {
                X509Certificate x509Certificate = trustedCert.getCertificate();
                try {
                    if (!CertUtils.certsAreEqual((X509Certificate)x509Certificate, (X509Certificate)x509CertificateArray[0]) || !CertUtils.isValid((X509Certificate)x509Certificate)) continue;
                }
                catch (FindException findException) {
                    throw TrustedCertServicesImpl.a((Exception)((Object)findException));
                }
                return true;
            }
            String[] stringArray = c;
            a.fine(stringArray[0] + string + stringArray[3]);
            return false;
        }
        catch (FindException findException) {
            a.log(Level.WARNING, findException.getMessage(), findException);
            throw new CertificateException(findException.getMessage());
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[7];
                var3_1 = 0;
                var2_2 = "Rhi\u001e\u001a\u000e\u0006bhi\u001c_[\u0014BL;+\u001a\u000eR)~2H\b\u0015Ri-_&_[ &-}\u0007\n\u0012B!on\u001c_\u0012Iu-o\u001a\n\u000fRdi;\u000e\u0010\u000e\u0006R^WF?&-}\u0007\n\u0012B!on\u001c_\u0012Iu-o\u001a\n\u000fRdi;\u000e\u0010\u000e\u0006R^WF_+Oma;\u000b\u0017\u0019Ej-r\u001b\f\tCs-x\r\r\b\n!d}H\u001e\u0012_\u001fBbn\u0004\u001b\u0012\u0001u-}\u0001\u0011\u0018\u0006BL;\u000b\u001a\u000eR!zr\u001c\u0017\\bO-<";
                var4_3 = "Rhi\u001e\u001a\u000e\u0006bhi\u001c_[\u0014BL;+\u001a\u000eR)~2H\b\u0015Ri-_&_[ &-}\u0007\n\u0012B!on\u001c_\u0012Iu-o\u001a\n\u000fRdi;\u000e\u0010\u000e\u0006R^WF?&-}\u0007\n\u0012B!on\u001c_\u0012Iu-o\u001a\n\u000fRdi;\u000e\u0010\u000e\u0006R^WF_+Oma;\u000b\u0017\u0019Ej-r\u001b\f\tCs-x\r\r\b\n!d}H\u001e\u0012_\u001fBbn\u0004\u001b\u0012\u0001u-}\u0001\u0011\u0018\u0006BL;\u000b\u001a\u000eR!zr\u001c\u0017\\bO-<".length();
                var1_4 = 13;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = TrustedCertServicesImpl.a(v0, TrustedCertServicesImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "'\u001d\u001cko{s\u0017\u001d\u001ci*.4SX\br\u007fg7T\u001a\u001bi*g<\u0000X\u001ao\u007fz'\u0011\u001cN{e{s\u0007\u0011\tscg4T+=Q*Z6\u0006\u000e\u000bo*J6\u0006\f\u001d";
                    var4_3 = "'\u001d\u001cko{s\u0017\u001d\u001ci*.4SX\br\u007fg7T\u001a\u001bi*g<\u0000X\u001ao\u007fz'\u0011\u001cN{e{s\u0007\u0011\tscg4T+=Q*Z6\u0006\u000e\u000bo*J6\u0006\f\u001d".length();
                    var1_4 = 13;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 7;
                        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++] = TrustedCertServicesImpl.a(v0, TrustedCertServicesImpl.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;
            }
        }
        TrustedCertServicesImpl.c = var5;
        TrustedCertServicesImpl.a = Logger.getLogger(TrustedCertServicesImpl.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] ^ 0x54);
        }
        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 -> 115;
                    case 1 -> 127;
                    case 2 -> 105;
                    case 3 -> 26;
                    case 4 -> 13;
                    case 5 -> 14;
                    default -> 84;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

