diff --git a/native/com_wolfssl_WolfSSLSession.c b/native/com_wolfssl_WolfSSLSession.c index 60e9431e..240e8323 100644 --- a/native/com_wolfssl_WolfSSLSession.c +++ b/native/com_wolfssl_WolfSSLSession.c @@ -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) { diff --git a/native/com_wolfssl_WolfSSLSession.h b/native/com_wolfssl_WolfSSLSession.h index e0c06b74..8df926fb 100644 --- a/native/com_wolfssl_WolfSSLSession.h +++ b/native/com_wolfssl_WolfSSLSession.h @@ -175,6 +175,14 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_wolfsslSessionIsSetup JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_wolfsslSessionIsResumable (JNIEnv *, jclass, jlong); +/* + * Class: com_wolfssl_WolfSSLSession + * Method: wolfsslSessionDup + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLSession_wolfsslSessionDup + (JNIEnv *, jclass, jlong); + /* * Class: com_wolfssl_WolfSSLSession * Method: freeNativeSession diff --git a/src/java/com/wolfssl/WolfSSLSession.java b/src/java/com/wolfssl/WolfSSLSession.java index 9d22e4f9..7c5dbc7e 100644 --- a/src/java/com/wolfssl/WolfSSLSession.java +++ b/src/java/com/wolfssl/WolfSSLSession.java @@ -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); @@ -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. * diff --git a/src/test/com/wolfssl/test/WolfSSLSessionTest.java b/src/test/com/wolfssl/test/WolfSSLSessionTest.java index f47e677a..403f63d0 100644 --- a/src/test/com/wolfssl/test/WolfSSLSessionTest.java +++ b/src/test/com/wolfssl/test/WolfSSLSessionTest.java @@ -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; @@ -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; @@ -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(); }