JSSE: close WolfSSLInput/OutputStream when SSLSocket.close() is called, allows stream objects to be garbage collected more easily

pull/189/head
Chris Conlon 2024-04-18 16:33:43 -06:00
parent e0d718e9b3
commit c5304ebb19
1 changed files with 41 additions and 1 deletions

View File

@ -1890,6 +1890,9 @@ public class WolfSSLSocket extends SSLSocket {
this.connectionClosed = true; this.connectionClosed = true;
/* Release native verify callback (JNI global) */
this.EngineHelper.unsetVerifyCallback();
/* Connection is closed, free native WOLFSSL session /* Connection is closed, free native WOLFSSL session
* to release native memory earlier than garbage * to release native memory earlier than garbage
* collector might with finalize(). */ * collector might with finalize(). */
@ -1905,6 +1908,17 @@ public class WolfSSLSocket extends SSLSocket {
"calling this.ssl.freeSSL()"); "calling this.ssl.freeSSL()");
this.ssl.freeSSL(); this.ssl.freeSSL();
this.ssl = null; this.ssl = null;
/* Release Input/OutputStream objects */
if (this.inStream != null) {
this.inStream.close();
this.inStream = null;
}
if (this.outStream != null) {
this.outStream.close();
this.outStream = null;
}
} /* handshakeLock */ } /* handshakeLock */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO, WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
@ -2326,13 +2340,20 @@ public class WolfSSLSocket extends SSLSocket {
private WolfSSLSession ssl; private WolfSSLSession ssl;
private WolfSSLSocket socket; private WolfSSLSocket socket;
private boolean isClosed = true;
public WolfSSLInputStream(WolfSSLSession ssl, WolfSSLSocket socket) { public WolfSSLInputStream(WolfSSLSession ssl, WolfSSLSocket socket) {
this.ssl = ssl; this.ssl = ssl;
this.socket = socket; /* parent socket */ this.socket = socket; /* parent socket */
this.isClosed = false;
} }
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
if (this.socket == null || this.isClosed) {
return;
}
if (this.socket.isClosed()) { if (this.socket.isClosed()) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO, WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"socket (input) already closed"); "socket (input) already closed");
@ -2343,6 +2364,10 @@ public class WolfSSLSocket extends SSLSocket {
"socket (input) closed: " + this.socket); "socket (input) closed: " + this.socket);
} }
this.socket = null;
this.ssl = null;
this.isClosed = true;
return; return;
} }
@ -2381,7 +2406,7 @@ public class WolfSSLSocket extends SSLSocket {
} }
/* check if socket is closed */ /* check if socket is closed */
if (socket.isClosed()) { if (this.isClosed || socket == null || socket.isClosed()) {
throw new SocketException("Socket is closed"); throw new SocketException("Socket is closed");
} }
@ -2470,13 +2495,20 @@ public class WolfSSLSocket extends SSLSocket {
private WolfSSLSession ssl; private WolfSSLSession ssl;
private WolfSSLSocket socket; private WolfSSLSocket socket;
private boolean isClosed = true;
public WolfSSLOutputStream(WolfSSLSession ssl, WolfSSLSocket socket) { public WolfSSLOutputStream(WolfSSLSession ssl, WolfSSLSocket socket) {
this.ssl = ssl; this.ssl = ssl;
this.socket = socket; /* parent socket */ this.socket = socket; /* parent socket */
this.isClosed = false;
} }
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
if (this.socket == null || this.isClosed) {
return;
}
if (this.socket.isClosed()) { if (this.socket.isClosed()) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO, WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"socket (output) already closed"); "socket (output) already closed");
@ -2487,6 +2519,10 @@ public class WolfSSLSocket extends SSLSocket {
"socket (output) closed: " + this.socket); "socket (output) closed: " + this.socket);
} }
this.socket = null;
this.ssl = null;
this.isClosed = true;
return; return;
} }
@ -2510,6 +2546,10 @@ public class WolfSSLSocket extends SSLSocket {
throw new NullPointerException("Input array is null"); throw new NullPointerException("Input array is null");
} }
if (this.socket == null || this.isClosed) {
throw new SocketException("Socket is closed");
}
/* check if connection has already been closed/shutdown */ /* check if connection has already been closed/shutdown */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO, WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"trying to get socket.handshakeLock (write)"); "trying to get socket.handshakeLock (write)");