jdk/src/share/classes/sun/security/ssl/ClientHandshaker.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 703 80722b883082
child 2942 37d9baeb7518
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 703
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
import javax.security.auth.kerberos.KerberosPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.jgss.krb5.Krb5Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.jgss.GSSUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.ssl.HandshakeMessage.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import static sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import static sun.security.ssl.CipherSuite.KeyExchange.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * ClientHandshaker does the protocol handshaking from the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * of view of a client.  It is driven asychronously by handshake messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * as delivered by the parent Handshaker class, and also uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * common functionality (e.g. key generation) that is provided there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
final class ClientHandshaker extends Handshaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // the server's public key from its certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private PublicKey serverKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // the server's ephemeral public key from the server key exchange message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // for ECDHE/ECDH_anon and RSA_EXPORT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private PublicKey ephemeralServerKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // server's ephemeral public value for DHE/DH_anon key exchanges
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private BigInteger          serverDH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private DHCrypt             dh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private ECDHCrypt ecdh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private CertificateRequest  certRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private boolean serverKeyExchangeReceived;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The RSA PreMasterSecret needs to know the version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * ClientHello that was used on this handshake.  This represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * the "max version" this client is supporting.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * case of an initial handshake, it's the max version enabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * but in the case of a resumption attempt, it's the version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * of the session we're trying to resume.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private ProtocolVersion maxProtocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    ClientHandshaker(SSLSocketImpl socket, SSLContextImpl context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ProtocolList enabledProtocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super(socket, context, enabledProtocols, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    ClientHandshaker(SSLEngineImpl engine, SSLContextImpl context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            ProtocolList enabledProtocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        super(engine, context, enabledProtocols, true, true);
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            kickstart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Server chooses session parameters given options created by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * client -- basically, cipher options, session id, and someday a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * set of compression options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * There are two branches of the state machine, decided by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * details of this message.  One is the "fast" handshake, where we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * can resume the pre-existing session we asked resume.  The other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * is a more expensive "full" handshake, with key exchange and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * probably authentication getting done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private void serverHello(ServerHello mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        serverKeyExchangeReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // check if the server selected protocol version is OK for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        ProtocolVersion mesgVersion = mesg.protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (enabledProtocols.contains(mesgVersion) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new SSLHandshakeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            ("Server chose unsupported or disabled protocol: " + mesgVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Set protocolVersion and propagate to SSLSocket and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // Handshake streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        setVersion(mesgVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Save server nonce, we always use it to compute connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // keys and it's also used to create the master secret if we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // creating a new session (i.e. in the full handshake).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        svr_random = mesg.svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (isEnabled(mesg.cipherSuite) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                "Server selected disabled ciphersuite " + cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        setCipherSuite(mesg.cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (mesg.compression_method != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                "compression type not supported, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                + mesg.compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // so far so good, let's look at the session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // we tried to resume, let's see what the server decided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (session.getSessionId().equals(mesg.sessionId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                // server resumed the session, let's make sure everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                // checks out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // Verify that the session ciphers are unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                CipherSuite sessionSuite = session.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (cipherSuite != sessionSuite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        ("Server returned wrong cipher suite for session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                // verify protocol version match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                ProtocolVersion sessionVersion = session.getProtocolVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (protocolVersion != sessionVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        ("Server resumed session with wrong protocol version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                // validate subject identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (sessionSuite.keyExchange == K_KRB5 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    sessionSuite.keyExchange == K_KRB5_EXPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    Principal localPrincipal = session.getLocalPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    Subject subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        subject = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            new PrivilegedExceptionAction<Subject>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            public Subject run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                return Krb5Util.getSubject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                    GSSUtil.CALLER_SSL_CLIENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                    getAccSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                            System.out.println("Attempt to obtain" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                        " subject failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    if (subject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        Set<KerberosPrincipal> principals =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                subject.getPrincipals(KerberosPrincipal.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        if (!principals.contains(localPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                            throw new SSLProtocolException("Server resumed" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                " session with wrong subject identity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            if (debug != null && Debug.isOn("session"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                System.out.println("Subject identity is same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        if (debug != null && Debug.isOn("session"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            System.out.println("Kerberos credentials are not" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                " present in the current Subject; check if " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                " javax.security.auth.useSubjectAsCreds" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                " system property has been set to false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            ("Server resumed session with no subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // looks fine; resume it, and update the state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                resumingSession = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                state = HandshakeMessage.ht_finished - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                calculateConnectionKeys(session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    System.out.println("%% Server resumed " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // we wanted to resume, but the server refused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    throw new SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        ("New session creation is disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // check extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        for (HelloExtension ext : mesg.extensions.list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            ExtensionType type = ext.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if ((type != ExtensionType.EXT_ELLIPTIC_CURVES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    && (type != ExtensionType.EXT_EC_POINT_FORMATS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                fatalSE(Alerts.alert_unsupported_extension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    "Server sent an unsupported extension: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // Create a new session, we need to do the full handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        session = new SSLSessionImpl(protocolVersion, cipherSuite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            mesg.sessionId, getHostSE(), getPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            System.out.println("** " + cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Server's own key was either a signing-only key, or was too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * large for export rules ... this message holds an ephemeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * RSA key to use for key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private void serverKeyExchange(RSA_ServerKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            throws IOException, GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (!mesg.verify(serverKey, clnt_random, svr_random)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                "server key exchange invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        ephemeralServerKey = mesg.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Diffie-Hellman key exchange.  We save the server public key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * our own D-H algorithm object so we can defer key calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * until after we've sent the client key exchange message (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * gives client and server some useful parallelism).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private void serverKeyExchange(DH_ServerKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        dh = new DHCrypt(mesg.getModulus(), mesg.getBase(), sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        serverDH = mesg.getServerPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private void serverKeyExchange(ECDH_ServerKeyExchange mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        ECPublicKey key = mesg.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        ecdh = new ECDHCrypt(key.getParams(), sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        ephemeralServerKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * The server's "Hello Done" message is the client's sign that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * it's time to do all the hard work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private void serverHelloDone(ServerHelloDone mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * Always make sure the input has been digested before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * start emitting data, to ensure the hashes are correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * computed for the Finished and CertificateVerify messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * which we send (here).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * FIRST ... if requested, send an appropriate Certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * to authenticate the client, and remember the associated private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * key to sign the CertificateVerify message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        PrivateKey signingKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (certRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            X509ExtendedKeyManager km = sslContext.getX509KeyManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            ArrayList<String> keytypesTmp = new ArrayList<String>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            for (int i = 0; i < certRequest.types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                switch (certRequest.types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                case CertificateRequest.cct_rsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    typeName = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                case CertificateRequest.cct_dss_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    typeName = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                case CertificateRequest.cct_ecdsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    // ignore if we do not have EC crypto available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    typeName = JsseJce.isEcAvailable() ? "EC" : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                // Fixed DH/ECDH client authentication not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                case CertificateRequest.cct_rsa_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                case CertificateRequest.cct_dss_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                case CertificateRequest.cct_rsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                case CertificateRequest.cct_ecdsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                // Any other values (currently not used in TLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                case CertificateRequest.cct_rsa_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                case CertificateRequest.cct_dss_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    typeName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                if ((typeName != null) && (!keytypesTmp.contains(typeName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    keytypesTmp.add(typeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            int keytypesTmpSize = keytypesTmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (keytypesTmpSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                String keytypes[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        keytypesTmp.toArray(new String[keytypesTmpSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    alias = km.chooseClientAlias(keytypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        certRequest.getAuthorities(), conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    alias = km.chooseEngineClientAlias(keytypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        certRequest.getAuthorities(), engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            CertificateMsg m1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                X509Certificate[] certs = km.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if ((certs != null) && (certs.length != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    PublicKey publicKey = certs[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    // for EC, make sure we use a supported named curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    if (publicKey instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        ECParameterSpec params = ((ECPublicKey)publicKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        int index = SupportedEllipticCurvesExtension.getCurveIndex(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        if (!SupportedEllipticCurvesExtension.isSupported(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    if (publicKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        m1 = new CertificateMsg(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        signingKey = km.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        session.setLocalPrivateKey(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        session.setLocalCertificates(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (m1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                // No appropriate cert was found ... report this to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                // server.  For SSLv3, send the no_certificate alert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                // TLS uses an empty cert chain instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    m1 = new CertificateMsg(new X509Certificate [0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    warningSE(Alerts.alert_no_certificate);
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
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            // At last ... send any client certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (m1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    m1.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                m1.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * SECOND ... send the client key exchange message.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * procedure used is a function of the cipher suite selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * one is always needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        HandshakeMessage m2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        case K_RSA_EXPORT:
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   612
            if (serverKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   613
                throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   614
                        ("Server did not send certificate message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   615
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   616
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   617
            if (!(serverKey instanceof RSAPublicKey)) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   618
                throw new SSLProtocolException
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   619
                        ("Server certificate does not include an RSA key");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   620
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   621
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
             * For RSA key exchange, we randomly generate a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
             * pre-master secret and encrypt it with the server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
             * public key.  Then we save that pre-master secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
             * so that we can calculate the keying data later;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
             * it's a performance speedup not to do that until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
             * the client's waiting for the server response, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
             * more of a speedup for the D-H case.
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   630
             *
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   631
             * If the RSA_EXPORT scheme is active, when the public
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   632
             * key in the server certificate is less than or equal
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   633
             * to 512 bits in length, use the cert's public key,
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   634
             * otherwise, the ephemeral one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
             */
703
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   636
            PublicKey key;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   637
            if (keyExchange == K_RSA) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   638
                key = serverKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   639
            } else {    // K_RSA_EXPORT
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   640
                if (JsseJce.getRSAKeyLength(serverKey) <= 512) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   641
                    // extraneous ephemeralServerKey check done
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   642
                    // above in processMessage()
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   643
                    key = serverKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   644
                } else {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   645
                    if (ephemeralServerKey == null) {
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   646
                        throw new SSLProtocolException("Server did not send" +
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   647
                            " a RSA_EXPORT Server Key Exchange message");
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   648
                    }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   649
                    key = ephemeralServerKey;
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   650
                }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   651
            }
80722b883082 6690018: RSAClientKeyExchange NullPointerException
xuelei
parents: 2
diff changeset
   652
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            m2 = new RSAClientKeyExchange(protocolVersion, maxProtocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                sslContext.getSecureRandom(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        case K_DH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        case K_DH_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
             * For DH Key exchange, we only need to make sure the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
             * knows our public key, so we calculate the same pre-master
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
             * secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
             * For certs that had DH keys in them, we send an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
             * handshake message (no key) ... we flag this case by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
             * passing a null "dhPublic" value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
             * Otherwise we send ephemeral DH keys, unsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            // if (useDH_RSA || useDH_DSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            m2 = new DHClientKeyExchange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (dh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    ("Server did not send a DH Server Key Exchange message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            m2 = new DHClientKeyExchange(dh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (ecdh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    ("Server did not send a ECDH Server Key Exchange message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            m2 = new ECDHClientKeyExchange(ecdh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if (serverKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        ("Server did not send certificate message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if (serverKey instanceof ECPublicKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        ("Server certificate does not include an EC key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            ECParameterSpec params = ((ECPublicKey)serverKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            ecdh = new ECDHCrypt(params, sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            m2 = new ECDHClientKeyExchange(ecdh.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            String hostname = getHostSE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if (hostname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                throw new IOException("Hostname is required" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                " to use Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            KerberosClientKeyExchange kerberosMsg = new KerberosClientKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                (hostname, isLoopbackSE(), getAccSE(), protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            // Record the principals involved in exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            session.setPeerPrincipal(kerberosMsg.getPeerPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            session.setLocalPrincipal(kerberosMsg.getLocalPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            m2 = kerberosMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            // somethings very wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                ("Unsupported key exchange: " + keyExchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            m2.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        m2.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
         * THIRD, send a "change_cipher_spec" record followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         * "Finished" message.  We flush the messages we've queued up, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * get concurrency between client and server.  The concurrency is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         * useful as we calculate the master secret, which is needed both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
         * to compute the "Finished" message, and to compute the keys used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
         * to protect all records following the change_cipher_spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        output.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * We deferred calculating the master secret and this connection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         * keying data; we do it now.  Deferring this calculation is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         * from a performance point of view, since it lets us do it during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         * some time that network delays and the server's own calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         * would otherwise cause to be "dead" in the critical path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        SecretKey preMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        case K_RSA_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            preMasterSecret = ((RSAClientKeyExchange)m2).preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        case K_KRB5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        case K_KRB5_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            byte[] secretBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                ((KerberosClientKeyExchange)m2).getPreMasterSecret().getUnencrypted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            preMasterSecret = new SecretKeySpec(secretBytes, "TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            preMasterSecret = dh.getAgreedSecret(serverDH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            preMasterSecret = ecdh.getAgreedSecret(ephemeralServerKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            preMasterSecret = ecdh.getAgreedSecret(serverKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            throw new IOException("Internal error: unknown key exchange " + keyExchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        calculateKeys(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * FOURTH, if we sent a Certificate, we need to send a signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * CertificateVerify (unless the key in the client's certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         * was a Diffie-Hellman key).).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         * This uses a hash of the previous handshake messages ... either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * a nonfinal one (if the particular implementation supports it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * or else using the third element in the arrays of hashes being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (signingKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            CertificateVerify m3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                m3 = new CertificateVerify(protocolVersion, handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    signingKey, session.getMasterSecret(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    "Error signing certificate verify", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                // NOTREACHED, make compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                m3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                m3.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            m3.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            output.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * OK, that's that!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        sendChangeCipherAndFinish(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * "Finished" is the last handshake message sent.  If we got this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * far, the MAC has been validated post-decryption.  We validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * the two hashes here as an additional sanity check, protecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * the handshake against various active attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private void serverFinished(Finished mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        boolean verified = mesg.verify(protocolVersion, handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                Finished.SERVER, session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (!verified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            fatalSE(Alerts.alert_illegal_parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                       "server 'finished' message doesn't verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * OK, it verified.  If we're doing the fast handshake, add that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * "Finished" message to the hash of handshake messages, then send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         * our own change_cipher_spec and Finished message for the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
         * to verify in turn.  These are the last handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
         * In any case, update the session cache.  We're done handshaking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
         * so there are no threats any more associated with partially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * completed handshakes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            sendChangeCipherAndFinish(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        session.setLastAccessedTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        if (!resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (session.isRejoinable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                ((SSLSessionContextImpl) sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        .engineGetClientSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        .put(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    System.out.println("%% Cached client session: " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            } else if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    "%% Didn't cache non-resumable client session: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Send my change-cipher-spec and Finished message ... done as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * last handshake act in either the short or long sequences.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * the short one, we've already seen the server's Finished; in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * long one, we wait for it now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    private void sendChangeCipherAndFinish(boolean finishedTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        Finished mesg = new Finished(protocolVersion, handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                                Finished.CLIENT, session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         * Send the change_cipher_spec message, then the Finished message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         * which we just calculated (and protected using the keys we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * calculated).  Server responds with its Finished message, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         * in the "fast handshake" (resume session) case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        sendChangeCipherSpec(mesg, finishedTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * Update state machine so server MUST send 'finished' next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * (In "long" handshake case; in short case, we're responding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * to its message.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        state = HandshakeMessage.ht_finished - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Returns a ClientHello message to kickstart renegotiations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    HandshakeMessage getKickstartMessage() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        ClientHello mesg = new ClientHello(sslContext.getSecureRandom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                        protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        maxProtocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        clnt_random = mesg.clnt_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // Try to resume an existing session.  This might be mandatory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        // given certain API options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        session = ((SSLSessionContextImpl)sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        .engineGetClientSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        .get(getHostSE(), getPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                System.out.println("%% Client cached "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    + session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    + (session.isRejoinable() ? "" : " (not rejoinable)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                System.out.println("%% No cached client session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        if ((session != null) && (session.isRejoinable() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            CipherSuite sessionSuite = session.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            ProtocolVersion sessionVersion = session.getProtocolVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (isEnabled(sessionSuite) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    System.out.println("%% can't resume, cipher disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            if ((session != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                        (enabledProtocols.contains(sessionVersion) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    System.out.println("%% can't resume, protocol disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (session != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    if (Debug.isOn("handshake") || Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        System.out.println("%% Try resuming " + session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                            + " from port " + getLocalPortSE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                mesg.sessionId = session.getSessionId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                mesg.protocolVersion = sessionVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                maxProtocolVersion = sessionVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                // Update SSL version number in underlying SSL socket and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                // handshake output stream, so that the output records (at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                // record layer) have the correct version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                setVersion(sessionVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            // don't say much beyond the obvious if we _must_ resume.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        "Can't reuse existing SSL client session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                mesg.setCipherSuites(new CipherSuiteList(sessionSuite));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                return mesg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                mesg.sessionId = SSLSessionImpl.nullSession.getSessionId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                throw new SSLException("No existing session to resume.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        // All we have left to do is fill out the cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        // (If this changes, change the 'return' above!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        mesg.setCipherSuites(enabledCipherSuites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        return mesg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Fault detected during handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    void handshakeAlert(byte description) throws SSLProtocolException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        String message = Alerts.alertDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            System.out.println("SSL - handshake alert: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        throw new SSLProtocolException("handshake alert:  " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * Unless we are using an anonymous ciphersuite, the server always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * sends a certificate message (for the CipherSuites we currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * support). The trust manager verifies the chain for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    private void serverCertificate(CertificateMsg mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        X509Certificate[] peerCerts = mesg.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        if (peerCerts.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            fatalSE(Alerts.alert_bad_certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                "empty certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // ask the trust manager to verify the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        X509TrustManager tm = sslContext.getX509TrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // find out the key exchange algorithm used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // use "RSA" for non-ephemeral "RSA_EXPORT"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            String keyExchangeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            if (keyExchange == K_RSA_EXPORT && !serverKeyExchangeReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                keyExchangeString = K_RSA.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                keyExchangeString = keyExchange.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            String identificator = getHostnameVerificationSE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (tm instanceof X509ExtendedTrustManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                ((X509ExtendedTrustManager)tm).checkServerTrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                        (peerCerts != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                            peerCerts.clone() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                            null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        keyExchangeString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        getHostSE(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        identificator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                if (identificator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                        "trust manager does not support peer identification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                tm.checkServerTrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    (peerCerts != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        peerCerts.clone() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                        peerCerts),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    keyExchangeString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            // This will throw an exception, so include the original error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            fatalSE(Alerts.alert_certificate_unknown, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        session.setPeerCertificates(peerCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
}