Skip to content

Commit 43858c6

Browse files
committed
JNI: implement get/set session tickets
1 parent ba7b482 commit 43858c6

3 files changed

Lines changed: 155 additions & 0 deletions

File tree

native/com_wolfssl_WolfSSLSession.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
/* Default wolfSSL_peek() timeout for wolfSSL_get_session(), ms */
4444
#define WOLFSSL_JNI_DEFAULT_PEEK_TIMEOUT 2000
4545
#endif
46+
#ifndef WOLFSSL_MAX_SESSION_TICKET_LEN
47+
#define WOLFSSL_MAX_SESSION_TICKET_LEN 2048
48+
#endif
4649

4750
#include <wolfssl/ssl.h>
4851
#include <wolfssl/error-ssl.h>
@@ -4799,6 +4802,80 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_useSessionTicket
47994802
return ret;
48004803
}
48014804

4805+
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLSession_getSessionTicket
4806+
(JNIEnv* jenv, jobject jcl, jlong sslPtr)
4807+
{
4808+
jbyteArray sessionTicket = NULL;
4809+
#ifdef HAVE_SESSION_TICKET
4810+
int ret = SSL_FAILURE;
4811+
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
4812+
word32 dataSz = WOLFSSL_MAX_SESSION_TICKET_LEN;
4813+
byte* dataBuf = NULL;
4814+
4815+
if (jenv == NULL || ssl == NULL) {
4816+
return NULL;
4817+
}
4818+
4819+
dataBuf = XMALLOC(dataSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4820+
if (dataBuf != NULL){
4821+
/* get ticket data and ticket size */
4822+
ret = wolfSSL_get_SessionTicket(ssl, dataBuf, &dataSz);
4823+
4824+
if (ret == WOLFSSL_SUCCESS && dataSz > 0){
4825+
sessionTicket = (*jenv)->NewByteArray(jenv, dataSz);
4826+
(*jenv)->SetByteArrayRegion(jenv, sessionTicket, 0, dataSz,
4827+
(jbyte*)dataBuf);
4828+
}
4829+
4830+
XFREE(dataBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4831+
}
4832+
#else
4833+
(void)jenv;
4834+
(void)jcl;
4835+
(void)sslPtr;
4836+
(void)dataSz;
4837+
#endif /* HAVE_SESSION_TICKET */
4838+
return sessionTicket;
4839+
}
4840+
4841+
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_setSessionTicket
4842+
(JNIEnv* jenv, jobject jcl, jlong sslPtr, jbyteArray dataBuf)
4843+
{
4844+
int ret = SSL_FAILURE;
4845+
#ifdef HAVE_SESSION_TICKET
4846+
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
4847+
byte* data = NULL;
4848+
word32 dataSz = 0;
4849+
4850+
if (jenv == NULL || ssl == NULL || dataBuf == NULL) {
4851+
return BAD_FUNC_ARG;
4852+
}
4853+
4854+
data = (byte*)(*jenv)->GetByteArrayElements(jenv, dataBuf, NULL);
4855+
dataSz = (*jenv)->GetArrayLength(jenv, dataBuf);
4856+
4857+
if (data != NULL && dataSz > 0) {
4858+
ret = wolfSSL_set_SessionTicket(ssl, data, dataSz);
4859+
if (ret != WOLFSSL_SUCCESS) {
4860+
(*jenv)->ThrowNew(jenv, jcl,
4861+
"failed to set session ticket!");
4862+
}
4863+
}
4864+
else {
4865+
ret = BAD_FUNC_ARG;
4866+
}
4867+
(*jenv)->ReleaseByteArrayElements(jenv, dataBuf,
4868+
(jbyte*)data, JNI_ABORT);
4869+
#else
4870+
(void)jenv;
4871+
(void)jcl;
4872+
(void)sslPtr;
4873+
(void)dataSz;
4874+
ret = NOT_COMPILED_IN;
4875+
#endif /* HAVE_SESSION_TICKET */
4876+
return ret;
4877+
}
4878+
48024879
/* return 1 if last alert received was a close_notify alert, otherwise 0 */
48034880
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_gotCloseNotify
48044881
(JNIEnv* jenv, jobject jcl, jlong sslPtr)

native/com_wolfssl_WolfSSLSession.h

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

src/java/com/wolfssl/WolfSSLSession.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ private native int setTlsHmacInner(long ssl, byte[] inner, long sz,
676676
private native int useSNI(long ssl, byte type, byte[] data);
677677
private native byte[] getSNIRequest(long ssl, byte type);
678678
private native int useSessionTicket(long ssl);
679+
private native byte[] getSessionTicket(long ssl);
680+
private native int setSessionTicket(long ssl, byte[] ticket);
679681
private native int gotCloseNotify(long ssl);
680682
private native int sslSetAlpnProtos(long ssl, byte[] alpnProtos);
681683
private native byte[] sslGet0AlpnSelected(long ssl);
@@ -5184,6 +5186,66 @@ public synchronized boolean sessionTicketsEnabled()
51845186
return this.sessionTicketsEnabled;
51855187
}
51865188

5189+
/**
5190+
* Get session ticket for this session if session tickets are enabled.
5191+
*
5192+
* @return session ticket as byte array, or null if not available.
5193+
* @throws IllegalStateException WolfSSLSession has been freed.
5194+
*/
5195+
public synchronized byte[] getSessionTicket() throws IllegalStateException {
5196+
if (sessionTicketsEnabled()) {
5197+
byte[] sessionTicket = null;
5198+
confirmObjectIsActive();
5199+
5200+
synchronized (sslLock) {
5201+
5202+
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
5203+
WolfSSLDebug.INFO, this.sslPtr,
5204+
() -> "entered getSessionTicket()");
5205+
5206+
sessionTicket = getSessionTicket(this.sslPtr);
5207+
}
5208+
return sessionTicket;
5209+
} else {
5210+
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
5211+
WolfSSLDebug.INFO, this.sslPtr,
5212+
() -> "session tickets not enabled, returning null");
5213+
return null;
5214+
}
5215+
}
5216+
5217+
/**
5218+
* Set session ticket for this session.
5219+
*
5220+
* @param sessionTicket session ticket to set for this session.
5221+
* @return WolfSSL.SSL_SUCCESS on success, otherwise negative.
5222+
*
5223+
* @throws IllegalStateException WolfSSLSession has been freed
5224+
*/
5225+
public int setSessionTicket(byte[] sessionTicket){
5226+
int ret = WolfSSL.SSL_SUCCESS;
5227+
if (sessionTicketsEnabled()){
5228+
confirmObjectIsActive();
5229+
synchronized (sslLock) {
5230+
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
5231+
WolfSSLDebug.INFO, this.sslPtr,
5232+
() -> "entered setSessionTicket()");
5233+
5234+
if (sessionTicket != null && sessionTicket.length > 0) {
5235+
ret = setSessionTicket(this.sslPtr, sessionTicket);
5236+
} else {
5237+
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
5238+
WolfSSLDebug.INFO, this.sslPtr,
5239+
() -> "session ticket is null, not setting");
5240+
ret = WolfSSL.SSL_FAILURE;
5241+
}
5242+
5243+
}
5244+
}
5245+
5246+
return ret;
5247+
}
5248+
51875249
/**
51885250
* Set ALPN extension protocol for this session from encoded byte array.
51895251
* Calls SSL_set_alpn_protos() at native level. Format starts with

0 commit comments

Comments
 (0)