Skip to content

Commit

Permalink
Merge pull request #228 from cconlon/initSockFixes
Browse files Browse the repository at this point in the history
JSSE: correct SSLSocket exception types, fix for setting fd
  • Loading branch information
JacobBarthelmeh authored Nov 1, 2024
2 parents 14685a6 + d8c18c5 commit 30e4042
Showing 1 changed file with 22 additions and 7 deletions.
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

0 comments on commit 30e4042

Please sign in to comment.