Package org.conscrypt

Class AbstractConscryptSocket

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    OpenSSLSocketImpl

    abstract class AbstractConscryptSocket
    extends javax.net.ssl.SSLSocket
    Abstract base class for all Conscrypt SSLSocket classes.
    • Field Detail

      • socket

        final java.net.Socket socket
      • autoClose

        private final boolean autoClose
      • peerHostname

        private java.lang.String peerHostname
        The peer's DNS hostname if it was supplied during creation. Note that this may be a raw IP address, so it should be checked before use with extensions that don't use it like Server Name Indication (SNI).
      • peerPort

        private final int peerPort
        The peer's port if it was supplied during creation. Should only be set if peerHostname is also set.
      • listeners

        private final java.util.List<javax.net.ssl.HandshakeCompletedListener> listeners
      • readTimeoutMilliseconds

        private int readTimeoutMilliseconds
        Local cache of timeout to avoid getsockopt on every read and write for non-wrapped sockets. Note that this is not used when delegating to another socket.
    • Constructor Detail

      • AbstractConscryptSocket

        AbstractConscryptSocket()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • AbstractConscryptSocket

        AbstractConscryptSocket​(java.lang.String hostname,
                                int port)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • AbstractConscryptSocket

        AbstractConscryptSocket​(java.net.InetAddress address,
                                int port)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • AbstractConscryptSocket

        AbstractConscryptSocket​(java.lang.String hostname,
                                int port,
                                java.net.InetAddress clientAddress,
                                int clientPort)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • AbstractConscryptSocket

        AbstractConscryptSocket​(java.net.InetAddress address,
                                int port,
                                java.net.InetAddress clientAddress,
                                int clientPort)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • AbstractConscryptSocket

        AbstractConscryptSocket​(java.net.Socket socket,
                                java.lang.String hostname,
                                int port,
                                boolean autoClose)
                         throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • connect

        public final void connect​(java.net.SocketAddress endpoint)
                           throws java.io.IOException
        Overrides:
        connect in class java.net.Socket
        Throws:
        java.io.IOException
      • connect

        public final void connect​(java.net.SocketAddress endpoint,
                                  int timeout)
                           throws java.io.IOException
        Try to extract the peer's hostname if it's available from the endpoint address.
        Overrides:
        connect in class java.net.Socket
        Throws:
        java.io.IOException
      • bind

        public void bind​(java.net.SocketAddress bindpoint)
                  throws java.io.IOException
        Overrides:
        bind in class java.net.Socket
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.net.Socket
        Throws:
        java.io.IOException
      • getInetAddress

        public java.net.InetAddress getInetAddress()
        Overrides:
        getInetAddress in class java.net.Socket
      • getLocalAddress

        public java.net.InetAddress getLocalAddress()
        Overrides:
        getLocalAddress in class java.net.Socket
      • getLocalPort

        public int getLocalPort()
        Overrides:
        getLocalPort in class java.net.Socket
      • getRemoteSocketAddress

        public java.net.SocketAddress getRemoteSocketAddress()
        Overrides:
        getRemoteSocketAddress in class java.net.Socket
      • getLocalSocketAddress

        public java.net.SocketAddress getLocalSocketAddress()
        Overrides:
        getLocalSocketAddress in class java.net.Socket
      • getPort

        public final int getPort()
        Overrides:
        getPort in class java.net.Socket
      • addHandshakeCompletedListener

        public void addHandshakeCompletedListener​(javax.net.ssl.HandshakeCompletedListener listener)
        Specified by:
        addHandshakeCompletedListener in class javax.net.ssl.SSLSocket
      • removeHandshakeCompletedListener

        public void removeHandshakeCompletedListener​(javax.net.ssl.HandshakeCompletedListener listener)
        Specified by:
        removeHandshakeCompletedListener in class javax.net.ssl.SSLSocket
      • getFileDescriptor$

        public java.io.FileDescriptor getFileDescriptor$()
      • setSoTimeout

        public final void setSoTimeout​(int readTimeoutMilliseconds)
                                throws java.net.SocketException
        Overrides:
        setSoTimeout in class java.net.Socket
        Throws:
        java.net.SocketException
      • getSoTimeout

        public final int getSoTimeout()
                               throws java.net.SocketException
        Overrides:
        getSoTimeout in class java.net.Socket
        Throws:
        java.net.SocketException
      • sendUrgentData

        public final void sendUrgentData​(int data)
                                  throws java.io.IOException
        Overrides:
        sendUrgentData in class java.net.Socket
        Throws:
        java.io.IOException
      • setOOBInline

        public final void setOOBInline​(boolean on)
                                throws java.net.SocketException
        Overrides:
        setOOBInline in class java.net.Socket
        Throws:
        java.net.SocketException
      • getOOBInline

        public boolean getOOBInline()
                             throws java.net.SocketException
        Overrides:
        getOOBInline in class java.net.Socket
        Throws:
        java.net.SocketException
      • getChannel

        public java.nio.channels.SocketChannel getChannel()
        Overrides:
        getChannel in class java.net.Socket
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Overrides:
        getInputStream in class java.net.Socket
        Throws:
        java.io.IOException
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        Overrides:
        getOutputStream in class java.net.Socket
        Throws:
        java.io.IOException
      • setTcpNoDelay

        public void setTcpNoDelay​(boolean on)
                           throws java.net.SocketException
        Overrides:
        setTcpNoDelay in class java.net.Socket
        Throws:
        java.net.SocketException
      • getTcpNoDelay

        public boolean getTcpNoDelay()
                              throws java.net.SocketException
        Overrides:
        getTcpNoDelay in class java.net.Socket
        Throws:
        java.net.SocketException
      • setSoLinger

        public void setSoLinger​(boolean on,
                                int linger)
                         throws java.net.SocketException
        Overrides:
        setSoLinger in class java.net.Socket
        Throws:
        java.net.SocketException
      • getSoLinger

        public int getSoLinger()
                        throws java.net.SocketException
        Overrides:
        getSoLinger in class java.net.Socket
        Throws:
        java.net.SocketException
      • setSendBufferSize

        public void setSendBufferSize​(int size)
                               throws java.net.SocketException
        Overrides:
        setSendBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • getSendBufferSize

        public int getSendBufferSize()
                              throws java.net.SocketException
        Overrides:
        getSendBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • setReceiveBufferSize

        public void setReceiveBufferSize​(int size)
                                  throws java.net.SocketException
        Overrides:
        setReceiveBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • getReceiveBufferSize

        public int getReceiveBufferSize()
                                 throws java.net.SocketException
        Overrides:
        getReceiveBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • setKeepAlive

        public void setKeepAlive​(boolean on)
                          throws java.net.SocketException
        Overrides:
        setKeepAlive in class java.net.Socket
        Throws:
        java.net.SocketException
      • getKeepAlive

        public boolean getKeepAlive()
                             throws java.net.SocketException
        Overrides:
        getKeepAlive in class java.net.Socket
        Throws:
        java.net.SocketException
      • setTrafficClass

        public void setTrafficClass​(int tc)
                             throws java.net.SocketException
        Overrides:
        setTrafficClass in class java.net.Socket
        Throws:
        java.net.SocketException
      • getTrafficClass

        public int getTrafficClass()
                            throws java.net.SocketException
        Overrides:
        getTrafficClass in class java.net.Socket
        Throws:
        java.net.SocketException
      • setReuseAddress

        public void setReuseAddress​(boolean on)
                             throws java.net.SocketException
        Overrides:
        setReuseAddress in class java.net.Socket
        Throws:
        java.net.SocketException
      • getReuseAddress

        public boolean getReuseAddress()
                                throws java.net.SocketException
        Overrides:
        getReuseAddress in class java.net.Socket
        Throws:
        java.net.SocketException
      • shutdownInput

        public void shutdownInput()
                           throws java.io.IOException
        Overrides:
        shutdownInput in class java.net.Socket
        Throws:
        java.io.IOException
      • shutdownOutput

        public void shutdownOutput()
                            throws java.io.IOException
        Overrides:
        shutdownOutput in class java.net.Socket
        Throws:
        java.io.IOException
      • isConnected

        public boolean isConnected()
        Overrides:
        isConnected in class java.net.Socket
      • isBound

        public boolean isBound()
        Overrides:
        isBound in class java.net.Socket
      • isClosed

        public boolean isClosed()
        Overrides:
        isClosed in class java.net.Socket
      • isInputShutdown

        public boolean isInputShutdown()
        Overrides:
        isInputShutdown in class java.net.Socket
      • isOutputShutdown

        public boolean isOutputShutdown()
        Overrides:
        isOutputShutdown in class java.net.Socket
      • setPerformancePreferences

        public void setPerformancePreferences​(int connectionTime,
                                              int latency,
                                              int bandwidth)
        Overrides:
        setPerformancePreferences in class java.net.Socket
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.net.Socket
      • getHostname

        java.lang.String getHostname()
        Returns the hostname that was supplied during socket creation. No DNS resolution is attempted before returning the hostname.
      • setHostname

        void setHostname​(java.lang.String hostname)
        This method enables Server Name Indication
        Parameters:
        hostname - the desired SNI hostname, or null to disable
      • getHostnameOrIP

        java.lang.String getHostnameOrIP()
        For the purposes of an SSLSession, we want a way to represent the supplied hostname or the IP address in a textual representation. We do not want to perform reverse DNS lookups on this address.
      • setSoWriteTimeout

        void setSoWriteTimeout​(int writeTimeoutMilliseconds)
                        throws java.net.SocketException
        Note write timeouts are not part of the javax.net.ssl.SSLSocket API
        Throws:
        java.net.SocketException
      • getSoWriteTimeout

        int getSoWriteTimeout()
                       throws java.net.SocketException
        Note write timeouts are not part of the javax.net.ssl.SSLSocket API
        Throws:
        java.net.SocketException
      • setHandshakeTimeout

        void setHandshakeTimeout​(int handshakeTimeoutMilliseconds)
                          throws java.net.SocketException
        Set the handshake timeout on this socket. This timeout is specified in milliseconds and will be used only during the handshake process.
        Throws:
        java.net.SocketException
      • checkOpen

        final void checkOpen()
                      throws java.net.SocketException
        Throws:
        java.net.SocketException
      • getActiveSession

        abstract javax.net.ssl.SSLSession getActiveSession()
        Called by notifyHandshakeCompletedListeners() to get the currently active session. Unlike SSLSocket.getSession(), this method must not block.
      • notifyHandshakeCompletedListeners

        final void notifyHandshakeCompletedListeners()
      • isDelegating

        private boolean isDelegating()
      • getHandshakeSession

        public abstract javax.net.ssl.SSLSession getHandshakeSession()
        Overrides:
        getHandshakeSession in class javax.net.ssl.SSLSocket
      • setUseSessionTickets

        abstract void setUseSessionTickets​(boolean useSessionTickets)
        This method enables session ticket support.
        Parameters:
        useSessionTickets - True to enable session tickets
      • setChannelIdEnabled

        abstract void setChannelIdEnabled​(boolean enabled)
        Enables/disables TLS Channel ID for this server socket.

        This method needs to be invoked before the handshake starts.

        Throws:
        java.lang.IllegalStateException - if this is a client socket or if the handshake has already started.
      • getChannelId

        abstract byte[] getChannelId()
                              throws javax.net.ssl.SSLException
        Gets the TLS Channel ID for this server socket. Channel ID is only available once the handshake completes.
        Returns:
        channel ID or null if not available.
        Throws:
        java.lang.IllegalStateException - if this is a client socket or if the handshake has not yet completed.
        javax.net.ssl.SSLException - if channel ID is available but could not be obtained.
      • setChannelIdPrivateKey

        abstract void setChannelIdPrivateKey​(java.security.PrivateKey privateKey)
        Sets the PrivateKey to be used for TLS Channel ID by this client socket.

        This method needs to be invoked before the handshake starts.

        Parameters:
        privateKey - private key (enables TLS Channel ID) or null for no key (disables TLS Channel ID). The private key must be an Elliptic Curve (EC) key based on the NIST P-256 curve (aka SECG secp256r1 or ANSI X9.62 prime256v1).
        Throws:
        java.lang.IllegalStateException - if this is a server socket or if the handshake has already started.
      • getNpnSelectedProtocol

        @Deprecated
        byte[] getNpnSelectedProtocol()
        Deprecated.
        NPN is not supported
        Returns null always for backward compatibility.
      • setNpnProtocols

        @Deprecated
        void setNpnProtocols​(byte[] npnProtocols)
        Deprecated.
        NPN is not supported
        This method does nothing and is kept for backward compatibility.
      • getAlpnSelectedProtocol

        @Deprecated
        abstract byte[] getAlpnSelectedProtocol()
        Deprecated.
        Returns the protocol agreed upon by client and server, or null if no protocol was agreed upon.
      • setAlpnProtocols

        @Deprecated
        abstract void setAlpnProtocols​(java.lang.String[] alpnProtocols)
        Deprecated.
        Sets the list of ALPN protocols. This method internally converts the protocols to their wire-format form.
        Parameters:
        alpnProtocols - the list of ALPN protocols
      • setAlpnProtocols

        @Deprecated
        abstract void setAlpnProtocols​(byte[] alpnProtocols)
        Deprecated.
        Alternate version of setAlpnProtocols(String[]) that directly sets the list of ALPN in the wire-format form used by BoringSSL (length-prefixed 8-bit strings). Requires that all strings be encoded with US-ASCII.
        Parameters:
        alpnProtocols - the encoded form of the ALPN protocol list
      • setApplicationProtocols

        abstract void setApplicationProtocols​(java.lang.String[] protocols)
        Sets the list of ALPN protocols.
        Parameters:
        protocols - the list of ALPN protocols
      • getApplicationProtocols

        abstract java.lang.String[] getApplicationProtocols()
        Returns the list of supported ALPN protocols.
      • getApplicationProtocol

        public abstract java.lang.String getApplicationProtocol()
        Overrides:
        getApplicationProtocol in class javax.net.ssl.SSLSocket
      • getHandshakeApplicationProtocol

        public abstract java.lang.String getHandshakeApplicationProtocol()
        Overrides:
        getHandshakeApplicationProtocol in class javax.net.ssl.SSLSocket
      • getTlsUnique

        abstract byte[] getTlsUnique()
        Returns the tls-unique channel binding value for this connection, per RFC 5929. This will return null if there is no such value available, such as if the handshake has not yet completed or this connection is closed.
      • exportKeyingMaterial

        abstract byte[] exportKeyingMaterial​(java.lang.String label,
                                             byte[] context,
                                             int length)
                                      throws javax.net.ssl.SSLException
        Exports a value derived from the TLS master secret as described in RFC 5705.
        Parameters:
        label - the label to use in calculating the exported value. This must be an ASCII-only string.
        context - the application-specific context value to use in calculating the exported value. This may be null to use no application context, which is treated differently than an empty byte array.
        length - the number of bytes of keying material to return.
        Returns:
        a value of the specified length, or null if the handshake has not yet completed or the connection has been closed.
        Throws:
        javax.net.ssl.SSLException - if the value could not be exported.