jdk/src/java.base/share/classes/sun/security/ssl/RSAClientKeyExchange.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 41961 12edcce2cb80
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36661
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
     2
 * Copyright (c) 1996, 2016, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 14212
diff changeset
    37
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This is the client key exchange message (CLIENT --> SERVER) used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * all RSA key exchanges; it holds the RSA-encrypted pre-master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The message is encrypted using PKCS #1 block type 02 encryption with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * server's public key.  The padding and resulting message size is a function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of this server's public key modulus size, but the pre-master secret is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * always exactly 48 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
final class RSAClientKeyExchange extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The following field values were encrypted with the server's public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * key (or temp key from server key exchange msg) and are presented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * here in DECRYPTED form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private ProtocolVersion protocolVersion; // preMaster [0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    SecretKey preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private byte[] encrypted;           // same size as public modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Client randomly creates a pre-master secret and encrypts it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * using the server's RSA public key; only the server can decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * it, using its RSA private key.  Result is the same size as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * server's public key, and uses PKCS #1 block format 02.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
    66
    @SuppressWarnings("deprecation")
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    67
    RSAClientKeyExchange(ProtocolVersion protocolVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    68
            ProtocolVersion maxVersion,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            SecureRandom generator, PublicKey publicKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (publicKey.getAlgorithm().equals("RSA") == false) {
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36661
diff changeset
    71
            throw new SSLKeyException("Public key not of type RSA: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36661
diff changeset
    72
                publicKey.getAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 27804
diff changeset
    77
            String s = protocolVersion.useTLS12PlusSpec() ?
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 27804
diff changeset
    78
                "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret";
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    79
            KeyGenerator kg = JsseJce.getKeyGenerator(s);
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
    80
            kg.init(new TlsRsaPremasterSecretParameterSpec(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
    81
                    maxVersion.v, protocolVersion.v), generator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            preMaster = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            cipher.init(Cipher.WRAP_MODE, publicKey, generator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            encrypted = cipher.wrap(preMaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw (SSLKeyException)new SSLKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                ("RSA premaster secret error").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /*
38741
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
    94
     * Retrieving the cipher's provider name for the debug purposes
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
    95
     * can throw an exception by itself.
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
    96
     */
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
    97
    private static String safeProviderName(Cipher cipher) {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
    98
        try {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
    99
            return cipher.getProvider().toString();
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   100
        } catch (Exception e) {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   101
            if (debug != null && Debug.isOn("handshake")) {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   102
                System.out.println("Retrieving The Cipher provider name" +
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   103
                        " caused exception " + e.getMessage());
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   104
            }
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   105
        }
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   106
        try {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   107
            return cipher.toString() + " (provider name not available)";
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   108
        } catch (Exception e) {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   109
            if (debug != null && Debug.isOn("handshake")) {
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   110
                System.out.println("Retrieving The Cipher name" +
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   111
                        " caused exception " + e.getMessage());
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   112
            }
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   113
        }
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   114
        return "(cipher/provider names not available)";
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   115
    }
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   116
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   117
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Server gets the PKCS #1 (block format 02) data, decrypts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * it with its private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   121
    @SuppressWarnings("deprecation")
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   122
    RSAClientKeyExchange(ProtocolVersion currentVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   123
            ProtocolVersion maxVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   124
            SecureRandom generator, HandshakeInStream input,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int messageSize, PrivateKey privateKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (privateKey.getAlgorithm().equals("RSA") == false) {
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36661
diff changeset
   128
            throw new SSLKeyException("Private key not of type RSA: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36661
diff changeset
   129
                 privateKey.getAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 27804
diff changeset
   132
        if (currentVersion.useTLS10PlusSpec()) {
2
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) {
22309
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   137
                throw new SSLProtocolException(
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   138
                        "SSL: read PreMasterSecret: short read");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   142
        byte[] encoded = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        try {
36661
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   144
            boolean needFailover = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
36661
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   146
            try {
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   147
                // Try UNWRAP_MODE mode firstly.
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   148
                cipher.init(Cipher.UNWRAP_MODE, privateKey,
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   149
                        new TlsRsaPremasterSecretParameterSpec(
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   150
                                maxVersion.v, currentVersion.v),
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   151
                        generator);
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   152
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   153
                // The provider selection can be delayed, please don't call
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   154
                // any Cipher method before the call to Cipher.init().
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   155
                needFailover = !KeyUtil.isOracleJCEProvider(
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   156
                        cipher.getProvider().getName());
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   157
            } catch (InvalidKeyException | UnsupportedOperationException iue) {
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   158
                if (debug != null && Debug.isOn("handshake")) {
38741
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   159
                    System.out.println("The Cipher provider "
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   160
                            + safeProviderName(cipher)
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   161
                            + " caused exception: " + iue.getMessage());
36661
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   162
                }
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   163
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   164
                needFailover = true;
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   165
            }
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   166
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   167
            if (needFailover) {
38741
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   168
                // The cipher might be spoiled by unsuccessful call to init(),
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   169
                // so request a fresh instance
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   170
                cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
b29b75c25015 8158111: Make handling of 3rd party providers more stable
igerasim
parents: 36952
diff changeset
   171
36661
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   172
                // Use DECRYPT_MODE and dispose the previous initialization.
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   173
                cipher.init(Cipher.DECRYPT_MODE, privateKey);
35287
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   174
                boolean failed = false;
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   175
                try {
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   176
                    encoded = cipher.doFinal(encrypted);
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   177
                } catch (BadPaddingException bpe) {
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   178
                    // Note: encoded == null
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   179
                    failed = true;
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   180
                }
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   181
                encoded = KeyUtil.checkTlsPreMasterSecretKey(
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   182
                                maxVersion.v, currentVersion.v,
35287
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 35279
diff changeset
   183
                                generator, encoded, failed);
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   184
                preMaster = generatePreMasterSecret(
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   185
                                maxVersion.v, currentVersion.v,
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   186
                                encoded, generator);
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   187
            } else {
36661
044bf6a5474a 8149017: Delayed provider selection broken in RSA client key exchange
xuelei
parents: 35287
diff changeset
   188
                // the cipher should have been initialized
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   189
                preMaster = (SecretKey)cipher.unwrap(encrypted,
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   190
                        "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   191
            }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   192
        } catch (InvalidKeyException ibk) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   193
            // the message is too big to process with RSA
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36661
diff changeset
   194
            throw new SSLException(
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36661
diff changeset
   195
                "Unable to process PreMasterSecret", ibk);
22309
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   196
        } catch (Exception e) {
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   197
            // unlikely to happen, otherwise, must be a provider exception
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   198
            if (debug != null && Debug.isOn("handshake")) {
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   199
                System.out.println("RSA premaster secret decryption error:");
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   200
                e.printStackTrace(System.out);
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   201
            }
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   202
            throw new RuntimeException("Could not generate dummy secret", e);
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 16100
diff changeset
   203
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
35279
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   206
    // generate a premaster secret with the specified version number
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   207
    @SuppressWarnings("deprecation")
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   208
    private static SecretKey generatePreMasterSecret(
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   209
            int clientVersion, int serverVersion,
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   210
            byte[] encodedSecret, SecureRandom generator) {
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   211
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   212
        if (debug != null && Debug.isOn("handshake")) {
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   213
            System.out.println("Generating a premaster secret");
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   214
        }
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   215
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   216
        try {
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   217
            String s = ((clientVersion >= ProtocolVersion.TLS12.v) ?
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   218
                "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret");
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   219
            KeyGenerator kg = JsseJce.getKeyGenerator(s);
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   220
            kg.init(new TlsRsaPremasterSecretParameterSpec(
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   221
                    clientVersion, serverVersion, encodedSecret),
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   222
                    generator);
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   223
            return kg.generateKey();
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   224
        } catch (InvalidAlgorithmParameterException |
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   225
                NoSuchAlgorithmException iae) {
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   226
            // unlikely to happen, otherwise, must be a provider exception
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   227
            if (debug != null && Debug.isOn("handshake")) {
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   228
                System.out.println("RSA premaster secret generation error:");
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   229
                iae.printStackTrace(System.out);
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   230
            }
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   231
            throw new RuntimeException("Could not generate premaster secret", iae);
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   232
        }
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   233
    }
5afd2941dfb7 8081297: SSL Problem with Tomcat
igerasim
parents: 30904
diff changeset
   234
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   235
    @Override
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   236
    int messageType() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   237
        return ht_client_key_exchange;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   238
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   239
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   240
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    int messageLength() {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 27804
diff changeset
   242
        if (protocolVersion.useTLS10PlusSpec()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return encrypted.length + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return encrypted.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   249
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    void send(HandshakeOutStream s) throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 27804
diff changeset
   251
        if (protocolVersion.useTLS10PlusSpec()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            s.putBytes16(encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            s.write(encrypted);
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    void print(PrintStream s) throws IOException {
41961
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   260
        String version = "version not available/extractable";
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   261
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   262
        byte[] ba = preMaster.getEncoded();
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   263
        if (ba != null && ba.length >= 2) {
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   264
            version = ProtocolVersion.valueOf(ba[0], ba[1]).name;
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   265
        }
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   266
12edcce2cb80 8169229: RSAClientKeyExchange debug info is incorrect
wetmore
parents: 38741
diff changeset
   267
        s.println("*** ClientKeyExchange, RSA PreMasterSecret, " + version);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
}