3838import java .security .NoSuchAlgorithmException ;
3939import java .security .UnrecoverableKeyException ;
4040import java .util .ArrayList ;
41+ import java .util .Arrays ;
4142import java .util .Collections ;
4243import java .util .Date ;
4344import java .util .Enumeration ;
@@ -96,8 +97,8 @@ protected WolfSSLAuthStore(KeyManager[] keyman, TrustManager[] trustman,
9697 this .currentVersion = version ;
9798 store = new SessionStore <Integer ,
9899 WolfSSLImplementSSLSession >(defaultCacheSize );
99- this .serverCtx = new WolfSSLSessionContext (this , defaultCacheSize );
100- this .clientCtx = new WolfSSLSessionContext (this , defaultCacheSize );
100+ this .serverCtx = new WolfSSLSessionContext (this , defaultCacheSize , WolfSSL . WOLFSSL_SERVER_END );
101+ this .clientCtx = new WolfSSLSessionContext (this , defaultCacheSize , WolfSSL . WOLFSSL_CLIENT_END );
101102 }
102103
103104 /**
@@ -259,11 +260,13 @@ protected WolfSSLSessionContext getClientContext() {
259260 /**
260261 * Reset the size of the array to cache sessions
261262 * @param sz new array size
263+ * @param side server/client side for cache resize
262264 */
263- protected void resizeCache (int sz ) {
265+ protected void resizeCache (int sz , int side ) {
264266 SessionStore <Integer , WolfSSLImplementSSLSession > newStore =
265267 new SessionStore <Integer , WolfSSLImplementSSLSession >(sz );
266268
269+ //@TODO check for side server/client, currently a resize is for all
267270 store .putAll (newStore );
268271 store = newStore ;
269272 }
@@ -303,12 +306,7 @@ protected WolfSSLImplementSSLSession getSession(WolfSSLSession ssl,
303306 /* not found in stored sessions create a new one */
304307 ses = new WolfSSLImplementSSLSession (ssl , port , host , this );
305308 ses .setValid (true ); /* new sessions marked as valid */
306- if (ssl .getSide () == WolfSSL .WOLFSSL_SERVER_END ) {
307- ses .setSessionContext (serverCtx );
308- }
309- else {
310- ses .setSessionContext (clientCtx );
311- }
309+ ses .setPseudoSessionId (Integer .toString (ssl .hashCode ()).getBytes ());
312310 }
313311 else {
314312 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
@@ -328,12 +326,7 @@ protected WolfSSLImplementSSLSession getSession(WolfSSLSession ssl) {
328326 WolfSSLImplementSSLSession ses = new WolfSSLImplementSSLSession (ssl , this );
329327 if (ses != null ) {
330328 ses .setValid (true );
331- if (ssl .getSide () == WolfSSL .WOLFSSL_SERVER_END ) {
332- ses .setSessionContext (serverCtx );
333- }
334- else {
335- ses .setSessionContext (clientCtx );
336- }
329+ ses .setPseudoSessionId (Integer .toString (ssl .hashCode ()).getBytes ());
337330 }
338331 return ses ;
339332 }
@@ -345,49 +338,64 @@ protected WolfSSLImplementSSLSession getSession(WolfSSLSession ssl) {
345338 */
346339 protected int addSession (WolfSSLImplementSSLSession session ) {
347340 String toHash ;
341+ int hashCode = 0 ;
348342
349343 if (session .getPeerHost () != null ) {
350344 /* register into session table for resumption */
351345 session .fromTable = true ;
352346 toHash = session .getPeerHost ().concat (Integer .toString (
353347 session .getPeerPort ()));
354- store .put (toHash .hashCode (), session );
355-
348+ hashCode = toHash .hashCode ();
349+ }
350+ else {
351+ /* if no peer host is available then create hash key from
352+ * session id */
353+ hashCode = Arrays .toString (session .getId ()).hashCode ();
354+ }
356355
356+ if (hashCode != 0 && store .containsKey (hashCode ) != true ) {
357357 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
358358 "stored session in cache table (host: " +
359359 session .getPeerHost () + ", port: " +
360- session .getPeerPort () + ")" );
360+ session .getPeerPort () + ") " +
361+ "hashCode = " + hashCode + " side = " + session .getSide ());
362+ store .put (hashCode , session );
361363 }
362-
363364 return WolfSSL .SSL_SUCCESS ;
364365 }
365366
366367
367368 /**
368- * @returns enumerated session IDs
369+ * Internal function to return a list of all session ID's
370+ * @param side server or client side to get list of ID's from
371+ * @return enumerated session IDs
369372 */
370- protected Enumeration <byte []> getAllIDs () {
373+ protected Enumeration <byte []> getAllIDs (int side ) {
371374 List <byte []> ret = new ArrayList <byte []>();
372375
373376 for (Object obj : store .values ()) {
374377 WolfSSLImplementSSLSession current = (WolfSSLImplementSSLSession )obj ;
375- ret .add (current .getId ());
378+ if (current .getSide () == side ) {
379+ ret .add (current .getId ());
380+ }
376381 }
377382 return Collections .enumeration (ret );
378383 }
379384
380385
381386 /**
382387 * Getter function for session with session id 'ID'
388+ * @param ID the session id to search for
389+ * @param side if the session is expected on the server or client side
383390 * @return session from the store that has session id 'ID'
384391 */
385- protected WolfSSLImplementSSLSession getSession (byte [] ID ) {
392+ protected WolfSSLImplementSSLSession getSession (byte [] ID , int side ) {
386393 WolfSSLImplementSSLSession ret = null ;
387394
388395 for (Object obj : store .values ()) {
389396 WolfSSLImplementSSLSession current = (WolfSSLImplementSSLSession )obj ;
390- if (java .util .Arrays .equals (ID , current .getId ())) {
397+ if (current .getSide () == side &&
398+ java .util .Arrays .equals (ID , current .getId ())) {
391399 ret = current ;
392400 break ;
393401 }
@@ -399,9 +407,10 @@ protected WolfSSLImplementSSLSession getSession(byte[] ID) {
399407 /**
400408 * Goes through the list of sessions and checks for timeouts. If timed out
401409 * then the session is invalidated.
402- * @params in the updated timeout value to check against
410+ * @param in the updated timeout value to check against
411+ * @param side server or client side getting the timeout update
403412 */
404- protected void updateTimeouts (int in ) {
413+ protected void updateTimeouts (int in , int side ) {
405414 Date currentDate = new Date ();
406415 long now = currentDate .getTime ();
407416
@@ -410,18 +419,20 @@ protected void updateTimeouts(int in) {
410419 WolfSSLImplementSSLSession current =
411420 (WolfSSLImplementSSLSession )obj ;
412421
413- /* difference in seconds */
414- diff = (now - current .creation .getTime ()) / 1000 ;
422+ if (current .getSide () == side ) {
423+ /* difference in seconds */
424+ diff = (now - current .creation .getTime ()) / 1000 ;
415425
416- if (diff < 0 ) {
426+ if (diff < 0 ) {
417427 /* session is from the future ... */ //@TODO
418428
419- }
429+ }
420430
421- if (in > 0 && diff > in ) {
422- current .invalidate ();
431+ if (in > 0 && diff > in ) {
432+ current .invalidate ();
433+ }
434+ current .setNativeTimeout (in );
423435 }
424- current .setNativeTimeout (in );
425436 }
426437 }
427438
0 commit comments