jdk/src/share/classes/sun/security/ssl/ClientHandshaker.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 8791 f5106bbf577d
child 11521 d7698e6c5f51
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
/*
8791
f5106bbf577d 7022855: Export "PKIX" as the standard algorithm name of KeyManagerFactory
xuelei
parents: 8782
diff changeset
     2
 * Copyright (c) 1996, 2011, 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: 5182
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: 5182
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: 5182
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.interfaces.ECPublicKey;
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
    34
import java.security.interfaces.RSAPublicKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.ECParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.ssl.HandshakeMessage.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import static sun.security.ssl.CipherSuite.KeyExchange.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    51
import sun.net.util.IPAddressUtil;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * ClientHandshaker does the protocol handshaking from the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * of view of a client.  It is driven asychronously by handshake messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * as delivered by the parent Handshaker class, and also uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * common functionality (e.g. key generation) that is provided there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
final class ClientHandshaker extends Handshaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // the server's public key from its certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private PublicKey serverKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // the server's ephemeral public key from the server key exchange message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // for ECDHE/ECDH_anon and RSA_EXPORT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private PublicKey ephemeralServerKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // server's ephemeral public value for DHE/DH_anon key exchanges
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private BigInteger          serverDH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private DHCrypt             dh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private ECDHCrypt ecdh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private CertificateRequest  certRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean serverKeyExchangeReceived;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The RSA PreMasterSecret needs to know the version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * ClientHello that was used on this handshake.  This represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the "max version" this client is supporting.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * case of an initial handshake, it's the max version enabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * but in the case of a resumption attempt, it's the version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * of the session we're trying to resume.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private ProtocolVersion maxProtocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    91
    // To switch off the SNI extension.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    92
    private final static boolean enableSNIExtension =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    93
            Debug.getBooleanProperty("jsse.enableSNIExtension", true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    ClientHandshaker(SSLSocketImpl socket, SSLContextImpl context,
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
    99
            ProtocolList enabledProtocols,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   100
            ProtocolVersion activeProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   101
            boolean isInitialHandshake, boolean secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   102
            byte[] clientVerifyData, byte[] serverVerifyData) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   103
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   104
        super(socket, context, enabledProtocols, true, true,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   105
            activeProtocolVersion, isInitialHandshake, secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   106
            clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    ClientHandshaker(SSLEngineImpl engine, SSLContextImpl context,
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   110
            ProtocolList enabledProtocols,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   111
            ProtocolVersion activeProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   112
            boolean isInitialHandshake, boolean secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   113
            byte[] clientVerifyData, byte[] serverVerifyData) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   114
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   115
        super(engine, context, enabledProtocols, true, true,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   116
            activeProtocolVersion, isInitialHandshake, secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   117
            clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * This routine handles all the client side handshake messages, one at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * a time.  Given the message type (and in some cases the pending cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * spec) it parses the type-specific message.  Then it calls a function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * that handles that specific message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * It updates the state machine (need to verify it) as each message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * is processed, and writes responses as needed using the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    void processMessage(byte type, int messageLen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (state > type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                && (type != HandshakeMessage.ht_hello_request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    && state != HandshakeMessage.ht_client_hello)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    "Handshake message sequence violation, " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        case HandshakeMessage.ht_hello_request:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            this.serverHelloRequest(new HelloRequest(input));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        case HandshakeMessage.ht_server_hello:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            this.serverHello(new ServerHello(input, messageLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        case HandshakeMessage.ht_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (keyExchange == K_DH_ANON || keyExchange == K_ECDH_ANON
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    || keyExchange == K_KRB5 || keyExchange == K_KRB5_EXPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                fatalSE(Alerts.alert_unexpected_message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    "unexpected server cert chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            this.serverCertificate(new CertificateMsg(input));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            serverKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                session.getPeerCertificates()[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        case HandshakeMessage.ht_server_key_exchange:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            serverKeyExchangeReceived = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            case K_RSA_EXPORT:
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   163
                /**
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   164
                 * The server key exchange message is sent by the server only
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   165
                 * when the server certificate message does not contain the
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   166
                 * proper amount of data to allow the client to exchange a
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   167
                 * premaster secret, such as when RSA_EXPORT is used and the
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   168
                 * public key in the server certificate is longer than 512 bits.
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   169
                 */
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   170
                if (serverKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   171
                    throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   172
                        ("Server did not send certificate message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   173
                }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   174
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   175
                if (!(serverKey instanceof RSAPublicKey)) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   176
                    throw new SSLProtocolException("Protocol violation:" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   177
                        " the certificate type must be appropriate for the" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   178
                        " selected cipher suite's key exchange algorithm");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   179
                }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   180
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   181
                if (JsseJce.getRSAKeyLength(serverKey) <= 512) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   182
                    throw new SSLProtocolException("Protocol violation:" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   183
                        " server sent a server key exchange message for" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   184
                        " key exchange " + keyExchange +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   185
                        " when the public key in the server certificate" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   186
                        " is less than or equal to 512 bits in length");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   187
                }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    this.serverKeyExchange(new RSA_ServerKeyExchange(input));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    throwSSLException("Server key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            case K_DH_ANON:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   196
                this.serverKeyExchange(new DH_ServerKeyExchange(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   197
                                                input, protocolVersion));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    this.serverKeyExchange(new DH_ServerKeyExchange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        input, serverKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        clnt_random.random_bytes, svr_random.random_bytes,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   205
                        messageLen,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   206
                        localSupportedSignAlgs, protocolVersion));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    throwSSLException("Server key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    this.serverKeyExchange(new ECDH_ServerKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        (input, serverKey, clnt_random.random_bytes,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   217
                        svr_random.random_bytes,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   218
                        localSupportedSignAlgs, protocolVersion));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    throwSSLException("Server key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                break;
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   223
            case K_RSA:
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   224
            case K_DH_RSA:
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   225
            case K_DH_DSS:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            case K_ECDH_RSA:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   228
                throw new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   229
                    "Protocol violation: server sent a server key exchange"
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   230
                    + "message for key exchange " + keyExchange);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    "unexpected receipt of server key exchange algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    "unsupported key exchange algorithm = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    + keyExchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        case HandshakeMessage.ht_certificate_request:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // save for later, it's handled by serverHelloDone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if ((keyExchange == K_DH_ANON) || (keyExchange == K_ECDH_ANON)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throw new SSLHandshakeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    "Client authentication requested for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    "anonymous cipher suite.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } else if (keyExchange == K_KRB5 || keyExchange == K_KRB5_EXPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw new SSLHandshakeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    "Client certificate requested for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    "kerberos cipher suite.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   253
            certRequest = new CertificateRequest(input, protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                certRequest.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   257
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   258
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   259
                Collection<SignatureAndHashAlgorithm> peerSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   260
                                        certRequest.getSignAlgorithms();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   261
                if (peerSignAlgs == null || peerSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   262
                    throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   263
                        "No peer supported signature algorithms");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   264
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   265
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   266
                Collection<SignatureAndHashAlgorithm> supportedPeerSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   267
                    SignatureAndHashAlgorithm.getSupportedAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   268
                                                            peerSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   269
                if (supportedPeerSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   270
                    throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   271
                        "No supported signature and hash algorithm in common");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   272
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   273
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   274
                setPeerSupportedSignAlgs(supportedPeerSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   275
                session.setPeerSupportedSignatureAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   276
                                                supportedPeerSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   277
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   278
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        case HandshakeMessage.ht_server_hello_done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            this.serverHelloDone(new ServerHelloDone(input));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        case HandshakeMessage.ht_finished:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   286
            this.serverFinished(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   287
                new Finished(protocolVersion, input, cipherSuite));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                "Illegal client handshake msg, " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Move state machine forward if the message handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // code didn't already do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (state < type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            state = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Used by the server to kickstart negotiations -- this requests a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * "client hello" to renegotiate current cipher specs (e.g. maybe lots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * of data has been encrypted with the same keys, or the server needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * the client to present a certificate).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private void serverHelloRequest(HelloRequest mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Could be (e.g. at connection setup) that we already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // sent the "client hello" but the server's not seen it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (state < HandshakeMessage.ht_client_hello) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   320
            if (!secureRenegotiation && !allowUnsafeRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   321
                // renegotiation is not allowed.
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   322
                if (activeProtocolVersion.v >= ProtocolVersion.TLS10.v) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   323
                    // response with a no_renegotiation warning,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   324
                    warningSE(Alerts.alert_no_renegotiation);
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   325
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   326
                    // invalidate the handshake so that the caller can
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   327
                    // dispose this object.
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   328
                    invalidated = true;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   329
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   330
                    // If there is still unread block in the handshake
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   331
                    // input stream, it would be truncated with the disposal
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   332
                    // and the next handshake message will become incomplete.
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   333
                    //
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   334
                    // However, according to SSL/TLS specifications, no more
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   335
                    // handshake message should immediately follow ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   336
                    // or HelloRequest. So just let it be.
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   337
                } else {
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   338
                    // For SSLv3, send the handshake_failure fatal error.
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   339
                    // Note that SSLv3 does not define a no_renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   340
                    // alert like TLSv1. However we cannot ignore the message
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   341
                    // simply, otherwise the other side was waiting for a
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   342
                    // response that would never come.
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   343
                    fatalSE(Alerts.alert_handshake_failure,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   344
                        "Renegotiation is not allowed");
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   345
                }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   346
            } else {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   347
                if (!secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   348
                    if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   349
                        System.out.println(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   350
                            "Warning: continue with insecure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   351
                    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   352
                }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   353
                kickstart();
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   354
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Server chooses session parameters given options created by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * client -- basically, cipher options, session id, and someday a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * set of compression options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * There are two branches of the state machine, decided by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * details of this message.  One is the "fast" handshake, where we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * can resume the pre-existing session we asked resume.  The other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * is a more expensive "full" handshake, with key exchange and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * probably authentication getting done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private void serverHello(ServerHello mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        serverKeyExchangeReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // check if the server selected protocol version is OK for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        ProtocolVersion mesgVersion = mesg.protocolVersion;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   378
        if (!isNegotiable(mesgVersion)) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   379
            throw new SSLHandshakeException(
8782
1ff0b643b793 7009794: misleading text in SSLHandshakeException exception message
xuelei
parents: 7990
diff changeset
   380
                "Server chose " + mesgVersion +
8791
f5106bbf577d 7022855: Export "PKIX" as the standard algorithm name of KeyManagerFactory
xuelei
parents: 8782
diff changeset
   381
                ", but that protocol version is not enabled or not supported " +
f5106bbf577d 7022855: Export "PKIX" as the standard algorithm name of KeyManagerFactory
xuelei
parents: 8782
diff changeset
   382
                "by the client.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
7804
c59149ba3780 6996367: improve HandshakeHash
weijun
parents: 7043
diff changeset
   385
        handshakeHash.protocolDetermined(mesgVersion);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   386
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Set protocolVersion and propagate to SSLSocket and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // Handshake streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        setVersion(mesgVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   391
        // check the "renegotiation_info" extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   392
        RenegotiationInfoExtension serverHelloRI = (RenegotiationInfoExtension)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   393
                    mesg.extensions.get(ExtensionType.EXT_RENEGOTIATION_INFO);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   394
        if (serverHelloRI != null) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   395
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   396
                // verify the length of the "renegotiated_connection" field
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   397
                if (!serverHelloRI.isEmpty()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   398
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   399
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   400
                        "The renegotiation_info field is not empty");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   401
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   402
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   403
                secureRenegotiation = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   404
            } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   405
                // For a legacy renegotiation, the client MUST verify that
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   406
                // it does not contain the "renegotiation_info" extension.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   407
                if (!secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   408
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   409
                        "Unexpected renegotiation indication extension");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   410
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   411
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   412
                // verify the client_verify_data and server_verify_data values
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   413
                byte[] verifyData =
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   414
                    new byte[clientVerifyData.length + serverVerifyData.length];
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   415
                System.arraycopy(clientVerifyData, 0, verifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   416
                        0, clientVerifyData.length);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   417
                System.arraycopy(serverVerifyData, 0, verifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   418
                        clientVerifyData.length, serverVerifyData.length);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   419
                if (!Arrays.equals(verifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   420
                                serverHelloRI.getRenegotiatedConnection())) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   421
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   422
                        "Incorrect verify data in ServerHello " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   423
                        "renegotiation_info message");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   424
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   425
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   426
        } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   427
            // no renegotiation indication extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   428
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   429
                if (!allowLegacyHelloMessages) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   430
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   431
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   432
                        "Failed to negotiate the use of secure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   433
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   434
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   435
                secureRenegotiation = false;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   436
                if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   437
                    System.out.println("Warning: No renegotiation " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   438
                                    "indication extension in ServerHello");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   439
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   440
            } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   441
                // For a secure renegotiation, the client must abort the
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   442
                // handshake if no "renegotiation_info" extension is present.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   443
                if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   444
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   445
                        "No renegotiation indication extension");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   446
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   447
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   448
                // we have already allowed unsafe renegotation before request
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   449
                // the renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   450
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   451
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   452
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // Save server nonce, we always use it to compute connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // keys and it's also used to create the master secret if we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // creating a new session (i.e. in the full handshake).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        svr_random = mesg.svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   460
        if (isNegotiable(mesg.cipherSuite) == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            fatalSE(Alerts.alert_illegal_parameter,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   462
                "Server selected improper ciphersuite " + mesg.cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   464
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        setCipherSuite(mesg.cipherSuite);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   466
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   467
            handshakeHash.setFinishedAlg(cipherSuite.prfAlg.getPRFHashAlg());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   468
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (mesg.compression_method != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                "compression type not supported, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                + mesg.compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // so far so good, let's look at the session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            // we tried to resume, let's see what the server decided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (session.getSessionId().equals(mesg.sessionId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // server resumed the session, let's make sure everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                // checks out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                // Verify that the session ciphers are unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                CipherSuite sessionSuite = session.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                if (cipherSuite != sessionSuite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        ("Server returned wrong cipher suite for session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                // verify protocol version match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                ProtocolVersion sessionVersion = session.getProtocolVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                if (protocolVersion != sessionVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        ("Server resumed session with wrong protocol version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                // validate subject identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (sessionSuite.keyExchange == K_KRB5 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    sessionSuite.keyExchange == K_KRB5_EXPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    Principal localPrincipal = session.getLocalPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    Subject subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        subject = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            new PrivilegedExceptionAction<Subject>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            public Subject run() throws Exception {
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   508
                                return Krb5Helper.getClientSubject(getAccSE());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                            System.out.println("Attempt to obtain" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                        " subject failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    if (subject != null) {
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   519
                        // Eliminate dependency on KerberosPrincipal
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   520
                        Set<Principal> principals =
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   521
                            subject.getPrincipals(Principal.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        if (!principals.contains(localPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            throw new SSLProtocolException("Server resumed" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                " session with wrong subject identity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            if (debug != null && Debug.isOn("session"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                System.out.println("Subject identity is same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        if (debug != null && Debug.isOn("session"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            System.out.println("Kerberos credentials are not" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                " present in the current Subject; check if " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                " javax.security.auth.useSubjectAsCreds" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                " system property has been set to false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            ("Server resumed session with no subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                // looks fine; resume it, and update the state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                resumingSession = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                state = HandshakeMessage.ht_finished - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                calculateConnectionKeys(session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    System.out.println("%% Server resumed " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                // we wanted to resume, but the server refused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    throw new SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        ("New session creation is disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   557
        if (resumingSession && session != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   558
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   559
                handshakeHash.setCertificateVerifyAlg(null);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   560
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   561
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   562
            setHandshakeSessionSE(session);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   563
            return;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   564
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   565
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        // check extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        for (HelloExtension ext : mesg.extensions.list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            ExtensionType type = ext.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if ((type != ExtensionType.EXT_ELLIPTIC_CURVES)
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   570
                    && (type != ExtensionType.EXT_EC_POINT_FORMATS)
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   571
                    && (type != ExtensionType.EXT_SERVER_NAME)
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   572
                    && (type != ExtensionType.EXT_RENEGOTIATION_INFO)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                fatalSE(Alerts.alert_unsupported_extension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    "Server sent an unsupported extension: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // Create a new session, we need to do the full handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        session = new SSLSessionImpl(protocolVersion, cipherSuite,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   580
                            getLocalSupportedSignAlgs(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            mesg.sessionId, getHostSE(), getPortSE());
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   582
        setHandshakeSessionSE(session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            System.out.println("** " + cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Server's own key was either a signing-only key, or was too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * large for export rules ... this message holds an ephemeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * RSA key to use for key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private void serverKeyExchange(RSA_ServerKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            throws IOException, GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (!mesg.verify(serverKey, clnt_random, svr_random)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                "server key exchange invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        ephemeralServerKey = mesg.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Diffie-Hellman key exchange.  We save the server public key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * our own D-H algorithm object so we can defer key calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * until after we've sent the client key exchange message (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * gives client and server some useful parallelism).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private void serverKeyExchange(DH_ServerKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   618
        dh = new DHCrypt(mesg.getModulus(), mesg.getBase(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   619
                                            sslContext.getSecureRandom());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        serverDH = mesg.getServerPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   623
    private void serverKeyExchange(ECDH_ServerKeyExchange mesg)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   624
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        ECPublicKey key = mesg.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        ecdh = new ECDHCrypt(key.getParams(), sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        ephemeralServerKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * The server's "Hello Done" message is the client's sign that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * it's time to do all the hard work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private void serverHelloDone(ServerHelloDone mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * Always make sure the input has been digested before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * start emitting data, to ensure the hashes are correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * computed for the Finished and CertificateVerify messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         * which we send (here).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * FIRST ... if requested, send an appropriate Certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * to authenticate the client, and remember the associated private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * key to sign the CertificateVerify message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        PrivateKey signingKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (certRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            X509ExtendedKeyManager km = sslContext.getX509KeyManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7804
diff changeset
   659
            ArrayList<String> keytypesTmp = new ArrayList<>(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            for (int i = 0; i < certRequest.types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                switch (certRequest.types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                case CertificateRequest.cct_rsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    typeName = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                case CertificateRequest.cct_dss_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    typeName = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                case CertificateRequest.cct_ecdsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    // ignore if we do not have EC crypto available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    typeName = JsseJce.isEcAvailable() ? "EC" : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                // Fixed DH/ECDH client authentication not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                case CertificateRequest.cct_rsa_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                case CertificateRequest.cct_dss_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                case CertificateRequest.cct_rsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                case CertificateRequest.cct_ecdsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                // Any other values (currently not used in TLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                case CertificateRequest.cct_rsa_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                case CertificateRequest.cct_dss_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    typeName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                if ((typeName != null) && (!keytypesTmp.contains(typeName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    keytypesTmp.add(typeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            int keytypesTmpSize = keytypesTmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (keytypesTmpSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                String keytypes[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        keytypesTmp.toArray(new String[keytypesTmpSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    alias = km.chooseClientAlias(keytypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        certRequest.getAuthorities(), conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    alias = km.chooseEngineClientAlias(keytypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        certRequest.getAuthorities(), engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            CertificateMsg m1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                X509Certificate[] certs = km.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                if ((certs != null) && (certs.length != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    PublicKey publicKey = certs[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    // for EC, make sure we use a supported named curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    if (publicKey instanceof ECPublicKey) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   718
                        ECParameterSpec params =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   719
                            ((ECPublicKey)publicKey).getParams();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   720
                        int index =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   721
                            SupportedEllipticCurvesExtension.getCurveIndex(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   722
                                params);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   723
                        if (!SupportedEllipticCurvesExtension.isSupported(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   724
                                index)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    if (publicKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        m1 = new CertificateMsg(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        signingKey = km.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        session.setLocalPrivateKey(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        session.setLocalCertificates(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (m1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                // No appropriate cert was found ... report this to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                // server.  For SSLv3, send the no_certificate alert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                // TLS uses an empty cert chain instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    m1 = new CertificateMsg(new X509Certificate [0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    warningSE(Alerts.alert_no_certificate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            // At last ... send any client certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            if (m1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    m1.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                m1.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * SECOND ... send the client key exchange message.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * procedure used is a function of the cipher suite selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         * one is always needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        HandshakeMessage m2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        case K_RSA_EXPORT:
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   771
            if (serverKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   772
                throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   773
                        ("Server did not send certificate message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   774
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   775
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   776
            if (!(serverKey instanceof RSAPublicKey)) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   777
                throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   778
                        ("Server certificate does not include an RSA key");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   779
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   780
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
             * For RSA key exchange, we randomly generate a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
             * pre-master secret and encrypt it with the server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
             * public key.  Then we save that pre-master secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
             * so that we can calculate the keying data later;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
             * it's a performance speedup not to do that until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
             * the client's waiting for the server response, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
             * more of a speedup for the D-H case.
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   789
             *
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   790
             * If the RSA_EXPORT scheme is active, when the public
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   791
             * key in the server certificate is less than or equal
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   792
             * to 512 bits in length, use the cert's public key,
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   793
             * otherwise, the ephemeral one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
             */
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   795
            PublicKey key;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   796
            if (keyExchange == K_RSA) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   797
                key = serverKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   798
            } else {    // K_RSA_EXPORT
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   799
                if (JsseJce.getRSAKeyLength(serverKey) <= 512) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   800
                    // extraneous ephemeralServerKey check done
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   801
                    // above in processMessage()
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   802
                    key = serverKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   803
                } else {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   804
                    if (ephemeralServerKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   805
                        throw new SSLProtocolException("Server did not send" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   806
                            " a RSA_EXPORT Server Key Exchange message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   807
                    }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   808
                    key = ephemeralServerKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   809
                }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   810
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   811
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            m2 = new RSAClientKeyExchange(protocolVersion, maxProtocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                                sslContext.getSecureRandom(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        case K_DH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        case K_DH_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
             * For DH Key exchange, we only need to make sure the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
             * knows our public key, so we calculate the same pre-master
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
             * secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
             * For certs that had DH keys in them, we send an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
             * handshake message (no key) ... we flag this case by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
             * passing a null "dhPublic" value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
             * Otherwise we send ephemeral DH keys, unsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            // if (useDH_RSA || useDH_DSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            m2 = new DHClientKeyExchange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            if (dh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    ("Server did not send a DH Server Key Exchange message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            m2 = new DHClientKeyExchange(dh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (ecdh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    ("Server did not send a ECDH Server Key Exchange message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            m2 = new ECDHClientKeyExchange(ecdh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (serverKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        ("Server did not send certificate message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if (serverKey instanceof ECPublicKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        ("Server certificate does not include an EC key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            ECParameterSpec params = ((ECPublicKey)serverKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            ecdh = new ECDHCrypt(params, sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            m2 = new ECDHClientKeyExchange(ecdh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            String hostname = getHostSE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            if (hostname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                throw new IOException("Hostname is required" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                " to use Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   870
            KerberosClientKeyExchange kerberosMsg =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   871
                new KerberosClientKeyExchange(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   872
                    hostname, isLoopbackSE(), getAccSE(), protocolVersion,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            // Record the principals involved in exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            session.setPeerPrincipal(kerberosMsg.getPeerPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            session.setLocalPrincipal(kerberosMsg.getLocalPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            m2 = kerberosMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            // somethings very wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                ("Unsupported key exchange: " + keyExchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            m2.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        m2.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * THIRD, send a "change_cipher_spec" record followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * "Finished" message.  We flush the messages we've queued up, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * get concurrency between client and server.  The concurrency is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * useful as we calculate the master secret, which is needed both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         * to compute the "Finished" message, and to compute the keys used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
         * to protect all records following the change_cipher_spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        output.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         * We deferred calculating the master secret and this connection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         * keying data; we do it now.  Deferring this calculation is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * from a performance point of view, since it lets us do it during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         * some time that network delays and the server's own calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
         * would otherwise cause to be "dead" in the critical path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        SecretKey preMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        case K_RSA_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            preMasterSecret = ((RSAClientKeyExchange)m2).preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            byte[] secretBytes =
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   918
                ((KerberosClientKeyExchange)m2).getUnencryptedPreMasterSecret();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   919
            preMasterSecret = new SecretKeySpec(secretBytes,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   920
                "TlsPremasterSecret");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            preMasterSecret = dh.getAgreedSecret(serverDH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            preMasterSecret = ecdh.getAgreedSecret(ephemeralServerKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            preMasterSecret = ecdh.getAgreedSecret(serverKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        default:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   937
            throw new IOException("Internal error: unknown key exchange "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   938
                + keyExchange);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        calculateKeys(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * FOURTH, if we sent a Certificate, we need to send a signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         * CertificateVerify (unless the key in the client's certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         * was a Diffie-Hellman key).).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         * This uses a hash of the previous handshake messages ... either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         * a nonfinal one (if the particular implementation supports it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
         * or else using the third element in the arrays of hashes being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        if (signingKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            CertificateVerify m3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   956
                SignatureAndHashAlgorithm preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   957
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   958
                    preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   959
                        SignatureAndHashAlgorithm.getPreferableAlgorithm(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   960
                            peerSupportedSignAlgs, signingKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   961
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   962
                    if (preferableSignatureAlgorithm == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   963
                        throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   964
                            "No supported signature algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   965
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   966
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   967
                    String hashAlg =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   968
                        SignatureAndHashAlgorithm.getHashAlgorithmName(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   969
                                preferableSignatureAlgorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   970
                    if (hashAlg == null || hashAlg.length() == 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   971
                        throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   972
                                "No supported hash algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   973
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   974
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   975
                    handshakeHash.setCertificateVerifyAlg(hashAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   976
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   977
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                m3 = new CertificateVerify(protocolVersion, handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    signingKey, session.getMasterSecret(),
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   980
                    sslContext.getSecureRandom(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   981
                    preferableSignatureAlgorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    "Error signing certificate verify", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                // NOTREACHED, make compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                m3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                m3.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            m3.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            output.doHashes();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   993
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   994
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   995
                handshakeHash.setCertificateVerifyAlg(null);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   996
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * OK, that's that!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        sendChangeCipherAndFinish(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * "Finished" is the last handshake message sent.  If we got this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * far, the MAC has been validated post-decryption.  We validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * the two hashes here as an additional sanity check, protecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * the handshake against various active attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    private void serverFinished(Finished mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1017
        boolean verified = mesg.verify(handshakeHash, Finished.SERVER,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1018
            session.getMasterSecret());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (!verified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                       "server 'finished' message doesn't verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1027
         * save server verify data for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1028
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1029
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1030
            serverVerifyData = mesg.getVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1031
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1032
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1033
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         * OK, it verified.  If we're doing the fast handshake, add that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
         * "Finished" message to the hash of handshake messages, then send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * our own change_cipher_spec and Finished message for the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * to verify in turn.  These are the last handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         * In any case, update the session cache.  We're done handshaking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * so there are no threats any more associated with partially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * completed handshakes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            sendChangeCipherAndFinish(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        session.setLastAccessedTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (!resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if (session.isRejoinable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                ((SSLSessionContextImpl) sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                        .engineGetClientSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                        .put(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    System.out.println("%% Cached client session: " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            } else if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    "%% Didn't cache non-resumable client session: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Send my change-cipher-spec and Finished message ... done as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * last handshake act in either the short or long sequences.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * the short one, we've already seen the server's Finished; in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * long one, we wait for it now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    private void sendChangeCipherAndFinish(boolean finishedTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        Finished mesg = new Finished(protocolVersion, handshakeHash,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1075
            Finished.CLIENT, session.getMasterSecret(), cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * Send the change_cipher_spec message, then the Finished message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * which we just calculated (and protected using the keys we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * calculated).  Server responds with its Finished message, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         * in the "fast handshake" (resume session) case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        sendChangeCipherSpec(mesg, finishedTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1086
         * save client verify data for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1087
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1088
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1089
            clientVerifyData = mesg.getVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1090
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1091
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1092
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         * Update state machine so server MUST send 'finished' next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         * (In "long" handshake case; in short case, we're responding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         * to its message.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        state = HandshakeMessage.ht_finished - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * Returns a ClientHello message to kickstart renegotiations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    HandshakeMessage getKickstartMessage() throws SSLException {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1105
        // session ID of the ClientHello message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1106
        SessionId sessionId = SSLSessionImpl.nullSession.getSessionId();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1107
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1108
        // a list of cipher suites sent by the client
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1109
        CipherSuiteList cipherSuites = getActiveCipherSuites();
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1110
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1111
        // set the max protocol version this client is supporting.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        maxProtocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        // Try to resume an existing session.  This might be mandatory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        // given certain API options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        session = ((SSLSessionContextImpl)sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        .engineGetClientSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        .get(getHostSE(), getPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                System.out.println("%% Client cached "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    + session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    + (session.isRejoinable() ? "" : " (not rejoinable)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                System.out.println("%% No cached client session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if ((session != null) && (session.isRejoinable() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            CipherSuite sessionSuite = session.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            ProtocolVersion sessionVersion = session.getProtocolVersion();
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1137
            if (isNegotiable(sessionSuite) == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                if (debug != null && Debug.isOn("session")) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1139
                    System.out.println("%% can't resume, unavailable cipher");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1144
            if ((session != null) && !isNegotiable(sessionVersion)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    System.out.println("%% can't resume, protocol disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    if (Debug.isOn("handshake") || Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                        System.out.println("%% Try resuming " + session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                            + " from port " + getLocalPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1159
                sessionId = session.getSessionId();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                maxProtocolVersion = sessionVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                // Update SSL version number in underlying SSL socket and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                // handshake output stream, so that the output records (at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                // record layer) have the correct version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                setVersion(sessionVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1168
            /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1169
             * Force use of the previous session ciphersuite, and
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1170
             * add the SCSV if enabled.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1171
             */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                if (session == null) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1174
                    throw new SSLHandshakeException(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                        "Can't reuse existing SSL client session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                }
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1177
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7804
diff changeset
  1178
                Collection<CipherSuite> cipherList = new ArrayList<>(2);
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1179
                cipherList.add(sessionSuite);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1180
                if (!secureRenegotiation &&
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1181
                        cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1182
                    cipherList.add(CipherSuite.C_SCSV);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1183
                }   // otherwise, renegotiation_info extension will be used
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1184
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1185
                cipherSuites = new CipherSuiteList(cipherList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1189
        if (session == null && !enableNewSession) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1190
            throw new SSLHandshakeException("No existing session to resume");
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1191
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1192
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1193
        // exclude SCSV for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1194
        if (secureRenegotiation && cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1195
            Collection<CipherSuite> cipherList =
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7804
diff changeset
  1196
                        new ArrayList<>(cipherSuites.size() - 1);
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1197
            for (CipherSuite suite : cipherSuites.collection()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1198
                if (suite != CipherSuite.C_SCSV) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1199
                    cipherList.add(suite);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1200
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1201
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1202
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1203
            cipherSuites = new CipherSuiteList(cipherList);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1204
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1206
        // make sure there is a negotiable cipher suite.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1207
        boolean negotiable = false;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1208
        for (CipherSuite suite : cipherSuites.collection()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1209
            if (isNegotiable(suite)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1210
                negotiable = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1211
                break;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1212
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1213
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1214
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1215
        if (!negotiable) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1216
            throw new SSLHandshakeException("No negotiable cipher suite");
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1217
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1218
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1219
        // Not a TLS1.2+ handshake
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1220
        // For SSLv2Hello, HandshakeHash.reset() will be called, so we
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1221
        // cannot call HandshakeHash.protocolDetermined() here. As it does
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1222
        // not follow the spec that HandshakeHash.reset() can be only be
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1223
        // called before protocolDetermined.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1224
        // if (maxProtocolVersion.v < ProtocolVersion.TLS12.v) {
7804
c59149ba3780 6996367: improve HandshakeHash
weijun
parents: 7043
diff changeset
  1225
        //     handshakeHash.protocolDetermined(maxProtocolVersion);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1226
        // }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1227
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1228
        // create the ClientHello message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1229
        ClientHello clientHelloMessage = new ClientHello(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1230
                sslContext.getSecureRandom(), maxProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1231
                sessionId, cipherSuites);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1232
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1233
        // add signature_algorithm extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1234
        if (maxProtocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1235
            // we will always send the signature_algorithm extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1236
            Collection<SignatureAndHashAlgorithm> localSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1237
                                                getLocalSupportedSignAlgs();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1238
            if (localSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1239
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1240
                            "No supported signature algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1241
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1242
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1243
            clientHelloMessage.addSignatureAlgorithmsExtension(localSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1244
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1245
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1246
        // add server_name extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1247
        if (enableSNIExtension) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1248
            // We cannot use the hostname resolved from name services.  For
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1249
            // virtual hosting, multiple hostnames may be bound to the same IP
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1250
            // address, so the hostname resolved from name services is not
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1251
            // reliable.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1252
            String hostname = getRawHostnameSE();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1253
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1254
            // we only allow FQDN
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1255
            if (hostname != null && hostname.indexOf('.') > 0 &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1256
                    !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1257
                    !IPAddressUtil.isIPv6LiteralAddress(hostname)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1258
                clientHelloMessage.addServerNameIndicationExtension(hostname);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1259
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1260
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1261
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1262
        // reset the client random cookie
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1263
        clnt_random = clientHelloMessage.clnt_random;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1264
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1265
        /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1266
         * need to set the renegotiation_info extension for:
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1267
         * 1: secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1268
         * 2: initial handshake and no SCSV in the ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1269
         * 3: insecure renegotiation and no SCSV in the ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1270
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1271
        if (secureRenegotiation ||
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1272
                !cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1273
            clientHelloMessage.addRenegotiationInfoExtension(clientVerifyData);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1274
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1275
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1276
        return clientHelloMessage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * Fault detected during handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    void handshakeAlert(byte description) throws SSLProtocolException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        String message = Alerts.alertDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            System.out.println("SSL - handshake alert: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        throw new SSLProtocolException("handshake alert:  " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Unless we are using an anonymous ciphersuite, the server always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * sends a certificate message (for the CipherSuites we currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * support). The trust manager verifies the chain for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    private void serverCertificate(CertificateMsg mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        X509Certificate[] peerCerts = mesg.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        if (peerCerts.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            fatalSE(Alerts.alert_bad_certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                "empty certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        // ask the trust manager to verify the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        X509TrustManager tm = sslContext.getX509TrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            // find out the key exchange algorithm used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            // use "RSA" for non-ephemeral "RSA_EXPORT"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            String keyExchangeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (keyExchange == K_RSA_EXPORT && !serverKeyExchangeReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                keyExchangeString = K_RSA.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                keyExchangeString = keyExchange.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            if (tm instanceof X509ExtendedTrustManager) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1318
                if (conn != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1319
                    ((X509ExtendedTrustManager)tm).checkServerTrusted(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1320
                        peerCerts.clone(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                        keyExchangeString,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1322
                        conn);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1323
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1324
                    ((X509ExtendedTrustManager)tm).checkServerTrusted(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1325
                        peerCerts.clone(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1326
                        keyExchangeString,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1327
                        engine);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1328
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1330
                // Unlikely to happen, because we have wrapped the old
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1331
                // X509TrustManager with the new X509ExtendedTrustManager.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1332
                throw new CertificateException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1333
                    "Improper X509TrustManager implementation");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            // This will throw an exception, so include the original error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            fatalSE(Alerts.alert_certificate_unknown, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        session.setPeerCertificates(peerCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
}