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 ;
@@ -309,113 +310,90 @@ final class SSLSessionImpl extends ExtendedSSLSession {
309
310
SSLSessionImpl (HandshakeContext hc , ByteBuffer buf ) throws IOException {
310
311
boundValues = new ConcurrentHashMap <>();
311
312
this .protocolVersion =
312
- ProtocolVersion .valueOf (Short . toUnsignedInt (buf . getShort () ));
313
+ ProtocolVersion .valueOf (Record . getInt16 (buf ));
313
314
314
315
// The CH session id may reset this if it's provided
315
316
this .sessionId = new SessionId (true ,
316
317
hc .sslContext .getSecureRandom ());
317
318
318
319
this .cipherSuite =
319
- CipherSuite .valueOf (Short . toUnsignedInt (buf . getShort () ));
320
+ CipherSuite .valueOf (Record . getInt16 (buf ));
320
321
321
322
// Local Supported signature algorithms
322
323
ArrayList <SignatureScheme > list = new ArrayList <>();
323
- int i = Byte . toUnsignedInt (buf . get () );
324
+ int i = Record . getInt8 (buf );
324
325
while (i -- > 0 ) {
325
326
list .add (SignatureScheme .valueOf (
326
- Short . toUnsignedInt (buf . getShort () )));
327
+ Record . getInt16 (buf )));
327
328
}
328
329
this .localSupportedSignAlgs = Collections .unmodifiableCollection (list );
329
330
330
331
// Peer Supported signature algorithms
331
- i = Byte . toUnsignedInt (buf . get () );
332
+ i = Record . getInt8 (buf );
332
333
list .clear ();
333
334
while (i -- > 0 ) {
334
335
list .add (SignatureScheme .valueOf (
335
- Short . toUnsignedInt (buf . getShort () )));
336
+ Record . getInt16 (buf )));
336
337
}
337
338
this .peerSupportedSignAlgs = Collections .unmodifiableCollection (list );
338
339
339
340
// 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 );
351
344
this .preSharedKey = new SecretKeySpec (b , "TlsMasterSecret" );
352
345
} else {
353
346
this .preSharedKey = null ;
354
347
}
355
348
356
349
// 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 ) {
361
352
this .pskIdentity = b ;
362
353
} else {
363
354
this .pskIdentity = null ;
364
355
}
365
356
366
357
// 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 );
377
361
this .masterSecret = new SecretKeySpec (b , "TlsMasterSecret" );
378
362
} else {
379
363
this .masterSecret = null ;
380
364
}
365
+
381
366
// Use extended master secret
382
- this .useExtendedMasterSecret = (buf . get ( ) != 0 );
367
+ this .useExtendedMasterSecret = (Record . getInt8 ( buf ) != 0 );
383
368
384
369
// Identification Protocol
385
- i = buf . get ( );
386
- if (i == 0 ) {
370
+ b = Record . getBytes8 ( buf );
371
+ if (b . length == 0 ) {
387
372
identificationProtocol = null ;
388
373
} else {
389
- b = new byte [i ];
390
- buf .get (b );
391
374
identificationProtocol = new String (b );
392
375
}
393
376
394
377
// SNI
395
- i = buf . get (); // length
396
- if (i == 0 ) {
378
+ b = Record . getBytes8 ( buf );
379
+ if (b . length == 0 ) {
397
380
serverNameIndication = null ;
398
381
} else {
399
- b = new byte [i ];
400
- buf .get (b , 0 , b .length );
401
382
serverNameIndication = new SNIHostName (b );
402
383
}
403
384
404
385
// List of SNIServerName
405
- int len = Short . toUnsignedInt (buf . getShort () );
386
+ int len = Record . getInt16 (buf );
406
387
if (len == 0 ) {
407
388
this .requestedServerNames = Collections .emptyList ();
408
389
} else {
409
390
requestedServerNames = new ArrayList <>();
410
391
while (len > 0 ) {
411
- int l = buf .get ();
412
- b = new byte [l ];
413
- buf .get (b , 0 , l );
392
+ b = Record .getBytes8 (buf );
414
393
requestedServerNames .add (new SNIHostName (new String (b )));
415
394
len --;
416
395
}
417
396
}
418
-
419
397
maximumPacketSize = buf .getInt ();
420
398
negotiatedMaxFragLen = buf .getInt ();
421
399
@@ -425,31 +403,28 @@ final class SSLSessionImpl extends ExtendedSSLSession {
425
403
// Get Buffer sizes
426
404
427
405
// Status Response
428
- len = Short . toUnsignedInt (buf . getShort () );
406
+ len = Record . getInt16 (buf );
429
407
if (len == 0 ) {
430
408
statusResponses = Collections .emptyList ();
431
409
} else {
432
410
statusResponses = new ArrayList <>();
433
411
}
434
412
while (len -- > 0 ) {
435
- b = new byte [Short .toUnsignedInt (buf .getShort ())];
436
- buf .get (b );
413
+ b = Record .getBytes16 (buf );
437
414
statusResponses .add (b );
438
415
}
439
416
440
417
// Get Peer host & port
441
- i = Byte . toUnsignedInt (buf . get () );
442
- if (i == 0 ) {
418
+ b = Record . getBytes8 (buf );
419
+ if (b . length == 0 ) {
443
420
this .host = "" ;
444
421
} else {
445
- b = new byte [i ];
446
- buf .get (b , 0 , i );
447
422
this .host = new String (b );
448
423
}
449
- this .port = Short . toUnsignedInt (buf . getShort () );
424
+ this .port = Record . getInt16 (buf );
450
425
451
426
// Peer certs
452
- i = buf . get ( );
427
+ i = Record . getInt8 ( buf );
453
428
if (i == 0 ) {
454
429
this .peerCerts = null ;
455
430
} else {
@@ -468,7 +443,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
468
443
}
469
444
470
445
// Get local certs of PSK
471
- switch (buf . get ( )) {
446
+ switch (Record . getInt8 ( buf )) {
472
447
case 0 :
473
448
break ;
474
449
case 1 :
@@ -490,19 +465,13 @@ final class SSLSessionImpl extends ExtendedSSLSession {
490
465
case 2 :
491
466
// pre-shared key
492
467
// 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 );
496
469
String alg = new String (b );
497
- // Get length of encoding
498
- i = Short .toUnsignedInt (buf .getShort ());
499
470
// Get encoding
500
- b = new byte [i ];
501
- buf .get (b );
471
+ b = Record .getBytes16 (buf );
502
472
this .preSharedKey = new SecretKeySpec (b , alg );
503
473
// Get identity len
504
- this .pskIdentity = new byte [buf .get ()];
505
- buf .get (pskIdentity );
474
+ this .pskIdentity = Record .getBytes8 (buf );
506
475
break ;
507
476
default :
508
477
throw new SSLException ("Failed local certs of session." );
@@ -513,6 +482,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
513
482
this .lastUsedTime = System .currentTimeMillis ();
514
483
}
515
484
485
+
516
486
// Some situations we cannot provide a stateless ticket, but after it
517
487
// has been negotiated
518
488
boolean isStatelessable () {
0 commit comments