package hr;

import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.net.ssl.SSLPeerUnverifiedException;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;

/* loaded from: classes3.dex */
public final class a extends c {

    /* renamed from: c, reason: collision with root package name */
    public static final C0473a f22074c = new C0473a(null);

    /* renamed from: b, reason: collision with root package name */
    private final e f22075b;

    /* renamed from: hr.a$a, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    public static final class C0473a {
        private C0473a() {
        }

        public /* synthetic */ C0473a(k kVar) {
            this();
        }
    }

    public a(e trustRootIndex) {
        t.g(trustRootIndex, "trustRootIndex");
        this.f22075b = trustRootIndex;
    }

    private final boolean b(X509Certificate x509Certificate, X509Certificate x509Certificate2, int i10) {
        if (!t.b(x509Certificate.getIssuerDN(), x509Certificate2.getSubjectDN()) || x509Certificate2.getBasicConstraints() < i10) {
            return false;
        }
        try {
            x509Certificate.verify(x509Certificate2.getPublicKey());
            return true;
        } catch (GeneralSecurityException unused) {
            return false;
        }
    }

    @Override // hr.c
    public List a(List chain, String hostname) {
        t.g(chain, "chain");
        t.g(hostname, "hostname");
        ArrayDeque arrayDeque = new ArrayDeque(chain);
        ArrayList arrayList = new ArrayList();
        Object removeFirst = arrayDeque.removeFirst();
        t.f(removeFirst, "queue.removeFirst()");
        arrayList.add(removeFirst);
        boolean z10 = false;
        for (int i10 = 0; i10 < 9; i10++) {
            Object obj = arrayList.get(arrayList.size() - 1);
            t.e(obj, "null cannot be cast to non-null type java.security.cert.X509Certificate");
            X509Certificate x509Certificate = (X509Certificate) obj;
            X509Certificate a10 = this.f22075b.a(x509Certificate);
            if (a10 == null) {
                Iterator it = arrayDeque.iterator();
                t.f(it, "queue.iterator()");
                while (it.hasNext()) {
                    Object next = it.next();
                    t.e(next, "null cannot be cast to non-null type java.security.cert.X509Certificate");
                    X509Certificate x509Certificate2 = (X509Certificate) next;
                    if (b(x509Certificate, x509Certificate2, arrayList.size() - 1)) {
                        it.remove();
                        arrayList.add(x509Certificate2);
                    }
                }
                if (z10) {
                    return arrayList;
                }
                throw new SSLPeerUnverifiedException("Failed to find a trusted cert that signed " + x509Certificate);
            }
            if (arrayList.size() > 1 || !t.b(x509Certificate, a10)) {
                arrayList.add(a10);
            }
            if (b(a10, a10, arrayList.size() - 2)) {
                return arrayList;
            }
            z10 = true;
        }
        throw new SSLPeerUnverifiedException("Certificate chain too long: " + arrayList);
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        return (obj instanceof a) && t.b(((a) obj).f22075b, this.f22075b);
    }

    public int hashCode() {
        return this.f22075b.hashCode();
    }
}
