package o4;

import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Pattern f21847b = Pattern.compile("([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\d.]+)");

    public static List a(X509Certificate x509Certificate, int i6) {
        Integer num;
        String str;
        ArrayList arrayList = new ArrayList();
        try {
            Collection<List<?>> subjectAlternativeNames = x509Certificate.getSubjectAlternativeNames();
            if (subjectAlternativeNames == null) {
                return Collections.EMPTY_LIST;
            }
            for (List<?> list : subjectAlternativeNames) {
                if (list != null && list.size() >= 2 && (num = (Integer) list.get(0)) != null && num.intValue() == i6 && (str = (String) list.get(1)) != null) {
                    arrayList.add(str);
                }
            }
            return arrayList;
        } catch (CertificateParsingException unused) {
            return Collections.EMPTY_LIST;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:43:0x00dc, code lost:
    
        r17 = r4;
     */
    /* JADX WARN: Code restructure failed: missing block: B:45:0x00f4, code lost:
    
        r4 = r5.f21842d;
        r8 = new java.lang.String(r10, r4, r5.f21843e - r4);
     */
    /* JADX WARN: Code restructure failed: missing block: B:92:0x01c2, code lost:
    
        throw new java.lang.IllegalStateException("Unexpected end of DN: ".concat(r1));
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r12v1 */
    /* JADX WARN: Type inference failed for: r12v2, types: [int] */
    /* JADX WARN: Type inference failed for: r12v8 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static boolean b(String str, X509Certificate x509Certificate) {
        boolean z6;
        String str2;
        int i6;
        char[] cArr;
        char c6;
        int i7;
        char c7;
        boolean z7 = true;
        boolean z8 = false;
        if (f21847b.matcher(str).matches()) {
            List listA = a(x509Certificate, 7);
            int size = listA.size();
            for (int i8 = 0; i8 < size; i8++) {
                if (str.equalsIgnoreCase((String) listA.get(i8))) {
                    return true;
                }
            }
            return false;
        }
        String lowerCase = str.toLowerCase(Locale.US);
        char c8 = 2;
        List listA2 = a(x509Certificate, 2);
        int size2 = listA2.size();
        int i9 = 0;
        boolean z9 = false;
        while (i9 < size2) {
            if (c(lowerCase, (String) listA2.get(i9))) {
                return true;
            }
            i9++;
            z9 = true;
        }
        if (!z9) {
            d dVar = new d(x509Certificate.getSubjectX500Principal());
            dVar.f21841c = 0;
            dVar.f21842d = 0;
            dVar.f21843e = 0;
            dVar.f21844f = 0;
            String str3 = dVar.f21839a;
            dVar.f21845g = str3.toCharArray();
            String strC = dVar.c();
            String str4 = null;
            if (strC == null) {
                z6 = z8;
            } else {
                while (true) {
                    int i10 = dVar.f21841c;
                    int i11 = dVar.f21840b;
                    if (i10 == i11) {
                        break;
                    }
                    char c9 = dVar.f21845g[i10];
                    char c10 = c8;
                    boolean z10 = z7;
                    if (c9 == '\"') {
                        z6 = z8;
                        int i12 = i10 + 1;
                        dVar.f21841c = i12;
                        dVar.f21842d = i12;
                        dVar.f21843e = i12;
                        while (true) {
                            int i13 = dVar.f21841c;
                            if (i13 == i11) {
                                throw new IllegalStateException("Unexpected end of DN: ".concat(str3));
                            }
                            char[] cArr2 = dVar.f21845g;
                            char c11 = cArr2[i13];
                            if (c11 == '\"') {
                                dVar.f21841c = i13 + 1;
                                while (true) {
                                    int i14 = dVar.f21841c;
                                    if (i14 >= i11 || dVar.f21845g[i14] != ' ') {
                                        break;
                                    }
                                    dVar.f21841c = i14 + 1;
                                }
                                char[] cArr3 = dVar.f21845g;
                                int i15 = dVar.f21842d;
                                str2 = new String(cArr3, i15, dVar.f21843e - i15);
                            } else {
                                if (c11 == '\\') {
                                    cArr2[dVar.f21843e] = dVar.b();
                                } else {
                                    cArr2[dVar.f21843e] = c11;
                                }
                                dVar.f21841c++;
                                dVar.f21843e++;
                            }
                        }
                    } else if (c9 == '#') {
                        z6 = z8;
                        if (i10 + 4 >= i11) {
                            throw new IllegalStateException("Unexpected end of DN: ".concat(str3));
                        }
                        dVar.f21842d = i10;
                        dVar.f21841c = i10 + 1;
                        while (true) {
                            i6 = dVar.f21841c;
                            if (i6 == i11 || (c6 = (cArr = dVar.f21845g)[i6]) == '+' || c6 == ',' || c6 == ';') {
                                break;
                            }
                            if (c6 == ' ') {
                                dVar.f21843e = i6;
                                dVar.f21841c = i6 + 1;
                                while (true) {
                                    int i16 = dVar.f21841c;
                                    if (i16 >= i11 || dVar.f21845g[i16] != ' ') {
                                        break;
                                    }
                                    dVar.f21841c = i16 + 1;
                                }
                            } else {
                                if (c6 >= 'A' && c6 <= 'F') {
                                    cArr[i6] = (char) (c6 + ' ');
                                }
                                dVar.f21841c = i6 + 1;
                            }
                        }
                        dVar.f21843e = i6;
                        int i17 = dVar.f21843e;
                        int i18 = dVar.f21842d;
                        int i19 = i17 - i18;
                        if (i19 < 5 || (i19 & 1) == 0) {
                            break;
                        }
                        int i20 = i19 / 2;
                        byte[] bArr = new byte[i20];
                        int i21 = i18 + 1;
                        for (?? r12 = z6; r12 < i20; r12++) {
                            bArr[r12] = (byte) dVar.a(i21);
                            i21 += 2;
                        }
                        str2 = new String(dVar.f21845g, dVar.f21842d, i19);
                    } else if (c9 == '+' || c9 == ',' || c9 == ';') {
                        z6 = z8;
                        str2 = "";
                    } else {
                        dVar.f21842d = i10;
                        dVar.f21843e = i10;
                        while (true) {
                            int i22 = dVar.f21841c;
                            if (i22 >= i11) {
                                char[] cArr4 = dVar.f21845g;
                                int i23 = dVar.f21842d;
                                str2 = new String(cArr4, i23, dVar.f21843e - i23);
                                z6 = z8;
                                break;
                            }
                            char[] cArr5 = dVar.f21845g;
                            char c12 = cArr5[i22];
                            if (c12 == ' ') {
                                z6 = z8;
                                int i24 = dVar.f21843e;
                                dVar.f21844f = i24;
                                dVar.f21841c = i22 + 1;
                                dVar.f21843e = i24 + 1;
                                cArr5[i24] = ' ';
                                while (true) {
                                    i7 = dVar.f21841c;
                                    if (i7 >= i11) {
                                        break;
                                    }
                                    char[] cArr6 = dVar.f21845g;
                                    if (cArr6[i7] != ' ') {
                                        break;
                                    }
                                    int i25 = dVar.f21843e;
                                    dVar.f21843e = i25 + 1;
                                    cArr6[i25] = ' ';
                                    dVar.f21841c = i7 + 1;
                                }
                                if (i7 == i11 || (c7 = dVar.f21845g[i7]) == ',' || c7 == '+' || c7 == ';') {
                                    break;
                                }
                                z8 = z6;
                            } else {
                                if (c12 == ';') {
                                    break;
                                }
                                if (c12 == '\\') {
                                    z6 = z8;
                                    int i26 = dVar.f21843e;
                                    dVar.f21843e = i26 + 1;
                                    cArr5[i26] = dVar.b();
                                    dVar.f21841c++;
                                } else {
                                    if (c12 == '+' || c12 == ',') {
                                        break;
                                    }
                                    int i27 = dVar.f21843e;
                                    z6 = z8;
                                    dVar.f21843e = i27 + 1;
                                    cArr5[i27] = c12;
                                    dVar.f21841c = i22 + 1;
                                }
                                z8 = z6;
                            }
                        }
                        char[] cArr7 = dVar.f21845g;
                        int i28 = dVar.f21842d;
                        str2 = new String(cArr7, i28, dVar.f21844f - i28);
                    }
                    if ("cn".equalsIgnoreCase(strC)) {
                        str4 = str2;
                        break;
                    }
                    int i29 = dVar.f21841c;
                    if (i29 >= i11) {
                        break;
                    }
                    char c13 = dVar.f21845g[i29];
                    if (c13 != ',' && c13 != ';' && c13 != '+') {
                        throw new IllegalStateException("Malformed DN: ".concat(str3));
                    }
                    dVar.f21841c = i29 + 1;
                    strC = dVar.c();
                    if (strC == null) {
                        throw new IllegalStateException("Malformed DN: ".concat(str3));
                    }
                    c8 = c10;
                    z7 = z10;
                    z8 = z6;
                }
            }
            return str4 != null ? c(lowerCase, str4) : z6;
        }
        return false;
    }

    public static boolean c(String str, String str2) {
        if (str == null || str.length() == 0 || str.startsWith(".") || str.endsWith("..") || str2 == null || str2.length() == 0 || str2.startsWith(".") || str2.endsWith("..")) {
            return false;
        }
        if (!str.endsWith(".")) {
            str = str.concat(".");
        }
        if (!str2.endsWith(".")) {
            str2 = str2.concat(".");
        }
        String lowerCase = str2.toLowerCase(Locale.US);
        if (!lowerCase.contains("*")) {
            return str.equals(lowerCase);
        }
        if (!lowerCase.startsWith("*.") || lowerCase.indexOf(42, 1) != -1 || str.length() < lowerCase.length() || "*.".equals(lowerCase)) {
            return false;
        }
        String strSubstring = lowerCase.substring(1);
        if (!str.endsWith(strSubstring)) {
            return false;
        }
        int length = str.length() - strSubstring.length();
        return length <= 0 || str.lastIndexOf(46, length - 1) == -1;
    }

    @Override // javax.net.ssl.HostnameVerifier
    public final boolean verify(String str, SSLSession sSLSession) {
        try {
            return b(str, (X509Certificate) sSLSession.getPeerCertificates()[0]);
        } catch (SSLException unused) {
            return false;
        }
    }
}
