what-the-bank/sources/okhttp3/internal/tls/OkHostnameVerifier.java

195 lines
7.3 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.tls;
import java.security.cert.Certificate;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import o.C14851gaI;
import o.C14881gav;
import o.C14957gcv;
import o.gdZ;
import okhttp3.internal.HostnamesKt;
import okhttp3.internal.Util;
/* loaded from: classes.dex */
public final class OkHostnameVerifier implements HostnameVerifier {
private static final int ALT_DNS_NAME = 2;
private static final int ALT_IPA_NAME = 7;
public static final OkHostnameVerifier INSTANCE = new OkHostnameVerifier();
private OkHostnameVerifier() {
}
@Override // javax.net.ssl.HostnameVerifier
public final boolean verify(String str, SSLSession sSLSession) {
C14957gcv.e(str, "");
C14957gcv.e(sSLSession, "");
if (!isAscii(str)) {
return false;
}
try {
Certificate certificate = sSLSession.getPeerCertificates()[0];
if (certificate != null) {
return verify(str, (X509Certificate) certificate);
}
throw new NullPointerException("null cannot be cast to non-null type java.security.cert.X509Certificate");
} catch (SSLException unused) {
return false;
}
}
public final boolean verify(String str, X509Certificate x509Certificate) {
C14957gcv.e(str, "");
C14957gcv.e(x509Certificate, "");
return Util.canParseAsIpAddress(str) ? verifyIpAddress(str, x509Certificate) : verifyHostname(str, x509Certificate);
}
private final boolean verifyIpAddress(String str, X509Certificate x509Certificate) {
String canonicalHost = HostnamesKt.toCanonicalHost(str);
List<String> subjectAltNames = getSubjectAltNames(x509Certificate, 7);
if ((subjectAltNames instanceof Collection) && subjectAltNames.isEmpty()) {
return false;
}
Iterator<T> it = subjectAltNames.iterator();
while (it.hasNext()) {
if (C14957gcv.b((Object) canonicalHost, (Object) HostnamesKt.toCanonicalHost((String) it.next()))) {
return true;
}
}
return false;
}
private final boolean verifyHostname(String str, X509Certificate x509Certificate) {
String asciiToLowercase = asciiToLowercase(str);
List<String> subjectAltNames = getSubjectAltNames(x509Certificate, 2);
if ((subjectAltNames instanceof Collection) && subjectAltNames.isEmpty()) {
return false;
}
Iterator<T> it = subjectAltNames.iterator();
while (it.hasNext()) {
if (INSTANCE.verifyHostname(asciiToLowercase, (String) it.next())) {
return true;
}
}
return false;
}
private final String asciiToLowercase(String str) {
if (!isAscii(str)) {
return str;
}
Locale locale = Locale.US;
C14957gcv.c(locale, "");
String lowerCase = str.toLowerCase(locale);
C14957gcv.c((Object) lowerCase, "");
return lowerCase;
}
private final boolean isAscii(String str) {
int i;
int length = str.length();
int length2 = str.length();
C14957gcv.e(str, "");
if (length2 < 0) {
StringBuilder sb = new StringBuilder("endIndex < beginIndex: ");
sb.append(length2);
sb.append(" < 0");
throw new IllegalArgumentException(sb.toString().toString());
}
if (length2 > str.length()) {
StringBuilder sb2 = new StringBuilder("endIndex > string.length: ");
sb2.append(length2);
sb2.append(" > ");
sb2.append(str.length());
throw new IllegalArgumentException(sb2.toString().toString());
}
long j = 0;
int i2 = 0;
while (i2 < length2) {
char charAt = str.charAt(i2);
if (charAt < 128) {
j++;
} else {
if (charAt < 2048) {
i = 2;
} else if (charAt < 55296 || charAt > 57343) {
i = 3;
} else {
int i3 = i2 + 1;
char charAt2 = i3 < length2 ? str.charAt(i3) : (char) 0;
if (charAt > 56319 || charAt2 < 56320 || charAt2 > 57343) {
j++;
i2 = i3;
} else {
j += 4;
i2 += 2;
}
}
j += i;
}
i2++;
}
return length == ((int) j);
}
private final boolean verifyHostname(String str, String str2) {
String str3;
String str4 = str;
if (str4 != null && str4.length() != 0 && !gdZ.e(str, ".", false) && !gdZ.d(str, "..", false) && (str3 = str2) != null && str3.length() != 0 && !gdZ.e(str2, ".", false) && !gdZ.d(str2, "..", false)) {
if (!gdZ.d(str, ".", false)) {
str = C14957gcv.c(str, (Object) ".");
}
if (!gdZ.d(str2, ".", false)) {
str2 = C14957gcv.c(str2, (Object) ".");
}
String asciiToLowercase = asciiToLowercase(str2);
String str5 = asciiToLowercase;
if (!gdZ.d((CharSequence) str5, (CharSequence) "*", false)) {
return C14957gcv.b((Object) str, (Object) asciiToLowercase);
}
if (!gdZ.e(asciiToLowercase, "*.", false) || gdZ.a((CharSequence) str5, '*', 1, false) != -1 || str.length() < asciiToLowercase.length() || C14957gcv.b((Object) "*.", (Object) asciiToLowercase)) {
return false;
}
String substring = asciiToLowercase.substring(1);
C14957gcv.c((Object) substring, "");
if (!gdZ.d(str, substring, false)) {
return false;
}
int length = str.length() - substring.length();
return length <= 0 || gdZ.b((CharSequence) str, '.', length - 1) == -1;
}
return false;
}
public final List<String> allSubjectAltNames(X509Certificate x509Certificate) {
C14957gcv.e(x509Certificate, "");
return C14881gav.e(getSubjectAltNames(x509Certificate, 7), getSubjectAltNames(x509Certificate, 2));
}
private final List<String> getSubjectAltNames(X509Certificate x509Certificate, int i) {
Object obj;
try {
Collection<List<?>> subjectAlternativeNames = x509Certificate.getSubjectAlternativeNames();
if (subjectAlternativeNames != null) {
ArrayList arrayList = new ArrayList();
for (List<?> list : subjectAlternativeNames) {
if (list != null && list.size() >= 2 && C14957gcv.b(list.get(0), Integer.valueOf(i)) && (obj = list.get(1)) != null) {
arrayList.add((String) obj);
}
}
return arrayList;
}
return C14851gaI.c;
} catch (CertificateParsingException unused) {
return C14851gaI.c;
}
}
}