Skip to content

Commit 2289eed

Browse files
committed
SSLSocket.close() NullPointerException fix, check EngineHelper for null in close()
1 parent ba7b482 commit 2289eed

2 files changed

Lines changed: 71 additions & 9 deletions

File tree

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,18 @@ private void checkAndInitSSLSocket() throws IOException {
534534

535535
try {
536536
/* Load private key and cert chain from WolfSSLAuthStore */
537-
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
538-
() -> "loading private key and cert chain");
537+
if (EngineHelper != null) {
538+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
539+
() -> "loading private key and cert chain");
539540

540-
if (this.socket != null) {
541-
EngineHelper.LoadKeyAndCertChain(this.socket, null);
541+
if (this.socket != null) {
542+
EngineHelper.LoadKeyAndCertChain(this.socket, null);
543+
} else {
544+
EngineHelper.LoadKeyAndCertChain(this, null);
545+
}
542546
} else {
543-
EngineHelper.LoadKeyAndCertChain(this, null);
547+
throw new WolfSSLException(
548+
"EngineHelper null, cannot load key and cert chain");
544549
}
545550

546551
isInitialized = true;
@@ -2067,7 +2072,9 @@ public synchronized void close() throws IOException {
20672072
(handshakeFinished == true)) {
20682073
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
20692074
() -> "saving WOLFSSL_SESSION into cache");
2070-
EngineHelper.saveSession();
2075+
if (EngineHelper != null) {
2076+
EngineHelper.saveSession();
2077+
}
20712078
}
20722079
else {
20732080
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
@@ -2106,7 +2113,9 @@ public synchronized void close() throws IOException {
21062113
this.connectionClosed = true;
21072114

21082115
/* Release native verify callback (JNI global) */
2109-
this.EngineHelper.unsetVerifyCallback();
2116+
if (this.EngineHelper != null) {
2117+
this.EngineHelper.unsetVerifyCallback();
2118+
}
21102119

21112120
/* Close ConsumedRecvCtx data stream */
21122121
Object readCtx = this.ssl.getIOReadCtx();
@@ -2179,8 +2188,10 @@ public synchronized void close() throws IOException {
21792188
}
21802189

21812190
/* Reset internal WolfSSLEngineHelper to null */
2182-
this.EngineHelper.clearObjectState();
2183-
this.EngineHelper = null;
2191+
if (this.EngineHelper != null) {
2192+
this.EngineHelper.clearObjectState();
2193+
this.EngineHelper = null;
2194+
}
21842195

21852196
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
21862197
() -> "thread exiting ioLock (shutdown)");

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,5 +3869,56 @@ public void testDoubleCloseNPERegression()
38693869

38703870
System.out.println("\t... passed");
38713871
}
3872+
3873+
@Test
3874+
public void testCloseWithNullEngineHelper()
3875+
throws NoSuchFieldException, IllegalAccessException {
3876+
3877+
System.out.print("\tclose() with null EngineHelper");
3878+
3879+
/* Create a normal WolfSSLSocket first using the factory */
3880+
SSLSocketFactory factory = null;
3881+
for (SSLSocketFactory f : sockFactories) {
3882+
if (f != null) {
3883+
factory = f;
3884+
break;
3885+
}
3886+
}
3887+
assertNotNull("No SSLSocketFactory available for test", factory);
3888+
3889+
WolfSSLSocket socket = null;
3890+
try {
3891+
/* Create a socket but don't connect it */
3892+
socket = (WolfSSLSocket) factory.createSocket();
3893+
3894+
/* Use reflection to set EngineHelper to null, simulating the
3895+
* scenario where constructor failed after partial
3896+
* initialization */
3897+
Field engineHelperField =
3898+
WolfSSLSocket.class.getDeclaredField("EngineHelper");
3899+
engineHelperField.setAccessible(true);
3900+
engineHelperField.set(socket, null);
3901+
3902+
} catch (Exception e) {
3903+
fail("Failed to create test socket or set EngineHelper to null: "
3904+
+ e.getMessage());
3905+
}
3906+
3907+
/* Verify that calling close() on the socket with null EngineHelper
3908+
* does not throw NullPointerException */
3909+
try {
3910+
if (socket != null) {
3911+
socket.close();
3912+
}
3913+
/* Test should fail here if NPE occurs */
3914+
} catch (NullPointerException npe) {
3915+
fail("close() threw NullPointerException when EngineHelper " +
3916+
"is null: " + npe.getMessage());
3917+
} catch (IOException e) {
3918+
/* IOException from close() is acceptable */
3919+
}
3920+
3921+
System.out.println("\t... passed");
3922+
}
38723923
}
38733924

0 commit comments

Comments
 (0)