@@ -331,8 +331,9 @@ protected WolfSSLImplementSSLSession getSession(
331331 return null ;
332332 }
333333
334- /* Return new session if in server mode, or if host is null */
335- if (!clientMode || host == null ) {
334+ /* Unknown port (-1) is a valid SSLEngine host hint.
335+ * Skip cache keying. */
336+ if (!clientMode || host == null || port < 0 ) {
336337 return this .getSession (ssl , clientMode , host , port );
337338 }
338339
@@ -693,9 +694,15 @@ protected int addSession(WolfSSLImplementSSLSession session) {
693694 }
694695
695696 /**
696- * Internal function to return a list of all session ID's
697+ * Internal function to return a list of valid session IDs.
698+ *
699+ * Expired sessions should already have been invalidated before this call
700+ * via updateTimeouts(), but callers may also invalidate sessions for
701+ * other reasons. Filter validity here so callers can avoid an extra
702+ * per-ID lookup.
703+ *
697704 * @param side server or client side to get list of ID's from
698- * @return enumerated session IDs
705+ * @return enumerated valid session IDs
699706 */
700707 protected Enumeration <byte []> getAllIDs (int side ) {
701708 List <byte []> ret = new ArrayList <>();
@@ -704,7 +711,7 @@ protected Enumeration<byte[]> getAllIDs(int side) {
704711 for (Object obj : store .values ()) {
705712 WolfSSLImplementSSLSession current =
706713 (WolfSSLImplementSSLSession )obj ;
707- if (current .getSide () == side ) {
714+ if (current .getSide () == side && current . isValid () ) {
708715 ret .add (current .getId ());
709716 }
710717 }
@@ -758,14 +765,24 @@ protected void updateTimeouts(int in, int side) {
758765 diff = (now - current .creation .getTime ()) / 1000 ;
759766
760767 if (diff < 0 ) {
761- /* session is from the future ... */ //@ TODO
768+ /* session is from the future ... */ /* TODO */
762769
763770 }
764771
765- if (in > 0 && diff > in ) {
772+ if (in > 0 && diff >= in ) {
773+ current .invalidate ();
774+ }
775+ try {
776+ current .setNativeTimeout (in );
777+ } catch (IllegalStateException e ) {
778+ /* Native WolfSSLSession has been freed,
779+ * invalidate this session entry */
780+ WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
781+ () -> "Native session freed while updating " +
782+ "timeout, invalidating cache entry: " +
783+ e .getMessage ());
766784 current .invalidate ();
767785 }
768- current .setNativeTimeout (in );
769786 }
770787 }
771788 }
@@ -803,4 +820,3 @@ protected synchronized void finalize() throws Throwable {
803820 super .finalize ();
804821 }
805822}
806-
0 commit comments