Skip to content

Commit c802d5c

Browse files
committed
correctly throw SocketTimeoutException from WolfSSLSession.connect() for JSSE WolfSSLSocket.startHandshake() to handle timeout
1 parent 193361e commit c802d5c

6 files changed

Lines changed: 33 additions & 6 deletions

File tree

native/com_wolfssl_WolfSSL.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/com_wolfssl_WolfSSLSession.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_getFd
554554
/* enum values used in socketSelect() */
555555
enum {
556556
WOLFJNI_SELECT_FAIL = -10,
557-
WOLFJNI_TIMEOUT = -11,
557+
WOLFJNI_TIMEOUT = -11, /* also in WolfSSL.java */
558558
WOLFJNI_RECV_READY = -12,
559559
WOLFJNI_SEND_READY = -13,
560560
WOLFJNI_ERROR_READY = -14
@@ -623,7 +623,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_connect
623623
(void)jcl;
624624

625625
if (jenv == NULL || ssl == NULL) {
626-
return SSL_FATAL_ERROR;
626+
return SSL_FAILURE;
627627
}
628628

629629
/* make sure we don't have any outstanding exceptions pending */
@@ -674,8 +674,12 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_connect
674674
if (ret == WOLFJNI_RECV_READY || ret == WOLFJNI_SEND_READY) {
675675
/* I/O ready, continue handshake and try again */
676676
continue;
677+
} else if (ret == WOLFJNI_TIMEOUT) {
678+
/* Java will throw SocketTimeoutException */
679+
break;
677680
} else {
678-
/* error or timeout */
681+
/* error */
682+
ret = SSL_FAILURE;
679683
break;
680684
}
681685
}

src/java/com/wolfssl/WolfSSL.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public static enum TLS_VERSION {
5656
/** Session unavailable */
5757
public final static int JNI_SESSION_UNAVAILABLE = -10001;
5858

59+
/**
60+
* Socket timed out, matches com_wolfssl_WolfSSLSession.c socketSelect()
61+
* return value */
62+
public final static int WOLFJNI_TIMEOUT = -11;
63+
5964
/* ----------------------- wolfSSL codes ---------------------------- */
6065

6166
/** Error code: no error */

src/java/com/wolfssl/WolfSSLSession.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,16 +508,26 @@ public int getFd()
508508
* before calling <code>newSSL()</code>, though it's not recommended.
509509
*
510510
* @return <code>SSL_SUCCESS</code> if successful, otherwise
511-
* <code>SSL_FATAL_ERROR</code> if an error occurred. To get
511+
* <code>SSL_FAILURE</code> if an error occurred. To get
512512
* a more detailed error code, call <code>getError()</code>.
513513
* @throws IllegalStateException WolfSSLContext has been freed
514+
* @throws SocketTimeoutException if underlying socket timed out
514515
*/
515-
public int connect() throws IllegalStateException {
516+
public int connect() throws IllegalStateException, SocketTimeoutException {
517+
518+
int ret = 0;
516519

517520
if (this.active == false)
518521
throw new IllegalStateException("Object has been freed");
519522

520-
return connect(getSessionPtr(), 0);
523+
ret = connect(getSessionPtr(), 0);
524+
525+
if (ret == WolfSSL.WOLFJNI_TIMEOUT) {
526+
throw new SocketTimeoutException(
527+
"Native socket timed out during SSL_connect()");
528+
}
529+
530+
return ret;
521531
}
522532

523533
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ protected int doHandshake(int isSSLEngine, int timeout)
697697
if (this.clientMode) {
698698
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
699699
"calling native wolfSSL_connect()");
700+
/* may throw SocketTimeoutException on socket timeout */
700701
ret = this.ssl.connect(timeout);
701702

702703
} else {

src/test/com/wolfssl/test/WolfSSLSessionTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.net.Socket;
3030
import java.net.UnknownHostException;
3131
import java.net.ConnectException;
32+
import java.net.SocketTimeoutException;
3233

3334
import com.wolfssl.WolfSSL;
3435
import com.wolfssl.WolfSSLContext;
@@ -459,6 +460,10 @@ public void test_WolfSSLSession_UseAfterFree() {
459460
} catch (IllegalStateException ise) {
460461
System.out.println("\t\t... passed");
461462
return;
463+
} catch (SocketTimeoutException e) {
464+
System.out.println("\t\t... failed");
465+
e.printStackTrace();
466+
return;
462467
}
463468

464469
/* fail here means WolfSSLSession was used after free without

0 commit comments

Comments
 (0)