jdk/src/share/classes/sun/security/ssl/RSAClientKeyExchange.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7039 6464c8e62a18
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2007, 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
     * Default is "false" (old behavior) for compatibility reasons. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * will be changed in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static String PROP_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                "com.sun.net.ssl.rsaPreMasterSecretFix";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static boolean rsaPreMasterSecretFix =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                Debug.getBooleanProperty(PROP_NAME, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    int messageType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return ht_client_key_exchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The following field values were encrypted with the server's public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * key (or temp key from server key exchange msg) and are presented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * here in DECRYPTED form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private ProtocolVersion protocolVersion; // preMaster [0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    SecretKey preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private byte[] encrypted;           // same size as public modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Client randomly creates a pre-master secret and encrypts it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * using the server's RSA public key; only the server can decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * it, using its RSA private key.  Result is the same size as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * server's public key, and uses PKCS #1 block format 02.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    RSAClientKeyExchange(ProtocolVersion protocolVersion, ProtocolVersion maxVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            SecureRandom generator, PublicKey publicKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (publicKey.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new SSLKeyException("Public key not of type RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int major, minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (rsaPreMasterSecretFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            major = maxVersion.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            minor = maxVersion.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            major = protocolVersion.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            minor = protocolVersion.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            KeyGenerator kg = JsseJce.getKeyGenerator("SunTlsRsaPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            kg.init(new TlsRsaPremasterSecretParameterSpec(major, minor));
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    RSAClientKeyExchange(ProtocolVersion currentVersion, HandshakeInStream input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int messageSize, PrivateKey privateKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (privateKey.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new SSLKeyException("Private key not of type RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.protocolVersion = currentVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (currentVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            encrypted = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            encrypted = new byte [messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (input.read(encrypted) != messageSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        ("SSL: read PreMasterSecret: short read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            cipher.init(Cipher.UNWRAP_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            preMaster = (SecretKey)cipher.unwrap(encrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             * Bogus decrypted ClientKeyExchange? If so, conjure a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             * a random preMaster secret that will fail later during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             * Finished message processing. This is a countermeasure against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             * the "interactive RSA PKCS#1 encryption envelop attack" reported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
             * in June 1998. Preserving the executation path will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
             * mitigate timing attacks and force consistent error handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             * that will prevent an attacking client from differentiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
             * different kinds of decrypted ClientKeyExchange bogosities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                System.out.println("Error decrypting premaster secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                System.out.println("Generating random secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            preMaster = generateDummySecret(currentVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // generate a premaster secret with the specified version number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static SecretKey generateDummySecret(ProtocolVersion version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            KeyGenerator kg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    JsseJce.getKeyGenerator("SunTlsRsaPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            kg.init(new TlsRsaPremasterSecretParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    (version.major, version.minor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new RuntimeException("Could not generate dummy secret", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return encrypted.length + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return encrypted.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            s.putBytes16(encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            s.write(encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        s.println("*** ClientKeyExchange, RSA PreMasterSecret, " + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}