@@ -532,11 +532,6 @@ private void serverHello(ServerHello mesg) throws IOException {
532
532
533
533
// -- token binding etc. changes begin --
534
534
setConnectionRandoms ();
535
-
536
- HelloExtension emsx = mesg .extensions .get (ExtensionType .EXT_EXTENDED_MASTER_SECRET );
537
- if (emsx != null ) {
538
- isExtendedMasterSecretExtension = true ;
539
- }
540
535
// -- token binding etc. changes end --
541
536
542
537
if (isNegotiable (mesg .cipherSuite ) == false ) {
@@ -556,23 +551,6 @@ private void serverHello(ServerHello mesg) throws IOException {
556
551
// NOTREACHED
557
552
}
558
553
559
-
560
- // -- token binding etc. changes begin --
561
- TokenBindingExtension tbx = (TokenBindingExtension ) mesg .extensions .get (ExtensionType .EXT_TOKEN_BINDING );
562
- if (tbx != null ) {
563
- byte [] requestedKeyParamsList = getConnectionSupportedTokenBindingKeyParams ();
564
-
565
- try {
566
- byte serverChosenKeyParams = tbx .processServerHello (isExtendedMasterSecretExtension ,
567
- secureRenegotiation , requestedKeyParamsList );
568
- setConnectionNegotiatedTokenBindingKeyParams (serverChosenKeyParams );
569
- }
570
- catch (SSLHandshakeException e ) {
571
- fatalSE (Alerts .alert_unsupported_extension , e .getMessage (), e );
572
- }
573
- }
574
- // -- token binding etc. changes end --
575
-
576
554
// so far so good, let's look at the session
577
555
if (session != null ) {
578
556
// we tried to resume, let's see what the server decided
@@ -659,6 +637,70 @@ public Subject run() throws Exception {
659
637
}
660
638
}
661
639
640
+ // check the "extended_master_secret" extension
641
+ ExtendedMasterSecretExtension extendedMasterSecretExt =
642
+ (ExtendedMasterSecretExtension )mesg .extensions .get (
643
+ ExtensionType .EXT_EXTENDED_MASTER_SECRET );
644
+ if (extendedMasterSecretExt != null ) {
645
+ // Is it the expected server extension?
646
+ if (!useExtendedMasterSecret ||
647
+ !(mesgVersion .v >= ProtocolVersion .TLS10 .v ) || !requestedToUseEMS ) {
648
+ fatalSE (Alerts .alert_unsupported_extension ,
649
+ "Server sent the extended_master_secret " +
650
+ "extension improperly" );
651
+ }
652
+
653
+ // For abbreviated handshake, if the original session did not use
654
+ // the "extended_master_secret" extension but the new ServerHello
655
+ // contains the extension, the client MUST abort the handshake.
656
+ if (resumingSession && (session != null ) &&
657
+ !session .getUseExtendedMasterSecret ()) {
658
+ fatalSE (Alerts .alert_unsupported_extension ,
659
+ "Server sent an unexpected extended_master_secret " +
660
+ "extension on session resumption" );
661
+ }
662
+ } else {
663
+ if (useExtendedMasterSecret && !allowLegacyMasterSecret ) {
664
+ // For full handshake, if a client receives a ServerHello
665
+ // without the extension, it SHOULD abort the handshake if
666
+ // it does not wish to interoperate with legacy servers.
667
+ fatalSE (Alerts .alert_handshake_failure ,
668
+ "Extended Master Secret extension is required" );
669
+ }
670
+
671
+ if (resumingSession && (session != null )) {
672
+ if (session .getUseExtendedMasterSecret ()) {
673
+ // For abbreviated handshake, if the original session used
674
+ // the "extended_master_secret" extension but the new
675
+ // ServerHello does not contain the extension, the client
676
+ // MUST abort the handshake.
677
+ fatalSE (Alerts .alert_handshake_failure ,
678
+ "Missing Extended Master Secret extension " +
679
+ "on session resumption" );
680
+ } else if (useExtendedMasterSecret && !allowLegacyResumption ) {
681
+ // Unlikely, abbreviated handshake should be discarded.
682
+ fatalSE (Alerts .alert_handshake_failure ,
683
+ "Extended Master Secret extension is required" );
684
+ }
685
+ }
686
+ }
687
+
688
+ // -- token binding etc. changes begin --
689
+ TokenBindingExtension tbx = (TokenBindingExtension ) mesg .extensions .get (ExtensionType .EXT_TOKEN_BINDING );
690
+ if (tbx != null ) {
691
+ byte [] requestedKeyParamsList = getConnectionSupportedTokenBindingKeyParams ();
692
+
693
+ try {
694
+ byte serverChosenKeyParams = tbx .processServerHello (extendedMasterSecretExt != null ,
695
+ secureRenegotiation , requestedKeyParamsList );
696
+ setConnectionNegotiatedTokenBindingKeyParams (serverChosenKeyParams );
697
+ }
698
+ catch (SSLHandshakeException e ) {
699
+ fatalSE (Alerts .alert_unsupported_extension , e .getMessage (), e );
700
+ }
701
+ }
702
+ // -- token binding etc. changes end --
703
+
662
704
if (resumingSession && session != null ) {
663
705
setHandshakeSessionSE (session );
664
706
// Reserve the handshake state if this is a session-resumption
@@ -681,8 +723,8 @@ public Subject run() throws Exception {
681
723
&& (type != ExtensionType .EXT_RENEGOTIATION_INFO )
682
724
// -- token binding etc. changes begin --
683
725
&& (type != ExtensionType .EXT_TOKEN_BINDING )
684
- && (type != ExtensionType .EXT_EXTENDED_MASTER_SECRET )) {
685
726
// -- token binding etc. changes end --
727
+ && (type != ExtensionType .EXT_EXTENDED_MASTER_SECRET )){
686
728
fatalSE (Alerts .alert_unsupported_extension ,
687
729
"Server sent an unsupported extension: " + type );
688
730
}
@@ -691,7 +733,8 @@ public Subject run() throws Exception {
691
733
// Create a new session, we need to do the full handshake
692
734
session = new SSLSessionImpl (protocolVersion , cipherSuite ,
693
735
getLocalSupportedSignAlgs (),
694
- mesg .sessionId , getHostSE (), getPortSE ());
736
+ mesg .sessionId , getHostSE (), getPortSE (),
737
+ (extendedMasterSecretExt != null ));
695
738
session .setRequestedServerNames (requestedServerNames );
696
739
setHandshakeSessionSE (session );
697
740
if (debug != null && Debug .isOn ("handshake" )) {
@@ -1327,6 +1370,44 @@ HandshakeMessage getKickstartMessage() throws SSLException {
1327
1370
session = null ;
1328
1371
}
1329
1372
1373
+ if ((session != null ) && useExtendedMasterSecret ) {
1374
+ boolean isTLS10Plus = sessionVersion .v >= ProtocolVersion .TLS10 .v ;
1375
+ if (isTLS10Plus && !session .getUseExtendedMasterSecret ()) {
1376
+ if (!allowLegacyResumption ) {
1377
+ // perform full handshake instead
1378
+ //
1379
+ // The client SHOULD NOT offer an abbreviated handshake
1380
+ // to resume a session that does not use an extended
1381
+ // master secret. Instead, it SHOULD offer a full
1382
+ // handshake.
1383
+ session = null ;
1384
+ }
1385
+ }
1386
+
1387
+ if ((session != null ) && !allowUnsafeServerCertChange ) {
1388
+ // It is fine to move on with abbreviate handshake if
1389
+ // endpoint identification is enabled.
1390
+ String identityAlg = getEndpointIdentificationAlgorithmSE ();
1391
+ if ((identityAlg == null || identityAlg .length () == 0 )) {
1392
+ if (isTLS10Plus ) {
1393
+ if (!session .getUseExtendedMasterSecret ()) {
1394
+ // perform full handshake instead
1395
+ session = null ;
1396
+ } // Otherwise, use extended master secret.
1397
+ } else {
1398
+ // The extended master secret extension does not
1399
+ // apply to SSL 3.0. Perform a full handshake
1400
+ // instead.
1401
+ //
1402
+ // Note that the useExtendedMasterSecret is
1403
+ // extended to protect SSL 3.0 connections,
1404
+ // by discarding abbreviate handshake.
1405
+ session = null ;
1406
+ }
1407
+ }
1408
+ }
1409
+ }
1410
+
1330
1411
if (session != null ) {
1331
1412
if (debug != null ) {
1332
1413
if (Debug .isOn ("handshake" ) || Debug .isOn ("session" )) {
@@ -1411,12 +1492,12 @@ HandshakeMessage getKickstartMessage() throws SSLException {
1411
1492
1412
1493
// add elliptic curves and point format extensions
1413
1494
if (cipherSuites .containsEC ()) {
1414
- SupportedEllipticCurvesExtension ece =
1415
- SupportedEllipticCurvesExtension .createExtension (algorithmConstraints );
1495
+ EllipticCurvesExtension ece =
1496
+ EllipticCurvesExtension .createExtension (algorithmConstraints );
1416
1497
if (ece != null ) {
1417
1498
clientHelloMessage .extensions .add (ece );
1418
1499
clientHelloMessage .extensions .add (
1419
- SupportedEllipticPointFormatsExtension .DEFAULT );
1500
+ EllipticPointFormatsExtension .DEFAULT );
1420
1501
}
1421
1502
}
1422
1503
@@ -1433,6 +1514,14 @@ HandshakeMessage getKickstartMessage() throws SSLException {
1433
1514
clientHelloMessage .addSignatureAlgorithmsExtension (localSignAlgs );
1434
1515
}
1435
1516
1517
+ // add Extended Master Secret extension
1518
+ if (useExtendedMasterSecret && (maxProtocolVersion .v >= ProtocolVersion .TLS10 .v )) {
1519
+ if ((session == null ) || session .getUseExtendedMasterSecret ()) {
1520
+ clientHelloMessage .addExtendedMasterSecretExtension ();
1521
+ requestedToUseEMS = true ;
1522
+ }
1523
+ }
1524
+
1436
1525
// add server_name extension
1437
1526
if (enableSNIExtension ) {
1438
1527
if (session != null ) {
@@ -1463,8 +1552,7 @@ HandshakeMessage getKickstartMessage() throws SSLException {
1463
1552
// -- token binding etc. changes begin --
1464
1553
byte [] supportedTokenBindingKeyParams = getConnectionSupportedTokenBindingKeyParams ();
1465
1554
1466
- if (supportedTokenBindingKeyParams != null && supportedTokenBindingKeyParams .length > 0 ) {
1467
- clientHelloMessage .extensions .add (new ExtendedMasterSecretExtension ());
1555
+ if (supportedTokenBindingKeyParams != null && supportedTokenBindingKeyParams .length > 0 && requestedToUseEMS ) {
1468
1556
clientHelloMessage .extensions .add (new TokenBindingExtension (1 , 0 , supportedTokenBindingKeyParams ));
1469
1557
}
1470
1558
// -- token binding etc. changes end --
@@ -1479,6 +1567,7 @@ byte[] getDefaultSupportedTokenBindingKeyParams() {
1479
1567
}
1480
1568
// -- token binding etc. changes end --
1481
1569
1570
+
1482
1571
/*
1483
1572
* Fault detected during handshake.
1484
1573
*/
@@ -1509,10 +1598,14 @@ private void serverCertificate(CertificateMsg mesg) throws IOException {
1509
1598
// Allow server certificate change in client side during renegotiation
1510
1599
// after a session-resumption abbreviated initial handshake?
1511
1600
//
1512
- // DO NOT need to check allowUnsafeServerCertChange here. We only
1601
+ // DO NOT need to check allowUnsafeServerCertChange here. We only
1513
1602
// reserve server certificates when allowUnsafeServerCertChange is
1514
1603
// flase.
1515
- if (reservedServerCerts != null ) {
1604
+ //
1605
+ // Allow server certificate change if it is negotiated to use the
1606
+ // extended master secret.
1607
+ if ((reservedServerCerts != null ) &&
1608
+ !session .getUseExtendedMasterSecret ()) {
1516
1609
// It is not necessary to check the certificate update if endpoint
1517
1610
// identification is enabled.
1518
1611
String identityAlg = getEndpointIdentificationAlgorithmSE ();
0 commit comments