11package quickfix .test .util ;
22
33import org .apache .mina .core .filterchain .IoFilterChain ;
4+ import org .apache .mina .core .session .AttributeKey ;
45import org .apache .mina .core .session .IoSession ;
56import org .apache .mina .filter .ssl .SslFilter ;
7+ import org .apache .mina .filter .ssl .SslHandler ;
68import quickfix .Session ;
79import quickfix .mina .IoSessionResponder ;
810import quickfix .mina .ssl .SSLSupport ;
911
12+ import javax .net .ssl .SSLEngine ;
1013import javax .net .ssl .SSLPeerUnverifiedException ;
1114import javax .net .ssl .SSLSession ;
1215import java .lang .reflect .Field ;
1316import java .security .Principal ;
1417import java .security .cert .Certificate ;
1518
1619/**
17- * A utility class for working with SSL/TLS sessions and retrieving SSL-related information
18- * from a {@link Session}. This class provides methods to find the underlying {@link SSLSession},
19- * retrieve peer certificates, and get the peer principal.
20+ * A utility class for working with SSL/TLS sessions and retrieving SSL-related information from a {@link Session}. This class provides methods to find the underlying {@link SSLSession}, retrieve peer
21+ * certificates, and get the peer principal.
2022 */
2123public final class SSLUtil {
2224
2325 private static final String IO_SESSION_FIELD_NAME = "ioSession" ;
26+ private static final String SSL_ENGINE_FIELD_NAME = "mEngine" ;
27+ private static final AttributeKey SSL_HANDLER_ATTRIBUTE_KEY = new AttributeKey (SslHandler .class , "handler" );
2428 private static final Field IO_SESSION_FIELD ;
29+ private static final Field SSL_ENGINE_FIELD ;
2530
2631 static {
2732 try {
2833 IO_SESSION_FIELD = IoSessionResponder .class .getDeclaredField (IO_SESSION_FIELD_NAME );
2934 IO_SESSION_FIELD .setAccessible (true );
3035 } catch (NoSuchFieldException e ) {
31- throw new RuntimeException ("Unable to get ioSession field" , e );
36+ throw new RuntimeException ("Unable to get field: " + IO_SESSION_FIELD_NAME , e );
37+ }
38+
39+ try {
40+ SSL_ENGINE_FIELD = SslHandler .class .getDeclaredField (SSL_ENGINE_FIELD_NAME );
41+ SSL_ENGINE_FIELD .setAccessible (true );
42+ } catch (NoSuchFieldException e ) {
43+ throw new RuntimeException ("Unable to get field: " + SSL_ENGINE_FIELD_NAME , e );
3244 }
3345 }
3446
@@ -41,7 +53,7 @@ private SSLUtil() {
4153 * @param session the session from which to retrieve the {@link SSLSession}.
4254 * @return the {@link SSLSession} if found, or {@code null} if no SSL session is available.
4355 */
44- public static SSLSession findSSLSession (Session session ) {
56+ public static SSLSession findSSLSession (Session session ) {
4557 IoSession ioSession = findIoSession (session );
4658
4759 if (ioSession == null ) {
@@ -58,7 +70,6 @@ public static SSLSession findSSLSession(Session session) {
5870 return (SSLSession ) ioSession .getAttribute (SslFilter .SSL_SECURED );
5971 }
6072
61-
6273 /**
6374 * Retrieves the {@link IoSession} associated with the given {@link Session}.
6475 *
@@ -75,16 +86,46 @@ public static IoSession findIoSession(Session session) {
7586 try {
7687 return (IoSession ) IO_SESSION_FIELD .get (ioSessionResponder );
7788 } catch (IllegalAccessException e ) {
78- throw new RuntimeException ("Failed to get IO session field" ,e );
89+ throw new RuntimeException ("Failed to get IO session field" , e );
90+ }
91+ }
92+
93+ public static SslHandler getSSLHandler (Session session ) {
94+ IoSession ioSession = findIoSession (session );
95+
96+ if (ioSession == null ) {
97+ return null ;
98+ }
99+
100+ IoFilterChain filterChain = ioSession .getFilterChain ();
101+ SslFilter sslFilter = (SslFilter ) filterChain .get (SSLSupport .FILTER_NAME );
102+
103+ if (sslFilter == null ) {
104+ return null ;
105+ }
106+
107+ return (SslHandler ) ioSession .getAttribute (SSL_HANDLER_ATTRIBUTE_KEY );
108+ }
109+
110+ public static SSLEngine getSSLEngine (Session session ) {
111+ SslHandler sslHandler = getSSLHandler (session );
112+
113+ if (sslHandler == null ) {
114+ return null ;
115+ }
116+
117+ try {
118+ return (SSLEngine ) SSL_ENGINE_FIELD .get (sslHandler );
119+ } catch (IllegalAccessException e ) {
120+ throw new RuntimeException ("Failed to get SSL engine field" , e );
79121 }
80122 }
81123
82124 /**
83125 * Retrieves the peer certificates from the given {@link SSLSession}.
84126 *
85127 * @param sslSession the SSL session from which to retrieve the peer certificates.
86- * @return an array of {@link Certificate} objects representing the peer certificates,
87- * or {@code null} if the peer is unverified.
128+ * @return an array of {@link Certificate} objects representing the peer certificates, or {@code null} if the peer is unverified.
88129 */
89130 public static Certificate [] getPeerCertificates (SSLSession sslSession ) {
90131 try {
0 commit comments