Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close socket if handshake fails #205

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/java/com/wolfssl/provider/jsse/WolfSSLSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,20 @@ public synchronized SSLSession getSession() {
/* Log error, but continue. Session returned will be empty */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"Handshake attempt failed in SSLSocket.getSession()");

/* close SSLSocket */
if (this.socket != null && !this.socket.isClosed()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this socket null/isClosed check? Since we call close() anyways, doesn't that check for this internal to that method?

try {
close();
} catch (Exception ex) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"close attempt failed in SSLSocket.getSession(): " +
ex);
}
}
/* return invalid session object with cipher suite
* "SSL_NULL_WITH_NULL_NULL" */
return new WolfSSLImplementSSLSession(this.authStore);
}

return EngineHelper.getSession();
Expand Down Expand Up @@ -1446,13 +1460,17 @@ public synchronized void startHandshake() throws IOException {
} catch (SocketTimeoutException e) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"got socket timeout in doHandshake()");
/* close socket if the handshake is unsuccessful */
close();
throw e;
}

if (ret != WolfSSL.SSL_SUCCESS) {
int err = ssl.getError(ret);
String errStr = WolfSSL.getErrorString(err);

/* close socket if the handshake is unsuccessful */
close();
throw new SSLHandshakeException(errStr + " (error code: " +
err + ", TID " + Thread.currentThread().getId() + ")");
}
Expand Down