Skip to content

Commit 706902d

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents b429e94 + a5ea53d commit 706902d

File tree

17 files changed

+276
-109
lines changed

17 files changed

+276
-109
lines changed

pg/src/main/java/org/bouncycastle/apache/bzip2/CBZip2InputStream.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public CBZip2InputStream(InputStream zStream)
140140
beginBlock();
141141
}
142142

143+
public void close() throws IOException
144+
{
145+
implClose(true);
146+
}
147+
143148
public int read()
144149
throws IOException
145150
{
@@ -186,7 +191,7 @@ private void beginBlock()
186191
throw new IOException("Stream CRC error");
187192
}
188193

189-
bsFinishedWithStream();
194+
// TODO If not a LeaveOpen stream, should we check that we are at the end of stream here?
190195
streamEnd = true;
191196
return;
192197
}
@@ -250,22 +255,16 @@ private void endBlock()
250255
streamCRC = Integers.rotateLeft(streamCRC, 1) ^ blockFinalCRC;
251256
}
252257

253-
private void bsFinishedWithStream()
258+
private void implClose(boolean closeInput) throws IOException
254259
{
255-
try
260+
if (this.bsStream != null)
256261
{
257-
if (this.bsStream != null)
262+
if (closeInput)
258263
{
259-
if (this.bsStream != System.in)
260-
{
261-
this.bsStream.close();
262-
this.bsStream = null;
263-
}
264+
this.bsStream.close();
264265
}
265-
}
266-
catch (IOException ioe)
267-
{
268-
//ignore
266+
267+
this.bsStream = null;
269268
}
270269
}
271270

tls/src/main/java/org/bouncycastle/jsse/BCExtendedSSLSession.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
import java.util.Collections;
44
import java.util.List;
55

6+
import javax.crypto.SecretKey;
7+
import javax.net.ssl.SSLKeyException;
68
import javax.net.ssl.SSLSession;
79

810
public abstract class BCExtendedSSLSession implements SSLSession
911
{
12+
public byte[] exportKeyingMaterialData(String label, byte[] context, int length) throws SSLKeyException
13+
{
14+
throw new UnsupportedOperationException();
15+
}
16+
17+
public SecretKey exportKeyingMaterialKey(String keyAlg, String label, byte[] context, int length)
18+
throws SSLKeyException
19+
{
20+
throw new UnsupportedOperationException();
21+
}
22+
1023
public abstract String[] getLocalSupportedSignatureAlgorithms();
1124

1225
public String[] getLocalSupportedSignatureAlgorithmsBC()

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSession.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.concurrent.atomic.AtomicLong;
56

67
import org.bouncycastle.jsse.BCSNIServerName;
78
import org.bouncycastle.tls.CipherSuite;
@@ -15,15 +16,28 @@ class ProvSSLSession
1516
protected final TlsSession tlsSession;
1617
protected final SessionParameters sessionParameters;
1718
protected final JsseSessionParameters jsseSessionParameters;
19+
protected final AtomicLong lastAccessedTime;
1820

1921
ProvSSLSession(ProvSSLSessionContext sslSessionContext, ConcurrentHashMap<String, Object> valueMap, String peerHost,
20-
int peerPort, TlsSession tlsSession, JsseSessionParameters jsseSessionParameters)
22+
int peerPort, long creationTime, TlsSession tlsSession, JsseSessionParameters jsseSessionParameters)
2123
{
22-
super(sslSessionContext, valueMap, peerHost, peerPort);
24+
super(sslSessionContext, valueMap, peerHost, peerPort, creationTime);
2325

2426
this.tlsSession = tlsSession;
2527
this.sessionParameters = tlsSession == null ? null : tlsSession.exportSessionParameters();
2628
this.jsseSessionParameters = jsseSessionParameters;
29+
this.lastAccessedTime = new AtomicLong(creationTime);
30+
}
31+
32+
long access()
33+
{
34+
long accessTime = getCurrentTime(), previous;
35+
do
36+
{
37+
previous = lastAccessedTime.get();
38+
}
39+
while (accessTime > previous && !lastAccessedTime.compareAndSet(previous, accessTime));
40+
return accessTime;
2741
}
2842

