package hr;

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.t;
import kotlin.text.y;
import kotlin.text.z;
import kr.c0;
import sm.u;
import vq.f;
import vq.g;

/* loaded from: classes3.dex */
public final class d implements HostnameVerifier {

    /* renamed from: a, reason: collision with root package name */
    public static final d f22078a = new d();

    private d() {
    }

    private final String b(String str) {
        if (!d(str)) {
            return str;
        }
        Locale US = Locale.US;
        t.f(US, "US");
        String lowerCase = str.toLowerCase(US);
        t.f(lowerCase, "this as java.lang.String).toLowerCase(locale)");
        return lowerCase;
    }

    private final List c(X509Certificate x509Certificate, int i10) {
        List k10;
        Object obj;
        List k11;
        try {
            Collection<List<?>> subjectAlternativeNames = x509Certificate.getSubjectAlternativeNames();
            if (subjectAlternativeNames == null) {
                k11 = u.k();
                return k11;
            }
            ArrayList arrayList = new ArrayList();
            for (List<?> list : subjectAlternativeNames) {
                if (list != null && list.size() >= 2 && t.b(list.get(0), Integer.valueOf(i10)) && (obj = list.get(1)) != null) {
                    arrayList.add((String) obj);
                }
            }
            return arrayList;
        } catch (CertificateParsingException unused) {
            k10 = u.k();
            return k10;
        }
    }

    private final boolean d(String str) {
        return str.length() == ((int) c0.b(str, 0, 0, 3, null));
    }

    private final boolean f(String str, String str2) {
        boolean P;
        boolean y10;
        boolean P2;
        boolean y11;
        boolean y12;
        boolean y13;
        String str3;
        boolean U;
        boolean P3;
        int g02;
        boolean y14;
        int l02;
        String str4 = str;
        if (!(str4 == null || str.length() == 0)) {
            P = y.P(str4, ".", false, 2, null);
            if (!P) {
                y10 = y.y(str4, "..", false, 2, null);
                if (!y10) {
                    if (!(str2 == null || str2.length() == 0)) {
                        P2 = y.P(str2, ".", false, 2, null);
                        if (!P2) {
                            y11 = y.y(str2, "..", false, 2, null);
                            if (!y11) {
                                y12 = y.y(str4, ".", false, 2, null);
                                if (!y12) {
                                    str4 = str4 + '.';
                                }
                                String str5 = str4;
                                y13 = y.y(str2, ".", false, 2, null);
                                if (y13) {
                                    str3 = str2;
                                } else {
                                    str3 = str2 + '.';
                                }
                                String b10 = b(str3);
                                U = z.U(b10, "*", false, 2, null);
                                if (!U) {
                                    return t.b(str5, b10);
                                }
                                P3 = y.P(b10, "*.", false, 2, null);
                                if (P3) {
                                    g02 = z.g0(b10, '*', 1, false, 4, null);
                                    if (g02 != -1 || str5.length() < b10.length() || t.b("*.", b10)) {
                                        return false;
                                    }
                                    String substring = b10.substring(1);
                                    t.f(substring, "this as java.lang.String).substring(startIndex)");
                                    y14 = y.y(str5, substring, false, 2, null);
                                    if (!y14) {
                                        return false;
                                    }
                                    int length = str5.length() - substring.length();
                                    if (length > 0) {
                                        l02 = z.l0(str5, '.', length - 1, false, 4, null);
                                        if (l02 != -1) {
                                            return false;
                                        }
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    private final boolean g(String str, X509Certificate x509Certificate) {
        String b10 = b(str);
        List c10 = c(x509Certificate, 2);
        if ((c10 instanceof Collection) && c10.isEmpty()) {
            return false;
        }
        Iterator it = c10.iterator();
        while (it.hasNext()) {
            if (f22078a.f(b10, (String) it.next())) {
                return true;
            }
        }
        return false;
    }

    private final boolean h(String str, X509Certificate x509Certificate) {
        String a10 = g.a(str);
        List c10 = c(x509Certificate, 7);
        if ((c10 instanceof Collection) && c10.isEmpty()) {
            return false;
        }
        Iterator it = c10.iterator();
        while (it.hasNext()) {
            if (t.b(a10, g.a((String) it.next()))) {
                return true;
            }
        }
        return false;
    }

    public final List a(X509Certificate certificate) {
        List z02;
        t.g(certificate, "certificate");
        z02 = sm.c0.z0(c(certificate, 7), c(certificate, 2));
        return z02;
    }

    public final boolean e(String host, X509Certificate certificate) {
        t.g(host, "host");
        t.g(certificate, "certificate");
        return f.a(host) ? h(host, certificate) : g(host, certificate);
    }

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