package n4;

import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/* JADX INFO: loaded from: classes3.dex */
public abstract class t {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final List f21766a = Collections.unmodifiableList(Arrays.asList(o4.m.HTTP_2));

    public static SSLSocket a(SSLSocketFactory sSLSocketFactory, Socket socket, String str, int i6, o4.c cVar) throws IOException {
        o4.m mVar;
        A4.g.o(sSLSocketFactory, "sslSocketFactory");
        A4.g.o(socket, "socket");
        A4.g.o(cVar, "spec");
        SSLSocket sSLSocket = (SSLSocket) sSLSocketFactory.createSocket(socket, str, i6, true);
        String[] strArr = cVar.f21836b;
        String[] strArr2 = strArr != null ? (String[]) o4.o.a(strArr, sSLSocket.getEnabledCipherSuites()) : null;
        String[] strArr3 = (String[]) o4.o.a(cVar.f21837c, sSLSocket.getEnabledProtocols());
        o4.b bVar = new o4.b(cVar);
        if (!bVar.f21830a) {
            throw new IllegalStateException("no cipher suites for cleartext connections");
        }
        if (strArr2 == null) {
            bVar.f21831b = null;
        } else {
            bVar.f21831b = (String[]) strArr2.clone();
        }
        if (!bVar.f21830a) {
            throw new IllegalStateException("no TLS versions for cleartext connections");
        }
        if (strArr3 == null) {
            bVar.f21832c = null;
        } else {
            bVar.f21832c = (String[]) strArr3.clone();
        }
        o4.c cVar2 = new o4.c(bVar);
        sSLSocket.setEnabledProtocols(cVar2.f21837c);
        String[] strArr4 = cVar2.f21836b;
        if (strArr4 != null) {
            sSLSocket.setEnabledCipherSuites(strArr4);
        }
        q qVar = q.f21763c;
        boolean z6 = cVar.f21838d;
        List list = f21766a;
        String strD = qVar.d(sSLSocket, str, z6 ? list : null);
        if (strD.equals("http/1.0")) {
            mVar = o4.m.HTTP_1_0;
        } else if (strD.equals("http/1.1")) {
            mVar = o4.m.HTTP_1_1;
        } else if (strD.equals("h2")) {
            mVar = o4.m.HTTP_2;
        } else {
            if (!strD.equals("spdy/3.1")) {
                throw new IOException("Unexpected protocol: ".concat(strD));
            }
            mVar = o4.m.SPDY_3;
        }
        A4.g.r(strD, "Only " + list + " are supported, but negotiated protocol is %s", list.contains(mVar));
        if (o4.e.f21846a.verify((str.startsWith("[") && str.endsWith("]")) ? str.substring(1, str.length() - 1) : str, sSLSocket.getSession())) {
            return sSLSocket;
        }
        throw new SSLPeerUnverifiedException("Cannot verify hostname: ".concat(str));
    }
}
