diff --git a/src/share/classes/com/sun/crypto/provider/SunJCE.java b/src/share/classes/com/sun/crypto/provider/SunJCE.java index 08e1e2ae7371996c69bf358f872b23dede5d381d..55c55e2de59b6f8042eed60104c0a40cd999cc79 100644 --- a/src/share/classes/com/sun/crypto/provider/SunJCE.java +++ b/src/share/classes/com/sun/crypto/provider/SunJCE.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -762,6 +762,8 @@ public final class SunJCE extends Provider { "com.sun.crypto.provider.TlsMasterSecretGenerator"); put("Alg.Alias.KeyGenerator.SunTls12MasterSecret", "SunTlsMasterSecret"); + put("Alg.Alias.KeyGenerator.SunTlsExtendedMasterSecret", + "SunTlsMasterSecret"); put("KeyGenerator.SunTlsKeyMaterial", "com.sun.crypto.provider.TlsKeyMaterialGenerator"); diff --git a/src/share/classes/com/sun/crypto/provider/TlsMasterSecretGenerator.java b/src/share/classes/com/sun/crypto/provider/TlsMasterSecretGenerator.java index 0efa0aef9bf1a41a489aa2af08fb52f6cccad384..9a6308f34469b815c77af419a06ec471d745a680 100644 --- a/src/share/classes/com/sun/crypto/provider/TlsMasterSecretGenerator.java +++ b/src/share/classes/com/sun/crypto/provider/TlsMasterSecretGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -100,21 +100,32 @@ public final class TlsMasterSecretGenerator extends KeyGeneratorSpi { try { byte[] master; - byte[] clientRandom = spec.getClientRandom(); - byte[] serverRandom = spec.getServerRandom(); - if (protocolVersion >= 0x0301) { - byte[] seed = concat(clientRandom, serverRandom); + byte[] label; + byte[] seed; + byte[] extendedMasterSecretSessionHash = + spec.getExtendedMasterSecretSessionHash(); + if (extendedMasterSecretSessionHash.length != 0) { + label = LABEL_EXTENDED_MASTER_SECRET; + seed = extendedMasterSecretSessionHash; + } else { + byte[] clientRandom = spec.getClientRandom(); + byte[] serverRandom = spec.getServerRandom(); + label = LABEL_MASTER_SECRET; + seed = concat(clientRandom, serverRandom); + } master = ((protocolVersion >= 0x0303) ? - doTLS12PRF(premaster, LABEL_MASTER_SECRET, seed, 48, - spec.getPRFHashAlg(), spec.getPRFHashLength(), - spec.getPRFBlockSize()) : - doTLS10PRF(premaster, LABEL_MASTER_SECRET, seed, 48)); + doTLS12PRF(premaster, label, seed, 48, + spec.getPRFHashAlg(), spec.getPRFHashLength(), + spec.getPRFBlockSize()) : + doTLS10PRF(premaster, label, seed, 48)); } else { master = new byte[48]; MessageDigest md5 = MessageDigest.getInstance("MD5"); MessageDigest sha = MessageDigest.getInstance("SHA"); + byte[] clientRandom = spec.getClientRandom(); + byte[] serverRandom = spec.getServerRandom(); byte[] tmp = new byte[20]; for (int i = 0; i < 3; i++) { sha.update(SSL3_CONST[i]); @@ -172,5 +183,5 @@ public final class TlsMasterSecretGenerator extends KeyGeneratorSpi { } } - } + diff --git a/src/share/classes/com/sun/crypto/provider/TlsPrfGenerator.java b/src/share/classes/com/sun/crypto/provider/TlsPrfGenerator.java index f09b7d875de2b071866d27fc63284569f68ef0f8..a84083947320863c0469178dab07e3de05f26929 100644 --- a/src/share/classes/com/sun/crypto/provider/TlsPrfGenerator.java +++ b/src/share/classes/com/sun/crypto/provider/TlsPrfGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -55,6 +55,11 @@ abstract class TlsPrfGenerator extends KeyGeneratorSpi { final static byte[] LABEL_MASTER_SECRET = // "master secret" { 109, 97, 115, 116, 101, 114, 32, 115, 101, 99, 114, 101, 116 }; + final static byte[] LABEL_EXTENDED_MASTER_SECRET = + // "extended master secret" + { 101, 120, 116, 101, 110, 100, 101, 100, 32, 109, 97, 115, 116, + 101, 114, 32, 115, 101, 99, 114, 101, 116 }; + final static byte[] LABEL_KEY_EXPANSION = // "key expansion" { 107, 101, 121, 32, 101, 120, 112, 97, 110, 115, 105, 111, 110 }; diff --git a/src/share/classes/sun/security/internal/spec/TlsMasterSecretParameterSpec.java b/src/share/classes/sun/security/internal/spec/TlsMasterSecretParameterSpec.java index 832b38edd76e71773e6fe8bf9905a27dfc403035..e563360481b2a04e6d98889787c486c63642a589 100644 --- a/src/share/classes/sun/security/internal/spec/TlsMasterSecretParameterSpec.java +++ b/src/share/classes/sun/security/internal/spec/TlsMasterSecretParameterSpec.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -48,6 +48,7 @@ public class TlsMasterSecretParameterSpec implements AlgorithmParameterSpec { private final SecretKey premasterSecret; private final int majorVersion, minorVersion; private final byte[] clientRandom, serverRandom; + private final byte[] extendedMasterSecretSessionHash; private final String prfHashAlg; private final int prfHashLength; private final int prfBlockSize; @@ -80,6 +81,50 @@ public class TlsMasterSecretParameterSpec implements AlgorithmParameterSpec { int majorVersion, int minorVersion, byte[] clientRandom, byte[] serverRandom, String prfHashAlg, int prfHashLength, int prfBlockSize) { + this(premasterSecret, majorVersion, minorVersion, + clientRandom, serverRandom, + new byte[0], + prfHashAlg, prfHashLength, prfBlockSize); + } + + /** + * Constructs a new TlsMasterSecretParameterSpec. + * + *
The getAlgorithm() method of premasterSecret
+ * should return "TlsRsaPremasterSecret" if the key exchange
+ * algorithm was RSA and "TlsPremasterSecret" otherwise.
+ *
+ * @param premasterSecret the premaster secret
+ * @param majorVersion the major number of the protocol version
+ * @param minorVersion the minor number of the protocol version
+ * @param extendedMasterSecretSessionHash the session hash for
+ * Extended Master Secret
+ * @param prfHashAlg the name of the TLS PRF hash algorithm to use.
+ * Used only for TLS 1.2+. TLS1.1 and earlier use a fixed PRF.
+ * @param prfHashLength the output length of the TLS PRF hash algorithm.
+ * Used only for TLS 1.2+.
+ * @param prfBlockSize the input block size of the TLS PRF hash algorithm.
+ * Used only for TLS 1.2+.
+ *
+ * @throws NullPointerException if premasterSecret is null
+ * @throws IllegalArgumentException if minorVersion or majorVersion are
+ * negative or larger than 255
+ */
+ public TlsMasterSecretParameterSpec(SecretKey premasterSecret,
+ int majorVersion, int minorVersion,
+ byte[] extendedMasterSecretSessionHash,
+ String prfHashAlg, int prfHashLength, int prfBlockSize) {
+ this(premasterSecret, majorVersion, minorVersion,
+ new byte[0], new byte[0],
+ extendedMasterSecretSessionHash,
+ prfHashAlg, prfHashLength, prfBlockSize);
+ }
+
+ private TlsMasterSecretParameterSpec(SecretKey premasterSecret,
+ int majorVersion, int minorVersion,
+ byte[] clientRandom, byte[] serverRandom,
+ byte[] extendedMasterSecretSessionHash,
+ String prfHashAlg, int prfHashLength, int prfBlockSize) {
if (premasterSecret == null) {
throw new NullPointerException("premasterSecret must not be null");
}
@@ -88,6 +133,9 @@ public class TlsMasterSecretParameterSpec implements AlgorithmParameterSpec {
this.minorVersion = checkVersion(minorVersion);
this.clientRandom = clientRandom.clone();
this.serverRandom = serverRandom.clone();
+ this.extendedMasterSecretSessionHash =
+ (extendedMasterSecretSessionHash != null ?
+ extendedMasterSecretSessionHash.clone() : new byte[0]);
this.prfHashAlg = prfHashAlg;
this.prfHashLength = prfHashLength;
this.prfBlockSize = prfBlockSize;
@@ -146,6 +194,17 @@ public class TlsMasterSecretParameterSpec implements AlgorithmParameterSpec {
return serverRandom.clone();
}
+ /**
+ * Returns a copy of the Extended Master Secret session hash.
+ *
+ * @return a copy of the Extended Master Secret session hash, or an empty
+ * array if no extended master secret session hash was provided
+ * at instantiation time
+ */
+ public byte[] getExtendedMasterSecretSessionHash() {
+ return extendedMasterSecretSessionHash.clone();
+ }
+
/**
* Obtains the PRF hash algorithm to use in the PRF calculation.
*
diff --git a/src/share/classes/sun/security/ssl/ClientHandshaker.java b/src/share/classes/sun/security/ssl/ClientHandshaker.java
index 73dc663a26ebbf34797de49997b5fce7996fc430..7edef24c4b0d57d7b2fbfcd4a9f820b6248d2afe 100644
--- a/src/share/classes/sun/security/ssl/ClientHandshaker.java
+++ b/src/share/classes/sun/security/ssl/ClientHandshaker.java
@@ -633,6 +633,54 @@ final class ClientHandshaker extends Handshaker {
}
}
+ // check the "extended_master_secret" extension
+ ExtendedMasterSecretExtension extendedMasterSecretExt =
+ (ExtendedMasterSecretExtension)mesg.extensions.get(
+ ExtensionType.EXT_EXTENDED_MASTER_SECRET);
+ if (extendedMasterSecretExt != null) {
+ // Is it the expected server extension?
+ if (!useExtendedMasterSecret ||
+ !(mesgVersion.v >= ProtocolVersion.TLS10.v) || !requestedToUseEMS) {
+ fatalSE(Alerts.alert_unsupported_extension,
+ "Server sent the extended_master_secret " +
+ "extension improperly");
+ }
+
+ // For abbreviated handshake, if the original session did not use
+ // the "extended_master_secret" extension but the new ServerHello
+ // contains the extension, the client MUST abort the handshake.
+ if (resumingSession && (session != null) &&
+ !session.getUseExtendedMasterSecret()) {
+ fatalSE(Alerts.alert_unsupported_extension,
+ "Server sent an unexpected extended_master_secret " +
+ "extension on session resumption");
+ }
+ } else {
+ if (useExtendedMasterSecret && !allowLegacyMasterSecret) {
+ // For full handshake, if a client receives a ServerHello
+ // without the extension, it SHOULD abort the handshake if
+ // it does not wish to interoperate with legacy servers.
+ fatalSE(Alerts.alert_handshake_failure,
+ "Extended Master Secret extension is required");
+ }
+
+ if (resumingSession && (session != null)) {
+ if (session.getUseExtendedMasterSecret()) {
+ // For abbreviated handshake, if the original session used
+ // the "extended_master_secret" extension but the new
+ // ServerHello does not contain the extension, the client
+ // MUST abort the handshake.
+ fatalSE(Alerts.alert_handshake_failure,
+ "Missing Extended Master Secret extension " +
+ "on session resumption");
+ } else if (useExtendedMasterSecret && !allowLegacyResumption) {
+ // Unlikely, abbreviated handshake should be discarded.
+ fatalSE(Alerts.alert_handshake_failure,
+ "Extended Master Secret extension is required");
+ }
+ }
+ }
+
if (resumingSession && session != null) {
setHandshakeSessionSE(session);
// Reserve the handshake state if this is a session-resumption
@@ -652,7 +700,8 @@ final class ClientHandshaker extends Handshaker {
} else if ((type != ExtensionType.EXT_ELLIPTIC_CURVES)
&& (type != ExtensionType.EXT_EC_POINT_FORMATS)
&& (type != ExtensionType.EXT_SERVER_NAME)
- && (type != ExtensionType.EXT_RENEGOTIATION_INFO)) {
+ && (type != ExtensionType.EXT_RENEGOTIATION_INFO)
+ && (type != ExtensionType.EXT_EXTENDED_MASTER_SECRET)){
fatalSE(Alerts.alert_unsupported_extension,
"Server sent an unsupported extension: " + type);
}
@@ -661,7 +710,8 @@ final class ClientHandshaker extends Handshaker {
// Create a new session, we need to do the full handshake
session = new SSLSessionImpl(protocolVersion, cipherSuite,
getLocalSupportedSignAlgs(),
- mesg.sessionId, getHostSE(), getPortSE());
+ mesg.sessionId, getHostSE(), getPortSE(),
+ (extendedMasterSecretExt != null));
session.setRequestedServerNames(requestedServerNames);
setHandshakeSessionSE(session);
if (debug != null && Debug.isOn("handshake")) {
@@ -1297,6 +1347,44 @@ final class ClientHandshaker extends Handshaker {
session = null;
}
+ if ((session != null) && useExtendedMasterSecret) {
+ boolean isTLS10Plus = sessionVersion.v >= ProtocolVersion.TLS10.v;
+ if (isTLS10Plus && !session.getUseExtendedMasterSecret()) {
+ if (!allowLegacyResumption) {
+ // perform full handshake instead
+ //
+ // The client SHOULD NOT offer an abbreviated handshake
+ // to resume a session that does not use an extended
+ // master secret. Instead, it SHOULD offer a full
+ // handshake.
+ session = null;
+ }
+ }
+
+ if ((session != null) && !allowUnsafeServerCertChange) {
+ // It is fine to move on with abbreviate handshake if
+ // endpoint identification is enabled.
+ String identityAlg = getEndpointIdentificationAlgorithmSE();
+ if ((identityAlg == null || identityAlg.length() == 0)) {
+ if (isTLS10Plus) {
+ if (!session.getUseExtendedMasterSecret()) {
+ // perform full handshake instead
+ session = null;
+ } // Otherwise, use extended master secret.
+ } else {
+ // The extended master secret extension does not
+ // apply to SSL 3.0. Perform a full handshake
+ // instead.
+ //
+ // Note that the useExtendedMasterSecret is
+ // extended to protect SSL 3.0 connections,
+ // by discarding abbreviate handshake.
+ session = null;
+ }
+ }
+ }
+ }
+
if (session != null) {
if (debug != null) {
if (Debug.isOn("handshake") || Debug.isOn("session")) {
@@ -1403,6 +1491,14 @@ final class ClientHandshaker extends Handshaker {
clientHelloMessage.addSignatureAlgorithmsExtension(localSignAlgs);
}
+ // add Extended Master Secret extension
+ if (useExtendedMasterSecret && (maxProtocolVersion.v >= ProtocolVersion.TLS10.v)) {
+ if ((session == null) || session.getUseExtendedMasterSecret()) {
+ clientHelloMessage.addExtendedMasterSecretExtension();
+ requestedToUseEMS = true;
+ }
+ }
+
// add server_name extension
if (enableSNIExtension) {
if (session != null) {
@@ -1463,10 +1559,14 @@ final class ClientHandshaker extends Handshaker {
// Allow server certificate change in client side during renegotiation
// after a session-resumption abbreviated initial handshake?
//
- // DO NOT need to check allowUnsafeServerCertChange here. We only
+ // DO NOT need to check allowUnsafeServerCertChange here. We only
// reserve server certificates when allowUnsafeServerCertChange is
// flase.
- if (reservedServerCerts != null) {
+ //
+ // Allow server certificate change if it is negotiated to use the
+ // extended master secret.
+ if ((reservedServerCerts != null) &&
+ !session.getUseExtendedMasterSecret()) {
// It is not necessary to check the certificate update if endpoint
// identification is enabled.
String identityAlg = getEndpointIdentificationAlgorithmSE();
diff --git a/src/share/classes/sun/security/ssl/ExtendedMasterSecretExtension.java b/src/share/classes/sun/security/ssl/ExtendedMasterSecretExtension.java
new file mode 100644
index 0000000000000000000000000000000000000000..bf7f7aefd75b8c485c3886ee062bcdb63b028d29
--- /dev/null
+++ b/src/share/classes/sun/security/ssl/ExtendedMasterSecretExtension.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.security.ssl;
+
+import java.io.IOException;
+import javax.net.ssl.SSLProtocolException;
+
+/**
+ * Extended Master Secret TLS extension (TLS 1.0+). This extension
+ * defines how to calculate the TLS connection master secret and
+ * mitigates some types of man-in-the-middle attacks.
+ *
+ * See further information in
+ * RFC 7627.
+ *
+ * @author Martin Balao (mbalao@redhat.com)
+ */
+final class ExtendedMasterSecretExtension extends HelloExtension {
+ ExtendedMasterSecretExtension() {
+ super(ExtensionType.EXT_EXTENDED_MASTER_SECRET);
+ }
+
+ ExtendedMasterSecretExtension(HandshakeInStream s,
+ int len) throws IOException {
+ super(ExtensionType.EXT_EXTENDED_MASTER_SECRET);
+
+ if (len != 0) {
+ throw new SSLProtocolException("Invalid " + type + " extension");
+ }
+ }
+
+ @Override
+ int length() {
+ return 4; // 4: extension type and length fields
+ }
+
+ @Override
+ void send(HandshakeOutStream s) throws IOException {
+ s.putInt16(type.id); // ExtensionType extension_type;
+ s.putInt16(0); // extension_data length
+ }
+
+ @Override
+ public String toString() {
+ return "Extension " + type;
+ }
+}
+
diff --git a/src/share/classes/sun/security/ssl/ExtensionType.java b/src/share/classes/sun/security/ssl/ExtensionType.java
index 99de20a6ca5bcf92a2804cea31a364d264f66180..4c5dcf2522ef4747f7f29a4fe6a05a4ff8294122 100644
--- a/src/share/classes/sun/security/ssl/ExtensionType.java
+++ b/src/share/classes/sun/security/ssl/ExtensionType.java
@@ -43,7 +43,7 @@ final class ExtensionType {
return name;
}
- static List