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