package okhttp3;

import d.b.a.a.a;
import g.c0.g;
import g.h0.c.f0;
import g.h0.c.l;
import g.h0.c.m;
import g.i;
import g.m0.f;
import j.h;
import java.security.Principal;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.net.ssl.SSLPeerUnverifiedException;
import okhttp3.internal.HostnamesKt;
import okhttp3.internal.tls.CertificateChainCleaner;

/* JADX INFO: compiled from: CertificatePinner.kt */
/* JADX INFO: loaded from: classes.dex */
public final class CertificatePinner {
    public static final Companion Companion = new Companion(null);
    public static final CertificatePinner DEFAULT = new Builder().build();
    private final CertificateChainCleaner certificateChainCleaner;
    private final Set<Pin> pins;

    /* JADX INFO: compiled from: CertificatePinner.kt */
    public static final class Builder {
        private final List<Pin> pins = new ArrayList();

        public final Builder add(String str, String... strArr) {
            l.f(str, "pattern");
            l.f(strArr, "pins");
            for (String str2 : strArr) {
                this.pins.add(new Pin(str, str2));
            }
            return this;
        }

        /* JADX WARN: Multi-variable type inference failed */
        public final CertificatePinner build() {
            return new CertificatePinner(g.M(this.pins), null, 2, 0 == true ? 1 : 0);
        }

        public final List<Pin> getPins() {
            return this.pins;
        }
    }

    /* JADX INFO: compiled from: CertificatePinner.kt */
    public static final class Companion {
        private Companion() {
        }

        public final String pin(Certificate certificate) {
            l.f(certificate, "certificate");
            if (!(certificate instanceof X509Certificate)) {
                throw new IllegalArgumentException("Certificate pinning requires X509 certificates".toString());
            }
            StringBuilder sbB = a.B("sha256/");
            sbB.append(sha256Hash((X509Certificate) certificate).a());
            return sbB.toString();
        }

        public final h sha1Hash(X509Certificate x509Certificate) {
            l.f(x509Certificate, "$this$sha1Hash");
            h.a aVar = h.f4612e;
            PublicKey publicKey = x509Certificate.getPublicKey();
            l.e(publicKey, "publicKey");
            byte[] encoded = publicKey.getEncoded();
            l.e(encoded, "publicKey.encoded");
            return h.a.d(aVar, encoded, 0, 0, 3).i();
        }

        public final h sha256Hash(X509Certificate x509Certificate) {
            l.f(x509Certificate, "$this$sha256Hash");
            h.a aVar = h.f4612e;
            PublicKey publicKey = x509Certificate.getPublicKey();
            l.e(publicKey, "publicKey");
            byte[] encoded = publicKey.getEncoded();
            l.e(encoded, "publicKey.encoded");
            return h.a.d(aVar, encoded, 0, 0, 3).b("SHA-256");
        }

        public /* synthetic */ Companion(g.h0.c.g gVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: CertificatePinner.kt */
    public static final class Pin {
        private final h hash;
        private final String hashAlgorithm;
        private final String pattern;

        public Pin(String str, String str2) {
            l.f(str, "pattern");
            l.f(str2, "pin");
            boolean z = true;
            if ((!f.J(str, "*.", false, 2) || f.m(str, "*", 1, false, 4) != -1) && ((!f.J(str, "**.", false, 2) || f.m(str, "*", 2, false, 4) != -1) && f.m(str, "*", 0, false, 6) != -1)) {
                z = false;
            }
            if (!z) {
                throw new IllegalArgumentException(a.p("Unexpected pattern: ", str).toString());
            }
            String canonicalHost = HostnamesKt.toCanonicalHost(str);
            if (canonicalHost == null) {
                throw new IllegalArgumentException(a.p("Invalid pattern: ", str));
            }
            this.pattern = canonicalHost;
            if (f.J(str2, "sha1/", false, 2)) {
                this.hashAlgorithm = "sha1";
                h.a aVar = h.f4612e;
                String strSubstring = str2.substring(5);
                l.e(strSubstring, "(this as java.lang.String).substring(startIndex)");
                h hVarA = aVar.a(strSubstring);
                if (hVarA == null) {
                    throw new IllegalArgumentException(a.p("Invalid pin hash: ", str2));
                }
                this.hash = hVarA;
                return;
            }
            if (!f.J(str2, "sha256/", false, 2)) {
                throw new IllegalArgumentException(a.p("pins must start with 'sha256/' or 'sha1/': ", str2));
            }
            this.hashAlgorithm = "sha256";
            h.a aVar2 = h.f4612e;
            String strSubstring2 = str2.substring(7);
            l.e(strSubstring2, "(this as java.lang.String).substring(startIndex)");
            h hVarA2 = aVar2.a(strSubstring2);
            if (hVarA2 == null) {
                throw new IllegalArgumentException(a.p("Invalid pin hash: ", str2));
            }
            this.hash = hVarA2;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Pin)) {
                return false;
            }
            Pin pin = (Pin) obj;
            return ((l.a(this.pattern, pin.pattern) ^ true) || (l.a(this.hashAlgorithm, pin.hashAlgorithm) ^ true) || (l.a(this.hash, pin.hash) ^ true)) ? false : true;
        }

