Skip to content

Commit

Permalink
Merge pull request #206 from cconlon/sessionDup
Browse files Browse the repository at this point in the history
JNI: wrap native wolfSSL_SESSION_dup() in WolfSSLSession.duplicateSession()
  • Loading branch information
JacobBarthelmeh authored Jul 19, 2024
2 parents fba6fc7 + 58c63de commit 1e9509d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
14 changes: 14 additions & 0 deletions native/com_wolfssl_WolfSSLSession.c
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,20 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_wolfsslSessionIsResumable
#endif
}

JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLSession_wolfsslSessionDup
(JNIEnv* jenv, jclass jcl, jlong sessionPtr)
{
WOLFSSL_SESSION* session = (WOLFSSL_SESSION*)(uintptr_t)sessionPtr;
(void)jcl;

if (jenv == NULL) {
return 0;
}

/* checks session for NULL */
return (jlong)(uintptr_t)wolfSSL_SESSION_dup(session);
}

JNIEXPORT void JNICALL Java_com_wolfssl_WolfSSLSession_freeNativeSession
(JNIEnv* jenv, jclass jcl, jlong sessionPtr)
{
Expand Down
8 changes: 8 additions & 0 deletions native/com_wolfssl_WolfSSLSession.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/java/com/wolfssl/WolfSSLSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ private native int read(long ssl, byte[] data, int offset, int sz,
private native long get1Session(long ssl);
private static native int wolfsslSessionIsSetup(long ssl);
private static native int wolfsslSessionIsResumable(long ssl);
private static native long wolfsslSessionDup(long session);
private static native void freeNativeSession(long session);
private native byte[] getSessionID(long session);
private native int setServerID(long ssl, byte[] id, int len, int newSess);
Expand Down Expand Up @@ -1364,6 +1365,30 @@ public static int sessionIsResumable(long session) {
return wolfsslSessionIsResumable(session);
}

/**
* Deep copy the contents of the WOLFSSL_SESSION, calling native
* wolfSSL_SESSION_dup().
*
* This session will create a new WOLFSSL_SESSION and deep copy it
* from the WOLFSSL_SESSION pointer provided. Note that if a non-zero
* value is returned the application is responsible for freeing this
* WOLFSSL_SESSION memory when finished by calling freeSession().
*
* @param session pointer to native WOLFSSL_SESSION structure. May have
* been obtained from getSession().
*
* @return long representing a native pointer to a new WOLFSSL_SESSION
* structure, or zero on error (equivalent to a NULL pointer).
*/
public static long duplicateSession(long session) {

if (session == 0) {
return 0;
}

return wolfsslSessionDup(session);
}

/**
* Free the native WOLFSSL_SESSION structure pointed to be session.
*
Expand Down
17 changes: 17 additions & 0 deletions src/test/com/wolfssl/test/WolfSSLSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ public void test_WolfSSLSession_getSetSession()
int ret = 0;
int err = 0;
long sessionPtr = 0;
long sesDup = 0;
Socket cliSock = null;
WolfSSLSession cliSes = null;

Expand Down Expand Up @@ -1107,6 +1108,19 @@ public Void call() throws Exception {
"WolfSSLSession.sessionIsSetup() did not return 1: " + ret);
}

/* Test duplicateSession(), wraps wolfSSL_SESSION_dup() */
sesDup = cliSes.duplicateSession(sessionPtr);
if (sesDup == 0) {
throw new Exception(
"WolfSSLSession.duplicateSession() returned 0");
}
if (sesDup == sessionPtr) {
throw new Exception(
"WolfSSLSession.duplicateSession() returned same pointer");
}
cliSes.freeSession(sesDup);
sesDup = 0;

cliSes.shutdownSSL();
cliSes.freeSSL();
cliSes = null;
Expand Down Expand Up @@ -1178,6 +1192,9 @@ public Void call() throws Exception {
if (sessionPtr != 0) {
cliSes.freeSession(sessionPtr);
}
if (sesDup != 0) {
cliSes.freeSession(sesDup);
}
if (cliSes != null) {
cliSes.freeSSL();
}
Expand Down

0 comments on commit 1e9509d

Please sign in to comment.