@@ -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 ("\t resumeWithSessionTickets()" );
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