Skip to content

Commit 7650f51

Browse files
authored
Merge pull request #304 from cconlon/ctxNullChecks
Allow SSLSessionContext access before SSLContext.init()
2 parents d4f416f + 04e90a0 commit 7650f51

3 files changed

Lines changed: 72 additions & 11 deletions

File tree

src/java/com/wolfssl/provider/jsse/WolfSSLAuthStore.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ public class WolfSSLAuthStore {
7373
* @param trustman trust manager to use
7474
* @param random secure random
7575
* @param version TLS protocol version to use
76+
* @param serverCtx server session context (created by WolfSSLContext)
77+
* @param clientCtx client session context (created by WolfSSLContext)
7678
* @throws IllegalArgumentException when bad values are passed in
7779
* @throws KeyManagementException in the case that getting keys fails
7880
*/
7981
protected WolfSSLAuthStore(KeyManager[] keyman, TrustManager[] trustman,
80-
SecureRandom random, TLS_VERSION version)
82+
SecureRandom random, TLS_VERSION version,
83+
WolfSSLSessionContext serverCtx, WolfSSLSessionContext clientCtx)
8184
throws IllegalArgumentException, KeyManagementException {
8285

8386
/* default session cache size of 33 to match native wolfSSL
@@ -99,10 +102,11 @@ protected WolfSSLAuthStore(KeyManager[] keyman, TrustManager[] trustman,
99102
if (store == null) {
100103
store = new SessionStore<>(defaultCacheSize);
101104
}
102-
this.serverCtx = new WolfSSLSessionContext(
103-
defaultCacheSize, WolfSSL.WOLFSSL_SERVER_END);
104-
this.clientCtx = new WolfSSLSessionContext(
105-
defaultCacheSize, WolfSSL.WOLFSSL_CLIENT_END);
105+
106+
/* Use session contexts passed in from WolfSSLContext, allowing
107+
* session context access before init() is called */
108+
this.serverCtx = serverCtx;
109+
this.clientCtx = clientCtx;
106110
}
107111

108112
/**

src/java/com/wolfssl/provider/jsse/WolfSSLContext.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,19 @@ public class WolfSSLContext extends SSLContextSpi {
5555
private com.wolfssl.WolfSSLContext ctx = null;
5656
private WolfSSLParameters params = null;
5757

58+
/* Created during construction to allow access before init() is called */
59+
private WolfSSLSessionContext serverSessionCtx = null;
60+
private WolfSSLSessionContext clientSessionCtx = null;
61+
62+
/* Default session cache size of 33 to match native wolfSSL default */
63+
private static final int DEFAULT_CACHE_SIZE = 33;
64+
5865
private WolfSSLContext(TLS_VERSION version) {
5966
this.currentVersion = version;
67+
this.serverSessionCtx = new WolfSSLSessionContext(
68+
DEFAULT_CACHE_SIZE, WolfSSL.WOLFSSL_SERVER_END);
69+
this.clientSessionCtx = new WolfSSLSessionContext(
70+
DEFAULT_CACHE_SIZE, WolfSSL.WOLFSSL_CLIENT_END);
6071
}
6172

6273
private void createCtx() throws WolfSSLException {
@@ -399,10 +410,15 @@ protected void engineInit(KeyManager[] km, TrustManager[] tm,
399410
Arrays.toString(tm) + ", sr=" + sr +")");
400411

401412
try {
402-
authStore = new WolfSSLAuthStore(km, tm, sr, currentVersion);
413+
authStore = new WolfSSLAuthStore(km, tm, sr, currentVersion,
414+
this.serverSessionCtx, this.clientSessionCtx);
403415
params = new WolfSSLParameters();
404416
createCtx();
405417

418+
/* Link authStore to session contexts for session operations */
419+
this.serverSessionCtx.setWolfSSLAuthStore(authStore);
420+
this.clientSessionCtx.setWolfSSLAuthStore(authStore);
421+
406422
} catch (IllegalArgumentException iae) {
407423
throw new KeyManagementException(iae);
408424

@@ -507,21 +523,27 @@ protected SSLEngine engineCreateSSLEngine(String host, int port)
507523
/**
508524
* Returns the SSLServerSessionContext associated with this SSLContext.
509525
*
510-
* @throws UnsupportedOperationException operation not yet supported
526+
* Session contexts are created during SSLContext construction to allow
527+
* access before init() is called.
528+
*
529+
* @return SSLSessionContext for server sessions
511530
*/
512531
@Override
513532
protected SSLSessionContext engineGetServerSessionContext() {
514-
return authStore.getServerContext();
533+
return this.serverSessionCtx;
515534
}
516535

517536
/**
518537
* Returns the SSLClientSessionContext associated with this SSLContext.
519538
*
520-
* @throws UnsupportedOperationException operation not yet supported
539+
* Session contexts are created during SSLContext construction to allow
540+
* access before init() is called.
541+
*
542+
* @return SSLSessionContext for client sessions
521543
*/
522544
@Override
523545
protected SSLSessionContext engineGetClientSessionContext() {
524-
return authStore.getClientContext();
546+
return this.clientSessionCtx;
525547
}
526548

527549
/**

src/test/com/wolfssl/provider/jsse/test/WolfSSLContextTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public static void testProviderInstallationAtRuntime()
9696
try {
9797
tf = new WolfSSLTestFactory();
9898
} catch (WolfSSLException e) {
99-
// TODO Auto-generated catch block
10099
e.printStackTrace();
101100
}
102101

@@ -285,6 +284,42 @@ public void testGetSessionContext() throws NoSuchProviderException,
285284
System.out.println("\t\t... passed");
286285
}
287286

287+
@Test
288+
public void testGetSessionContextBeforeInit()
289+
throws NoSuchProviderException, NoSuchAlgorithmException {
290+
291+
System.out.print("\tgetSessionContext before init");
292+
293+
SSLContext ctx = null;
294+
SSLSessionContext sess = null;
295+
296+
for (int i = 0; i < enabledProtocols.size(); i++) {
297+
298+
ctx = SSLContext.getInstance(enabledProtocols.get(i), ctxProvider);
299+
300+
/* getServerSessionContext() should work before init() */
301+
sess = ctx.getServerSessionContext();
302+
if (sess == null) {
303+
System.out.println("\t... failed");
304+
fail("getServerSessionContext() returned null before init()");
305+
return;
306+
}
307+
308+
/* getClientSessionContext() should work before init() */
309+
sess = ctx.getClientSessionContext();
310+
if (sess == null) {
311+
System.out.println("\t... failed");
312+
fail("getClientSessionContext() returned null before init()");
313+
return;
314+
}
315+
316+
/* Verify session context operations work before init() */
317+
int timeout = sess.getSessionTimeout();
318+
sess.setSessionTimeout(timeout);
319+
}
320+
System.out.println("\t... passed");
321+
}
322+
288323
@Test
289324
public void testGetSupportedSSLParameters() throws NoSuchProviderException,
290325
NoSuchAlgorithmException, IllegalStateException,

0 commit comments

Comments
 (0)