jdk/src/java.base/share/classes/sun/security/ssl/DHCrypt.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25807 jdk/src/share/classes/sun/security/ssl/DHCrypt.java@6c325960c9ee
child 31712 e4d5230193da
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25807
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 16080
diff changeset
     2
 * Copyright (c) 1996, 2014, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    31
import java.io.IOException;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    32
import javax.net.ssl.SSLHandshakeException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.KeyAgreement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.interfaces.DHPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    38
import sun.security.util.KeyUtil;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class implements the Diffie-Hellman key exchange algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * D-H means combining your private key with your partners public key to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * generate a number. The peer does the same with its private key and our
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * public key. Through the magic of Diffie-Hellman we both come up with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * same number. This number is secret (discounting MITM attacks) and hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * called the shared secret. It has the same length as the modulus, e.g. 512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * or 1024 bit. Man-in-the-middle attacks are typically countered by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * independent authentication step using certificates (RSA, DSA, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The thing to note is that the shared secret is constant for two partners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * with constant private keys. This is often not what we want, which is why
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * it is generally a good idea to create a new private key for each session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Generating a private key involves one modular exponentiation assuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * suitable D-H parameters are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * General usage of this class (TLS DHE case):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  . if we are server, call DHCrypt(keyLength,random). This generates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *    an ephemeral keypair of the request length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  . if we are client, call DHCrypt(modulus, base, random). This
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    60
 *    generates an ephemeral keypair using the parameters specified by
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    61
 *    the server.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *  . send parameters and public value to remote peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *  . receive peers ephemeral public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  . call getAgreedSecret() to calculate the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * In TLS the server chooses the parameter values itself, the client must use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * those sent to it by the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The use of ephemeral keys as described above also achieves what is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * "forward secrecy". This means that even if the authentication keys are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * broken at a later date, the shared secret remains secure. The session is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * compromised only if the authentication keys are already broken at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * time the key exchange takes place and an active MITM attack is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * This is in contrast to straightforward encrypting RSA key exchanges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
final class DHCrypt {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // group parameters (prime modulus and generator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private BigInteger modulus;                 // P (aka N)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private BigInteger base;                    // G (aka alpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // our private key (including private component x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // public component of our key, X = (g ^ x) mod p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private BigInteger publicValue;             // X (aka y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    90
    // the times to recove from failure if public key validation
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    91
    private static int MAX_FAILOVER_TIMES = 2;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    92
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Generate a Diffie-Hellman keypair of the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    DHCrypt(int keyLength, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            kpg.initialize(keyLength, random);
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   100
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   101
            DHPublicKeySpec spec = generateDHPublicKeySpec(kpg);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   102
            if (spec == null) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   103
                throw new RuntimeException("Could not generate DH keypair");
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   104
            }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            publicValue = spec.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            modulus = spec.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            base = spec.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new RuntimeException("Could not generate DH keypair", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Generate a Diffie-Hellman keypair using the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param modulus the Diffie-Hellman modulus P
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param base the Diffie-Hellman base G
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    DHCrypt(BigInteger modulus, BigInteger base, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.modulus = modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.base = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            DHParameterSpec params = new DHParameterSpec(modulus, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            kpg.initialize(params, random);
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   128
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   129
            DHPublicKeySpec spec = generateDHPublicKeySpec(kpg);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   130
            if (spec == null) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   131
                throw new RuntimeException("Could not generate DH keypair");
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   132
            }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   133
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            publicValue = spec.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new RuntimeException("Could not generate DH keypair", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   140
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (key instanceof DHPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            DHPublicKey dhKey = (DHPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            DHParameterSpec params = dhKey.getParams();
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   145
            return new DHPublicKeySpec(dhKey.getY(),
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   146
                                    params.getP(), params.getG());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            KeyFactory factory = JsseJce.getKeyFactory("DH");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   150
            return factory.getKeySpec(key, DHPublicKeySpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /** Returns the Diffie-Hellman modulus. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /** Returns the Diffie-Hellman base (generator).  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    BigInteger getBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Gets the public key of this end of the key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    BigInteger getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return publicValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Get the secret data that has been agreed on through Diffie-Hellman
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * key agreement protocol.  Note that in the two party protocol, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * the peer keys are already known, no other data needs to be sent in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * order to agree on a secret.  That is, a secured message may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * sent without any mandatory round-trip overheads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <P>It is illegal to call this member function if the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * has not been set (or generated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   184
     * @param  peerPublicKey the peer's public key.
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   185
     * @param  keyIsValidated whether the {@code peerPublicKey} has beed
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   186
     *         validated
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   187
     * @return the secret, which is an unsigned big-endian integer
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   188
     *         the same size as the Diffie-Hellman modulus.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   190
    SecretKey getAgreedSecret(BigInteger peerPublicValue,
25807
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 16080
diff changeset
   191
            boolean keyIsValidated) throws SSLHandshakeException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            DHPublicKeySpec spec =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        new DHPublicKeySpec(peerPublicValue, modulus, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            PublicKey publicKey = kf.generatePublic(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   198
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   199
            // validate the Diffie-Hellman public key
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   200
            if (!keyIsValidated &&
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   201
                    !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   202
                try {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   203
                    KeyUtil.validate(spec);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   204
                } catch (InvalidKeyException ike) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   205
                    // prefer handshake_failure alert to internal_error alert
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   206
                    throw new SSLHandshakeException(ike.getMessage());
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   207
                }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   208
            }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            ka.init(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            ka.doPhase(publicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return ka.generateSecret("TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } catch (GeneralSecurityException e) {
25807
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 16080
diff changeset
   214
            throw (SSLHandshakeException) new SSLHandshakeException(
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 16080
diff changeset
   215
                "Could not generate secret").initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   219
    // Generate and validate DHPublicKeySpec
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   220
    private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg)
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   221
            throws GeneralSecurityException {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   222
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   223
        boolean doExtraValiadtion =
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   224
                    (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName()));
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   225
        for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   226
            KeyPair kp = kpg.generateKeyPair();
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   227
            privateKey = kp.getPrivate();
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   228
            DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   229
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   230
            // validate the Diffie-Hellman public key
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   231
            if (doExtraValiadtion) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   232
                try {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   233
                    KeyUtil.validate(spec);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   234
                } catch (InvalidKeyException ivke) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   235
                    if (i == MAX_FAILOVER_TIMES) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   236
                        throw ivke;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   237
                    }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   238
                    // otherwise, ignore the exception and try the next one
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   239
                    continue;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   240
                }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   241
            }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   242
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   243
            return spec;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   244
        }
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   245
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   246
        return null;
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
   247
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
}