Class WolfSSLSocket

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class WolfSSLSocket
    extends javax.net.ssl.SSLSocket
    wolfSSL implementation of SSLSocket
    Author:
    wolfSSL
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addHandshakeCompletedListener​(javax.net.ssl.HandshakeCompletedListener listener)
      Registers a HandshakeCompletedListener with this SSLSocket.
      void close()
      Closes this SSLSocket.
      void connect​(java.net.SocketAddress endpoint)
      Connects the underlying Socket associated with this SSLSocket.
      void connect​(java.net.SocketAddress endpoint, int timeout)
      Connects the underlying Socket associated with this SSLSocket.
      protected void finalize()  
      byte[] getAlpnSelectedProtocol()
      Return ALPN protocol established for this session.
      java.lang.String[] getEnabledCipherSuites()
      Returns array of enabled cipher suites for this Socket.
      java.lang.String[] getEnabledProtocols()
      Returns SSL/TLS protocols enabled for this SSLSocket.
      boolean getEnableSessionCreation()
      Returns whether this SSLSocket can create new sessions.
      java.io.InputStream getInputStream()
      Return the InputStream associated with this SSLSocket.
      boolean getNeedClientAuth()
      Return if mandatory client authentication is set for this SSLSocket.
      java.io.OutputStream getOutputStream()
      Return the OutputStream associated with this SSLSocket.
      javax.net.ssl.SSLSession getSession()
      Returns the SSLSession in use by this SSLSocket.
      int getSoTimeout()
      Get the SO_TIMEOUT value, in milliseconds.
      java.lang.String[] getSupportedCipherSuites()
      Returns the supported cipher suite list for this socket, and that have been compiled into native wolfSSL library.
      java.lang.String[] getSupportedProtocols()
      Returns array of protocols supported by this SSLSocket.
      boolean getUseClientMode()
      Return the client mode of this SSLSocket.
      boolean getWantClientAuth()
      Returns true if SSLSocket will request client authentication.
      void removeHandshakeCompletedListener​(javax.net.ssl.HandshakeCompletedListener listener)
      Removes a registered HandshakeCompletedListener from this SSLSocket.
      void setAlpnProtocols​(byte[] alpnProtos)
      Set ALPN extension protocol for this session.
      void setEnabledCipherSuites​(java.lang.String[] suites)
      Sets the cipher suites enabled for this SSLSocket.
      void setEnabledProtocols​(java.lang.String[] protocols)
      Sets the SSL/TLS protocols enabled on this SSLSocket.
      void setEnableSessionCreation​(boolean flag)
      Enables this SSLSocket to create new sessions.
      void setNeedClientAuth​(boolean need)
      Configures the SSLSocket to require client authentication.
      void setSoTimeout​(int timeout)
      Set the SO_TIMEOUT with specified timeout in milliseconds.
      void setSSLParameters​(javax.net.ssl.SSLParameters params)
      Set the SSLParameters for this SSLSocket.
      void setUseClientMode​(boolean mode)
      Sets the SSLSocket to use client or server mode.
      void setUseSessionTickets​(boolean useTickets)
      Enables use of session tickets with this session.
      void setWantClientAuth​(boolean want)
      Configures the SSLSocket to request client authentication, but not require it.
      void startHandshake()
      Begins the SSL/TLS handshake on this SSLSocket.
      • Methods inherited from class javax.net.ssl.SSLSocket

        getApplicationProtocol, getHandshakeApplicationProtocol, getHandshakeApplicationProtocolSelector, getHandshakeSession, getSSLParameters, setHandshakeApplicationProtocolSelector
      • Methods inherited from class java.net.Socket

        bind, getChannel, getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getOption, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoLinger, getTcpNoDelay, getTrafficClass, isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setOption, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, supportedOptions, toString
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • handshakeInitCalled

        protected volatile boolean handshakeInitCalled
      • handshakeComplete

        protected volatile boolean handshakeComplete
      • connectionClosed

        protected volatile boolean connectionClosed
    • Constructor Detail

      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode,
                             java.net.InetAddress host,
                             int port)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode,
                             java.net.InetAddress address,
                             int port,
                             java.net.InetAddress localAddress,
                             int localPort)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode,
                             java.lang.String host,
                             int port)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode,
                             java.lang.String host,
                             int port,
                             java.net.InetAddress localHost,
                             int localPort)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode,
                             java.net.Socket s,
                             java.lang.String host,
                             int port,
                             boolean autoClose)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             boolean clientMode,
                             java.net.Socket s,
                             boolean autoClose)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • WolfSSLSocket

        public WolfSSLSocket​(WolfSSLContext context,
                             WolfSSLAuthStore authStore,
                             com.wolfssl.provider.jsse.WolfSSLParameters params,
                             java.net.Socket s,
                             java.io.InputStream consumed,
                             boolean autoClose)
                      throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • getSupportedCipherSuites

        public java.lang.String[] getSupportedCipherSuites()
        Returns the supported cipher suite list for this socket, and that have been compiled into native wolfSSL library.
        Specified by:
        getSupportedCipherSuites in class javax.net.ssl.SSLSocket
        Returns:
        array of supported cipher suite Strings
      • getEnabledCipherSuites

        public java.lang.String[] getEnabledCipherSuites()
        Returns array of enabled cipher suites for this Socket. This array is pre-populated by wolfJSSE with the cipher suites supported by the native wolfSSL library
        Specified by:
        getEnabledCipherSuites in class javax.net.ssl.SSLSocket
        Returns:
        array of enabled cipher suite Strings
      • setEnabledCipherSuites

        public void setEnabledCipherSuites​(java.lang.String[] suites)
                                    throws java.lang.IllegalArgumentException
        Sets the cipher suites enabled for this SSLSocket.
        Specified by:
        setEnabledCipherSuites in class javax.net.ssl.SSLSocket
        Parameters:
        suites - array of cipher suites to enable for this Socket
        Throws:
        java.lang.IllegalArgumentException - when suites array contains cipher suites unsupported by native wolfSSL
      • getSupportedProtocols

        public java.lang.String[] getSupportedProtocols()
        Returns array of protocols supported by this SSLSocket.
        Specified by:
        getSupportedProtocols in class javax.net.ssl.SSLSocket
        Returns:
        String array containing supported SSL/TLS protocols
      • getEnabledProtocols

        public java.lang.String[] getEnabledProtocols()
        Returns SSL/TLS protocols enabled for this SSLSocket.
        Specified by:
        getEnabledProtocols in class javax.net.ssl.SSLSocket
        Returns:
        String array containing enabled protocols
      • setEnabledProtocols

        public void setEnabledProtocols​(java.lang.String[] protocols)
                                 throws java.lang.IllegalArgumentException
        Sets the SSL/TLS protocols enabled on this SSLSocket.
        Specified by:
        setEnabledProtocols in class javax.net.ssl.SSLSocket
        Parameters:
        protocols - String array of SSL/TLS protocols to enable
        Throws:
        java.lang.IllegalArgumentException - when protocols array contains protocols unsupported by native wolfSSL
      • setAlpnProtocols

        public void setAlpnProtocols​(byte[] alpnProtos)
        Set ALPN extension protocol for this session. Calls native SSL_set_alpn_protos() at native level. Format starts with length, where length does not include length byte itself. Example format: Non-standard JSSE API, needed for Android compatibility. Some frameworks such as OkHttp expect this API to be here. byte[] p = "http/1.1".getBytes();
        Parameters:
        alpnProtos - ALPN protocols, encoded as byte array vector
      • getAlpnSelectedProtocol

        public byte[] getAlpnSelectedProtocol()
        Return ALPN protocol established for this session. Calls native SSL_get0_alpn_selected(). Non-standard JSSE API, needed for Android compatibility. Some frameworks such as OkHttp expect this API to be here.
        Returns:
        byte array representation of selected protocol, starting with length byte. Length does not include length byte itself.
      • getSession

        public javax.net.ssl.SSLSession getSession()
        Returns the SSLSession in use by this SSLSocket.
        Specified by:
        getSession in class javax.net.ssl.SSLSocket
        Returns:
        SSLSession object, otherwise null if not handshaking or Socket has not progressed enough to create the session
      • addHandshakeCompletedListener

        public void addHandshakeCompletedListener​(javax.net.ssl.HandshakeCompletedListener listener)
                                           throws java.lang.IllegalArgumentException
        Registers a HandshakeCompletedListener with this SSLSocket. The handshake completed listener will be notified when the SSL/TLS handshake on this Socket has completed.
        Specified by:
        addHandshakeCompletedListener in class javax.net.ssl.SSLSocket
        Parameters:
        listener - the handshake listener to register
        Throws:
        java.lang.IllegalArgumentException - when listener is null
      • removeHandshakeCompletedListener

        public void removeHandshakeCompletedListener​(javax.net.ssl.HandshakeCompletedListener listener)
                                              throws java.lang.IllegalArgumentException
        Removes a registered HandshakeCompletedListener from this SSLSocket.
        Specified by:
        removeHandshakeCompletedListener in class javax.net.ssl.SSLSocket
        Parameters:
        listener - the listener to be removed
        Throws:
        java.lang.IllegalArgumentException - if listener is null, or has not been registered wit this Socket
      • startHandshake

        public void startHandshake()
                            throws java.io.IOException
        Begins the SSL/TLS handshake on this SSLSocket.
        Specified by:
        startHandshake in class javax.net.ssl.SSLSocket
        Throws:
        java.io.IOException - if a network error occurs
      • setUseClientMode

        public void setUseClientMode​(boolean mode)
                              throws java.lang.IllegalArgumentException
        Sets the SSLSocket to use client or server mode. This must be called before the handshake begins on this Socket.
        Specified by:
        setUseClientMode in class javax.net.ssl.SSLSocket
        Parameters:
        mode - true for client mode, false for server mode
        Throws:
        java.lang.IllegalArgumentException - if caller tries to set the mode after handshaking has completed
      • getUseClientMode

        public boolean getUseClientMode()
        Return the client mode of this SSLSocket.
        Specified by:
        getUseClientMode in class javax.net.ssl.SSLSocket
        Returns:
        true if in client mode, otherwise false for server mode
      • setNeedClientAuth

        public void setNeedClientAuth​(boolean need)
        Configures the SSLSocket to require client authentication. Only useful in server mode. Similar to setWantClientAuth(), but if a client does not provide a cert/method for the server to authenticate it, the connection will fail.
        Specified by:
        setNeedClientAuth in class javax.net.ssl.SSLSocket
        Parameters:
        need - true sets client auth requirement, otherwise false
      • getNeedClientAuth

        public boolean getNeedClientAuth()
        Return if mandatory client authentication is set for this SSLSocket.
        Specified by:
        getNeedClientAuth in class javax.net.ssl.SSLSocket
        Returns:
        true if Socket has been configured to require client auth, otherwise false
      • setWantClientAuth

        public void setWantClientAuth​(boolean want)
        Configures the SSLSocket to request client authentication, but not require it. Similar to setNeedClientAuth(), but the handshake does not abort if the client does not send a certificate back.
        Specified by:
        setWantClientAuth in class javax.net.ssl.SSLSocket
        Parameters:
        want - true to enable server to request certificate from client, false if client auth should be disabled
      • getWantClientAuth

        public boolean getWantClientAuth()
        Returns true if SSLSocket will request client authentication. "want" client auth indicates that a server socket will request that the client sends a certificate to authenticate itself, but the server will not abort the handshake if the client does not send it.
        Specified by:
        getWantClientAuth in class javax.net.ssl.SSLSocket
        Returns:
        true if Socket will request client auth, false otherwise
      • setEnableSessionCreation

        public void setEnableSessionCreation​(boolean flag)
        Enables this SSLSocket to create new sessions. If this is set to false, and there are not sessions to resume, this Socket will not be allowed to create new sessions.
        Specified by:
        setEnableSessionCreation in class javax.net.ssl.SSLSocket
        Parameters:
        flag - true to allow session creation, otherwise false
      • getEnableSessionCreation

        public boolean getEnableSessionCreation()
        Returns whether this SSLSocket can create new sessions.
        Specified by:
        getEnableSessionCreation in class javax.net.ssl.SSLSocket
        Returns:
        true if this Socket can create new sessions, otherwise false
      • setUseSessionTickets

        public void setUseSessionTickets​(boolean useTickets)
        Enables use of session tickets with this session. Disabled by default.
        Parameters:
        useTickets - true to enable session tickets, otherwise false
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Return the InputStream associated with this SSLSocket.
        Overrides:
        getInputStream in class java.net.Socket
        Returns:
        InputStream for this Socket
        Throws:
        java.io.IOException - if InputStream is not able to be returned
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        Return the OutputStream associated with this SSLSocket.
        Overrides:
        getOutputStream in class java.net.Socket
        Returns:
        OutputStream for this Socket
        Throws:
        java.io.IOException - if OutputStream is not able to be returned
      • setSoTimeout

        public void setSoTimeout​(int timeout)
                          throws java.net.SocketException
        Set the SO_TIMEOUT with specified timeout in milliseconds. Must be called prior to socket operations to have an effect.
        Overrides:
        setSoTimeout in class java.net.Socket
        Parameters:
        timeout - Read timeout in milliseconds, or 0 for infinite
        Throws:
        java.net.SocketException - if there is an error setting the timeout value
      • getSoTimeout

        public int getSoTimeout()
                         throws java.net.SocketException
        Get the SO_TIMEOUT value, in milliseconds.
        Overrides:
        getSoTimeout in class java.net.Socket
        Returns:
        Timeout value in milliseconds, or 0 if disabled/infinite
        Throws:
        java.net.SocketException - if there is an error getting timeout value
      • setSSLParameters

        public void setSSLParameters​(javax.net.ssl.SSLParameters params)
        Set the SSLParameters for this SSLSocket.
        Overrides:
        setSSLParameters in class javax.net.ssl.SSLSocket
        Parameters:
        params - SSLParameters to set for this SSLSocket object
      • close

        public void close()
                   throws java.io.IOException
        Closes this SSLSocket. If this socket was created with an autoClose value set to true, this will also close the underlying Socket.
        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 - upon error closing the connection
      • connect

        public void connect​(java.net.SocketAddress endpoint)
                     throws java.io.IOException
        Connects the underlying Socket associated with this SSLSocket.
        Overrides:
        connect in class java.net.Socket
        Parameters:
        endpoint - address of peer to connect underlying Socket to
        Throws:
        java.io.IOException - upon error connecting Socket
      • connect

        public void connect​(java.net.SocketAddress endpoint,
                            int timeout)
                     throws java.io.IOException
        Connects the underlying Socket associated with this SSLSocket.
        Overrides:
        connect in class java.net.Socket
        Parameters:
        endpoint - address of peer to connect underlying socket to
        timeout - timeout value to set for underlying Socket connection
        Throws:
        java.io.IOException - upon error connecting Socket
      • finalize

        protected void finalize()
                         throws java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable