package XnFJ5_9.Ft6VRog_36.WwEq_13;

import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
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;
/* compiled from: BasicCertificateChainCleaner.java */
/* loaded from: classes.dex */
public final class KQgRr5e_0 extends PGPF6hQ_2 {
    public final X6FRs_4 KQgRr5e_0;

    public KQgRr5e_0(X6FRs_4 x6FRs_4) {
        this.KQgRr5e_0 = x6FRs_4;
    }

    @Override // XnFJ5_9.Ft6VRog_36.WwEq_13.PGPF6hQ_2
    public List<Certificate> KQgRr5e_0(List<Certificate> list, String str) throws SSLPeerUnverifiedException {
        ArrayDeque arrayDeque = new ArrayDeque(list);
        ArrayList arrayList = new ArrayList();
        arrayList.add((Certificate) arrayDeque.removeFirst());
        boolean z = false;
        for (int i = 0; i < 9; i++) {
            X509Certificate x509Certificate = (X509Certificate) arrayList.get(arrayList.size() - 1);
            X509Certificate KQgRr5e_0 = this.KQgRr5e_0.KQgRr5e_0(x509Certificate);
            if (KQgRr5e_0 != null) {
                if (arrayList.size() > 1 || !x509Certificate.equals(KQgRr5e_0)) {
                    arrayList.add(KQgRr5e_0);
                }
                if (KQgRr5e_0(KQgRr5e_0, KQgRr5e_0)) {
                    return arrayList;
                }
                z = true;
            } else {
                Iterator it = arrayDeque.iterator();
                while (it.hasNext()) {
                    X509Certificate x509Certificate2 = (X509Certificate) it.next();
                    if (KQgRr5e_0(x509Certificate, x509Certificate2)) {
                        it.remove();
                        arrayList.add(x509Certificate2);
                    }
                }
                if (z) {
                    return arrayList;
                }
                throw new SSLPeerUnverifiedException("Failed to find a trusted cert that signed " + x509Certificate);
            }
        }
        throw new SSLPeerUnverifiedException("Certificate chain too long: " + arrayList);
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        return (obj instanceof KQgRr5e_0) && ((KQgRr5e_0) obj).KQgRr5e_0.equals(this.KQgRr5e_0);
    }

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

    public final boolean KQgRr5e_0(X509Certificate x509Certificate, X509Certificate x509Certificate2) {
        if (x509Certificate.getIssuerDN().equals(x509Certificate2.getSubjectDN())) {
            try {
                x509Certificate.verify(x509Certificate2.getPublicKey());
                return true;
            } catch (GeneralSecurityException unused) {
                return false;
            }
        }
        return false;
    }
}
