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

JSSE: correct SSLSocket exception types, fix for setting fd #228

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
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
29 changes: 22 additions & 7 deletions src/java/com/wolfssl/provider/jsse/WolfSSLSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class WolfSSLSocket extends SSLSocket {
protected volatile boolean connectionClosed = false;
/** Flag representing if I/O callbacks have been set */
private boolean ioCallbacksSet = false;
/** Flag representing if native fd has been set */
private boolean fdSet = false;

/* lock for handshakInitCalled and handshakeComplete */
private final Object handshakeLock = new Object();
Expand Down Expand Up @@ -502,23 +504,33 @@ private void checkAndInitSSLSocket() throws IOException {

synchronized (initLock) {

/* If underlying Socket connected, set fd. Check before
* initialized flag, since we may have already initialized
* certs/keys but not fd in previous call */
if (!this.fdSet && isConnected()) {
try {
setFd();
} catch (WolfSSLException e) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"Failed to set native fd, may try again later");
}
}

if (isInitialized) {
return;
}

try {
/* Load private key and cert chain from WolfSSLAuthStore */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
"loading private key and cert chain");

if (this.socket != null) {
EngineHelper.LoadKeyAndCertChain(this.socket, null);
} else {
EngineHelper.LoadKeyAndCertChain(this, null);
}

/* If underlying Socket connected, set fd */
if (isConnected()) {
setFd();
}

isInitialized = true;

} catch (WolfSSLException | CertificateEncodingException |
Expand Down Expand Up @@ -610,6 +622,9 @@ private void setFd() throws IllegalArgumentException, WolfSSLException {
"registered Socket(this.socket) with native wolfSSL");
}
}

/* Mark fd set */
this.fdSet = true;
}
}

Expand Down Expand Up @@ -1715,7 +1730,7 @@ public synchronized InputStream getInputStream() throws IOException {
checkAndInitSSLSocket();

if (!this.isConnected()) {
throw new IOException("Socket is not connected");
throw new SocketException("Socket is not connected");
}

if (this.isClosed()) {
Expand Down Expand Up @@ -1747,7 +1762,7 @@ public synchronized OutputStream getOutputStream() throws IOException {
checkAndInitSSLSocket();

if (!this.isConnected()) {
throw new IOException("Socket is not connected");
throw new SocketException("Socket is not connected");
}

if (this.isClosed()) {
Expand Down