/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.CertUtils
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.security.cert.CertVerifier
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.cert.TrustedCert$TrustedFor
 *  com.l7tech.util.Config
 *  com.l7tech.util.InetAddressUtil
 */
package com.l7tech.server.transport.http;

import com.l7tech.common.io.CertUtils;
import com.l7tech.objectmodel.FindException;
import com.l7tech.security.cert.CertVerifier;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.server.identity.cert.TrustedCertServices;
import com.l7tech.util.Config;
import com.l7tech.util.InetAddressUtil;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;

public class SslClientHostnameVerifier
implements HostnameVerifier {
    private static final Logger a;
    private final Config b;
    private final TrustedCertServices c;
    private final boolean d;
    private static final String[] e;

    public SslClientHostnameVerifier(Config config, TrustedCertServices trustedCertServices) {
        this(config, trustedCertServices, false);
    }

    public SslClientHostnameVerifier(Config config, TrustedCertServices trustedCertServices, boolean bl2) {
        this.b = config;
        this.c = trustedCertServices;
        this.d = bl2;
    }

    /*
     * Loose catch block
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    public boolean verify(String string, SSLSession sSLSession) {
        block6: {
            if (string == null) return this.a();
            try {
                if (sSLSession != null) break block6;
                return this.a();
                catch (Exception exception) {
                    throw SslClientHostnameVerifier.a(exception);
                }
            }
            catch (Exception exception) {
                throw SslClientHostnameVerifier.a(exception);
            }
        }
        try {
            return this.a(string, sSLSession);
        }
        catch (Exception exception) {
            String[] stringArray = e;
            a.log(Level.WARNING, stringArray[1] + string + stringArray[2], exception);
            return this.a();
        }
    }

    private boolean a(String string, SSLSession sSLSession) throws SSLPeerUnverifiedException, FindException, CertificateException {
        boolean bl2;
        block10: {
            block9: {
                Certificate[] certificateArray;
                block8: {
                    certificateArray = sSLSession.getPeerCertificates();
                    try {
                        try {
                            if (certificateArray.length >= 1 && certificateArray[0] instanceof X509Certificate) break block8;
                        }
                        catch (SSLPeerUnverifiedException sSLPeerUnverifiedException) {
                            throw SslClientHostnameVerifier.a(sSLPeerUnverifiedException);
                        }
                        return this.a();
                    }
                    catch (SSLPeerUnverifiedException sSLPeerUnverifiedException) {
                        throw SslClientHostnameVerifier.a(sSLPeerUnverifiedException);
                    }
                }
                X509Certificate x509Certificate = (X509Certificate)certificateArray[0];
                try {
                    try {
                        if (!this.a(string, x509Certificate) && !this.a(x509Certificate)) break block9;
                    }
                    catch (SSLPeerUnverifiedException sSLPeerUnverifiedException) {
                        throw SslClientHostnameVerifier.a(sSLPeerUnverifiedException);
                    }
                    bl2 = true;
                    break block10;
                }
                catch (SSLPeerUnverifiedException sSLPeerUnverifiedException) {
                    throw SslClientHostnameVerifier.a(sSLPeerUnverifiedException);
                }
            }
            bl2 = false;
        }
        return bl2;
    }

    private boolean a(X509Certificate x509Certificate) throws FindException {
        boolean bl2;
        block7: {
            block5: {
                block6: {
                    TrustedCert trustedCert = this.c(x509Certificate);
                    if (null == trustedCert) {
                        trustedCert = this.b(x509Certificate);
                    }
                    try {
                        try {
                            if (null == trustedCert) break block5;
                            if (trustedCert.isVerifyHostname()) break block6;
                        }
                        catch (FindException findException) {
                            throw SslClientHostnameVerifier.a((Exception)((Object)findException));
                        }
                        bl2 = true;
                        break block7;
                    }
                    catch (FindException findException) {
                        throw SslClientHostnameVerifier.a((Exception)((Object)findException));
                    }
                }
                bl2 = false;
                break block7;
            }
            bl2 = this.a();
        }
        return bl2;
    }

    private boolean a() {
        boolean bl2 = true;
        String string = this.b.getProperty(e[3]);
        if (string != null) {
            bl2 = Boolean.valueOf(string.trim());
        }
        return !bl2;
    }

    private boolean b() {
        boolean bl2 = false;
        String string = this.b.getProperty(e[0]);
        if (string != null) {
            bl2 = Boolean.valueOf(string.trim());
        }
        return bl2;
    }

    private boolean a(String string, X509Certificate x509Certificate) throws CertificateParsingException {
        String string2;
        int n2;
        X509Certificate x509Certificate2;
        try {
            if (string == null) {
                return false;
            }
        }
        catch (CertificateParsingException certificateParsingException) {
            throw SslClientHostnameVerifier.a(certificateParsingException);
        }
        boolean bl2 = InetAddressUtil.looksLikeIpAddressV4OrV6((String)string);
        try {
            x509Certificate2 = x509Certificate;
            n2 = bl2 ? 7 : 2;
        }
        catch (CertificateParsingException certificateParsingException) {
            throw SslClientHostnameVerifier.a(certificateParsingException);
        }
        List<String> list = CertUtils.getSubjectAlternativeNames((X509Certificate)x509Certificate2, (Integer)n2);
        if (list.isEmpty() && (string2 = CertUtils.extractFirstCommonNameFromCertificate((X509Certificate)x509Certificate)) != null) {
            list = Collections.singletonList(string2);
        }
        boolean bl3 = false;
        for (String string3 : list) {
            block12: {
                block11: {
                    try {
                        if (bl2 || !this.b()) break block11;
                    }
                    catch (CertificateParsingException certificateParsingException) {
                        throw SslClientHostnameVerifier.a(certificateParsingException);
                    }
                    bl3 = CertUtils.domainNameMatchesPattern((String)string, (String)string3, (boolean)this.d);
                    break block12;
                }
                bl3 = string.equalsIgnoreCase(string3);
            }
            try {
                if (!bl3) continue;
                break;
            }
            catch (CertificateParsingException certificateParsingException) {
                throw SslClientHostnameVerifier.a(certificateParsingException);
            }
        }
        return bl3;
    }

    private TrustedCert b(X509Certificate x509Certificate) throws FindException {
        String string = CertUtils.getIssuerDN((X509Certificate)x509Certificate);
        Collection<TrustedCert> collection = this.c.getCertsBySubjectDnFiltered(string, false, null, null);
        for (TrustedCert trustedCert : collection) {
            try {
                try {
                    if (!trustedCert.isTrustedFor(TrustedCert.TrustedFor.SIGNING_SERVER_CERTS) || !this.a(x509Certificate, trustedCert)) continue;
                }
                catch (FindException findException) {
                    throw SslClientHostnameVerifier.a((Exception)((Object)findException));
                }
                return trustedCert;
            }
            catch (FindException findException) {
                throw SslClientHostnameVerifier.a((Exception)((Object)findException));
            }
        }
        return null;
    }

    boolean a(X509Certificate x509Certificate, TrustedCert trustedCert) {
        try {
            CertVerifier.cachedVerify((X509Certificate)x509Certificate, (X509Certificate)trustedCert.getCertificate());
            return true;
        }
        catch (Exception exception) {
            return false;
        }
    }

    private TrustedCert c(X509Certificate x509Certificate) throws FindException {
        String string = CertUtils.getSubjectDN((X509Certificate)x509Certificate);
        Collection<TrustedCert> collection = this.c.getCertsBySubjectDnFiltered(string, false, null, null);
        for (TrustedCert trustedCert : collection) {
            try {
                if (!CertUtils.certsAreEqual((X509Certificate)trustedCert.getCertificate(), (X509Certificate)x509Certificate)) continue;
                return trustedCert;
            }
            catch (FindException findException) {
                throw SslClientHostnameVerifier.a((Exception)((Object)findException));
            }
        }
        return null;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[4];
                var3_1 = 0;
                var2_2 = "R&8)4BQs&\u0003)\u0001^NT>'4,VAZ;\u0014\u001bx&\u00051$\u0012LT=P+%@K]0P5/AVU(\u001d8`\u0015";
                var4_3 = "R&8)4BQs&\u0003)\u0001^NT>'4,VAZ;\u0014\u001bx&\u00051$\u0012LT=P+%@K]0P5/AVU(\u001d8`\u0015".length();
                var1_4 = 24;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = SslClientHostnameVerifier.a(v0, SslClientHostnameVerifier.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "5N\u0011{\u000f\u0011\u0000\u001dkxZ\u000f*\u0000?~y{\u0006 ";
                    var4_3 = "5N\u0011{\u000f\u0011\u0000\u001dkxZ\u000f*\u0000?~y{\u0006 ".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 26;
                        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++] = SslClientHostnameVerifier.a(v0, SslClientHostnameVerifier.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;
            }
        }
        SslClientHostnameVerifier.e = var5;
        SslClientHostnameVerifier.a = Logger.getLogger(SslClientHostnameVerifier.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] ^ 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 -> 8;
                    case 1 -> 122;
                    case 2 -> 67;
                    case 3 -> 110;
                    case 4 -> 115;
                    case 5 -> 1;
                    default -> 17;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

