package com.android.org.conscrypt;

import android.compat.annotation.UnsupportedAppUsage;
import com.android.org.conscrypt.ExternalSession;
import com.android.org.conscrypt.NativeCrypto;
import com.android.org.conscrypt.NativeRef;
import com.android.org.conscrypt.SSLParametersImpl;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECKey;
import java.security.spec.ECParameterSpec;
import javax.crypto.SecretKey;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLProtocolException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;
import javax.security.auth.x500.X500Principal;

/* JADX INFO: loaded from: classes.dex */
class ConscryptFileDescriptorSocket extends OpenSSLSocketImpl implements NativeCrypto.SSLHandshakeCallbacks, SSLParametersImpl.PSKCallbacks, SSLParametersImpl.AliasChooser {
    private static final boolean DBG_STATE = false;
    private final ActiveSession activeSession;
    private OpenSSLKey channelIdPrivateKey;
    private SessionSnapshot closedSession;
    private final SSLSession externalSession;
    private final Object guard;
    private long handshakeStartedMillis;
    private int handshakeTimeoutMilliseconds;
    private SSLInputStream is;
    private SSLOutputStream os;
    private final NativeSsl ssl;
    private final SSLParametersImpl sslParameters;
    private int state;
    private int writeTimeoutMilliseconds;

    private class SSLOutputStream extends OutputStream {
        private final Object writeLock = new Object();

        SSLOutputStream() {
        }

        void awaitPendingOps() {
            synchronized (this.writeLock) {
            }
        }

        @Override // java.io.OutputStream
        public void write(int i3) throws IOException {
            write(new byte[]{(byte) (i3 & 255)});
        }

        @Override // java.io.OutputStream
        public void write(byte[] bArr, int i3, int i4) throws IOException {
            Platform.blockGuardOnNetwork();
            ConscryptFileDescriptorSocket.this.checkOpen();
            ArrayUtils.checkOffsetAndCount(bArr.length, i3, i4);
            if (i4 == 0) {
                return;
            }
            synchronized (this.writeLock) {
                synchronized (ConscryptFileDescriptorSocket.this.ssl) {
                    if (ConscryptFileDescriptorSocket.this.state == 8) {
                        throw new SocketException("socket is closed");
                    }
                }
                ConscryptFileDescriptorSocket.this.ssl.write(Platform.getFileDescriptor(ConscryptFileDescriptorSocket.this.socket), bArr, i3, i4, ConscryptFileDescriptorSocket.this.writeTimeoutMilliseconds);
                synchronized (ConscryptFileDescriptorSocket.this.ssl) {
                    if (ConscryptFileDescriptorSocket.this.state == 8) {
                        throw new SocketException("socket is closed");
                    }
                }
            }
        }
    }

