package okhttp3.internal.tls;

import S4.p;
import S4.w;
import com.google.android.recaptcha.internal.a;
import java.security.cert.Certificate;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import kotlin.jvm.internal.o;
import okhttp3.internal.HostnamesKt;
import okhttp3.internal.Util;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class OkHostnameVerifier implements HostnameVerifier {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final OkHostnameVerifier f22608a = new OkHostnameVerifier();

    private OkHostnameVerifier() {
    }

    public static List a(X509Certificate x509Certificate, int i6) {
        Object obj;
        y yVar = y.f26065a;
        try {
            Collection<List<?>> subjectAlternativeNames = x509Certificate.getSubjectAlternativeNames();
            if (subjectAlternativeNames != null) {
                ArrayList arrayList = new ArrayList();
                for (List<?> list : subjectAlternativeNames) {
                    if (list != null && list.size() >= 2 && o.c(list.get(0), Integer.valueOf(i6)) && (obj = list.get(1)) != null) {
                        arrayList.add((String) obj);
                    }
                }
                return arrayList;
            }
        } catch (CertificateParsingException unused) {
        }
        return yVar;
    }

    public static boolean b(String str) {
        int i6;
        int length = str.length();
        int length2 = str.length();
        if (length2 < 0) {
            throw new IllegalArgumentException(Z0.o.j(length2, "endIndex < beginIndex: ", " < 0").toString());
        }
        if (length2 > str.length()) {
            StringBuilder sbR = a.r(length2, "endIndex > string.length: ", " > ");
            sbR.append(str.length());
            throw new IllegalArgumentException(sbR.toString().toString());
        }
        long j4 = 0;
        int i7 = 0;
        while (i7 < length2) {
            char cCharAt = str.charAt(i7);
            if (cCharAt < 128) {
                j4++;
            } else {
                if (cCharAt < 2048) {
                    i6 = 2;
                } else if (cCharAt < 55296 || cCharAt > 57343) {
                    i6 = 3;
                } else {
                    int i8 = i7 + 1;
                    char cCharAt2 = i8 < length2 ? str.charAt(i8) : (char) 0;
                    if (cCharAt > 56319 || cCharAt2 < 56320 || cCharAt2 > 57343) {
                        j4++;
                        i7 = i8;
                    } else {
                        j4 += (long) 4;
                        i7 += 2;
                    }
                }
                j4 += (long) i6;
            }
            i7++;
        }
        return length == ((int) j4);
    }

    /* JADX WARN: Removed duplicated region for block: B:59:0x00eb  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static boolean c(String host, X509Certificate x509Certificate) {
        boolean zC;
        int length;
        o.h(host, "host");
        byte[] bArr = Util.f22179a;
        if (Util.f22184f.d(host)) {
            String strB = HostnamesKt.b(host);
            List listA = a(x509Certificate, 7);
            if (!listA.isEmpty()) {
                Iterator it = listA.iterator();
                while (it.hasNext()) {
                    if (o.c(strB, HostnamesKt.b((String) it.next()))) {
                        return true;
                    }
                }
            }
            return false;
        }
        if (b(host)) {
            Locale locale = Locale.US;
            host = a.o(locale, "US", host, locale, "this as java.lang.String).toLowerCase(locale)");
        }
        List<String> listA2 = a(x509Certificate, 2);
        if (!listA2.isEmpty()) {
            for (String strO : listA2) {
                f22608a.getClass();
                if (host.length() != 0 && !w.A0(host, ".", false) && !w.s0(host, "..", false) && strO != null && strO.length() != 0 && !w.A0(strO, ".", false) && !w.s0(strO, "..", false)) {
                    String strConcat = !w.s0(host, ".", false) ? host.concat(".") : host;
                    if (!w.s0(strO, ".", false)) {
                        strO = strO.concat(".");
                    }
                    if (b(strO)) {
                        Locale locale2 = Locale.US;
                        strO = a.o(locale2, "US", strO, locale2, "this as java.lang.String).toLowerCase(locale)");
                    }
                    if (!p.D0(strO, "*", false)) {
                        zC = o.c(strConcat, strO);
                    } else if (!w.A0(strO, "*.", false) || p.N0(strO, '*', 1, 4) != -1 || strConcat.length() < strO.length() || "*.".equals(strO)) {
                        zC = false;
                    } else {
                        String strSubstring = strO.substring(1);
                        o.g(strSubstring, "this as java.lang.String).substring(startIndex)");
                        if (w.s0(strConcat, strSubstring, false) && ((length = strConcat.length() - strSubstring.length()) <= 0 || p.T0(strConcat, '.', length - 1, 4) == -1)) {
                            zC = true;
                        }
                    }
                    if (zC) {
                    }
                }
                return true;
            }
        }
        return false;
    }

    @Override // javax.net.ssl.HostnameVerifier
    public final boolean verify(String host, SSLSession session) {
        o.h(host, "host");
        o.h(session, "session");
        if (b(host)) {
            try {
                Certificate certificate = session.getPeerCertificates()[0];
                o.f(certificate, "null cannot be cast to non-null type java.security.cert.X509Certificate");
                return c(host, (X509Certificate) certificate);
            } catch (SSLException unused) {
            }
        }
        return false;
    }
}
