Skip to content

Commit 321b350

Browse files
committed
JNI: add tests for session tickets
remove whitespace test changes from copilot
1 parent b0049cc commit 321b350

2 files changed

Lines changed: 233 additions & 6 deletions

File tree

src/java/com/wolfssl/WolfSSLSession.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5186,9 +5186,9 @@ public synchronized boolean sessionTicketsEnabled()
51865186
return this.sessionTicketsEnabled;
51875187
}
51885188

5189-
/**
5189+
/**
51905190
* Get session ticket for this session if session tickets are enabled.
5191-
*
5191+
*
51925192
* @return session ticket as byte array, or null if not available.
51935193
* @throws IllegalStateException WolfSSLSession has been freed.
51945194
*/
@@ -5215,10 +5215,10 @@ public synchronized byte[] getSessionTicket() throws IllegalStateException {
52155215

52165216
/**
52175217
* Set session ticket for this session.
5218-
*
5218+
*
52195219
* @param sessionTicket session ticket to set for this session.
52205220
* @return WolfSSL.SSL_SUCCESS on success, otherwise negative.
5221-
*
5221+
*
52225222
* @throws IllegalStateException WolfSSLSession has been freed
52235223
*/
52245224
public int setSessionTicket(byte[] sessionTicket){
@@ -5229,7 +5229,7 @@ public int setSessionTicket(byte[] sessionTicket){
52295229
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
52305230
WolfSSLDebug.INFO, this.sslPtr,
52315231
() -> "entered setSessionTicket()");
5232-
5232+
52335233
if (sessionTicket != null && sessionTicket.length > 0) {
52345234
ret = setSessionTicket(this.sslPtr, sessionTicket);
52355235
} else {
@@ -5240,8 +5240,12 @@ public int setSessionTicket(byte[] sessionTicket){
52405240
}
52415241

52425242
}
5243+
} else {
5244+
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
5245+
WolfSSLDebug.INFO, this.sslPtr,
5246+
() -> "session tickets not enabled");
52435247
}
5244-
5248+
52455249
return ret;
52465250
}
52475251

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

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,229 @@ public void test_WolfSSLSession_useSessionTicket()
457457
System.out.println("\t\t... passed");
458458
}
459459

