Skip to content

Commit aee23c7

Browse files
committed
fixing SSL test assertions for TLS 1.3
1 parent d07c1dd commit aee23c7

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

quickfixj-core/src/test/java/quickfix/mina/ssl/SSLCertificateTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import quickfix.mina.ProtocolFactory;
4444
import quickfix.mina.SessionConnector;
4545

46+
import javax.net.ssl.SSLEngine;
47+
import javax.net.ssl.SSLEngineResult;
4648
import javax.net.ssl.SSLHandshakeException;
4749
import javax.net.ssl.SSLSession;
4850
import java.math.BigInteger;
@@ -757,8 +759,9 @@ public void assertNotAuthenticated(SessionID sessionID, boolean authOn) {
757759
Session session = findSession(sessionID);
758760
SSLSession sslSession = SSLUtil.findSSLSession(session);
759761

760-
if (sslSession == null)
762+
if (sslSession == null) {
761763
return;
764+
}
762765

763766
if (authOn) {
764767
// when authentication is on, the SSL session maybe still be alive (invalid) for some time
@@ -880,9 +883,11 @@ private void logSSLInfo() {
880883
Class<?> exceptionType = exception != null ? exception.getClass() : null;
881884
Certificate[] peerCertificates = SSLUtil.getPeerCertificates(sslSession);
882885
Principal peerPrincipal = SSLUtil.getPeerPrincipal(sslSession);
886+
SSLEngine sslEngine = SSLUtil.getSSLEngine(session);
887+
SSLEngineResult.HandshakeStatus handshakeStatus = sslEngine != null ? sslEngine.getHandshakeStatus() : null;
883888

884-
LOGGER.info("SSL session info [sessionID={},isLoggedOn={},sslSession={},sslSession.valid={},peerCertificates={},peerPrincipal={},exceptionMessage={},exceptionType={}]",
885-
sessionID, session.isLoggedOn(), sslSession, sslSession.isValid(), peerCertificates, peerPrincipal, exceptionMessage, exceptionType);
889+
LOGGER.info("SSL session info [sessionID={},isLoggedOn={},sslSession={},sslSession.valid={},peerCertificates={},peerPrincipal={},exceptionMessage={},exceptionType={},handshakeStatus={}]",
890+
sessionID, session.isLoggedOn(), sslSession, sslSession.isValid(), peerCertificates, peerPrincipal, exceptionMessage, exceptionType, handshakeStatus);
886891
}
887892
}
888893
}

quickfixj-core/src/test/java/quickfix/test/util/SSLUtil.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
package quickfix.test.util;
22

33
import org.apache.mina.core.filterchain.IoFilterChain;
4+
import org.apache.mina.core.session.AttributeKey;
45
import org.apache.mina.core.session.IoSession;
56
import org.apache.mina.filter.ssl.SslFilter;
7+
import org.apache.mina.filter.ssl.SslHandler;
68
import quickfix.Session;
79
import quickfix.mina.IoSessionResponder;
810
import quickfix.mina.ssl.SSLSupport;
911

12+
import javax.net.ssl.SSLEngine;
1013
import javax.net.ssl.SSLPeerUnverifiedException;
1114
import javax.net.ssl.SSLSession;
1215
import java.lang.reflect.Field;
1316
import java.security.Principal;
1417
import 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
*/
2123
public 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

Comments
 (0)