2943
@Override
@@ -50,6 +64,11 @@ protected JsseSessionParameters getJsseSessionParameters()
5064
return jsseSessionParameters;
5165
}
5266

67+
public long getLastAccessedTime()
68+
{
69+
return lastAccessedTime.get();
70+
}
71+
5372
@Override
5473
protected org.bouncycastle.tls.Certificate getLocalCertificateTLS()
5574
{
@@ -110,6 +129,7 @@ public boolean isValid()
110129
static final ProvSSLSession createDummySession()
111130
{
112131
// NB: Allow session value binding on failed connections for SunJSSE compatibility
113-
return new ProvSSLSession(null, createValueMap(), null, -1, null, new JsseSessionParameters(null, null));
132+
return new ProvSSLSession(null, createValueMap(), null, -1, getCurrentTime(), null,
133+
new JsseSessionParameters(null, null));
114134
}
115135
}

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSessionBase.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.security.cert.Certificate;
55
import java.security.cert.X509Certificate;
66
import java.util.concurrent.ConcurrentHashMap;
7-
import java.util.concurrent.atomic.AtomicLong;
87
import java.util.concurrent.atomic.AtomicReference;
98

109
import javax.net.ssl.SSLPeerUnverifiedException;
@@ -33,20 +32,18 @@ abstract class ProvSSLSessionBase
3332
protected final int peerPort;
3433
protected final long creationTime;
3534
protected final SSLSession exportSSLSession;
36-
protected final AtomicLong lastAccessedTime;
3735

3836
ProvSSLSessionBase(ProvSSLSessionContext sslSessionContext, ConcurrentHashMap<String, Object> valueMap,
39-
String peerHost, int peerPort)
37+
String peerHost, int peerPort, long creationTime)
4038
{
4139
this.sslSessionContext = new AtomicReference<ProvSSLSessionContext>(sslSessionContext);
4240
this.valueMap = valueMap;
4341
this.fipsMode = (null == sslSessionContext) ? false : sslSessionContext.getContextData().isFipsMode();
4442
this.crypto = (null == sslSessionContext) ? null : sslSessionContext.getContextData().getCrypto();
4543
this.peerHost = peerHost;
4644
this.peerPort = peerPort;
47-
this.creationTime = System.currentTimeMillis();
45+
this.creationTime = creationTime;
4846
this.exportSSLSession = SSLSessionUtil.exportSSLSession(this);
49-
this.lastAccessedTime = new AtomicLong(creationTime);
5047
}
5148

5249
protected abstract int getCipherSuiteTLS();
@@ -70,15 +67,6 @@ SSLSession getExportSSLSession()
7067
return exportSSLSession;
7168
}
7269

73-
void accessedAt(long accessTime)
74-
{
75-
long current = lastAccessedTime.get();
76-
if (accessTime > current)
77-
{
78-
lastAccessedTime.compareAndSet(current, accessTime);
79-
}
80-
}
81-
8270
@Override
8371
public boolean equals(Object obj)
8472
{
@@ -117,11 +105,6 @@ public byte[] getId()
117105
return TlsUtils.isNullOrEmpty(id) ? TlsUtils.EMPTY_BYTES : id.clone();
118106
}
119107

120-
public long getLastAccessedTime()
121-
{
122-
return lastAccessedTime.get();
123-
}
124-
125108
public Certificate[] getLocalCertificates()
126109
{
127110
if (null != crypto)
@@ -362,4 +345,9 @@ protected static ConcurrentHashMap<String, Object> createValueMap()
362345
{
363346
return new ConcurrentHashMap<String, Object>();
364347
}
348+
349+
protected static long getCurrentTime()
350+
{
351+
return System.currentTimeMillis();
352+
}
365353
}

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSessionContext.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ synchronized ProvSSLSession getSessionImpl(byte[] sessionID)
6363
{
6464
processQueue();
6565

66-
return accessSession(mapGet(sessionsByID, makeSessionID(sessionID)));
66+
return getSessionImpl(mapGet(sessionsByID, makeSessionID(sessionID)));
6767
}
6868

