what-the-bank/sources/io/grpc/okhttp/OkHttpTlsUpgrader.java

50 lines
2.2 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.okhttp;
import com.google.common.base.Preconditions;
import io.grpc.okhttp.internal.ConnectionSpec;
import io.grpc.okhttp.internal.OkHostnameVerifier;
import io.grpc.okhttp.internal.Protocol;
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/* loaded from: classes6.dex */
final class OkHttpTlsUpgrader {
static final List<Protocol> TLS_PROTOCOLS = Collections.unmodifiableList(Arrays.asList(Protocol.HTTP_2));
OkHttpTlsUpgrader() {
}
public static SSLSocket upgrade(SSLSocketFactory sSLSocketFactory, HostnameVerifier hostnameVerifier, Socket socket, String str, int i, ConnectionSpec connectionSpec) throws IOException {
Preconditions.checkNotNull(sSLSocketFactory, "sslSocketFactory");
Preconditions.checkNotNull(socket, "socket");
Preconditions.checkNotNull(connectionSpec, "spec");
SSLSocket sSLSocket = (SSLSocket) sSLSocketFactory.createSocket(socket, str, i, true);
connectionSpec.apply(sSLSocket, false);
String negotiate = OkHttpProtocolNegotiator.get().negotiate(sSLSocket, str, connectionSpec.supportsTlsExtensions() ? TLS_PROTOCOLS : null);
List<Protocol> list = TLS_PROTOCOLS;
boolean contains = list.contains(Protocol.get(negotiate));
StringBuilder sb = new StringBuilder("Only ");
sb.append(list);
sb.append(" are supported, but negotiated protocol is %s");
Preconditions.checkState(contains, sb.toString(), negotiate);
if (hostnameVerifier == null) {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
if (hostnameVerifier.verify(canonicalizeHost(str), sSLSocket.getSession())) {
return sSLSocket;
}
throw new SSLPeerUnverifiedException("Cannot verify hostname: ".concat(String.valueOf(str)));
}
static String canonicalizeHost(String str) {
return (str.startsWith("[") && str.endsWith("]")) ? str.substring(1, str.length() - 1) : str;
}
}