Merge pull request #206 from cconlon/sessionDup

JNI: wrap native wolfSSL_SESSION_dup() in WolfSSLSession.duplicateSession()
pull/212/head
JacobBarthelmeh 2024-07-19 09:08:00 -06:00 committed by GitHub
commit 1e9509d9e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 0 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -259,6 +259,7 @@ public class WolfSSLSession {
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 class WolfSSLSession {
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.
*

View File

@ -938,6 +938,7 @@ public class WolfSSLSessionTest {
int ret = 0;
int err = 0;
long sessionPtr = 0;
long sesDup = 0;
Socket cliSock = null;
WolfSSLSession cliSes = null;
@ -1107,6 +1108,19 @@ public class WolfSSLSessionTest {
"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 class WolfSSLSessionTest {
if (sessionPtr != 0) {
cliSes.freeSession(sessionPtr);
}
if (sesDup != 0) {
cliSes.freeSession(sesDup);
}
if (cliSes != null) {
cliSes.freeSSL();
}