    ConscryptFileDescriptorSocket(SSLParametersImpl sSLParametersImpl) throws IOException {
        this.state = 0;
        this.guard = Platform.closeGuardGet();
        this.externalSession = Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.1
            @Override // com.android.org.conscrypt.ExternalSession.Provider
            public ConscryptSession provideSession() {
                return ConscryptFileDescriptorSocket.this.provideSession();
            }
        }));
        this.writeTimeoutMilliseconds = 0;
        this.handshakeTimeoutMilliseconds = -1;
        this.sslParameters = sSLParametersImpl;
        NativeSsl nativeSslNewSsl = newSsl(sSLParametersImpl, this);
        this.ssl = nativeSslNewSsl;
        this.activeSession = new ActiveSession(nativeSslNewSsl, sSLParametersImpl.getSessionContext());
    }

    private void assertReadableOrWriteableState() {
        int i3 = this.state;
        if (i3 == 5 || i3 == 4) {
            return;
        }
        throw new AssertionError("Invalid state: " + this.state);
    }

    private ClientSessionContext clientSessionContext() {
        return this.sslParameters.getClientSessionContext();
    }

    private void closeUnderlyingSocket() throws IOException {
        super.close();
    }

    private void free() {
        if (this.ssl.isClosed()) {
            return;
        }
        this.ssl.close();
        Platform.closeGuardClose(this.guard);
    }

    private static NativeSsl newSsl(SSLParametersImpl sSLParametersImpl, ConscryptFileDescriptorSocket conscryptFileDescriptorSocket) throws SSLException {
        return NativeSsl.newInstance(sSLParametersImpl, conscryptFileDescriptorSocket, conscryptFileDescriptorSocket, conscryptFileDescriptorSocket);
    }

    private ConscryptSession provideAfterHandshakeSession() {
        return this.state < 2 ? SSLNullSession.getNullSession() : provideSession();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public ConscryptSession provideHandshakeSession() {
        ConscryptSession nullSession;
        synchronized (this.ssl) {
            int i3 = this.state;
            nullSession = (i3 < 2 || i3 >= 5) ? SSLNullSession.getNullSession() : this.activeSession;
        }
        return nullSession;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0027  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public com.android.org.conscrypt.ConscryptSession provideSession() {
        /*
            r4 = this;
            com.android.org.conscrypt.NativeSsl r0 = r4.ssl
            monitor-enter(r0)
            int r1 = r4.state     // Catch: java.lang.Throwable -> L34
            r2 = 8
            if (r1 != r2) goto L14
            com.android.org.conscrypt.SessionSnapshot r4 = r4.closedSession     // Catch: java.lang.Throwable -> L34
            if (r4 == 0) goto Le
            goto L12
        Le:
            com.android.org.conscrypt.ConscryptSession r4 = com.android.org.conscrypt.SSLNullSession.getNullSession()     // Catch: java.lang.Throwable -> L34
        L12:
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L34
            return r4
        L14:
            r2 = 5
            r3 = 1
            if (r1 < r2) goto L1a
            r1 = r3
            goto L1b
        L1a:
            r1 = 0
        L1b:
            if (r1 != 0) goto L27
            boolean r2 = r4.isConnected()     // Catch: java.io.IOException -> L29 java.lang.Throwable -> L34
            if (r2 == 0) goto L27
            r4.waitForHandshake()     // Catch: java.io.IOException -> L29 java.lang.Throwable -> L34
            goto L28
        L27:
            r3 = r1
        L28:
            r1 = r3
        L29:
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L34
            if (r1 != 0) goto L31
            com.android.org.conscrypt.ConscryptSession r4 = com.android.org.conscrypt.SSLNullSession.getNullSession()
            return r4
        L31:
            com.android.org.conscrypt.ActiveSession r4 = r4.activeSession
            return r4
        L34:
            r4 = move-exception
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L34
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: com.android.org.conscrypt.ConscryptFileDescriptorSocket.provideSession():com.android.org.conscrypt.ConscryptSession");
    }

    private AbstractSessionContext sessionContext() {
        return this.sslParameters.getSessionContext();
    }

    private void shutdownAndFreeSslNative() throws IOException {
        try {
            Platform.blockGuardOnNetwork();
            this.ssl.shutdown(Platform.getFileDescriptor(this.socket));
        } catch (IOException unused) {
        } catch (Throwable th) {
            free();
            closeUnderlyingSocket();
            throw th;
        }
        free();
        closeUnderlyingSocket();
    }

    private void transitionTo(int i3) {
        int i4;
        if (i3 == 8 && !this.ssl.isClosed() && (i4 = this.state) >= 2 && i4 < 8) {
            this.closedSession = new SessionSnapshot(this.activeSession);
        }
        this.state = i3;
    }

    private void waitForHandshake() throws IOException {
        int i3;
        startHandshake();
        synchronized (this.ssl) {
            while (true) {
                i3 = this.state;
                if (i3 == 5 || i3 == 4 || i3 == 8) {
                    break;
                }
                try {
                    this.ssl.wait();
                } catch (InterruptedException e3) {
                    Thread.currentThread().interrupt();
                    throw new IOException("Interrupted waiting for handshake", e3);
                }
            }
            if (i3 == 8) {
                throw new SocketException("Socket is closed");
            }
        }
    }

    @Override // com.android.org.conscrypt.SSLParametersImpl.AliasChooser
    public final String chooseClientAlias(X509KeyManager x509KeyManager, X500Principal[] x500PrincipalArr, String[] strArr) {
        return x509KeyManager.chooseClientAlias(strArr, x500PrincipalArr, this);
    }

    @Override // com.android.org.conscrypt.SSLParametersImpl.PSKCallbacks
    public final String chooseClientPSKIdentity(PSKKeyManager pSKKeyManager, String str) {
        return pSKKeyManager.chooseClientKeyIdentity(str, this);
    }

    @Override // com.android.org.conscrypt.SSLParametersImpl.AliasChooser
    public final String chooseServerAlias(X509KeyManager x509KeyManager, String str) {
        return x509KeyManager.chooseServerAlias(str, null, this);
    }

    @Override // com.android.org.conscrypt.SSLParametersImpl.PSKCallbacks
    public final String chooseServerPSKIdentityHint(PSKKeyManager pSKKeyManager) {
        return pSKKeyManager.chooseServerKeyIdentityHint(this);
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final void clientCertificateRequested(byte[] bArr, int[] iArr, byte[][] bArr2) throws SSLException, CertificateEncodingException {
        this.ssl.chooseClientCertificate(bArr, iArr, bArr2);
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final int clientPSKKeyRequested(String str, byte[] bArr, byte[] bArr2) {
        return this.ssl.clientPSKKeyRequested(str, bArr, bArr2);
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket, java.net.Socket, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        NativeSsl nativeSsl = this.ssl;
        if (nativeSsl == null) {
            return;
        }
        synchronized (nativeSsl) {
            int i3 = this.state;
            if (i3 == 8) {
                return;
            }
            transitionTo(8);
            if (i3 == 0) {
                free();
                closeUnderlyingSocket();
                this.ssl.notifyAll();
                return;
            }
            if (i3 != 5 && i3 != 4) {
                this.ssl.interrupt();
                this.ssl.notifyAll();
                return;
            }
            this.ssl.notifyAll();
            SSLInputStream sSLInputStream = this.is;
            SSLOutputStream sSLOutputStream = this.os;
            if (sSLInputStream != null || sSLOutputStream != null) {
                this.ssl.interrupt();
            }
            if (sSLInputStream != null) {
                sSLInputStream.awaitPendingOps();
            }
            if (sSLOutputStream != null) {
                sSLOutputStream.awaitPendingOps();
            }
            shutdownAndFreeSslNative();
        }
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    byte[] exportKeyingMaterial(String str, byte[] bArr, int i3) throws SSLException {
        synchronized (this.ssl) {
            int i4 = this.state;
            if (i4 >= 3 && i4 != 8) {
                return this.ssl.exportKeyingMaterial(str, bArr, i3);
            }
            return null;
        }
    }

    protected final void finalize() throws Throwable {
        try {
            Object obj = this.guard;
            if (obj != null) {
                Platform.closeGuardWarnIfOpen(obj);
            }
            NativeSsl nativeSsl = this.ssl;
            if (nativeSsl != null) {
                synchronized (nativeSsl) {
                    transitionTo(8);
                }
            }
        } finally {
            super.finalize();
        }
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    final SSLSession getActiveSession() {
        return this.activeSession;
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket, javax.net.ssl.SSLSocket
    public final String getApplicationProtocol() {
        return provideAfterHandshakeSession().getApplicationProtocol();
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    final String[] getApplicationProtocols() {
        return this.sslParameters.getApplicationProtocols();
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    public final byte[] getChannelId() throws SSLException {
        if (getUseClientMode()) {
            throw new IllegalStateException("Client mode");
        }
        synchronized (this.ssl) {
            if (this.state != 5) {
                throw new IllegalStateException("Channel ID is only available after handshake completes");
            }
        }
        return this.ssl.getTlsChannelId();
    }

    @Override // javax.net.ssl.SSLSocket
    public final boolean getEnableSessionCreation() {
        return this.sslParameters.getEnableSessionCreation();
    }

    @Override // javax.net.ssl.SSLSocket
    public final String[] getEnabledCipherSuites() {
        return this.sslParameters.getEnabledCipherSuites();
    }

    @Override // javax.net.ssl.SSLSocket
    public final String[] getEnabledProtocols() {
        return this.sslParameters.getEnabledProtocols();
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket, javax.net.ssl.SSLSocket
    public final String getHandshakeApplicationProtocol() {
        String applicationProtocol;
        synchronized (this.ssl) {
            int i3 = this.state;
            applicationProtocol = (i3 < 2 || i3 >= 5) ? null : getApplicationProtocol();
        }
        return applicationProtocol;
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket, javax.net.ssl.SSLSocket
    public final SSLSession getHandshakeSession() {
        synchronized (this.ssl) {
            int i3 = this.state;
            if (i3 < 2 || i3 >= 5) {
                return null;
            }
            return Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.2
                @Override // com.android.org.conscrypt.ExternalSession.Provider
                public ConscryptSession provideSession() {
                    return ConscryptFileDescriptorSocket.this.provideHandshakeSession();
                }
            }));
        }
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket, java.net.Socket
    public final InputStream getInputStream() throws IOException {
        SSLInputStream sSLInputStream;
        checkOpen();
        synchronized (this.ssl) {
            if (this.state == 8) {
                throw new SocketException("Socket is closed.");
            }
            if (this.is == null) {
                this.is = new SSLInputStream();
            }
            sSLInputStream = this.is;
        }
        waitForHandshake();
        return sSLInputStream;
    }

    @Override // javax.net.ssl.SSLSocket
    public final boolean getNeedClientAuth() {
        return this.sslParameters.getNeedClientAuth();
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket, java.net.Socket
    public final OutputStream getOutputStream() throws IOException {
        SSLOutputStream sSLOutputStream;
        checkOpen();
        synchronized (this.ssl) {
            if (this.state == 8) {
                throw new SocketException("Socket is closed.");
            }
            if (this.os == null) {
                this.os = new SSLOutputStream();
            }
            sSLOutputStream = this.os;
        }
        waitForHandshake();
        return sSLOutputStream;
    }

    @Override // com.android.org.conscrypt.SSLParametersImpl.PSKCallbacks
    public final SecretKey getPSKKey(PSKKeyManager pSKKeyManager, String str, String str2) {
        return pSKKeyManager.getKey(str, str2, this);
    }

    @Override // javax.net.ssl.SSLSocket
    public final SSLParameters getSSLParameters() {
        SSLParameters sSLParameters = super.getSSLParameters();
        Platform.getSSLParameters(sSLParameters, this.sslParameters, this);
        return sSLParameters;
    }

    @Override // javax.net.ssl.SSLSocket
    public final SSLSession getSession() {
        return this.externalSession;
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    public final int getSoWriteTimeout() {
        return this.writeTimeoutMilliseconds;
    }

    @Override // javax.net.ssl.SSLSocket
    public final String[] getSupportedCipherSuites() {
        return NativeCrypto.getSupportedCipherSuites();
    }

    @Override // javax.net.ssl.SSLSocket
    public final String[] getSupportedProtocols() {
        return NativeCrypto.getSupportedProtocols();
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    byte[] getTlsUnique() {
        return this.ssl.getTlsUnique();
    }

    @Override // javax.net.ssl.SSLSocket
    public final boolean getUseClientMode() {
        return this.sslParameters.getUseClientMode();
    }

    @Override // javax.net.ssl.SSLSocket
    public final boolean getWantClientAuth() {
        return this.sslParameters.getWantClientAuth();
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final void onNewSessionEstablished(long j3) {
        try {
            NativeCrypto.SSL_SESSION_up_ref(j3);
            sessionContext().cacheSession(NativeSslSession.newInstance(new NativeRef.SSL_SESSION(j3), this.activeSession));
        } catch (Exception unused) {
        }
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final void onSSLStateChange(int i3, int i4) {
        if (i3 != 32) {
            return;
        }
        Platform.countTlsHandshake(true, this.activeSession.getProtocol(), this.activeSession.getCipherSuite(), Platform.getMillisSinceBoot() - this.handshakeStartedMillis);
        synchronized (this.ssl) {
            if (this.state == 8) {
                return;
            }
            transitionTo(5);
            notifyHandshakeCompletedListeners();
            synchronized (this.ssl) {
                this.ssl.notifyAll();
            }
        }
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public int selectApplicationProtocol(byte[] bArr) {
        ApplicationProtocolSelectorAdapter applicationProtocolSelector = this.sslParameters.getApplicationProtocolSelector();
        if (applicationProtocolSelector == null) {
            return 3;
        }
        return applicationProtocolSelector.selectApplicationProtocol(bArr);
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final void serverCertificateRequested() throws IOException {
        synchronized (this.ssl) {
            this.ssl.configureServerCertificate();
        }
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final int serverPSKKeyRequested(String str, String str2, byte[] bArr) {
        return this.ssl.serverPSKKeyRequested(str, str2, bArr);
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final long serverSessionRequested(byte[] bArr) {
        return 0L;
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    public final void setApplicationProtocolSelector(ApplicationProtocolSelector applicationProtocolSelector) {
        setApplicationProtocolSelector(applicationProtocolSelector == null ? null : new ApplicationProtocolSelectorAdapter(this, applicationProtocolSelector));
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    final void setApplicationProtocols(String[] strArr) {
        this.sslParameters.setApplicationProtocols(strArr);
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    public final void setChannelIdEnabled(boolean z2) {
        if (getUseClientMode()) {
            throw new IllegalStateException("Client mode");
        }
        synchronized (this.ssl) {
            if (this.state != 0) {
                throw new IllegalStateException("Could not enable/disable Channel ID after the initial handshake has begun.");
            }
        }
        this.sslParameters.channelIdEnabled = z2;
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    public final void setChannelIdPrivateKey(PrivateKey privateKey) {
        if (!getUseClientMode()) {
            throw new IllegalStateException("Server mode");
        }
        synchronized (this.ssl) {
            if (this.state != 0) {
                throw new IllegalStateException("Could not change Channel ID private key after the initial handshake has begun.");
            }
        }
        if (privateKey == null) {
            this.sslParameters.channelIdEnabled = DBG_STATE;
            this.channelIdPrivateKey = null;
            return;
        }
        this.sslParameters.channelIdEnabled = true;
        try {
            ECParameterSpec params = privateKey instanceof ECKey ? ((ECKey) privateKey).getParams() : null;
            if (params == null) {
                params = OpenSSLECGroupContext.getCurveByName("prime256v1").getECParameterSpec();
            }
            this.channelIdPrivateKey = OpenSSLKey.fromECPrivateKeyForTLSStackOnly(privateKey, params);
        } catch (InvalidKeyException unused) {
        }
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setEnableSessionCreation(boolean z2) {
        this.sslParameters.setEnableSessionCreation(z2);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setEnabledCipherSuites(String[] strArr) {
        this.sslParameters.setEnabledCipherSuites(strArr);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setEnabledProtocols(String[] strArr) {
        this.sslParameters.setEnabledProtocols(strArr);
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    public final void setHandshakeTimeout(int i3) throws SocketException {
        this.handshakeTimeoutMilliseconds = i3;
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    @UnsupportedAppUsage(maxTargetSdk = 29, publicAlternatives = "Use {@code javax.net.ssl.SSLParameters#setServerNames}.")
    public final void setHostname(String str) {
        this.sslParameters.setUseSni(str != null ? true : DBG_STATE);
        super.setHostname(str);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setNeedClientAuth(boolean z2) {
        this.sslParameters.setNeedClientAuth(z2);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setSSLParameters(SSLParameters sSLParameters) {
        super.setSSLParameters(sSLParameters);
        Platform.setSSLParameters(sSLParameters, this.sslParameters, this);
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    public final void setSoWriteTimeout(int i3) throws SocketException {
        this.writeTimeoutMilliseconds = i3;
        Platform.setSocketWriteTimeout(this, i3);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setUseClientMode(boolean z2) {
        synchronized (this.ssl) {
            if (this.state != 0) {
                throw new IllegalArgumentException("Could not change the mode after the initial handshake has begun.");
            }
        }
        this.sslParameters.setUseClientMode(z2);
    }

    @Override // com.android.org.conscrypt.OpenSSLSocketImpl, com.android.org.conscrypt.AbstractConscryptSocket
    @UnsupportedAppUsage(maxTargetSdk = 29, publicAlternatives = "Use {@link android.net.ssl.SSLSockets#setUseSessionTickets}.")
    public final void setUseSessionTickets(boolean z2) {
        this.sslParameters.setUseSessionTickets(z2);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void setWantClientAuth(boolean z2) {
        this.sslParameters.setWantClientAuth(z2);
    }

    @Override // javax.net.ssl.SSLSocket
    public final void startHandshake() throws IOException {
        NativeSslSession cachedSession;
        checkOpen();
        synchronized (this.ssl) {
            if (this.state == 0) {
                this.handshakeStartedMillis = Platform.getMillisSinceBoot();
                transitionTo(2);
                boolean z2 = true;
                try {
                    try {
                        Platform.closeGuardOpen(this.guard, "close");
                        this.ssl.initialize(getHostname(), this.channelIdPrivateKey);
                        if (getUseClientMode() && (cachedSession = clientSessionContext().getCachedSession(getHostnameOrIP(), getPort(), this.sslParameters)) != null) {
                            cachedSession.offerToResume(this.ssl);
                        }
                        int soTimeout = getSoTimeout();
                        int soWriteTimeout = getSoWriteTimeout();
                        int i3 = this.handshakeTimeoutMilliseconds;
                        if (i3 >= 0) {
                            setSoTimeout(i3);
                            setSoWriteTimeout(this.handshakeTimeoutMilliseconds);
                        }
                        synchronized (this.ssl) {
                            if (this.state == 8) {
                                synchronized (this.ssl) {
                                    transitionTo(8);
                                    this.ssl.notifyAll();
                                }
                                try {
                                    shutdownAndFreeSslNative();
                                    return;
                                } catch (IOException unused) {
                                    return;
                                }
                            }
                            try {
                                this.ssl.doHandshake(Platform.getFileDescriptor(this.socket), getSoTimeout());
                                this.activeSession.onPeerCertificateAvailable(getHostnameOrIP(), getPort());
                                synchronized (this.ssl) {
                                    if (this.state == 8) {
                                        synchronized (this.ssl) {
                                            transitionTo(8);
                                            this.ssl.notifyAll();
                                        }
                                        try {
                                            shutdownAndFreeSslNative();
                                            return;
                                        } catch (IOException unused2) {
                                            return;
                                        }
                                    }
                                    if (this.handshakeTimeoutMilliseconds >= 0) {
                                        setSoTimeout(soTimeout);
                                        setSoWriteTimeout(soWriteTimeout);
                                    }
                                    synchronized (this.ssl) {
                                        int i4 = this.state;
                                        if (i4 != 8) {
                                            z2 = false;
                                        }
                                        if (i4 == 2) {
                                            transitionTo(4);
                                        } else {
                                            transitionTo(5);
                                        }
                                        if (!z2) {
                                            this.ssl.notifyAll();
                                        }
                                    }
                                    if (z2) {
                                        synchronized (this.ssl) {
                                            transitionTo(8);
                                            this.ssl.notifyAll();
                                        }
                                        try {
                                            shutdownAndFreeSslNative();
                                        } catch (IOException unused3) {
                                        }
                                    }
                                }
                            } catch (CertificateException e3) {
                                Platform.countTlsHandshake(DBG_STATE, this.activeSession.getProtocol(), this.activeSession.getCipherSuite(), Platform.getMillisSinceBoot() - this.handshakeStartedMillis);
                                SSLHandshakeException sSLHandshakeException = new SSLHandshakeException(e3.getMessage());
                                sSLHandshakeException.initCause(e3);
                                throw sSLHandshakeException;
                            } catch (SSLException e4) {
                                synchronized (this.ssl) {
                                    if (this.state != 8) {
                                        if (e4.getMessage().contains("unexpected CCS")) {
                                            Platform.logEvent(String.format("ssl_unexpected_ccs: host=%s", getHostnameOrIP()));
                                        }
                                        throw e4;
                                    }
                                    synchronized (this.ssl) {
                                        transitionTo(8);
                                        this.ssl.notifyAll();
                                        try {
                                            shutdownAndFreeSslNative();
                                        } catch (IOException unused4) {
                                        }
                                    }
                                }
                            }
                        }
                    } catch (SSLProtocolException e5) {
                        Platform.countTlsHandshake(DBG_STATE, this.activeSession.getProtocol(), this.activeSession.getCipherSuite(), Platform.getMillisSinceBoot() - this.handshakeStartedMillis);
                        throw ((SSLHandshakeException) new SSLHandshakeException("Handshake failed").initCause(e5));
                    }
                } catch (Throwable th) {
                    if (1 != 0) {
                        synchronized (this.ssl) {
                            transitionTo(8);
                            this.ssl.notifyAll();
                            try {
                                shutdownAndFreeSslNative();
                            } catch (IOException unused5) {
                            }
                        }
                    }
                    throw th;
                }
            }
        }
    }

    @Override // com.android.org.conscrypt.NativeCrypto.SSLHandshakeCallbacks
    public final void verifyCertificateChain(byte[][] bArr, String str) throws CertificateException {
        if (bArr != null) {
            try {
                if (bArr.length != 0) {
                    X509Certificate[] x509CertificateArrDecodeX509CertificateChain = SSLUtils.decodeX509CertificateChain(bArr);
                    X509TrustManager x509TrustManager = this.sslParameters.getX509TrustManager();
                    if (x509TrustManager == null) {
                        throw new CertificateException("No X.509 TrustManager");
                    }
                    this.activeSession.onPeerCertificatesReceived(getHostnameOrIP(), getPort(), x509CertificateArrDecodeX509CertificateChain);
                    if (getUseClientMode()) {
                        Platform.checkServerTrusted(x509TrustManager, x509CertificateArrDecodeX509CertificateChain, str, this);
                        return;
                    } else {
                        Platform.checkClientTrusted(x509TrustManager, x509CertificateArrDecodeX509CertificateChain, x509CertificateArrDecodeX509CertificateChain[0].getPublicKey().getAlgorithm(), this);
                        return;
                    }
                }
            } catch (CertificateException e3) {
                throw e3;
            } catch (Exception e4) {
                throw new CertificateException(e4);
            }
        }
        throw new CertificateException("Peer sent no certificate");
    }

    private class SSLInputStream extends InputStream {
        private final Object readLock = new Object();

        SSLInputStream() {
        }

        @Override // java.io.InputStream
        public int available() {
            return ConscryptFileDescriptorSocket.this.ssl.getPendingReadableBytes();
        }

        void awaitPendingOps() {
            synchronized (this.readLock) {
            }
        }

        @Override // java.io.InputStream
        public int read() throws IOException {
            byte[] bArr = new byte[1];
            if (read(bArr, 0, 1) != -1) {
                return bArr[0] & 255;
            }
            return -1;
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i3, int i4) throws IOException {
            int i5;
            Platform.blockGuardOnNetwork();
            ConscryptFileDescriptorSocket.this.checkOpen();
            ArrayUtils.checkOffsetAndCount(bArr.length, i3, i4);
            if (i4 == 0) {
                return 0;
            }
            synchronized (this.readLock) {
                synchronized (ConscryptFileDescriptorSocket.this.ssl) {
                    if (ConscryptFileDescriptorSocket.this.state == 8) {
                        throw new SocketException("socket is closed");
                    }
                }
                i5 = ConscryptFileDescriptorSocket.this.ssl.read(Platform.getFileDescriptor(ConscryptFileDescriptorSocket.this.socket), bArr, i3, i4, ConscryptFileDescriptorSocket.this.getSoTimeout());
                if (i5 == -1) {
                    synchronized (ConscryptFileDescriptorSocket.this.ssl) {
                        if (ConscryptFileDescriptorSocket.this.state == 8) {
                            throw new SocketException("socket is closed");
                        }
                    }
                }
            }
            return i5;
        }
    }

    @Override // com.android.org.conscrypt.AbstractConscryptSocket
    final void setApplicationProtocolSelector(ApplicationProtocolSelectorAdapter applicationProtocolSelectorAdapter) {
        this.sslParameters.setApplicationProtocolSelector(applicationProtocolSelectorAdapter);
    }

    ConscryptFileDescriptorSocket(String str, int i3, SSLParametersImpl sSLParametersImpl) throws IOException {
        super(str, i3);
        this.state = 0;
        this.guard = Platform.closeGuardGet();
        this.externalSession = Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.1
            @Override // com.android.org.conscrypt.ExternalSession.Provider
            public ConscryptSession provideSession() {
                return ConscryptFileDescriptorSocket.this.provideSession();
            }
        }));
        this.writeTimeoutMilliseconds = 0;
        this.handshakeTimeoutMilliseconds = -1;
        this.sslParameters = sSLParametersImpl;
        NativeSsl nativeSslNewSsl = newSsl(sSLParametersImpl, this);
        this.ssl = nativeSslNewSsl;
        this.activeSession = new ActiveSession(nativeSslNewSsl, sSLParametersImpl.getSessionContext());
    }

    ConscryptFileDescriptorSocket(InetAddress inetAddress, int i3, SSLParametersImpl sSLParametersImpl) throws IOException {
        super(inetAddress, i3);
        this.state = 0;
        this.guard = Platform.closeGuardGet();
        this.externalSession = Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.1
            @Override // com.android.org.conscrypt.ExternalSession.Provider
            public ConscryptSession provideSession() {
                return ConscryptFileDescriptorSocket.this.provideSession();
            }
        }));
        this.writeTimeoutMilliseconds = 0;
        this.handshakeTimeoutMilliseconds = -1;
        this.sslParameters = sSLParametersImpl;
        NativeSsl nativeSslNewSsl = newSsl(sSLParametersImpl, this);
        this.ssl = nativeSslNewSsl;
        this.activeSession = new ActiveSession(nativeSslNewSsl, sSLParametersImpl.getSessionContext());
    }

    ConscryptFileDescriptorSocket(String str, int i3, InetAddress inetAddress, int i4, SSLParametersImpl sSLParametersImpl) throws IOException {
        super(str, i3, inetAddress, i4);
        this.state = 0;
        this.guard = Platform.closeGuardGet();
        this.externalSession = Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.1
            @Override // com.android.org.conscrypt.ExternalSession.Provider
            public ConscryptSession provideSession() {
                return ConscryptFileDescriptorSocket.this.provideSession();
            }
        }));
        this.writeTimeoutMilliseconds = 0;
        this.handshakeTimeoutMilliseconds = -1;
        this.sslParameters = sSLParametersImpl;
        NativeSsl nativeSslNewSsl = newSsl(sSLParametersImpl, this);
        this.ssl = nativeSslNewSsl;
        this.activeSession = new ActiveSession(nativeSslNewSsl, sSLParametersImpl.getSessionContext());
    }

    ConscryptFileDescriptorSocket(InetAddress inetAddress, int i3, InetAddress inetAddress2, int i4, SSLParametersImpl sSLParametersImpl) throws IOException {
        super(inetAddress, i3, inetAddress2, i4);
        this.state = 0;
        this.guard = Platform.closeGuardGet();
        this.externalSession = Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.1
            @Override // com.android.org.conscrypt.ExternalSession.Provider
            public ConscryptSession provideSession() {
                return ConscryptFileDescriptorSocket.this.provideSession();
            }
        }));
        this.writeTimeoutMilliseconds = 0;
        this.handshakeTimeoutMilliseconds = -1;
        this.sslParameters = sSLParametersImpl;
        NativeSsl nativeSslNewSsl = newSsl(sSLParametersImpl, this);
        this.ssl = nativeSslNewSsl;
        this.activeSession = new ActiveSession(nativeSslNewSsl, sSLParametersImpl.getSessionContext());
    }

    ConscryptFileDescriptorSocket(Socket socket, String str, int i3, boolean z2, SSLParametersImpl sSLParametersImpl) throws IOException {
        super(socket, str, i3, z2);
        this.state = 0;
        this.guard = Platform.closeGuardGet();
        this.externalSession = Platform.wrapSSLSession(new ExternalSession(new ExternalSession.Provider() { // from class: com.android.org.conscrypt.ConscryptFileDescriptorSocket.1
            @Override // com.android.org.conscrypt.ExternalSession.Provider
            public ConscryptSession provideSession() {
                return ConscryptFileDescriptorSocket.this.provideSession();
            }
        }));
        this.writeTimeoutMilliseconds = 0;
        this.handshakeTimeoutMilliseconds = -1;
        this.sslParameters = sSLParametersImpl;
        NativeSsl nativeSslNewSsl = newSsl(sSLParametersImpl, this);
        this.ssl = nativeSslNewSsl;
        this.activeSession = new ActiveSession(nativeSslNewSsl, sSLParametersImpl.getSessionContext());
    }
}
