Skip to content

Commit ee2a6ee

Browse files
add pseudo session ID for TLS 1.3
1 parent c8ee9d5 commit ee2a6ee

5 files changed

Lines changed: 277 additions & 79 deletions

File tree

src/java/com/wolfssl/provider/jsse/WolfSSLAuthStore.java

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.security.NoSuchAlgorithmException;
3939
import java.security.UnrecoverableKeyException;
4040
import java.util.ArrayList;
41+
import java.util.Arrays;
4142
import java.util.Collections;
4243
import java.util.Date;
4344
import 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

src/java/com/wolfssl/provider/jsse/WolfSSLEngineHelper.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,15 @@ protected void initHandshake() throws SSLException {
478478
this.session = this.authStore.getSession(ssl, this.port, this.hostname,
479479
this.clientMode);
480480

481+
if (this.session != null && this.clientMode) {
482+
this.session.setSessionContext(authStore.getClientContext());
483+
this.session.setSide(WolfSSL.WOLFSSL_CLIENT_END);
484+
}
485+
else {
486+
this.session.setSessionContext(authStore.getServerContext());
487+
this.session.setSide(WolfSSL.WOLFSSL_SERVER_END);
488+
}
489+
481490
if (this.session != null && this.sessionCreation == false &&
482491
!this.session.fromTable) {
483492
/* new handshakes can not be made in this case. */
@@ -491,8 +500,7 @@ protected void initHandshake() throws SSLException {
491500
throw new SSLHandshakeException("Session creation not allowed");
492501
}
493502

494-
if (this.session != null && this.clientMode == true &&
495-
this.sessionCreation) {
503+
if (this.session != null && this.sessionCreation) {
496504
/* can only add new sessions to the resumption table if session
497505
* creation is allowed */
498506
this.authStore.addSession(this.session);
@@ -564,7 +572,7 @@ protected int doHandshake(int isSSLEngine) throws SSLException {
564572
* Saves session on connection close for resumption
565573
*/
566574
protected void saveSession() {
567-
if (this.session.isValid()) {
575+
if (this.session != null && this.session.isValid()) {
568576
this.session.setResume();
569577
}
570578
}

src/java/com/wolfssl/provider/jsse/WolfSSLImplementSSLSession.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.security.Principal;
2828
import java.security.cert.Certificate;
2929
import java.util.Date;
30-
import java.util.Enumeration;
3130
import java.util.HashMap;
3231
import java.util.logging.Level;
3332
import java.util.logging.Logger;
@@ -55,6 +54,8 @@ public class WolfSSLImplementSSLSession implements SSLSession {
5554
private final String host;
5655
Date creation;
5756
Date accessed; /* when new connection was made using session */
57+
byte pseudoSessionID[] = null; /* used with TLS 1.3*/
58+
private int side = 0;
5859

5960
/**
6061
* has this session been registered
@@ -106,7 +107,17 @@ public synchronized byte[] getId() {
106107
if (ssl == null) {
107108
return new byte[0];
108109
}
109-
return this.ssl.getSessionID();
110+
try {
111+
if (this.ssl.getVersion().equals("TLSv1.3")) {
112+
return this.pseudoSessionID;
113+
}
114+
else {
115+
return this.ssl.getSessionID();
116+
}
117+
} catch (IllegalStateException | WolfSSLJNIException e) {
118+
e.printStackTrace();
119+
return null;
120+
}
110121
}
111122

112123
public synchronized SSLSessionContext getSessionContext() {
@@ -138,8 +149,8 @@ public boolean isValid() {
138149
}
139150

140151
/**
141-
* After a connection has been established or on restoring connection the session
142-
* is then valid and can be joined or resumed
152+
* After a connection has been established or on restoring connection the
153+
* session is then valid and can be joined or resumed
143154
* @param in true/false valid boolean
144155
*/
145156
protected void setValid(boolean in) {
@@ -376,4 +387,32 @@ protected synchronized void setResume() {
376387
protected void setNativeTimeout(long in) {
377388
ssl.setSessTimeout(in);
378389
}
390+
391+
392+
/**
393+
* TLS 1.3 removed session ID's, this can be used instead to
394+
* search for sessions.
395+
* @param id pseudo session ID at the java wrapper level
396+
*/
397+
protected synchronized void setPseudoSessionId(byte id[]) {
398+
this.pseudoSessionID = id.clone();
399+
}
400+
401+
402+
/**
403+
* Sets (server/client) side of the connection for session
404+
* @param in the side to be set, server or client
405+
*/
406+
protected void setSide(int in) {
407+
this.side = in;
408+
}
409+
410+
411+
/**
412+
* Returns the side session is on (server/client)
413+
* @return WolfSSL.* integer value of side on
414+
*/
415+
protected int getSide() {
416+
return this.side;
417+
}
379418
}

src/java/com/wolfssl/provider/jsse/WolfSSLSessionContext.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,34 @@
2121

2222
package com.wolfssl.provider.jsse;
2323

24-
import com.wolfssl.WolfSSL;
25-
import com.wolfssl.WolfSSLException;
26-
import com.wolfssl.WolfSSLJNIException;
27-
import com.wolfssl.WolfSSLSession;
2824
import java.util.Enumeration;
29-
import java.util.logging.Level;
30-
import java.util.logging.Logger;
3125
import javax.net.ssl.SSLSession;
3226
import javax.net.ssl.SSLSessionContext;
3327

3428
public class WolfSSLSessionContext implements SSLSessionContext {
3529
private WolfSSLAuthStore store;
36-
private WolfSSLSession sslCtx;
37-
3830
private int sesTimout;
3931
private int sesCache;
32+
private int side;
4033

41-
public WolfSSLSessionContext(WolfSSLAuthStore in, int defaultCacheSize) {
34+
public WolfSSLSessionContext(WolfSSLAuthStore in, int defaultCacheSize,
35+
int side) {
4236
this.store = in;
4337
this.sesCache = defaultCacheSize;
44-
this.sesTimout = 86400; /* this is the default value found in SunJSSE too */
38+
this.sesTimout = 86400; /* this is the default value in SunJSSE too */
39+
this.side = side;
4540
}
4641

4742

4843
@Override
4944
public SSLSession getSession(byte[] sessionId) {
50-
return store.getSession(sessionId);
45+
return store.getSession(sessionId, side);
5146
}
5247

5348

5449
@Override
5550
public Enumeration<byte[]> getIds() {
56-
return store.getAllIDs();
51+
return store.getAllIDs(side);
5752
}
5853

5954

@@ -62,7 +57,7 @@ public void setSessionTimeout(int in) throws IllegalArgumentException {
6257
this.sesTimout = in;
6358

6459
/* check for any new timeouts after timeout has been set */
65-
store.updateTimeouts(in);
60+
store.updateTimeouts(in, this.side);
6661
}
6762

6863
@Override
@@ -81,7 +76,7 @@ public void setSessionCacheSize(int in)
8176

8277
/* resize store array if needed */
8378
if (this.sesCache != in) {
84-
store.resizeCache(in);
79+
store.resizeCache(in, this.side);
8580
}
8681
this.sesCache = in;
8782
}
@@ -90,5 +85,4 @@ public void setSessionCacheSize(int in)
9085
public int getSessionCacheSize() {
9186
return this.sesCache;
9287
}
93-
9488
}

0 commit comments

Comments
 (0)