6969
synchronized ProvSSLSession getSessionImpl(String hostName, int port)
7070
{
7171
processQueue();
7272

7373
SessionEntry sessionEntry = mapGet(sessionsByPeer, makePeerKey(hostName, port));
74-
ProvSSLSession session = accessSession(sessionEntry);
74+
ProvSSLSession session = getSessionImpl(sessionEntry);
7575
if (session != null)
7676
{
7777
// NOTE: For the current simple cache implementation, need to 'access' the sessionByIDs entry
@@ -96,8 +96,8 @@ synchronized ProvSSLSession reportSession(ProvSSLSessionHandshake handshakeSessi
9696

9797
if (!addToCache)
9898
{
99-
return new ProvSSLSession(this, handshakeSession.getValueMap(), peerHost, peerPort, tlsSession,
100-
jsseSessionParameters);
99+
return new ProvSSLSession(this, handshakeSession.getValueMap(), peerHost, peerPort,
100+
handshakeSession.getCreationTime(), tlsSession, jsseSessionParameters);
101101
}
102102

103103
SessionID sessionID = makeSessionID(tlsSession.getSessionID());
@@ -106,8 +106,8 @@ synchronized ProvSSLSession reportSession(ProvSSLSessionHandshake handshakeSessi
106106
ProvSSLSession session = sessionEntry == null ? null : sessionEntry.get();
107107
if (null == session || session.getTlsSession() != tlsSession)
108108
{
109-
session = new ProvSSLSession(this, handshakeSession.getValueMap(), peerHost, peerPort, tlsSession,
110-
jsseSessionParameters);
109+
session = new ProvSSLSession(this, handshakeSession.getValueMap(), peerHost, peerPort,
110+
handshakeSession.getCreationTime(), tlsSession, jsseSessionParameters);
111111

112112
if (null != sessionID)
113113
{
@@ -207,17 +207,15 @@ public synchronized void setSessionTimeout(int seconds) throws IllegalArgumentEx
207207
removeAllExpiredSessions();
208208
}
209209

210-
private ProvSSLSession accessSession(SessionEntry sessionEntry)
210+
private ProvSSLSession getSessionImpl(SessionEntry sessionEntry)
211211
{
212212
if (sessionEntry != null)
213213
{
214214
ProvSSLSession session = sessionEntry.get();
215215
if (session != null)
216216
{
217-
long currentTimeMillis = System.currentTimeMillis();
218-
if (!invalidateIfCreatedBefore(sessionEntry, getCreationTimeLimit(currentTimeMillis)))
217+
if (!invalidateIfCreatedBefore(sessionEntry, getCreationTimeLimit()))
219218
{
220-
session.accessedAt(currentTimeMillis);
221219
return session;
222220
}
223221
}
@@ -227,9 +225,9 @@ private ProvSSLSession accessSession(SessionEntry sessionEntry)
227225
return null;
228226
}
229227

230-
private long getCreationTimeLimit(long expiryTimeMillis)
228+
private long getCreationTimeLimit()
231229
{
232-
return sessionTimeoutSeconds < 1 ? Long.MIN_VALUE : (expiryTimeMillis - 1000L * sessionTimeoutSeconds);
230+
return sessionTimeoutSeconds < 1 ? Long.MIN_VALUE : (System.currentTimeMillis() - 1000L * sessionTimeoutSeconds);
233231
}
234232

235233
private boolean invalidateIfCreatedBefore(SessionEntry sessionEntry, long creationTimeLimit)
@@ -267,7 +265,7 @@ private void removeAllExpiredSessions()
267265
{
268266
processQueue();
269267

270-
long creationTimeLimit = getCreationTimeLimit(System.currentTimeMillis());
268+
long creationTimeLimit = getCreationTimeLimit();
271269

272270
Iterator<SessionEntry> iter = sessionsByID.values().iterator();
273271
while (iter.hasNext())

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSessionHandshake.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ class ProvSSLSessionHandshake
2020
ProvSSLSessionHandshake(ProvSSLSessionContext sslSessionContext, String peerHost, int peerPort,
2121
SecurityParameters securityParameters, JsseSecurityParameters jsseSecurityParameters)
2222
{
23-
this(sslSessionContext, createValueMap(), peerHost, peerPort, securityParameters, jsseSecurityParameters);
23+
this(sslSessionContext, createValueMap(), peerHost, peerPort, getCurrentTime(), securityParameters,
24+
jsseSecurityParameters);
2425
}
2526

2627
protected ProvSSLSessionHandshake(ProvSSLSessionContext sslSessionContext,
27-
ConcurrentHashMap<String, Object> valueMap, String peerHost, int peerPort,
28+
ConcurrentHashMap<String, Object> valueMap, String peerHost, int peerPort, long creationTime,
2829
SecurityParameters securityParameters, JsseSecurityParameters jsseSecurityParameters)
2930
{
30-
super(sslSessionContext, valueMap, peerHost, peerPort);
31+
super(sslSessionContext, valueMap, peerHost, peerPort, creationTime);
3132

3233
this.securityParameters = securityParameters;
3334
this.jsseSecurityParameters = jsseSecurityParameters;
@@ -62,6 +63,11 @@ protected JsseSessionParameters getJsseSessionParameters()
6263
return null;
6364
}
6465

66+
public long getLastAccessedTime()
67+
{
68+
return getCreationTime();
69+
}
70+
6571
@Override
6672
protected org.bouncycastle.tls.Certificate getLocalCertificateTLS()
6773
{

tls/src/main/java/org/bouncycastle/jsse/provider/ProvSSLSessionResumed.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ class ProvSSLSessionResumed
1111
protected final TlsSession tlsSession;
1212
protected final SessionParameters sessionParameters;
1313
protected final JsseSessionParameters jsseSessionParameters;
14+
protected final long lastAccessedTime;
1415

1516
ProvSSLSessionResumed(ProvSSLSessionContext sslSessionContext, String peerHost, int peerPort,
1617
SecurityParameters securityParameters, JsseSecurityParameters jsseSecurityParameters,
1718
ProvSSLSession resumedSession)
1819
{
19-
super(sslSessionContext, resumedSession.getValueMap(), peerHost, peerPort, securityParameters,
20-
jsseSecurityParameters);
20+
super(sslSessionContext, resumedSession.getValueMap(), peerHost, peerPort, resumedSession.getCreationTime(),
21+
securityParameters, jsseSecurityParameters);
2122

2223
this.tlsSession = resumedSession.getTlsSession();
2324
this.sessionParameters = tlsSession.exportSessionParameters();
2425
this.jsseSessionParameters = resumedSession.getJsseSessionParameters();
26+
this.lastAccessedTime = resumedSession.access();
2527
}
2628

2729
@Override
@@ -36,6 +38,11 @@ protected byte[] getIDArray()
3638
return tlsSession.getSessionID();
3739
}
3840

41+
public long getLastAccessedTime()
42+
{
43+
return lastAccessedTime;
44+
}
45+
3946
@Override
4047
protected JsseSessionParameters getJsseSessionParameters()
4148
{

tls/src/main/java/org/bouncycastle/jsse/provider/ProvTlsServer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,10 @@ protected boolean selectCipherSuite(int cipherSuite) throws IOException
340340
}
341341
}
342342

343-
boolean result = super.selectCipherSuite(cipherSuite);
344-
if (result)
345-
{
346-
this.credentials = cipherSuiteCredentials;
347-
}
348-
return result;
343+
this.selectedCipherSuite = cipherSuite;
344+
this.credentials = cipherSuiteCredentials;
345+
346+
return true;
349347
}
350348

351349
@Override

0 commit comments

Comments
 (0)