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