218 lines
7.4 KiB
Java
218 lines
7.4 KiB
Java
package okhttp3.repackaged;
|
|
|
|
import java.security.cert.Certificate;
|
|
import java.security.cert.X509Certificate;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
|
import o.C15180gja;
|
|
import o.giP;
|
|
import okhttp3.repackaged.internal.Util;
|
|
import okhttp3.repackaged.internal.tls.CertificateChainCleaner;
|
|
import okhttp3.repackaged.internal.tls.TrustRootIndex;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class CertificatePinner {
|
|
public static final CertificatePinner DEFAULT = new Builder().build();
|
|
private final List<a> ahe;
|
|
private final TrustRootIndex ahf;
|
|
|
|
private CertificatePinner(Builder builder) {
|
|
this.ahe = Util.immutableList(builder.ahe);
|
|
this.ahf = builder.ahf;
|
|
}
|
|
|
|
public final void check(String str, List<Certificate> list) throws SSLPeerUnverifiedException {
|
|
List<a> findMatchingPins = findMatchingPins(str);
|
|
if (findMatchingPins.isEmpty()) {
|
|
return;
|
|
}
|
|
TrustRootIndex trustRootIndex = this.ahf;
|
|
if (trustRootIndex != null) {
|
|
list = new CertificateChainCleaner(trustRootIndex).clean(list);
|
|
}
|
|
int size = list.size();
|
|
for (int i = 0; i < size; i++) {
|
|
X509Certificate x509Certificate = (X509Certificate) list.get(i);
|
|
int size2 = findMatchingPins.size();
|
|
giP gip = null;
|
|
giP gip2 = null;
|
|
for (int i2 = 0; i2 < size2; i2++) {
|
|
a aVar = findMatchingPins.get(i2);
|
|
if (aVar.hashAlgorithm.equals("sha256/")) {
|
|
if (gip == null) {
|
|
gip = b(x509Certificate);
|
|
}
|
|
if (aVar.ahg.equals(gip)) {
|
|
return;
|
|
}
|
|
} else if (aVar.hashAlgorithm.equals("sha1/")) {
|
|
if (gip2 == null) {
|
|
gip2 = a(x509Certificate);
|
|
}
|
|
if (aVar.ahg.equals(gip2)) {
|
|
return;
|
|
}
|
|
} else {
|
|
throw new AssertionError();
|
|
}
|
|
}
|
|
}
|
|
StringBuilder sb = new StringBuilder("Certificate pinning failure!\n Peer certificate chain:");
|
|
int size3 = list.size();
|
|
for (int i3 = 0; i3 < size3; i3++) {
|
|
X509Certificate x509Certificate2 = (X509Certificate) list.get(i3);
|
|
sb.append("\n ");
|
|
sb.append(pin(x509Certificate2));
|
|
sb.append(": ");
|
|
sb.append(x509Certificate2.getSubjectDN().getName());
|
|
}
|
|
sb.append("\n Pinned certificates for ");
|
|
sb.append(str);
|
|
sb.append(":");
|
|
int size4 = findMatchingPins.size();
|
|
for (int i4 = 0; i4 < size4; i4++) {
|
|
a aVar2 = findMatchingPins.get(i4);
|
|
sb.append("\n ");
|
|
sb.append(aVar2);
|
|
}
|
|
throw new SSLPeerUnverifiedException(sb.toString());
|
|
}
|
|
|
|
public final void check(String str, Certificate... certificateArr) throws SSLPeerUnverifiedException {
|
|
check(str, Arrays.asList(certificateArr));
|
|
}
|
|
|
|
final List<a> findMatchingPins(String str) {
|
|
List<a> emptyList = Collections.emptyList();
|
|
for (a aVar : this.ahe) {
|
|
if (aVar.matches(str)) {
|
|
if (emptyList.isEmpty()) {
|
|
emptyList = new ArrayList<>();
|
|
}
|
|
emptyList.add(aVar);
|
|
}
|
|
}
|
|
return emptyList;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final Builder um() {
|
|
return new Builder(this);
|
|
}
|
|
|
|
public static String pin(Certificate certificate) {
|
|
if (!(certificate instanceof X509Certificate)) {
|
|
throw new IllegalArgumentException("Certificate pinning requires X509 certificates");
|
|
}
|
|
StringBuilder sb = new StringBuilder("sha256/");
|
|
sb.append(C15180gja.c(b((X509Certificate) certificate).d));
|
|
return sb.toString();
|
|
}
|
|
|
|
static giP a(X509Certificate x509Certificate) {
|
|
return Util.sha1(giP.c(x509Certificate.getPublicKey().getEncoded()));
|
|
}
|
|
|
|
static giP b(X509Certificate x509Certificate) {
|
|
return Util.sha256(giP.c(x509Certificate.getPublicKey().getEncoded()));
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes6.dex */
|
|
public static final class a {
|
|
final giP ahg;
|
|
final String hashAlgorithm;
|
|
final String pattern;
|
|
|
|
a(String str, String str2) {
|
|
this.pattern = str;
|
|
if (str2.startsWith("sha1/")) {
|
|
this.hashAlgorithm = "sha1/";
|
|
this.ahg = giP.d(str2.substring(5));
|
|
} else if (str2.startsWith("sha256/")) {
|
|
this.hashAlgorithm = "sha256/";
|
|
this.ahg = giP.d(str2.substring(7));
|
|
} else {
|
|
throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': ".concat(String.valueOf(str2)));
|
|
}
|
|
if (this.ahg == null) {
|
|
throw new IllegalArgumentException("pins must be base64: ".concat(String.valueOf(str2)));
|
|
}
|
|
}
|
|
|
|
final boolean matches(String str) {
|
|
if (this.pattern.equals(str)) {
|
|
return true;
|
|
}
|
|
int indexOf = str.indexOf(46);
|
|
if (this.pattern.startsWith("*.")) {
|
|
if (str.regionMatches(false, indexOf + 1, this.pattern, 2, r6.length() - 2)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public final boolean equals(Object obj) {
|
|
if (obj instanceof a) {
|
|
a aVar = (a) obj;
|
|
if (this.pattern.equals(aVar.pattern) && this.hashAlgorithm.equals(aVar.hashAlgorithm) && this.ahg.equals(aVar.ahg)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public final int hashCode() {
|
|
int hashCode = this.pattern.hashCode();
|
|
return ((((hashCode + 527) * 31) + this.hashAlgorithm.hashCode()) * 31) + this.ahg.hashCode();
|
|
}
|
|
|
|
public final String toString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(this.hashAlgorithm);
|
|
sb.append(C15180gja.c(this.ahg.d));
|
|
return sb.toString();
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static final class Builder {
|
|
private final List<a> ahe;
|
|
private TrustRootIndex ahf;
|
|
|
|
public Builder() {
|
|
this.ahe = new ArrayList();
|
|
}
|
|
|
|
Builder(CertificatePinner certificatePinner) {
|
|
ArrayList arrayList = new ArrayList();
|
|
this.ahe = arrayList;
|
|
arrayList.addAll(certificatePinner.ahe);
|
|
this.ahf = certificatePinner.ahf;
|
|
}
|
|
|
|
public final Builder add(String str, String... strArr) {
|
|
if (str == null) {
|
|
throw new IllegalArgumentException("pattern == null");
|
|
}
|
|
for (String str2 : strArr) {
|
|
this.ahe.add(new a(str, str2));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public final CertificatePinner build() {
|
|
return new CertificatePinner(this);
|
|
}
|
|
|
|
public final Builder trustRootIndex(TrustRootIndex trustRootIndex) {
|
|
this.ahf = trustRootIndex;
|
|
return this;
|
|
}
|
|
}
|
|
}
|