        public final h getHash() {
            return this.hash;
        }

        public final String getHashAlgorithm() {
            return this.hashAlgorithm;
        }

        public final String getPattern() {
            return this.pattern;
        }

        public int hashCode() {
            return this.hash.hashCode() + a.x(this.hashAlgorithm, this.pattern.hashCode() * 31, 31);
        }

        public final boolean matchesCertificate(X509Certificate x509Certificate) {
            l.f(x509Certificate, "certificate");
            String str = this.hashAlgorithm;
            int iHashCode = str.hashCode();
            if (iHashCode != -903629273) {
                if (iHashCode == 3528965 && str.equals("sha1")) {
                    return l.a(this.hash, CertificatePinner.Companion.sha1Hash(x509Certificate));
                }
            } else if (str.equals("sha256")) {
                return l.a(this.hash, CertificatePinner.Companion.sha256Hash(x509Certificate));
            }
            return false;
        }

        public final boolean matchesHostname(String str) {
            l.f(str, "hostname");
            if (f.J(this.pattern, "**.", false, 2)) {
                int length = this.pattern.length() - 3;
                int length2 = str.length() - length;
                if (!f.w(str, str.length() - length, this.pattern, 3, length, false, 16)) {
                    return false;
                }
                if (length2 != 0 && str.charAt(length2 - 1) != '.') {
                    return false;
                }
            } else {
                if (!f.J(this.pattern, "*.", false, 2)) {
                    return l.a(str, this.pattern);
                }
                int length3 = this.pattern.length() - 1;
                int length4 = str.length() - length3;
                if (!f.w(str, str.length() - length3, this.pattern, 1, length3, false, 16) || f.p(str, '.', length4 - 1, false, 4) != -1) {
                    return false;
                }
            }
            return true;
        }

        public String toString() {
            return this.hashAlgorithm + '/' + this.hash.a();
        }
    }

    /* JADX INFO: renamed from: okhttp3.CertificatePinner$check$1, reason: invalid class name */
    /* JADX INFO: compiled from: CertificatePinner.kt */
    @i
    public static final class AnonymousClass1 extends m implements g.h0.b.a<List<? extends X509Certificate>> {
        public final /* synthetic */ String $hostname;
        public final /* synthetic */ List $peerCertificates;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass1(List list, String str) {
            super(0);
            this.$peerCertificates = list;
            this.$hostname = str;
        }

        @Override // g.h0.b.a
        public final List<? extends X509Certificate> invoke() {
            List<Certificate> listClean;
            CertificateChainCleaner certificateChainCleaner$okhttp = CertificatePinner.this.getCertificateChainCleaner$okhttp();
            if (certificateChainCleaner$okhttp == null || (listClean = certificateChainCleaner$okhttp.clean(this.$peerCertificates, this.$hostname)) == null) {
                listClean = this.$peerCertificates;
            }
            ArrayList arrayList = new ArrayList(f.a.y.a.C(listClean, 10));
            for (Certificate certificate : listClean) {
                Objects.requireNonNull(certificate, "null cannot be cast to non-null type java.security.cert.X509Certificate");
                arrayList.add((X509Certificate) certificate);
            }
            return arrayList;
        }
    }

    public CertificatePinner(Set<Pin> set, CertificateChainCleaner certificateChainCleaner) {
        l.f(set, "pins");
        this.pins = set;
        this.certificateChainCleaner = certificateChainCleaner;
    }

    public static final String pin(Certificate certificate) {
        return Companion.pin(certificate);
    }

    public static final h sha1Hash(X509Certificate x509Certificate) {
        return Companion.sha1Hash(x509Certificate);
    }

    public static final h sha256Hash(X509Certificate x509Certificate) {
        return Companion.sha256Hash(x509Certificate);
    }

    public final void check(String str, List<? extends Certificate> list) throws SSLPeerUnverifiedException {
        l.f(str, "hostname");
        l.f(list, "peerCertificates");
        check$okhttp(str, new AnonymousClass1(list, str));
    }

