Skip to content

Commit cddc6ce

Browse files
djelinskiXueleiFan
authored andcommitted
8275811: Incorrect instance to dispose
Reviewed-by: xuelei
1 parent b0a463f commit cddc6ce

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

Diff for: src/java.base/share/classes/sun/security/ssl/InputRecord.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void changeReadCiphers(SSLReadCipher readCipher) {
122122
* Since MAC's doFinal() is called for every SSL/TLS packet, it's
123123
* not necessary to do the same with MAC's.
124124
*/
125-
readCipher.dispose();
125+
this.readCipher.dispose();
126126

127127
this.readCipher = readCipher;
128128
}

Diff for: src/java.base/share/classes/sun/security/ssl/OutputRecord.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ abstract void encodeHandshake(byte[] buffer,
141141
// SSLEngine and SSLSocket
142142
abstract void encodeChangeCipherSpec() throws IOException;
143143

144+
// SSLEngine and SSLSocket
145+
void disposeWriteCipher() {
146+
throw new UnsupportedOperationException();
147+
}
148+
144149
// apply to SSLEngine only
145150
Ciphertext encode(
146151
ByteBuffer[] srcs, int srcsOffset, int srcsLength,
@@ -190,7 +195,7 @@ void changeWriteCiphers(SSLWriteCipher writeCipher,
190195
* Since MAC's doFinal() is called for every SSL/TLS packet, it's
191196
* not necessary to do the same with MAC's.
192197
*/
193-
writeCipher.dispose();
198+
disposeWriteCipher();
194199

195200
this.writeCipher = writeCipher;
196201
this.isFirstAppOutputRecord = true;
@@ -219,7 +224,7 @@ void changeWriteCiphers(SSLWriteCipher writeCipher,
219224
flush();
220225

221226
// Dispose of any intermediate state in the underlying cipher.
222-
writeCipher.dispose();
227+
disposeWriteCipher();
223228

224229
this.writeCipher = writeCipher;
225230
this.isFirstAppOutputRecord = true;

Diff for: src/java.base/share/classes/sun/security/ssl/SSLEngineOutputRecord.java

+22
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ void encodeChangeCipherSpec() throws IOException {
151151
fragmenter.queueUpChangeCipherSpec();
152152
}
153153

154+
@Override
155+
void disposeWriteCipher() {
156+
if (fragmenter == null) {
157+
writeCipher.dispose();
158+
} else {
159+
fragmenter.queueUpCipherDispose();
160+
}
161+
}
162+
154163
@Override
155164
void encodeV2NoCipher() throws IOException {
156165
isTalkingToV2 = true;
@@ -361,6 +370,7 @@ private static class RecordMemo {
361370
byte majorVersion;
362371
byte minorVersion;
363372
SSLWriteCipher encodeCipher;
373+
boolean disposeCipher;
364374

365375
byte[] fragment;
366376
}
@@ -422,6 +432,15 @@ void queueUpAlert(byte level, byte description) {
422432
handshakeMemos.add(memo);
423433
}
424434

435+
void queueUpCipherDispose() {
436+
RecordMemo lastMemo = handshakeMemos.peekLast();
437+
if (lastMemo != null) {
438+
lastMemo.disposeCipher = true;
439+
} else {
440+
writeCipher.dispose();
441+
}
442+
}
443+
425444
Ciphertext acquireCiphertext(ByteBuffer dstBuf) throws IOException {
426445
if (isEmpty()) {
427446
return null;
@@ -521,6 +540,9 @@ Ciphertext acquireCiphertext(ByteBuffer dstBuf) throws IOException {
521540
dstPos, dstLim, headerSize,
522541
ProtocolVersion.valueOf(memo.majorVersion,
523542
memo.minorVersion));
543+
if (memo.disposeCipher) {
544+
memo.encodeCipher.dispose();
545+
}
524546

525547
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
526548
ByteBuffer temporary = dstBuf.duplicate();

Diff for: src/java.base/share/classes/sun/security/ssl/SSLSocketOutputRecord.java

+5
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ void encodeChangeCipherSpec() throws IOException {
243243
}
244244
}
245245

246+
@Override
247+
void disposeWriteCipher() {
248+
writeCipher.dispose();
249+
}
250+
246251
@Override
247252
public void flush() throws IOException {
248253
recordLock.lock();

0 commit comments

Comments
 (0)