Skip to content

Commit c730336

Browse files
committed
8350830: Values converted incorrectly when reading TLS session tickets
Reviewed-by: mbaesken Backport-of: 2c1eb339d6c9b6cc6fa4a8780b0e0b8d4d9a5f01
1 parent b34a3ee commit c730336

File tree

2 files changed

+401
-64
lines changed

2 files changed

+401
-64
lines changed

src/java.base/share/classes/sun/security/ssl/SSLSessionImpl.java

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.math.BigInteger;
3131
import java.net.InetAddress;
3232
import java.nio.ByteBuffer;
33+
import java.nio.charset.StandardCharsets;
3334
import java.security.Principal;
3435
import java.security.PrivateKey;
3536
import java.security.cert.X509Certificate;
@@ -309,113 +310,90 @@ final class SSLSessionImpl extends ExtendedSSLSession {
309310
SSLSessionImpl(HandshakeContext hc, ByteBuffer buf) throws IOException {
310311
boundValues = new ConcurrentHashMap<>();
311312
this.protocolVersion =
312-
ProtocolVersion.valueOf(Short.toUnsignedInt(buf.getShort()));
313+
ProtocolVersion.valueOf(Record.getInt16(buf));
313314

314315
// The CH session id may reset this if it's provided
315316
this.sessionId = new SessionId(true,
316317
hc.sslContext.getSecureRandom());
317318

318319
this.cipherSuite =
319-
CipherSuite.valueOf(Short.toUnsignedInt(buf.getShort()));
320+
CipherSuite.valueOf(Record.getInt16(buf));
320321

321322
// Local Supported signature algorithms
322323
ArrayList<SignatureScheme> list = new ArrayList<>();
323-
int i = Byte.toUnsignedInt(buf.get());
324+
int i = Record.getInt8(buf);
324325
while (i-- > 0) {
325326
list.add(SignatureScheme.valueOf(
326-
Short.toUnsignedInt(buf.getShort())));
327+
Record.getInt16(buf)));
327328
}
328329
this.localSupportedSignAlgs = Collections.unmodifiableCollection(list);
329330

330331
// Peer Supported signature algorithms
331-
i = Byte.toUnsignedInt(buf.get());
332+
i = Record.getInt8(buf);
332333
list.clear();
333334
while (i-- > 0) {
334335
list.add(SignatureScheme.valueOf(
335-
Short.toUnsignedInt(buf.getShort())));
336+
Record.getInt16(buf)));
336337
}
337338
this.peerSupportedSignAlgs = Collections.unmodifiableCollection(list);
338339