460+
@Test
461+
public void test_WolfSSLSession_getSetSessionTickets()
462+
throws WolfSSLException, WolfSSLJNIException {
463+
int ret = 0;
464+
WolfSSLSession ssl = null;
465+
String ticketStr = "This is a session ticket";
466+
byte[] ticket = null;
467+
byte[] retrievedTicket = null;
468+
469+
System.out.print("\t(get/set)SessionTicket()");
470+
471+
try {
472+
ssl = new WolfSSLSession(ctx);
473+
474+
ret = ssl.useSessionTicket();
475+
if (ret != WolfSSL.SSL_SUCCESS &&
476+
ret != WolfSSL.NOT_COMPILED_IN) {
477+
System.out.println("\t\t... failed");
478+
fail("useSessionTicket failed");
479+
}
480+
481+
/* set session ticket */
482+
ticket = ticketStr.getBytes();
483+
484+
ret = ssl.setSessionTicket(ticket);
485+
if (ret != WolfSSL.SSL_SUCCESS &&
486+
ret != WolfSSL.NOT_COMPILED_IN) {
487+
System.out.println("\t... failed");
488+
fail("setSessionTicket failed");
489+
}
490+
491+
retrievedTicket = ssl.getSessionTicket();
492+
493+
if (retrievedTicket == null) {
494+
System.out.println("\t... failed" );
495+
fail("getSessionTicket failed");
496+
}
497+
498+
for (int i = 0; i < ticket.length; i++) {
499+
if (ticket[i] != retrievedTicket[i]) {
500+
System.out.println("\t... failed");
501+
fail("getSessionTicket failed");
502+
}
503+
}
504+
505+
} catch (IllegalStateException e) {
506+
System.out.println("\t... failed");
507+
e.printStackTrace();
508+
509+
} finally {
510+
if (ssl != null) {
511+
ssl.freeSSL();
512+
}
513+
}
514+
515+
System.out.println("\t... passed");
516+
}
517+
518+
public void test_WolfSSLSession_resumeWithSessionTickets()
519+
throws WolfSSLException, WolfSSLJNIException, Exception {
520+
int ret = 0;
521+
int err = 0;
522+
Socket cliSock = null;
523+
byte[] sessionTicket = "This is a session ticket".getBytes();
524+
WolfSSLSession ssl = null;
525+
526+
/* Create client/server WolfSSLContext objects, Server context
527+
* must be final since used inside inner class. */
528+
final WolfSSLContext srvCtx;
529+
WolfSSLContext cliCtx;
530+
531+
System.out.println("\tresumeWithSessionTickets()");
532+
533+
/* Create ServerSocket first to get ephemeral port */
534+
final ServerSocket srvSocket = new ServerSocket(0);
535+
final int port = srvSocket.getLocalPort();
536+
537+
srvCtx = createAndSetupWolfSSLContext(srvCert, srvKey,
538+
WolfSSL.SSL_FILETYPE_PEM, cliCert,
539+
WolfSSL.TLSv1_3_ServerMethod());
540+
cliCtx = createAndSetupWolfSSLContext(cliCert, cliKey,
541+
WolfSSL.SSL_FILETYPE_PEM, caCert,
542+
WolfSSL.TLSv1_3_ClientMethod());
543+
/* Start server, handles 1 resumption */
544+
try {
545+
ExecutorService es = Executors.newSingleThreadExecutor();
546+
es.submit(new Callable<Void>() {
547+
@Override
548+
public Void call() throws Exception {
549+
int ret;
550+
int err;
551+
Socket server = null;
552+
WolfSSLSession srvSes = null;
553+
554+
try {
555+
/* Loop twice to allow handle one resumption */
556+
for (int i = 0; i < 2; i++) {
557+
server = srvSocket.accept();
558+
srvSes = new WolfSSLSession(srvCtx);
559+
560+
ret = srvSes.setFd(server);
561+
if (ret != WolfSSL.SSL_SUCCESS) {
562+
throw new Exception(
563+
"WolfSSLSession.setFd() failed: " + ret);
564+
}
565+
566+
do {
567+
ret = srvSes.accept();
568+
err = srvSes.getError(ret);
569+
} while (ret != WolfSSL.SSL_SUCCESS &&
570+
(err == WolfSSL.SSL_ERROR_WANT_READ ||
571+
err == WolfSSL.SSL_ERROR_WANT_WRITE));
572+
573+
if (ret != WolfSSL.SSL_SUCCESS) {
574+
throw new Exception(
575+
"WolfSSLSession.accept() failed: " + ret);
576+
}
577+
578+
srvSes.shutdownSSL();
579+
srvSes.freeSSL();
580+
srvSes = null;
581+
}
582+
583+
} finally {
584+
if (srvSes != null) {
585+
srvSes.freeSSL();
586+
}
587+
if (server != null) {
588+
server.close();
589+
}
590+
}
591+
592+
return null;
593+
}
594+
});
595+
596+
} catch (Exception e) {
597+
System.out.println("\t... failed");
598+
e.printStackTrace();
599+
fail();
600+
}
601+
602+
try {
603+
/* ------------------------------------------------------------- */
604+
/* Client connection #1 */
605+
/* ------------------------------------------------------------- */
606+
cliSock = new Socket("localhost", port);
607+
ssl = new WolfSSLSession(cliCtx);
608+
609+
ret = ssl.setFd(cliSock);
610+
if (ret != WolfSSL.SSL_SUCCESS)
611+
throw new Exception("setFd() failed");
612+
613+
do {
614+
ret = ssl.connect();
615+
err = ssl.getError(ret);
616+
} while (ret != WolfSSL.SSL_SUCCESS &&
617+
(err == WolfSSL.SSL_ERROR_WANT_READ ||
618+
err == WolfSSL.SSL_ERROR_WANT_WRITE));
619+
620+
if (ret != WolfSSL.SSL_SUCCESS)
621+
throw new Exception("Initial connect failed");
622+
623+
/* Get session ticket after handshake */
624+
sessionTicket = ssl.getSessionTicket();
625+
626+
assertNotNull("Session ticket was null", sessionTicket);
627+
assertTrue("Session ticket empty", sessionTicket.length > 0);
628+
629+
ssl.shutdownSSL();
630+
ssl.freeSSL();
631+
cliSock.close();
632+
633+
/* ------------------------------------------------------------- */
634+
/* Client connection #2, set session and try resumption */
635+
/* ------------------------------------------------------------- */
636+
cliSock = new Socket("localhost", port);
637+
ssl = new WolfSSLSession(cliCtx);
638+
639+
ret = ssl.setFd(cliSock);
640+
if (ret != WolfSSL.SSL_SUCCESS)
641+
throw new Exception("setFd() failed");
642+
643+
ret = ssl.setSessionTicket(sessionTicket);
644+
if (ret != WolfSSL.SSL_SUCCESS)
645+
throw new Exception("setSessionTicket() failed");
646+
647+
do {
648+
ret = ssl.connect();
649+
err = ssl.getError(ret);
650+
} while (ret != WolfSSL.SSL_SUCCESS &&
651+
(err == WolfSSL.SSL_ERROR_WANT_READ ||
652+
err == WolfSSL.SSL_ERROR_WANT_WRITE));
653+
654+
if (ret != WolfSSL.SSL_SUCCESS)
655+
throw new Exception("Resumption connect failed");
656+
657+
/* Check if session was resumed */
658+
assertEquals("Session was not resumed", 1, ssl.sessionReused());
659+
660+
ssl.shutdownSSL();
661+
ssl.freeSSL();
662+
cliSock.close();
663+
664+
} finally {
665+
/* Free resources */
666+
if (ssl != null) {
667+
ssl.freeSSL();
668+
}
669+
if (cliSock != null) {
670+
cliSock.close();
671+
}
672+
if (srvSocket != null) {
673+
srvSocket.close();
674+
}
675+
if (srvCtx != null) {
676+
srvCtx.free();
677+
}
678+
}
679+
680+
System.out.println("\t... passed");
681+
}
682+
460683
@Test
461684
public void test_WolfSSLSession_getPskIdentity()
462685
throws WolfSSLJNIException, WolfSSLException {

0 commit comments

Comments
 (0)