Package org.apache.mina.filter
Class SSLFilter
- java.lang.Object
-
- org.apache.mina.common.IoFilterAdapter
-
- org.apache.mina.filter.SSLFilter
-
- All Implemented Interfaces:
org.apache.mina.common.IoFilter
public class SSLFilter extends org.apache.mina.common.IoFilterAdapterAn SSL filter that encrypts and decrypts the data exchanged in the session. Adding this filter triggers SSL handshake procedure immediately by sending a SSL 'hello' message, so you don't need to callstartSSL(IoSession)manually unless you are implementing StartTLS (see below).This filter uses an
SSLEnginewhich was introduced in Java 5, so Java version 5 or above is mandatory to use this filter. And please note that this filter only works for TCP/IP connections.This filter logs debug information using
SessionLog.Implementing StartTLS
You can use
DISABLE_ENCRYPTION_ONCEattribute to implement StartTLS:public void messageReceived(IoSession session, Object message) { if (message instanceof MyStartTLSRequest) { // Insert SSLFilter to get ready for handshaking session.getFilterChain().addFirst(sslFilter); // Disable encryption temporarilly. // This attribute will be removed by SSLFilter // inside the Session.write() call below. session.setAttribute(SSLFilter.DISABLE_ENCRYPTION_ONCE, Boolean.TRUE); // Write StartTLSResponse which won't be encrypted. session.write(new MyStartTLSResponse(OK)); // Now DISABLE_ENCRYPTION_ONCE attribute is cleared. assert session.getAttribute(SSLFilter.DISABLE_ENCRYPTION_ONCE) == null; } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSSLFilter.SSLFilterMessageA message that is sent fromSSLFilterwhen the connection became secure or is not secure anymore.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDISABLE_ENCRYPTION_ONCEA session attribute key that makes next one write request bypass this filter (not encrypting the data).static SSLFilter.SSLFilterMessageSESSION_SECUREDA special message object which is emitted with aIoHandler.messageReceived(IoSession, Object)event when the session is secured and itsUSE_NOTIFICATIONattribute is set.static SSLFilter.SSLFilterMessageSESSION_UNSECUREDA special message object which is emitted with aIoHandler.messageReceived(IoSession, Object)event when the session is not secure anymore and itsUSE_NOTIFICATIONattribute is set.static java.lang.StringSSL_SESSIONA session attribute key that stores underlyingSSLSessionfor each session.static java.lang.StringUSE_NOTIFICATIONA session attribute key that makes this filter to emit aIoHandler.messageReceived(IoSession, Object)event with a special message (SESSION_SECUREDorSESSION_UNSECURED).
-
Constructor Summary
Constructors Constructor Description SSLFilter(javax.net.ssl.SSLContext sslContext)Creates a new SSL filter using the specifiedSSLContext.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidfilterClose(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session)voidfilterWrite(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session, org.apache.mina.common.IoFilter.WriteRequest writeRequest)java.lang.String[]getEnabledCipherSuites()Returns the list of cipher suites to be enabled whenSSLEngineis initialized.java.lang.String[]getEnabledProtocols()Returns the list of protocols to be enabled whenSSLEngineis initialized.javax.net.ssl.SSLSessiongetSSLSession(org.apache.mina.common.IoSession session)Returns the underlyingSSLSessionfor the specified session.booleanisNeedClientAuth()Returns true if the engine will require client authentication.booleanisSSLStarted(org.apache.mina.common.IoSession session)Returns true if and only if the specified session is encrypted/decrypted over SSL/TLS currently.booleanisUseClientMode()Returns true if the engine is set to use client mode when handshaking.booleanisWantClientAuth()Returns true if the engine will request client authentication.voidmessageReceived(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session, java.lang.Object message)voidmessageSent(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session, java.lang.Object message)voidonPostAdd(org.apache.mina.common.IoFilterChain parent, java.lang.String name, org.apache.mina.common.IoFilter.NextFilter nextFilter)voidonPreAdd(org.apache.mina.common.IoFilterChain parent, java.lang.String name, org.apache.mina.common.IoFilter.NextFilter nextFilter)voidonPreRemove(org.apache.mina.common.IoFilterChain parent, java.lang.String name, org.apache.mina.common.IoFilter.NextFilter nextFilter)voidsessionClosed(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session)voidsetEnabledCipherSuites(java.lang.String[] cipherSuites)Sets the list of cipher suites to be enabled whenSSLEngineis initialized.voidsetEnabledProtocols(java.lang.String[] protocols)Sets the list of protocols to be enabled whenSSLEngineis initialized.voidsetNeedClientAuth(boolean needClientAuth)Configures the engine to require client authentication.voidsetUseClientMode(boolean clientMode)Configures the engine to use client (or server) mode when handshaking.voidsetWantClientAuth(boolean wantClientAuth)Configures the engine to request client authentication.booleanstartSSL(org.apache.mina.common.IoSession session)(Re)starts SSL session for the specified session if not started yet.org.apache.mina.common.WriteFuturestopSSL(org.apache.mina.common.IoSession session)Stops the SSL session by sending TLS close_notify message to initiate TLS closure.
-
-
-
Field Detail
-
SSL_SESSION
public static final java.lang.String SSL_SESSION
A session attribute key that stores underlyingSSLSessionfor each session.
-
DISABLE_ENCRYPTION_ONCE
public static final java.lang.String DISABLE_ENCRYPTION_ONCE
A session attribute key that makes next one write request bypass this filter (not encrypting the data). This is a marker attribute, which means that you can put whatever as its value. (Boolean.TRUEis preferred.) The attribute is automatically removed from the session attribute map as soon asIoSession.write(Object)is invoked, and therefore should be put again if you want to make more messages bypass this filter. This is especially useful when you implement StartTLS.
-
USE_NOTIFICATION
public static final java.lang.String USE_NOTIFICATION
A session attribute key that makes this filter to emit aIoHandler.messageReceived(IoSession, Object)event with a special message (SESSION_SECUREDorSESSION_UNSECURED). This is a marker attribute, which means that you can put whatever as its value. (Boolean.TRUEis preferred.) By default, this filter doesn't emit any events related with SSL session flow control.
-
SESSION_SECURED
public static final SSLFilter.SSLFilterMessage SESSION_SECURED
A special message object which is emitted with aIoHandler.messageReceived(IoSession, Object)event when the session is secured and itsUSE_NOTIFICATIONattribute is set.
-
SESSION_UNSECURED
public static final SSLFilter.SSLFilterMessage SESSION_UNSECURED
A special message object which is emitted with aIoHandler.messageReceived(IoSession, Object)event when the session is not secure anymore and itsUSE_NOTIFICATIONattribute is set.
-
-
Method Detail
-
getSSLSession
public javax.net.ssl.SSLSession getSSLSession(org.apache.mina.common.IoSession session)
Returns the underlyingSSLSessionfor the specified session.- Returns:
- null if no
SSLSessionis initialized yet.
-
startSSL
public boolean startSSL(org.apache.mina.common.IoSession session) throws javax.net.ssl.SSLException(Re)starts SSL session for the specified session if not started yet. Please note that SSL session is automatically started by default, and therefore you don't need to call this method unless you've used TLS closure.- Returns:
- true if the SSL session has been started, false if already started.
- Throws:
javax.net.ssl.SSLException- if failed to start the SSL session
-
isSSLStarted
public boolean isSSLStarted(org.apache.mina.common.IoSession session)
Returns true if and only if the specified session is encrypted/decrypted over SSL/TLS currently. This method will start to retun false after TLS close_notify message is sent and any messages written after then is not goinf to get encrypted.
-
stopSSL
public org.apache.mina.common.WriteFuture stopSSL(org.apache.mina.common.IoSession session) throws javax.net.ssl.SSLExceptionStops the SSL session by sending TLS close_notify message to initiate TLS closure.- Parameters:
session- theIoSessionto initiate TLS closure- Throws:
javax.net.ssl.SSLException- if failed to initiate TLS closurejava.lang.IllegalArgumentException- if this filter is not managing the specified session
-
isUseClientMode
public boolean isUseClientMode()
Returns true if the engine is set to use client mode when handshaking.
-
setUseClientMode
public void setUseClientMode(boolean clientMode)
Configures the engine to use client (or server) mode when handshaking.
-
isNeedClientAuth
public boolean isNeedClientAuth()
Returns true if the engine will require client authentication. This option is only useful to engines in the server mode.
-
setNeedClientAuth
public void setNeedClientAuth(boolean needClientAuth)
Configures the engine to require client authentication. This option is only useful for engines in the server mode.
-
isWantClientAuth
public boolean isWantClientAuth()
Returns true if the engine will request client authentication. This option is only useful to engines in the server mode.
-
setWantClientAuth
public void setWantClientAuth(boolean wantClientAuth)
Configures the engine to request client authentication. This option is only useful for engines in the server mode.
-
getEnabledCipherSuites
public java.lang.String[] getEnabledCipherSuites()
Returns the list of cipher suites to be enabled whenSSLEngineis initialized.- Returns:
- null means 'use
SSLEngine's default.'
-
setEnabledCipherSuites
public void setEnabledCipherSuites(java.lang.String[] cipherSuites)
Sets the list of cipher suites to be enabled whenSSLEngineis initialized.- Parameters:
cipherSuites- null means 'useSSLEngine's default.'
-
getEnabledProtocols
public java.lang.String[] getEnabledProtocols()
Returns the list of protocols to be enabled whenSSLEngineis initialized.- Returns:
- null means 'use
SSLEngine's default.'
-
setEnabledProtocols
public void setEnabledProtocols(java.lang.String[] protocols)
Sets the list of protocols to be enabled whenSSLEngineis initialized.- Parameters:
protocols- null means 'useSSLEngine's default.'
-
onPreAdd
public void onPreAdd(org.apache.mina.common.IoFilterChain parent, java.lang.String name, org.apache.mina.common.IoFilter.NextFilter nextFilter) throws javax.net.ssl.SSLException- Specified by:
onPreAddin interfaceorg.apache.mina.common.IoFilter- Overrides:
onPreAddin classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
onPostAdd
public void onPostAdd(org.apache.mina.common.IoFilterChain parent, java.lang.String name, org.apache.mina.common.IoFilter.NextFilter nextFilter) throws javax.net.ssl.SSLException- Specified by:
onPostAddin interfaceorg.apache.mina.common.IoFilter- Overrides:
onPostAddin classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
onPreRemove
public void onPreRemove(org.apache.mina.common.IoFilterChain parent, java.lang.String name, org.apache.mina.common.IoFilter.NextFilter nextFilter) throws javax.net.ssl.SSLException- Specified by:
onPreRemovein interfaceorg.apache.mina.common.IoFilter- Overrides:
onPreRemovein classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
sessionClosed
public void sessionClosed(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session) throws javax.net.ssl.SSLException- Specified by:
sessionClosedin interfaceorg.apache.mina.common.IoFilter- Overrides:
sessionClosedin classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
messageReceived
public void messageReceived(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session, java.lang.Object message) throws javax.net.ssl.SSLException- Specified by:
messageReceivedin interfaceorg.apache.mina.common.IoFilter- Overrides:
messageReceivedin classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
messageSent
public void messageSent(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session, java.lang.Object message)- Specified by:
messageSentin interfaceorg.apache.mina.common.IoFilter- Overrides:
messageSentin classorg.apache.mina.common.IoFilterAdapter
-
filterWrite
public void filterWrite(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session, org.apache.mina.common.IoFilter.WriteRequest writeRequest) throws javax.net.ssl.SSLException- Specified by:
filterWritein interfaceorg.apache.mina.common.IoFilter- Overrides:
filterWritein classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
filterClose
public void filterClose(org.apache.mina.common.IoFilter.NextFilter nextFilter, org.apache.mina.common.IoSession session) throws javax.net.ssl.SSLException- Specified by:
filterClosein interfaceorg.apache.mina.common.IoFilter- Overrides:
filterClosein classorg.apache.mina.common.IoFilterAdapter- Throws:
javax.net.ssl.SSLException
-
-