@@ -532,11 +532,6 @@ private void serverHello(ServerHello mesg) throws IOException {
532532
533533 // -- token binding etc. changes begin --
534534 setConnectionRandoms ();
535-
536- HelloExtension emsx = mesg .extensions .get (ExtensionType .EXT_EXTENDED_MASTER_SECRET );
537- if (emsx != null ) {
538- isExtendedMasterSecretExtension = true ;
539- }
540535 // -- token binding etc. changes end --
541536
542537 if (isNegotiable (mesg .cipherSuite ) == false ) {
@@ -556,23 +551,6 @@ private void serverHello(ServerHello mesg) throws IOException {
556551 // NOTREACHED
557552 }
558553
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-
576554 // so far so good, let's look at the session
577555 if (session != null ) {
578556 // we tried to resume, let's see what the server decided
@@ -659,6 +637,70 @@ public Subject run() throws Exception {
659637 }
660638 }
661639
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+
662704 if (resumingSession && session != null ) {
663705 setHandshakeSessionSE (session );
664706 // Reserve the handshake state if this is a session-resumption
@@ -681,8 +723,8 @@ public Subject run() throws Exception {
681723 && (type != ExtensionType .EXT_RENEGOTIATION_INFO )
682724 // -- token binding etc. changes begin --
683725 && (type != ExtensionType .EXT_TOKEN_BINDING )
684- && (type != ExtensionType .EXT_EXTENDED_MASTER_SECRET )) {
685726 // -- token binding etc. changes end --
727+ && (type != ExtensionType .EXT_EXTENDED_MASTER_SECRET )){
686728 fatalSE (Alerts .alert_unsupported_extension ,
687729 "Server sent an unsupported extension: " + type );
688730 }
@@ -691,7 +733,8 @@ public Subject run() throws Exception {
691733 // Create a new session, we need to do the full handshake
692734 session = new SSLSessionImpl (protocolVersion , cipherSuite ,
693735 getLocalSupportedSignAlgs (),
694- mesg .sessionId , getHostSE (), getPortSE ());
736+ mesg .sessionId , getHostSE (), getPortSE (),
737+ (extendedMasterSecretExt != null ));
695738 session .setRequestedServerNames (requestedServerNames );
696739 setHandshakeSessionSE (session );
697740 if (debug != null && Debug .isOn ("handshake" )) {
@@ -1327,6 +1370,44 @@ HandshakeMessage getKickstartMessage() throws SSLException {
13271370 session = null ;
13281371 }
13291372
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+
13301411 if (session != null ) {
13311412 if (debug != null ) {
13321413 if (Debug .isOn ("handshake" ) || Debug .isOn ("session" )) {
@@ -1411,12 +1492,12 @@ HandshakeMessage getKickstartMessage() throws SSLException {
14111492
14121493 // add elliptic curves and point format extensions
14131494 if (cipherSuites .containsEC ()) {
1414- SupportedEllipticCurvesExtension ece =
1415- SupportedEllipticCurvesExtension .createExtension (algorithmConstraints );
1495+ EllipticCurvesExtension ece =
1496+ EllipticCurvesExtension .createExtension (algorithmConstraints );
14161497 if (ece != null ) {
14171498 clientHelloMessage .extensions .add (ece );
14181499 clientHelloMessage .extensions .add (
1419- SupportedEllipticPointFormatsExtension .DEFAULT );
1500+ EllipticPointFormatsExtension .DEFAULT );
14201501 }
14211502 }
14221503
@@ -1433,6 +1514,14 @@ HandshakeMessage getKickstartMessage() throws SSLException {
14331514 clientHelloMessage .addSignatureAlgorithmsExtension (localSignAlgs );
14341515 }
14351516
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+
14361525 // add server_name extension
14371526 if (enableSNIExtension ) {
14381527 if (session != null ) {
@@ -1463,8 +1552,7 @@ HandshakeMessage getKickstartMessage() throws SSLException {
14631552 // -- token binding etc. changes begin --
14641553 byte [] supportedTokenBindingKeyParams = getConnectionSupportedTokenBindingKeyParams ();
14651554
1466- if (supportedTokenBindingKeyParams != null && supportedTokenBindingKeyParams .length > 0 ) {
1467- clientHelloMessage .extensions .add (new ExtendedMasterSecretExtension ());
1555+ if (supportedTokenBindingKeyParams != null && supportedTokenBindingKeyParams .length > 0 && requestedToUseEMS ) {
14681556 clientHelloMessage .extensions .add (new TokenBindingExtension (1 , 0 , supportedTokenBindingKeyParams ));
14691557 }
14701558 // -- token binding etc. changes end --
@@ -1479,6 +1567,7 @@ byte[] getDefaultSupportedTokenBindingKeyParams() {
14791567 }
14801568 // -- token binding etc. changes end --
14811569
1570+
14821571 /*
14831572 * Fault detected during handshake.
14841573 */
@@ -1509,10 +1598,14 @@ private void serverCertificate(CertificateMsg mesg) throws IOException {
15091598 // Allow server certificate change in client side during renegotiation
15101599 // after a session-resumption abbreviated initial handshake?
15111600 //
1512- // DO NOT need to check allowUnsafeServerCertChange here. We only
1601+ // DO NOT need to check allowUnsafeServerCertChange here. We only
15131602 // reserve server certificates when allowUnsafeServerCertChange is
15141603 // 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 ()) {
15161609 // It is not necessary to check the certificate update if endpoint
15171610 // identification is enabled.
15181611 String identityAlg = getEndpointIdentificationAlgorithmSE ();
0 commit comments