339340
// PSK
340-
byte[] b;
341-
i = Short.toUnsignedInt(buf.getShort());
342-
if (i > 0) {
343-
b = new byte[i];
344-
// Get algorithm string
345-
buf.get(b, 0, i);
346-
// Encoded length
347-
i = Short.toUnsignedInt(buf.getShort());
348-
// Encoded SecretKey
349-
b = new byte[i];
350-
buf.get(b);
341+
byte[] b = Record.getBytes16(buf);
342+
if (b.length > 0) {
343+
b = Record.getBytes16(buf);
351344
this.preSharedKey = new SecretKeySpec(b, "TlsMasterSecret");
352345
} else {
353346
this.preSharedKey = null;
354347
}
355348

356349
// PSK identity
357-
i = buf.get();
358-
if (i > 0) {
359-
b = new byte[i];
360-
buf.get(b);
350+
b = Record.getBytes8(buf);
351+
if (b.length > 0) {
361352
this.pskIdentity = b;
362353
} else {
363354
this.pskIdentity = null;
364355
}
365356

366357
// Master secret length of secret key algorithm (one byte)
367-
i = buf.get();
368-
if (i > 0) {
369-
b = new byte[i];
370-
// Get algorithm string
371-
buf.get(b, 0, i);
372-
// Encoded length
373-
i = Short.toUnsignedInt(buf.getShort());
374-
// Encoded SecretKey
375-
b = new byte[i];
376-
buf.get(b);
358+
b = Record.getBytes8(buf);
359+
if (b.length > 0) {
360+
b = Record.getBytes16(buf);
377361
this.masterSecret = new SecretKeySpec(b, "TlsMasterSecret");
378362
} else {
379363
this.masterSecret = null;
380364
}
365+
381366
// Use extended master secret
382-
this.useExtendedMasterSecret = (buf.get() != 0);
367+
this.useExtendedMasterSecret = (Record.getInt8(buf) != 0);
383368

384369
// Identification Protocol
385-
i = buf.get();
386-
if (i == 0) {
370+
b = Record.getBytes8(buf);
371+
if (b.length == 0) {
387372
identificationProtocol = null;
388373
} else {
389-
b = new byte[i];
390-
buf.get(b);
391374
identificationProtocol = new String(b);
392375
}
393376

394377
// SNI
395-
i = buf.get(); // length
396-
if (i == 0) {
378+
b = Record.getBytes8(buf);
379+
if (b.length == 0) {
397380
serverNameIndication = null;
398381
} else {
399-
b = new byte[i];
400-
buf.get(b, 0, b.length);
401382
serverNameIndication = new SNIHostName(b);
402383
}
403384

404385
// List of SNIServerName
405-
int len = Short.toUnsignedInt(buf.getShort());
386+
int len = Record.getInt16(buf);
406387
if (len == 0) {
407388
this.requestedServerNames = Collections.emptyList();
408389
} else {
409390
requestedServerNames = new ArrayList<>();
410391
while (len > 0) {
411-
int l = buf.get();
412-
b = new byte[l];
413-
buf.get(b, 0, l);
392+
b = Record.getBytes8(buf);
414393
requestedServerNames.add(new SNIHostName(new String(b)));
415394
len--;
416395
}
417396
}
418-
419397
maximumPacketSize = buf.getInt();
420398
negotiatedMaxFragLen = buf.getInt();
421399

@@ -425,31 +403,28 @@ final class SSLSessionImpl extends ExtendedSSLSession {
425403
// Get Buffer sizes
426404

427405
// Status Response
428-
len = Short.toUnsignedInt(buf.getShort());
406+
len = Record.getInt16(buf);
429407
if (len == 0) {
430408
statusResponses = Collections.emptyList();
431409
} else {
432410
statusResponses = new ArrayList<>();
433411
}
434412
while (len-- > 0) {
435-
b = new byte[Short.toUnsignedInt(buf.getShort())];
436-
buf.get(b);
413+
b = Record.getBytes16(buf);
437414
statusResponses.add(b);
438415
}
439416

440417
// Get Peer host & port
441-
i = Byte.toUnsignedInt(buf.get());
442-
if (i == 0) {
418+
b = Record.getBytes8(buf);
419+
if (b.length == 0) {
443420
this.host = "";
444421
} else {
445-
b = new byte[i];
446-
buf.get(b, 0, i);
447422
this.host = new String(b);
448423
}
449-
this.port = Short.toUnsignedInt(buf.getShort());
424+
this.port = Record.getInt16(buf);
450425

451426
// Peer certs
452-
i = buf.get();
427+
i = Record.getInt8(buf);
453428
if (i == 0) {
454429
this.peerCerts = null;
455430
} else {
@@ -468,7 +443,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
468443
}
469444

470445
// Get local certs of PSK
471-
switch (buf.get()) {
446+
switch (Record.getInt8(buf)) {
472447
case 0:
473448
break;
474449
case 1:
@@ -490,19 +465,13 @@ final class SSLSessionImpl extends ExtendedSSLSession {
490465
case 2:
491466
// pre-shared key
492467
// Length of pre-shared key algorithm (one byte)
493-
i = buf.get();
494-
b = new byte[i];
495-
buf.get(b, 0 , i);
468+
b = Record.getBytes8(buf);
496469
String alg = new String(b);
497-
// Get length of encoding
498-
i = Short.toUnsignedInt(buf.getShort());
499470
// Get encoding
500-
b = new byte[i];
501-
buf.get(b);
471+
b = Record.getBytes16(buf);
502472
this.preSharedKey = new SecretKeySpec(b, alg);
503473
// Get identity len
504-
this.pskIdentity = new byte[buf.get()];
505-
buf.get(pskIdentity);
474+
this.pskIdentity = Record.getBytes8(buf);
506475
break;
507476
default:
508477
throw new SSLException("Failed local certs of session.");
@@ -513,6 +482,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
513482
this.lastUsedTime = System.currentTimeMillis();
514483
}
515484

485+
516486
// Some situations we cannot provide a stateless ticket, but after it
517487
// has been negotiated
518488
boolean isStatelessable() {

0 commit comments

Comments
 (0)