jdk/src/share/classes/sun/security/ssl/RSAClientKeyExchange.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 7527 287acfa1a9f2
child 14212 faa4afc89a09
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This is the client key exchange message (CLIENT --> SERVER) used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * all RSA key exchanges; it holds the RSA-encrypted pre-master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The message is encrypted using PKCS #1 block type 02 encryption with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * server's public key.  The padding and resulting message size is a function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of this server's public key modulus size, but the pre-master secret is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * always exactly 48 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
final class RSAClientKeyExchange extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The TLS spec says that the version in the RSA premaster secret must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * be the maximum version supported by the client (i.e. the version it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * requested in its client hello version). However, we (and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * implementations) used to send the active negotiated version. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * system property below allows to toggle the behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final static String PROP_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                "com.sun.net.ssl.rsaPreMasterSecretFix";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    62
    /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    63
     * Default is "false" (old behavior) for compatibility reasons in
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    64
     * SSLv3/TLSv1.  Later protocols (TLSv1.1+) do not use this property.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    65
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static boolean rsaPreMasterSecretFix =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                Debug.getBooleanProperty(PROP_NAME, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The following field values were encrypted with the server's public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * key (or temp key from server key exchange msg) and are presented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * here in DECRYPTED form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private ProtocolVersion protocolVersion; // preMaster [0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    SecretKey preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private byte[] encrypted;           // same size as public modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Client randomly creates a pre-master secret and encrypts it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * using the server's RSA public key; only the server can decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * it, using its RSA private key.  Result is the same size as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * server's public key, and uses PKCS #1 block format 02.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    84
    RSAClientKeyExchange(ProtocolVersion protocolVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    85
            ProtocolVersion maxVersion,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            SecureRandom generator, PublicKey publicKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (publicKey.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new SSLKeyException("Public key not of type RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int major, minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    94
        if (rsaPreMasterSecretFix || maxVersion.v >= ProtocolVersion.TLS11.v) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            major = maxVersion.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            minor = maxVersion.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            major = protocolVersion.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            minor = protocolVersion.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   103
            String s = ((protocolVersion.v >= ProtocolVersion.TLS12.v) ?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   104
                "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   105
            KeyGenerator kg = JsseJce.getKeyGenerator(s);
7527
287acfa1a9f2 6943352: SSL regression: RSAClientKeyExchange fails to pass securerandom arg to KeyGen
weijun
parents: 7043
diff changeset
   106
            kg.init(new TlsRsaPremasterSecretParameterSpec(major, minor),
287acfa1a9f2 6943352: SSL regression: RSAClientKeyExchange fails to pass securerandom arg to KeyGen
weijun
parents: 7043
diff changeset
   107
                    generator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            preMaster = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            cipher.init(Cipher.WRAP_MODE, publicKey, generator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            encrypted = cipher.wrap(preMaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw (SSLKeyException)new SSLKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                ("RSA premaster secret error").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Server gets the PKCS #1 (block format 02) data, decrypts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * it with its private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   123
    RSAClientKeyExchange(ProtocolVersion currentVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   124
            ProtocolVersion maxVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   125
            SecureRandom generator, HandshakeInStream input,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int messageSize, PrivateKey privateKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (privateKey.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new SSLKeyException("Private key not of type RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (currentVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            encrypted = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            encrypted = new byte [messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (input.read(encrypted) != messageSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        ("SSL: read PreMasterSecret: short read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            cipher.init(Cipher.UNWRAP_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            preMaster = (SecretKey)cipher.unwrap(encrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   147
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   148
            // polish the premaster secret
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   149
            preMaster = polishPreMasterSecretKey(currentVersion, maxVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   150
                                                generator, preMaster, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (Exception e) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   152
            // polish the premaster secret
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   153
            preMaster =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   154
                    polishPreMasterSecretKey(currentVersion, maxVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   155
                                                generator, null, e);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   156
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   157
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   158
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   159
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   160
     * To avoid vulnerabilities described by section 7.4.7.1, RFC 5246,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   161
     * treating incorrectly formatted message blocks and/or mismatched
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   162
     * version numbers in a manner indistinguishable from correctly
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   163
     * formatted RSA blocks.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   164
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   165
     * RFC 5246 describes the approach as :
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   166
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   167
     *  1. Generate a string R of 46 random bytes
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   168
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   169
     *  2. Decrypt the message to recover the plaintext M
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   170
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   171
     *  3. If the PKCS#1 padding is not correct, or the length of message
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   172
     *     M is not exactly 48 bytes:
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   173
     *        pre_master_secret = ClientHello.client_version || R
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   174
     *     else If ClientHello.client_version <= TLS 1.0, and version
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   175
     *     number check is explicitly disabled:
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   176
     *        pre_master_secret = M
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   177
     *     else:
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   178
     *        pre_master_secret = ClientHello.client_version || M[2..47]
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   179
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   180
    private SecretKey polishPreMasterSecretKey(ProtocolVersion currentVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   181
            ProtocolVersion clientHelloVersion, SecureRandom generator,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   182
            SecretKey secretKey, Exception failoverException) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   183
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   184
        this.protocolVersion = clientHelloVersion;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   185
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   186
        if (failoverException == null && secretKey != null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   187
            // check the length
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   188
            byte[] encoded = secretKey.getEncoded();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   189
            if (encoded == null) {      // unable to get the encoded key
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   190
                if (debug != null && Debug.isOn("handshake")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   191
                    System.out.println(
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   192
                        "unable to get the plaintext of the premaster secret");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   193
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   194
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   195
                // We are not always able to get the encoded key of the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   196
                // premaster secret. Pass the cheking to master secret
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   197
                // calculation.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   198
                return secretKey;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   199
            } else if (encoded.length == 48) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   200
                // check the version
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   201
                if (clientHelloVersion.major == encoded[0] &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   202
                    clientHelloVersion.minor == encoded[1]) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   203
                    return secretKey;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   204
                } else if (clientHelloVersion.v <= ProtocolVersion.TLS10.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   205
                    /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   206
                     * we never checked the client_version in server side
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   207
                     * for TLS v1.0 and SSL v3.0. For compatibility, we
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   208
                     * maintain this behavior.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   209
                     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   210
                    if (currentVersion.major == encoded[0] &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   211
                        currentVersion.minor == encoded[1]) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   212
                        this.protocolVersion = currentVersion;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   213
                        return secretKey;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   214
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   215
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   216
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   217
                if (debug != null && Debug.isOn("handshake")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   218
                    System.out.println("Mismatching Protocol Versions, " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   219
                        "ClientHello.client_version is " + clientHelloVersion +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   220
                        ", while PreMasterSecret.client_version is " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   221
                        ProtocolVersion.valueOf(encoded[0], encoded[1]));
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   222
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   223
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   224
                if (debug != null && Debug.isOn("handshake")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   225
                    System.out.println(
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   226
                        "incorrect length of premaster secret: " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   227
                        encoded.length);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   228
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   229
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   230
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   231
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   232
        if (debug != null && Debug.isOn("handshake")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   233
            if (failoverException != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                System.out.println("Error decrypting premaster secret:");
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   235
                failoverException.printStackTrace(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   237
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   238
            System.out.println("Generating random secret");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   240
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   241
        return generateDummySecret(clientHelloVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    // generate a premaster secret with the specified version number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    static SecretKey generateDummySecret(ProtocolVersion version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   247
            String s = ((version.v >= ProtocolVersion.TLS12.v) ?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   248
                "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   249
            KeyGenerator kg = JsseJce.getKeyGenerator(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            kg.init(new TlsRsaPremasterSecretParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    (version.major, version.minor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new RuntimeException("Could not generate dummy secret", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   258
    @Override
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   259
    int messageType() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   260
        return ht_client_key_exchange;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   261
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   262
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   263
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return encrypted.length + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return encrypted.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   272
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            s.putBytes16(encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            s.write(encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   281
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    void print(PrintStream s) throws IOException {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   283
        s.println("*** ClientKeyExchange, RSA PreMasterSecret, " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   284
                                                        protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}