@@ -457,6 +457,224 @@ 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 , 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_ServerMethod ());
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 ) throw new Exception ("setFd() failed" );
611+
612+ do {
613+ ret = ssl .connect ();
614+ err = ssl .getError (ret );
615+ } while (ret != WolfSSL .SSL_SUCCESS &&
616+ (err == WolfSSL .SSL_ERROR_WANT_READ || err == WolfSSL .SSL_ERROR_WANT_WRITE ));
617+
618+ if (ret != WolfSSL .SSL_SUCCESS ) throw new Exception ("Initial connect failed" );
619+
620+ // Get session ticket after handshake
621+ sessionTicket = ssl .getSessionTicket ();
622+
623+ assertNotNull ("Session ticket was null" , sessionTicket );
624+ assertTrue ("Session ticket empty" , sessionTicket .length > 0 );
625+
626+ ssl .shutdownSSL ();
627+ ssl .freeSSL ();
628+ cliSock .close ();
629+
630+ /* -------------------------------------------------------------- */
631+ /* Client connection #2, set session and try resumption */
632+ /* -------------------------------------------------------------- */
633+ cliSock = new Socket ("localhost" , port );
634+ ssl = new WolfSSLSession (cliCtx );
635+
636+ ret = ssl .setFd (cliSock );
637+ if (ret != WolfSSL .SSL_SUCCESS )
638+ throw new Exception ("setFd() failed" );
639+
640+ ret = ssl .setSessionTicket (sessionTicket );
641+ if (ret != WolfSSL .SSL_SUCCESS )
642+ throw new Exception ("setSessionTicket() failed" );
643+
644+ do {
645+ ret = ssl .connect ();
646+ err = ssl .getError (ret );
647+ } while (ret != WolfSSL .SSL_SUCCESS &&
648+ (err == WolfSSL .SSL_ERROR_WANT_READ || err == WolfSSL .SSL_ERROR_WANT_WRITE ));
649+
650+ if (ret != WolfSSL .SSL_SUCCESS ) throw new Exception ("Resumption connect failed" );
651+
652+ // Check if session was resumed
653+ assertEquals ("Session was not resumed" , 1 , ssl .sessionReused ());
654+
655+ ssl .shutdownSSL ();
656+ ssl .freeSSL ();
657+ cliSock .close ();
658+
659+ } finally {
660+ /* Free resources */
661+ if (ssl != null ) {
662+ ssl .freeSSL ();
663+ }
664+ if (cliSock != null ) {
665+ cliSock .close ();
666+ }
667+ if (srvSocket != null ) {
668+ srvSocket .close ();
669+ }
670+ if (srvCtx != null ) {
671+ srvCtx .free ();
672+ }
673+ }
674+
675+ System .out .println ("\t ... passed" );
676+ }
677+
460678 @ Test
461679 public void test_WolfSSLSession_getPskIdentity ()
462680 throws WolfSSLJNIException , WolfSSLException {
0 commit comments