    public final void check$okhttp(String str, g.h0.b.a<? extends List<? extends X509Certificate>> aVar) throws SSLPeerUnverifiedException {
        l.f(str, "hostname");
        l.f(aVar, "cleanedPeerCertificatesFn");
        List<Pin> listFindMatchingPins = findMatchingPins(str);
        if (listFindMatchingPins.isEmpty()) {
            return;
        }
        List<? extends X509Certificate> listInvoke = aVar.invoke();
        for (X509Certificate x509Certificate : listInvoke) {
            h hVarSha256Hash = null;
            h hVarSha1Hash = null;
            for (Pin pin : listFindMatchingPins) {
                String hashAlgorithm = pin.getHashAlgorithm();
                int iHashCode = hashAlgorithm.hashCode();
                if (iHashCode != -903629273) {
                    if (iHashCode != 3528965 || !hashAlgorithm.equals("sha1")) {
                        StringBuilder sbB = a.B("unsupported hashAlgorithm: ");
                        sbB.append(pin.getHashAlgorithm());
                        throw new AssertionError(sbB.toString());
                    }
                    if (hVarSha1Hash == null) {
                        hVarSha1Hash = Companion.sha1Hash(x509Certificate);
                    }
                    if (l.a(pin.getHash(), hVarSha1Hash)) {
                        return;
                    }
                } else {
                    if (!hashAlgorithm.equals("sha256")) {
                        StringBuilder sbB2 = a.B("unsupported hashAlgorithm: ");
                        sbB2.append(pin.getHashAlgorithm());
                        throw new AssertionError(sbB2.toString());
                    }
                    if (hVarSha256Hash == null) {
                        hVarSha256Hash = Companion.sha256Hash(x509Certificate);
                    }
                    if (l.a(pin.getHash(), hVarSha256Hash)) {
                        return;
                    }
                }
            }
        }
        StringBuilder sbD = a.D("Certificate pinning failure!", "\n  Peer certificate chain:");
        for (X509Certificate x509Certificate2 : listInvoke) {
            sbD.append("\n    ");
            sbD.append(Companion.pin(x509Certificate2));
            sbD.append(": ");
            Principal subjectDN = x509Certificate2.getSubjectDN();
            l.e(subjectDN, "element.subjectDN");
            sbD.append(subjectDN.getName());
        }
        sbD.append("\n  Pinned certificates for ");
        sbD.append(str);
        sbD.append(":");
        for (Pin pin2 : listFindMatchingPins) {
            sbD.append("\n    ");
            sbD.append(pin2);
        }
        String string = sbD.toString();
        l.e(string, "StringBuilder().apply(builderAction).toString()");
        throw new SSLPeerUnverifiedException(string);
    }

    public boolean equals(Object obj) {
        if (obj instanceof CertificatePinner) {
            CertificatePinner certificatePinner = (CertificatePinner) obj;
            if (l.a(certificatePinner.pins, this.pins) && l.a(certificatePinner.certificateChainCleaner, this.certificateChainCleaner)) {
                return true;
            }
        }
        return false;
    }

    public final List<Pin> findMatchingPins(String str) {
        l.f(str, "hostname");
        Set<Pin> set = this.pins;
        List arrayList = g.c0.l.f4067b;
        for (Object obj : set) {
            if (((Pin) obj).matchesHostname(str)) {
                if (arrayList.isEmpty()) {
                    arrayList = new ArrayList();
                }
                f0.a(arrayList).add(obj);
            }
        }
        return arrayList;
    }

    public final CertificateChainCleaner getCertificateChainCleaner$okhttp() {
        return this.certificateChainCleaner;
    }

    public final Set<Pin> getPins() {
        return this.pins;
    }

    public int hashCode() {
        int iHashCode = (this.pins.hashCode() + 1517) * 41;
        CertificateChainCleaner certificateChainCleaner = this.certificateChainCleaner;
        return iHashCode + (certificateChainCleaner != null ? certificateChainCleaner.hashCode() : 0);
    }

    public final CertificatePinner withCertificateChainCleaner$okhttp(CertificateChainCleaner certificateChainCleaner) {
        l.f(certificateChainCleaner, "certificateChainCleaner");
        return l.a(this.certificateChainCleaner, certificateChainCleaner) ? this : new CertificatePinner(this.pins, certificateChainCleaner);
    }

    public /* synthetic */ CertificatePinner(Set set, CertificateChainCleaner certificateChainCleaner, int i2, g.h0.c.g gVar) {
        this(set, (i2 & 2) != 0 ? null : certificateChainCleaner);
    }

    public final void check(String str, Certificate... certificateArr) throws SSLPeerUnverifiedException {
        l.f(str, "hostname");
        l.f(certificateArr, "peerCertificates");
        check(str, f.a.y.a.k1(certificateArr));
    }
}
