package okhttp3.repackaged.internal.tls; 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; /* loaded from: classes6.dex */ public final class CertificateChainCleaner { private static final int amS = 9; private final TrustRootIndex ahf; public CertificateChainCleaner(TrustRootIndex trustRootIndex) { this.ahf = trustRootIndex; } public final List clean(List list) throws SSLPeerUnverifiedException { ArrayDeque arrayDeque = new ArrayDeque(list); ArrayList arrayList = new ArrayList(); arrayList.add(arrayDeque.removeFirst()); boolean z = false; for (int i = 0; i < 9; i++) { X509Certificate x509Certificate = (X509Certificate) arrayList.get(arrayList.size() - 1); X509Certificate findByIssuerAndSignature = this.ahf.findByIssuerAndSignature(x509Certificate); if (findByIssuerAndSignature != null) { if (arrayList.size() > 1 || !x509Certificate.equals(findByIssuerAndSignature)) { arrayList.add(findByIssuerAndSignature); } if (a(findByIssuerAndSignature, findByIssuerAndSignature)) { return arrayList; } z = true; } else { Iterator it = arrayDeque.iterator(); while (it.hasNext()) { X509Certificate x509Certificate2 = (X509Certificate) it.next(); if (a(x509Certificate, x509Certificate2)) { it.remove(); arrayList.add(x509Certificate2); } } if (z) { return arrayList; } throw new SSLPeerUnverifiedException("Failed to find a trusted cert that signed ".concat(String.valueOf(x509Certificate))); } } throw new SSLPeerUnverifiedException("Certificate chain too long: ".concat(String.valueOf(arrayList))); } private boolean a(X509Certificate x509Certificate, X509Certificate x509Certificate2) { if (!x509Certificate.getIssuerDN().equals(x509Certificate2.getSubjectDN())) { return false; } try { x509Certificate.verify(x509Certificate2.getPublicKey()); return true; } catch (GeneralSecurityException unused) { return false; } } }