30
30
import java .math .BigInteger ;
31
31
import java .net .InetAddress ;
32
32
import java .nio .ByteBuffer ;
33
+ import java .nio .charset .StandardCharsets ;
33
34
import java .security .Principal ;
34
35
import java .security .PrivateKey ;
35
36
import java .security .cert .X509Certificate ;
@@ -311,113 +312,90 @@ final class SSLSessionImpl extends ExtendedSSLSession {
311
312
SSLSessionImpl (HandshakeContext hc , ByteBuffer buf ) throws IOException {
312
313
boundValues = new ConcurrentHashMap <>();
313
314
this .protocolVersion =
314
- ProtocolVersion .valueOf (Short . toUnsignedInt (buf . getShort () ));
315
+ ProtocolVersion .valueOf (Record . getInt16 (buf ));
315
316
316
317
// The CH session id may reset this if it's provided
317
318
this .sessionId = new SessionId (true ,
318
319
hc .sslContext .getSecureRandom ());
319
320
320
321
this .cipherSuite =
321
- CipherSuite .valueOf (Short . toUnsignedInt (buf . getShort () ));
322
+ CipherSuite .valueOf (Record . getInt16 (buf ));
322
323
323
324
// Local Supported signature algorithms
324
325
ArrayList <SignatureScheme > list = new ArrayList <>();
325
- int i = Byte . toUnsignedInt (buf . get () );
326
+ int i = Record . getInt8 (buf );
326
327
while (i -- > 0 ) {
327
328
list .add (SignatureScheme .valueOf (
328
- Short . toUnsignedInt (buf . getShort () )));
329
+ Record . getInt16 (buf )));
329
330
}
330
331
this .localSupportedSignAlgs = Collections .unmodifiableCollection (list );
331
332
332
333
// Peer Supported signature algorithms
333
- i = Byte . toUnsignedInt (buf . get () );
334
+ i = Record . getInt8 (buf );
334
335
list .clear ();
335
336
while (i -- > 0 ) {
336
337
list .add (SignatureScheme .valueOf (
337
- Short . toUnsignedInt (buf . getShort () )));
338
+ Record . getInt16 (buf )));
338
339
}
339
340
this .peerSupportedSignAlgs = Collections .unmodifiableCollection (list );
340
341
341
342
// PSK
342
- byte [] b ;
343
- i = Short .toUnsignedInt (buf .getShort ());
344
- if (i > 0 ) {
345
- b = new byte [i ];
346
- // Get algorithm string
347
- buf .get (b , 0 , i );
348
- // Encoded length
349
- i = Short .toUnsignedInt (buf .getShort ());
350
- // Encoded SecretKey
351
- b = new byte [i ];
352
- buf .get (b );
343
+ byte [] b = Record .getBytes16 (buf );
344
+ if (b .length > 0 ) {
345
+ b = Record .getBytes16 (buf );
353
346
this .preSharedKey = new SecretKeySpec (b , "TlsMasterSecret" );
354
347
} else {
355
348
this .preSharedKey = null ;
356
349
}
357
350
358
351
// PSK identity
359
- i = Byte .toUnsignedInt (buf .get ());
360
- if (i > 0 ) {
361
- b = new byte [i ];
362
- buf .get (b );
352
+ b = Record .getBytes8 (buf );
353
+ if (b .length > 0 ) {
363
354
this .pskIdentity = b ;
364
355
} else {
365
356
this .pskIdentity = null ;
366
357
}
367
358
368
359
// Master secret length of secret key algorithm (one byte)
369
- i = buf .get ();
370
- if (i > 0 ) {
371
- b = new byte [i ];
372
- // Get algorithm string
373
- buf .get (b , 0 , i );
374
- // Encoded length
375
- i = Short .toUnsignedInt (buf .getShort ());
376
- // Encoded SecretKey
377
- b = new byte [i ];
378
- buf .get (b );
360
+ b = Record .getBytes8 (buf );
361
+ if (b .length > 0 ) {
362
+ b = Record .getBytes16 (buf );
379
363
this .masterSecret = new SecretKeySpec (b , "TlsMasterSecret" );
380
364
} else {
381
365
this .masterSecret = null ;
382
366
}
367
+
383
368
// Use extended master secret
384
- this .useExtendedMasterSecret = (buf . get ( ) != 0 );
369
+ this .useExtendedMasterSecret = (Record . getInt8 ( buf ) != 0 );
385
370
386
371
// Identification Protocol
387
- i = buf . get ( );
388
- if (i == 0 ) {
372
+ b = Record . getBytes8 ( buf );
373
+ if (b . length == 0 ) {
389
374
identificationProtocol = null ;
390
375
} else {
391
- b = new byte [i ];
392
- buf .get (b );
393
376
identificationProtocol = new String (b );
394
377
}
395
378
396
379
// SNI
397
- i = Byte . toUnsignedInt (buf . get ()); // length
398
- if (i == 0 ) {
380
+ b = Record . getBytes8 (buf );
381
+ if (b . length == 0 ) {
399
382
serverNameIndication = null ;
400
383
} else {
401
- b = new byte [i ];
402
- buf .get (b , 0 , b .length );
403
384
serverNameIndication = new SNIHostName (b );
404
385
}
405
386
406
387
// List of SNIServerName
407
- int len = Short . toUnsignedInt (buf . getShort () );
388
+ int len = Record . getInt16 (buf );
408
389
if (len == 0 ) {
409
390
this .requestedServerNames = Collections .emptyList ();
410
391
} else {
411
392
requestedServerNames = new ArrayList <>();
412
393
while (len > 0 ) {
413
- int l = Byte .toUnsignedInt (buf .get ());
414
- b = new byte [l ];
415
- buf .get (b , 0 , l );
394
+ b = Record .getBytes8 (buf );
416
395
requestedServerNames .add (new SNIHostName (new String (b )));
417
396
len --;
418
397
}
419
398
}
420
-
421
399
maximumPacketSize = buf .getInt ();
422
400
negotiatedMaxFragLen = buf .getInt ();
423
401
@@ -427,31 +405,28 @@ final class SSLSessionImpl extends ExtendedSSLSession {
427
405
// Get Buffer sizes
428
406
429
407
// Status Response
430
- len = Short . toUnsignedInt (buf . getShort () );
408
+ len = Record . getInt16 (buf );
431
409
if (len == 0 ) {
432
410
statusResponses = Collections .emptyList ();
433
411
} else {
434
412
statusResponses = new ArrayList <>();
435
413
}
436
414
while (len -- > 0 ) {
437
- b = new byte [Short .toUnsignedInt (buf .getShort ())];
438
- buf .get (b );
415
+ b = Record .getBytes16 (buf );
439
416
statusResponses .add (b );
440
417
}
441
418
442
419
// Get Peer host & port
443
- i = Byte . toUnsignedInt (buf . get () );
444
- if (i == 0 ) {
420
+ b = Record . getBytes8 (buf );
421
+ if (b . length == 0 ) {
445
422
this .host = "" ;
446
423
} else {
447
- b = new byte [i ];
448
- buf .get (b , 0 , i );
449
424
this .host = new String (b );
450
425
}
451
- this .port = Short . toUnsignedInt (buf . getShort () );
426
+ this .port = Record . getInt16 (buf );
452
427
453
428
// Peer certs
454
- i = buf . get ( );
429
+ i = Record . getInt8 ( buf );
455
430
if (i == 0 ) {
456
431
this .peerCerts = null ;
457
432
} else {
@@ -470,7 +445,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
470
445
}
471
446
472
447
// Get local certs of PSK
473
- switch (buf . get ( )) {
448
+ switch (Record . getInt8 ( buf )) {
474
449
case 0 :
475
450
break ;
476
451
case 1 :
@@ -492,21 +467,15 @@ final class SSLSessionImpl extends ExtendedSSLSession {
492
467
case 2 :
493
468
// pre-shared key
494
469
// Length of pre-shared key algorithm (one byte)
495
- i = buf .get ();
496
- b = new byte [i ];
497
- buf .get (b , 0 , i );
470
+ b = Record .getBytes8 (buf );
498
471
String alg = new String (b );
499
- // Get length of encoding
500
- i = Short .toUnsignedInt (buf .getShort ());
501
472
// Get encoding
502
- b = new byte [i ];
503
- buf .get (b );
473
+ b = Record .getBytes16 (buf );
504
474
this .preSharedKey = new SecretKeySpec (b , alg );
505
475
// Get identity len
506
- i = buf . get ( );
476
+ i = Record . getInt8 ( buf );
507
477
if (i > 0 ) {
508
- this .pskIdentity = new byte [buf .get ()];
509
- buf .get (pskIdentity );
478
+ this .pskIdentity = Record .getBytes8 (buf );
510
479
} else {
511
480
this .pskIdentity = null ;
512
481
}
@@ -520,6 +489,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
520
489
this .lastUsedTime = System .currentTimeMillis ();
521
490
}
522
491
492
+
523
493
// Some situations we cannot provide a stateless ticket, but after it
524
494
// has been negotiated
525
495
boolean isStatelessable () {
0 commit comments