78 lines
3.0 KiB
Java
78 lines
3.0 KiB
Java
package okhttp3.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: classes2.dex */
|
|
public final class BasicCertificateChainCleaner extends CertificateChainCleaner {
|
|
private static final int MAX_SIGNERS = 9;
|
|
private final TrustRootIndex trustRootIndex;
|
|
|
|
public BasicCertificateChainCleaner(TrustRootIndex trustRootIndex) {
|
|
this.trustRootIndex = trustRootIndex;
|
|
}
|
|
|
|
private boolean verifySignature(X509Certificate x509Certificate, X509Certificate x509Certificate2) {
|
|
if (!x509Certificate.getIssuerDN().equals(x509Certificate2.getSubjectDN())) {
|
|
return false;
|
|
}
|
|
try {
|
|
x509Certificate.verify(x509Certificate2.getPublicKey());
|
|
return true;
|
|
} catch (GeneralSecurityException unused) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override // okhttp3.internal.tls.CertificateChainCleaner
|
|
public List<Certificate> clean(List<Certificate> list, String str) 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.trustRootIndex.findByIssuerAndSignature(x509Certificate);
|
|
if (findByIssuerAndSignature == null) {
|
|
Iterator it = arrayDeque.iterator();
|
|
while (it.hasNext()) {
|
|
X509Certificate x509Certificate2 = (X509Certificate) it.next();
|
|
if (verifySignature(x509Certificate, x509Certificate2)) {
|
|
it.remove();
|
|
arrayList.add(x509Certificate2);
|
|
}
|
|
}
|
|
if (z) {
|
|
return arrayList;
|
|
}
|
|
throw new SSLPeerUnverifiedException("Failed to find a trusted cert that signed " + x509Certificate);
|
|
}
|
|
if (arrayList.size() > 1 || !x509Certificate.equals(findByIssuerAndSignature)) {
|
|
arrayList.add(findByIssuerAndSignature);
|
|
}
|
|
if (verifySignature(findByIssuerAndSignature, findByIssuerAndSignature)) {
|
|
return arrayList;
|
|
}
|
|
z = true;
|
|
}
|
|
throw new SSLPeerUnverifiedException("Certificate chain too long: " + arrayList);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (obj == this) {
|
|
return true;
|
|
}
|
|
return (obj instanceof BasicCertificateChainCleaner) && ((BasicCertificateChainCleaner) obj).trustRootIndex.equals(this.trustRootIndex);
|
|
}
|
|
|
|
public int hashCode() {
|
|
return this.trustRootIndex.hashCode();
|
|
}
|
|
}
|