jdk/src/share/classes/sun/security/ssl/ClientHandshaker.java
author smarks
Fri, 14 Jan 2011 15:31:45 -0800
changeset 7990 57019dc81b66
parent 7804 c59149ba3780
child 8782 1ff0b643b793
permissions -rw-r--r--
7012003: diamond conversion for ssl Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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(
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   380
                    "Server chose unsupported or disabled protocol: " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   381
                    mesgVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
7804
c59149ba3780 6996367: improve HandshakeHash
weijun
parents: 7043
diff changeset
   384
        handshakeHash.protocolDetermined(mesgVersion);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   385
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // Set protocolVersion and propagate to SSLSocket and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Handshake streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        setVersion(mesgVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   390
        // check the "renegotiation_info" extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   391
        RenegotiationInfoExtension serverHelloRI = (RenegotiationInfoExtension)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   392
                    mesg.extensions.get(ExtensionType.EXT_RENEGOTIATION_INFO);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   393
        if (serverHelloRI != null) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   394
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   395
                // verify the length of the "renegotiated_connection" field
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   396
                if (!serverHelloRI.isEmpty()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   397
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   398
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   399
                        "The renegotiation_info field is not empty");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   400
                }
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
                secureRenegotiation = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   403
            } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   404
                // For a legacy renegotiation, the client MUST verify that
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   405
                // it does not contain the "renegotiation_info" extension.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   406
                if (!secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   407
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   408
                        "Unexpected renegotiation indication extension");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   409
                }
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
                // verify the client_verify_data and server_verify_data values
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   412
                byte[] verifyData =
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   413
                    new byte[clientVerifyData.length + serverVerifyData.length];
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   414
                System.arraycopy(clientVerifyData, 0, verifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   415
                        0, clientVerifyData.length);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   416
                System.arraycopy(serverVerifyData, 0, verifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   417
                        clientVerifyData.length, serverVerifyData.length);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   418
                if (!Arrays.equals(verifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   419
                                serverHelloRI.getRenegotiatedConnection())) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   420
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   421
                        "Incorrect verify data in ServerHello " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   422
                        "renegotiation_info message");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   423
                }
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
        } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   426
            // no renegotiation indication extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   427
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   428
                if (!allowLegacyHelloMessages) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   429
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   430
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   431
                        "Failed to negotiate the use of secure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   432
                }
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
                secureRenegotiation = false;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   435
                if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   436
                    System.out.println("Warning: No renegotiation " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   437
                                    "indication extension in ServerHello");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   438
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   439
            } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   440
                // For a secure renegotiation, the client must abort the
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   441
                // handshake if no "renegotiation_info" extension is present.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   442
                if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   443
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   444
                        "No renegotiation indication extension");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   445
                }
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
                // we have already allowed unsafe renegotation before request
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   448
                // the renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   449
            }
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Save server nonce, we always use it to compute connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // keys and it's also used to create the master secret if we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // creating a new session (i.e. in the full handshake).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        svr_random = mesg.svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   459
        if (isNegotiable(mesg.cipherSuite) == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            fatalSE(Alerts.alert_illegal_parameter,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   461
                "Server selected improper ciphersuite " + mesg.cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   463
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        setCipherSuite(mesg.cipherSuite);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   465
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   466
            handshakeHash.setFinishedAlg(cipherSuite.prfAlg.getPRFHashAlg());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   467
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (mesg.compression_method != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                "compression type not supported, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                + mesg.compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // so far so good, let's look at the session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // we tried to resume, let's see what the server decided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (session.getSessionId().equals(mesg.sessionId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                // server resumed the session, let's make sure everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // checks out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                // Verify that the session ciphers are unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                CipherSuite sessionSuite = session.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (cipherSuite != sessionSuite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        ("Server returned wrong cipher suite for session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                // verify protocol version match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                ProtocolVersion sessionVersion = session.getProtocolVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                if (protocolVersion != sessionVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        ("Server resumed session with wrong protocol version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                // validate subject identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                if (sessionSuite.keyExchange == K_KRB5 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    sessionSuite.keyExchange == K_KRB5_EXPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    Principal localPrincipal = session.getLocalPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    Subject subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        subject = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            new PrivilegedExceptionAction<Subject>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            public Subject run() throws Exception {
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   507
                                return Krb5Helper.getClientSubject(getAccSE());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            System.out.println("Attempt to obtain" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                        " subject failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    if (subject != null) {
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   518
                        // Eliminate dependency on KerberosPrincipal
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   519
                        Set<Principal> principals =
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   520
                            subject.getPrincipals(Principal.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        if (!principals.contains(localPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            throw new SSLProtocolException("Server resumed" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                " session with wrong subject identity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                            if (debug != null && Debug.isOn("session"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                System.out.println("Subject identity is same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        if (debug != null && Debug.isOn("session"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            System.out.println("Kerberos credentials are not" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                " present in the current Subject; check if " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                " javax.security.auth.useSubjectAsCreds" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                " system property has been set to false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                            ("Server resumed session with no subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                // looks fine; resume it, and update the state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                resumingSession = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                state = HandshakeMessage.ht_finished - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                calculateConnectionKeys(session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    System.out.println("%% Server resumed " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                // we wanted to resume, but the server refused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    throw new SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        ("New session creation is disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   556
        if (resumingSession && session != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   557
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   558
                handshakeHash.setCertificateVerifyAlg(null);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   559
            }
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
            setHandshakeSessionSE(session);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   562
            return;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   563
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   564
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // check extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        for (HelloExtension ext : mesg.extensions.list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            ExtensionType type = ext.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if ((type != ExtensionType.EXT_ELLIPTIC_CURVES)
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   569
                    && (type != ExtensionType.EXT_EC_POINT_FORMATS)
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   570
                    && (type != ExtensionType.EXT_SERVER_NAME)
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   571
                    && (type != ExtensionType.EXT_RENEGOTIATION_INFO)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                fatalSE(Alerts.alert_unsupported_extension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    "Server sent an unsupported extension: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // Create a new session, we need to do the full handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        session = new SSLSessionImpl(protocolVersion, cipherSuite,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   579
                            getLocalSupportedSignAlgs(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            mesg.sessionId, getHostSE(), getPortSE());
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   581
        setHandshakeSessionSE(session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            System.out.println("** " + cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
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
     * Server's own key was either a signing-only key, or was too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * large for export rules ... this message holds an ephemeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * RSA key to use for key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    private void serverKeyExchange(RSA_ServerKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            throws IOException, GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (!mesg.verify(serverKey, clnt_random, svr_random)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                "server key exchange invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        ephemeralServerKey = mesg.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
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
     * Diffie-Hellman key exchange.  We save the server public key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * our own D-H algorithm object so we can defer key calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * until after we've sent the client key exchange message (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * gives client and server some useful parallelism).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    private void serverKeyExchange(DH_ServerKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   617
        dh = new DHCrypt(mesg.getModulus(), mesg.getBase(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   618
                                            sslContext.getSecureRandom());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        serverDH = mesg.getServerPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   622
    private void serverKeyExchange(ECDH_ServerKeyExchange mesg)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   623
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        ECPublicKey key = mesg.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        ecdh = new ECDHCrypt(key.getParams(), sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        ephemeralServerKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * The server's "Hello Done" message is the client's sign that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * it's time to do all the hard work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    private void serverHelloDone(ServerHelloDone mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * Always make sure the input has been digested before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * start emitting data, to ensure the hashes are correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * computed for the Finished and CertificateVerify messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * which we send (here).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * FIRST ... if requested, send an appropriate Certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * to authenticate the client, and remember the associated private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * key to sign the CertificateVerify message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        PrivateKey signingKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (certRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            X509ExtendedKeyManager km = sslContext.getX509KeyManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7804
diff changeset
   658
            ArrayList<String> keytypesTmp = new ArrayList<>(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            for (int i = 0; i < certRequest.types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                switch (certRequest.types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                case CertificateRequest.cct_rsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    typeName = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                case CertificateRequest.cct_dss_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    typeName = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                case CertificateRequest.cct_ecdsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    // ignore if we do not have EC crypto available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    typeName = JsseJce.isEcAvailable() ? "EC" : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                // Fixed DH/ECDH client authentication not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                case CertificateRequest.cct_rsa_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                case CertificateRequest.cct_dss_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                case CertificateRequest.cct_rsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                case CertificateRequest.cct_ecdsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                // Any other values (currently not used in TLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                case CertificateRequest.cct_rsa_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                case CertificateRequest.cct_dss_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    typeName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                if ((typeName != null) && (!keytypesTmp.contains(typeName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    keytypesTmp.add(typeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            int keytypesTmpSize = keytypesTmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (keytypesTmpSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                String keytypes[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        keytypesTmp.toArray(new String[keytypesTmpSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    alias = km.chooseClientAlias(keytypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        certRequest.getAuthorities(), conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    alias = km.chooseEngineClientAlias(keytypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        certRequest.getAuthorities(), engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            CertificateMsg m1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                X509Certificate[] certs = km.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                if ((certs != null) && (certs.length != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    PublicKey publicKey = certs[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    // for EC, make sure we use a supported named curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    if (publicKey instanceof ECPublicKey) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   717
                        ECParameterSpec params =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   718
                            ((ECPublicKey)publicKey).getParams();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   719
                        int index =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   720
                            SupportedEllipticCurvesExtension.getCurveIndex(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   721
                                params);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   722
                        if (!SupportedEllipticCurvesExtension.isSupported(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   723
                                index)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    if (publicKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        m1 = new CertificateMsg(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        signingKey = km.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        session.setLocalPrivateKey(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        session.setLocalCertificates(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            if (m1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                // No appropriate cert was found ... report this to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                // server.  For SSLv3, send the no_certificate alert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                // TLS uses an empty cert chain instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    m1 = new CertificateMsg(new X509Certificate [0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    warningSE(Alerts.alert_no_certificate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                }
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
            // At last ... send any client certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if (m1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    m1.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                m1.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
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
         * SECOND ... send the client key exchange message.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * procedure used is a function of the cipher suite selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * one is always needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        HandshakeMessage m2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        case K_RSA_EXPORT:
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   770
            if (serverKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   771
                throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   772
                        ("Server did not send certificate message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   773
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   774
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   775
            if (!(serverKey instanceof RSAPublicKey)) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   776
                throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   777
                        ("Server certificate does not include an RSA key");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   778
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   779
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
             * For RSA key exchange, we randomly generate a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
             * pre-master secret and encrypt it with the server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
             * public key.  Then we save that pre-master secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
             * so that we can calculate the keying data later;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
             * it's a performance speedup not to do that until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
             * the client's waiting for the server response, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
             * more of a speedup for the D-H case.
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   788
             *
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   789
             * If the RSA_EXPORT scheme is active, when the public
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   790
             * key in the server certificate is less than or equal
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   791
             * to 512 bits in length, use the cert's public key,
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   792
             * otherwise, the ephemeral one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
             */
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   794
            PublicKey key;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   795
            if (keyExchange == K_RSA) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   796
                key = serverKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   797
            } else {    // K_RSA_EXPORT
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   798
                if (JsseJce.getRSAKeyLength(serverKey) <= 512) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   799
                    // extraneous ephemeralServerKey check done
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   800
                    // above in processMessage()
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   801
                    key = serverKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   802
                } else {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   803
                    if (ephemeralServerKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   804
                        throw new SSLProtocolException("Server did not send" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   805
                            " a RSA_EXPORT Server Key Exchange message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   806
                    }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   807
                    key = ephemeralServerKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   808
                }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   809
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   810
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            m2 = new RSAClientKeyExchange(protocolVersion, maxProtocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                sslContext.getSecureRandom(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        case K_DH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        case K_DH_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
             * For DH Key exchange, we only need to make sure the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
             * knows our public key, so we calculate the same pre-master
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
             * secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
             * For certs that had DH keys in them, we send an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
             * handshake message (no key) ... we flag this case by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
             * passing a null "dhPublic" value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
             * Otherwise we send ephemeral DH keys, unsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            // if (useDH_RSA || useDH_DSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            m2 = new DHClientKeyExchange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (dh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    ("Server did not send a DH Server Key Exchange message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            m2 = new DHClientKeyExchange(dh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (ecdh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    ("Server did not send a ECDH Server Key Exchange message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            m2 = new ECDHClientKeyExchange(ecdh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (serverKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        ("Server did not send certificate message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (serverKey instanceof ECPublicKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        ("Server certificate does not include an EC key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            ECParameterSpec params = ((ECPublicKey)serverKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            ecdh = new ECDHCrypt(params, sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            m2 = new ECDHClientKeyExchange(ecdh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            String hostname = getHostSE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (hostname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                throw new IOException("Hostname is required" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                " to use Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   869
            KerberosClientKeyExchange kerberosMsg =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   870
                new KerberosClientKeyExchange(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   871
                    hostname, isLoopbackSE(), getAccSE(), protocolVersion,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // Record the principals involved in exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            session.setPeerPrincipal(kerberosMsg.getPeerPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            session.setLocalPrincipal(kerberosMsg.getLocalPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            m2 = kerberosMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            // somethings very wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                ("Unsupported key exchange: " + keyExchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            m2.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        m2.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * THIRD, send a "change_cipher_spec" record followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * "Finished" message.  We flush the messages we've queued up, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * get concurrency between client and server.  The concurrency is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * useful as we calculate the master secret, which is needed both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * to compute the "Finished" message, and to compute the keys used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         * to protect all records following the change_cipher_spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        output.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * We deferred calculating the master secret and this connection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         * keying data; we do it now.  Deferring this calculation is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         * from a performance point of view, since it lets us do it during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * some time that network delays and the server's own calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         * would otherwise cause to be "dead" in the critical path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        SecretKey preMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        case K_RSA_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            preMasterSecret = ((RSAClientKeyExchange)m2).preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            byte[] secretBytes =
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2942
diff changeset
   917
                ((KerberosClientKeyExchange)m2).getUnencryptedPreMasterSecret();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   918
            preMasterSecret = new SecretKeySpec(secretBytes,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   919
                "TlsPremasterSecret");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            preMasterSecret = dh.getAgreedSecret(serverDH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            preMasterSecret = ecdh.getAgreedSecret(ephemeralServerKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            preMasterSecret = ecdh.getAgreedSecret(serverKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        default:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   936
            throw new IOException("Internal error: unknown key exchange "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   937
                + keyExchange);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        calculateKeys(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         * FOURTH, if we sent a Certificate, we need to send a signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * CertificateVerify (unless the key in the client's certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         * was a Diffie-Hellman key).).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         * This uses a hash of the previous handshake messages ... either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         * a nonfinal one (if the particular implementation supports it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         * or else using the third element in the arrays of hashes being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
         * computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (signingKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            CertificateVerify m3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   955
                SignatureAndHashAlgorithm preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   956
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   957
                    preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   958
                        SignatureAndHashAlgorithm.getPreferableAlgorithm(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   959
                            peerSupportedSignAlgs, signingKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   960
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   961
                    if (preferableSignatureAlgorithm == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   962
                        throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   963
                            "No supported signature algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   964
                    }
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
                    String hashAlg =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   967
                        SignatureAndHashAlgorithm.getHashAlgorithmName(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   968
                                preferableSignatureAlgorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   969
                    if (hashAlg == null || hashAlg.length() == 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   970
                        throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   971
                                "No supported hash algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   972
                    }
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
                    handshakeHash.setCertificateVerifyAlg(hashAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   975
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   976
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                m3 = new CertificateVerify(protocolVersion, handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    signingKey, session.getMasterSecret(),
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   979
                    sslContext.getSecureRandom(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   980
                    preferableSignatureAlgorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    "Error signing certificate verify", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                // NOTREACHED, make compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                m3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                m3.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            m3.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            output.doHashes();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   992
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   993
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   994
                handshakeHash.setCertificateVerifyAlg(null);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   995
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * OK, that's that!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        sendChangeCipherAndFinish(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
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
     * "Finished" is the last handshake message sent.  If we got this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * far, the MAC has been validated post-decryption.  We validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * the two hashes here as an additional sanity check, protecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * the handshake against various active attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    private void serverFinished(Finished mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1016
        boolean verified = mesg.verify(handshakeHash, Finished.SERVER,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1017
            session.getMasterSecret());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (!verified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                       "server 'finished' message doesn't verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1026
         * save server verify data for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1027
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1028
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1029
            serverVerifyData = mesg.getVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1030
        }
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
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
         * OK, it verified.  If we're doing the fast handshake, add that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         * "Finished" message to the hash of handshake messages, then send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
         * our own change_cipher_spec and Finished message for the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * to verify in turn.  These are the last handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         * In any case, update the session cache.  We're done handshaking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         * so there are no threats any more associated with partially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * completed handshakes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            sendChangeCipherAndFinish(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        session.setLastAccessedTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (!resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (session.isRejoinable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                ((SSLSessionContextImpl) sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                        .engineGetClientSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                        .put(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    System.out.println("%% Cached client session: " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            } else if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    "%% Didn't cache non-resumable client session: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
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
     * Send my change-cipher-spec and Finished message ... done as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * last handshake act in either the short or long sequences.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * the short one, we've already seen the server's Finished; in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * long one, we wait for it now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    private void sendChangeCipherAndFinish(boolean finishedTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        Finished mesg = new Finished(protocolVersion, handshakeHash,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1074
            Finished.CLIENT, session.getMasterSecret(), cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         * Send the change_cipher_spec message, then the Finished message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * which we just calculated (and protected using the keys we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * calculated).  Server responds with its Finished message, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * in the "fast handshake" (resume session) case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        sendChangeCipherSpec(mesg, finishedTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1085
         * save client verify data for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1086
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1087
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1088
            clientVerifyData = mesg.getVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1089
        }
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
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
         * Update state machine so server MUST send 'finished' next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         * (In "long" handshake case; in short case, we're responding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         * to its message.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        state = HandshakeMessage.ht_finished - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
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
     * Returns a ClientHello message to kickstart renegotiations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    HandshakeMessage getKickstartMessage() throws SSLException {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1104
        // session ID of the ClientHello message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1105
        SessionId sessionId = SSLSessionImpl.nullSession.getSessionId();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1106
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1107
        // a list of cipher suites sent by the client
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1108
        CipherSuiteList cipherSuites = getActiveCipherSuites();
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1109
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1110
        // set the max protocol version this client is supporting.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        maxProtocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        // Try to resume an existing session.  This might be mandatory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        // given certain API options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        session = ((SSLSessionContextImpl)sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                        .engineGetClientSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        .get(getHostSE(), getPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                System.out.println("%% Client cached "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    + session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    + (session.isRejoinable() ? "" : " (not rejoinable)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                System.out.println("%% No cached client session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if ((session != null) && (session.isRejoinable() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            CipherSuite sessionSuite = session.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            ProtocolVersion sessionVersion = session.getProtocolVersion();
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1136
            if (isNegotiable(sessionSuite) == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                if (debug != null && Debug.isOn("session")) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1138
                    System.out.println("%% can't resume, unavailable cipher");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1143
            if ((session != null) && !isNegotiable(sessionVersion)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    System.out.println("%% can't resume, protocol disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    if (Debug.isOn("handshake") || Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                        System.out.println("%% Try resuming " + session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                            + " from port " + getLocalPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1158
                sessionId = session.getSessionId();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                maxProtocolVersion = sessionVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                // Update SSL version number in underlying SSL socket and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                // handshake output stream, so that the output records (at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                // record layer) have the correct version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                setVersion(sessionVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1167
            /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1168
             * Force use of the previous session ciphersuite, and
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1169
             * add the SCSV if enabled.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1170
             */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                if (session == null) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1173
                    throw new SSLHandshakeException(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                        "Can't reuse existing SSL client session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                }
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1176
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7804
diff changeset
  1177
                Collection<CipherSuite> cipherList = new ArrayList<>(2);
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1178
                cipherList.add(sessionSuite);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1179
                if (!secureRenegotiation &&
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1180
                        cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1181
                    cipherList.add(CipherSuite.C_SCSV);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1182
                }   // otherwise, renegotiation_info extension will be used
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1183
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1184
                cipherSuites = new CipherSuiteList(cipherList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1188
        if (session == null && !enableNewSession) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1189
            throw new SSLHandshakeException("No existing session to resume");
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1190
        }
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
        // exclude SCSV for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1193
        if (secureRenegotiation && cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1194
            Collection<CipherSuite> cipherList =
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7804
diff changeset
  1195
                        new ArrayList<>(cipherSuites.size() - 1);
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1196
            for (CipherSuite suite : cipherSuites.collection()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1197
                if (suite != CipherSuite.C_SCSV) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1198
                    cipherList.add(suite);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1199
                }
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
            cipherSuites = new CipherSuiteList(cipherList);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1203
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1205
        // make sure there is a negotiable cipher suite.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1206
        boolean negotiable = false;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1207
        for (CipherSuite suite : cipherSuites.collection()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1208
            if (isNegotiable(suite)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1209
                negotiable = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1210
                break;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1211
            }
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
        if (!negotiable) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1215
            throw new SSLHandshakeException("No negotiable cipher suite");
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1216
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1217
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1218
        // Not a TLS1.2+ handshake
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1219
        // For SSLv2Hello, HandshakeHash.reset() will be called, so we
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1220
        // cannot call HandshakeHash.protocolDetermined() here. As it does
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1221
        // not follow the spec that HandshakeHash.reset() can be only be
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1222
        // called before protocolDetermined.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1223
        // if (maxProtocolVersion.v < ProtocolVersion.TLS12.v) {
7804
c59149ba3780 6996367: improve HandshakeHash
weijun
parents: 7043
diff changeset
  1224
        //     handshakeHash.protocolDetermined(maxProtocolVersion);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1225
        // }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1226
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1227
        // create the ClientHello message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1228
        ClientHello clientHelloMessage = new ClientHello(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1229
                sslContext.getSecureRandom(), maxProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1230
                sessionId, cipherSuites);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1231
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1232
        // add signature_algorithm extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1233
        if (maxProtocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1234
            // we will always send the signature_algorithm extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1235
            Collection<SignatureAndHashAlgorithm> localSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1236
                                                getLocalSupportedSignAlgs();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1237
            if (localSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1238
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1239
                            "No supported signature algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1240
            }
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
            clientHelloMessage.addSignatureAlgorithmsExtension(localSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1243
        }
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
        // add server_name extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1246
        if (enableSNIExtension) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1247
            // We cannot use the hostname resolved from name services.  For
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1248
            // virtual hosting, multiple hostnames may be bound to the same IP
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1249
            // address, so the hostname resolved from name services is not
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1250
            // reliable.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1251
            String hostname = getRawHostnameSE();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1252
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1253
            // we only allow FQDN
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1254
            if (hostname != null && hostname.indexOf('.') > 0 &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1255
                    !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1256
                    !IPAddressUtil.isIPv6LiteralAddress(hostname)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1257
                clientHelloMessage.addServerNameIndicationExtension(hostname);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1258
            }
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
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1261
        // reset the client random cookie
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1262
        clnt_random = clientHelloMessage.clnt_random;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1263
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
         * need to set the renegotiation_info extension for:
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1266
         * 1: secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1267
         * 2: initial handshake and no SCSV in the ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1268
         * 3: insecure renegotiation and no SCSV in the ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1269
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1270
        if (secureRenegotiation ||
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1271
                !cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1272
            clientHelloMessage.addRenegotiationInfoExtension(clientVerifyData);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1273
        }
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
        return clientHelloMessage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * Fault detected during handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    void handshakeAlert(byte description) throws SSLProtocolException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        String message = Alerts.alertDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            System.out.println("SSL - handshake alert: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        throw new SSLProtocolException("handshake alert:  " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * Unless we are using an anonymous ciphersuite, the server always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * sends a certificate message (for the CipherSuites we currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * support). The trust manager verifies the chain for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    private void serverCertificate(CertificateMsg mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        X509Certificate[] peerCerts = mesg.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        if (peerCerts.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            fatalSE(Alerts.alert_bad_certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                "empty certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        // ask the trust manager to verify the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        X509TrustManager tm = sslContext.getX509TrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            // find out the key exchange algorithm used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            // use "RSA" for non-ephemeral "RSA_EXPORT"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            String keyExchangeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            if (keyExchange == K_RSA_EXPORT && !serverKeyExchangeReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                keyExchangeString = K_RSA.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                keyExchangeString = keyExchange.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            if (tm instanceof X509ExtendedTrustManager) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1317
                if (conn != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1318
                    ((X509ExtendedTrustManager)tm).checkServerTrusted(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1319
                        peerCerts.clone(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                        keyExchangeString,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1321
                        conn);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1322
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1323
                    ((X509ExtendedTrustManager)tm).checkServerTrusted(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1324
                        peerCerts.clone(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1325
                        keyExchangeString,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1326
                        engine);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1327
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1329
                // Unlikely to happen, because we have wrapped the old
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1330
                // X509TrustManager with the new X509ExtendedTrustManager.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1331
                throw new CertificateException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1332
                    "Improper X509TrustManager implementation");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            // This will throw an exception, so include the original error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            fatalSE(Alerts.alert_certificate_unknown, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        session.setPeerCertificates(peerCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
}