Skip to content

Commit fd143de

Browse files
committed
Fix TLS 1.3 session resumption to preserve SNI extension
1 parent ad23ac2 commit fd143de

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

Diff for: src/java/com/wolfssl/provider/jsse/WolfSSLAuthStore.java

+21
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.wolfssl.WolfSSLSession;
2828
import javax.net.ssl.KeyManager;
2929
import javax.net.ssl.KeyManagerFactory;
30+
import javax.net.ssl.SNIHostName;
31+
import javax.net.ssl.SNIServerName;
3032
import javax.net.ssl.X509KeyManager;
3133
import javax.net.ssl.TrustManager;
3234
import javax.net.ssl.X509TrustManager;
@@ -399,6 +401,25 @@ else if (!sessionCipherSuiteAvailable(
399401

400402
ses.isFromTable = true;
401403

404+
/* Check if the session has stored SNI server names */
405+
List<SNIServerName> sniNames = ses.getSNIServerNames();
406+
if (sniNames != null && !sniNames.isEmpty()) {
407+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
408+
"Found SNI server names in cached session");
409+
410+
/* Apply SNI settings to the SSL connection */
411+
for (SNIServerName name : sniNames) {
412+
if (name instanceof SNIHostName) {
413+
String hostName = ((SNIHostName)name).getAsciiName();
414+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
415+
"Applying SNI hostname for resumption: " + hostName);
416+
417+
/* Set the SNI directly on the SSL object */
418+
ssl.useSNI((byte)WolfSSL.WOLFSSL_SNI_HOST_NAME, hostName.getBytes());
419+
}
420+
}
421+
}
422+
402423
if (ses.resume(ssl) != WolfSSL.SSL_SUCCESS) {
403424
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
404425
"native wolfSSL_set_session() failed, " +

Diff for: src/java/com/wolfssl/provider/jsse/WolfSSLEngine.java

+11
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,17 @@ public synchronized void setSSLParameters(SSLParameters params) {
20732073
"entered setSSLParameters()");
20742074
if (params != null) {
20752075
WolfSSLParametersHelper.importParams(params, this.params);
2076+
2077+
/* Store SNI server names in the session for potential resumption */
2078+
if (params.getServerNames() != null && !params.getServerNames().isEmpty()) {
2079+
WolfSSLImplementSSLSession session =
2080+
(WolfSSLImplementSSLSession)this.getSession();
2081+
if (session != null) {
2082+
session.setSNIServerNames(params.getServerNames());
2083+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
2084+
"Captured SNI server names for session caching");
2085+
}
2086+
}
20762087
}
20772088
}
20782089

Diff for: src/java/com/wolfssl/provider/jsse/WolfSSLImplementSSLSession.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ public class WolfSSLImplementSSLSession extends ExtendedSSLSession
115115
* in resumption cases. */
116116
private static final Object sesPtrLock = new Object();
117117

118+
/**
119+
* Stored SNI server names from original session, used during resumption
120+
*/
121+
private List<SNIServerName> sniServerNames = null;
122+
123+
/**
124+
* Store SNI server names for this session for later resumption
125+
* @param serverNames list of SNI server names to store
126+
*/
127+
public synchronized void setSNIServerNames(List<SNIServerName> serverNames) {
128+
if (serverNames != null && !serverNames.isEmpty()) {
129+
this.sniServerNames = new ArrayList<>(serverNames);
130+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
131+
"Stored SNI server names for session resumption");
132+
}
133+
}
134+
135+
/**
136+
* Get stored SNI server names for this session
137+
* @return list of stored SNI server names, may be null
138+
*/
139+
public synchronized List<SNIServerName> getSNIServerNames() {
140+
return this.sniServerNames;
141+
}
142+
118143
/**
119144
* Create new WolfSSLImplementSSLSession
120145
*
@@ -846,13 +871,27 @@ protected synchronized void setResume() {
846871
* Update internally-stored session values.
847872
*/
848873
protected synchronized void updateStoredSessionValues() {
849-
850874
try {
851875
this.protocol = this.ssl.getVersion();
852876
} catch (IllegalStateException | WolfSSLJNIException ex) {
853877
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
854878
"Not able to update stored WOLFSSL protocol");
855879
}
880+
881+
/* Also store SNI server names if not already set */
882+
if (this.sniServerNames == null || this.sniServerNames.isEmpty()) {
883+
try {
884+
List<SNIServerName> names = this.getRequestedServerNames();
885+
if (names != null && !names.isEmpty()) {
886+
this.sniServerNames = new ArrayList<>(names);
887+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
888+
"Extracted SNI server names from session");
889+
}
890+
} catch (UnsupportedOperationException ex) {
891+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
892+
"Error extracting SNI server names: " + ex.getMessage());
893+
}
894+
}
856895
}
857896

858897
/**

0 commit comments

Comments
 (0)