jdk/src/share/classes/sun/security/ssl/Handshaker.java
author xuelei
Mon, 07 Dec 2009 21:16:41 -0800
changeset 5182 62836694baeb
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6898739: TLS renegotiation issue Summary: the interim fix disables TLS/SSL renegotiation Reviewed-by: mullan, chegar, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
     2
 * Copyright 1996-2009 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.internal.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.internal.interfaces.TlsMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.ssl.HandshakeMessage.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Handshaker ... processes handshake records from an SSL V3.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * data stream, handling all the details of the handshake protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Note that the real protocol work is done in two subclasses, the  base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * class just provides the control flow and key generation framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
abstract class Handshaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    63
    // protocol version being established using this Handshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    ProtocolVersion protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    66
    // the currently active protocol version during a renegotiation
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    67
    ProtocolVersion     activeProtocolVersion;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // list of enabled protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    ProtocolList enabledProtocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean             isClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    SSLSocketImpl               conn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    SSLEngineImpl               engine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    HandshakeHash               handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    HandshakeInStream           input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    HandshakeOutStream          output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int                         state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    SSLContextImpl              sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    RandomCookie                clnt_random, svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    SSLSessionImpl              session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Temporary MD5 and SHA message digests. Must always be left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // in reset state after use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private MessageDigest md5Tmp, shaTmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // list of enabled CipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    CipherSuiteList     enabledCipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // current CipherSuite. Never null, initially SSL_NULL_WITH_NULL_NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    CipherSuite         cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // current key exchange. Never null, initially K_NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    KeyExchange         keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* True if this session is being resumed (fast handshake) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    boolean             resumingSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* True if it's OK to start a new SSL session */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    boolean             enableNewSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // Temporary storage for the individual keys. Set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // calculateConnectionKeys() and cleared once the ciphers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private SecretKey clntWriteKey, svrWriteKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private IvParameterSpec clntWriteIV, svrWriteIV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private SecretKey clntMacSecret, svrMacSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Delegated task subsystem data structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * If thrown is set, we need to propagate this back immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * on entry into processMessage().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Data is protected by the SSLEngine.this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private volatile boolean taskDelegated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private volatile DelegatedTask delegatedTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private volatile Exception thrown = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // Could probably use a java.util.concurrent.atomic.AtomicReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // here instead of using this lock.  Consider changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private Object thrownLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   130
    // By default, disable the unsafe legacy session renegotiation
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   131
    static final boolean renegotiable = Debug.getBooleanProperty(
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   132
                    "sun.security.ssl.allowUnsafeRenegotiation", false);
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   133
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   134
    // need to dispose the object when it is invalidated
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   135
    boolean invalidated;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   136
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    Handshaker(SSLSocketImpl c, SSLContextImpl context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ProtocolList enabledProtocols, boolean needCertVerify,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            boolean isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.conn = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        init(context, enabledProtocols, needCertVerify, isClient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    Handshaker(SSLEngineImpl engine, SSLContextImpl context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            ProtocolList enabledProtocols, boolean needCertVerify,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            boolean isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        init(context, enabledProtocols, needCertVerify, isClient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private void init(SSLContextImpl context, ProtocolList enabledProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            boolean needCertVerify, boolean isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.sslContext = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.isClient = isClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        enableNewSession = true;
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   157
        invalidated = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        setCipherSuite(CipherSuite.C_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        md5Tmp = JsseJce.getMD5();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        shaTmp = JsseJce.getSHA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // We accumulate digests of the handshake messages so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // we can read/write CertificateVerify and Finished messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // getting assurance against some particular active attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        handshakeHash = new HandshakeHash(needCertVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        setEnabledProtocols(enabledProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            conn.getAppInputStream().r.setHandshakeHash(handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {        // engine != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            engine.inputRecord.setHandshakeHash(handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // In addition to the connection state machine, controlling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // how the connection deals with the different sorts of records
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // that get sent (notably handshake transitions!), there's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // also a handshaking state machine that controls message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // sequencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // It's a convenient artifact of the protocol that this can,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // with only a couple of minor exceptions, be driven by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // type constant for the last message seen:  except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // client's cert verify, those constants are in a convenient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // order to drastically simplify state machine checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Reroutes calls to the SSLSocket or SSLEngine (*SE).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * We could have also done it by extra classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * and letting them override, but this seemed much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * less involved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    void fatalSE(byte b, String diagnostic) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        fatalSE(b, diagnostic, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    void fatalSE(byte b, Throwable cause) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        fatalSE(b, null, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    void fatalSE(byte b, String diagnostic, Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            conn.fatal(b, diagnostic, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            engine.fatal(b, diagnostic, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    void warningSE(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            conn.warning(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            engine.warning(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    String getHostSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return conn.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return engine.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    String getHostAddressSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return conn.getInetAddress().getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
             * This is for caching only, doesn't matter that's is really
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
             * a hostname.  The main thing is that it doesn't do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
             * a reverse DNS lookup, potentially slowing things down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return engine.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    boolean isLoopbackSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return conn.getInetAddress().isLoopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    int getPortSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return conn.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return engine.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    int getLocalPortSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return conn.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    String getHostnameVerificationSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return conn.getHostnameVerification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return engine.getHostnameVerification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    AccessControlContext getAccSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return conn.getAcc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return engine.getAcc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private void setVersionSE(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            conn.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            engine.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Set the active protocol version and propagate it to the SSLSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * and our handshake streams. Called from ClientHandshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * and ServerHandshaker with the negotiated protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    void setVersion(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        setVersionSE(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        output.r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Set the enabled protocols. Called from the constructor or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * SSLSocketImpl.setEnabledProtocols() (if the handshake is not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * in progress).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    void setEnabledProtocols(ProtocolList enabledProtocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        this.enabledProtocols = enabledProtocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // temporary protocol version until the actual protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // is negotiated in the Hello exchange. This affects the record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // version we sent with the ClientHello. Using max() as the record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // version is not really correct but some implementations fail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // correctly negotiate TLS otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        protocolVersion = enabledProtocols.max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        ProtocolVersion helloVersion = enabledProtocols.helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        input = new HandshakeInStream(handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            output = new HandshakeOutStream(protocolVersion, helloVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                        handshakeHash, conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            conn.getAppInputStream().r.setHelloVersion(helloVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            output = new HandshakeOutStream(protocolVersion, helloVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                        handshakeHash, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            engine.outputRecord.setHelloVersion(helloVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Set cipherSuite and keyExchange to the given CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Does not perform any verification that this is a valid selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * this must be done before calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    void setCipherSuite(CipherSuite s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        this.cipherSuite = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        this.keyExchange = s.keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Check if the given ciphersuite is enabled and available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * (Enabled ciphersuites are always available unless the status has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * changed due to change in JCE providers since it was enabled).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Does not check if the required server certificates are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    boolean isEnabled(CipherSuite s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return enabledCipherSuites.contains(s) && s.isAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * As long as handshaking has not started, we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * change whether session creations are allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Callers should do their own checking if handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * has started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    void setEnableSessionCreation(boolean newSessions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        enableNewSession = newSessions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Create a new read cipher and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    CipherBox newReadCipher() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        BulkCipher cipher = cipherSuite.cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        CipherBox box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            box = cipher.newCipher(protocolVersion, svrWriteKey, svrWriteIV,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                   false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            svrWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            svrWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            box = cipher.newCipher(protocolVersion, clntWriteKey, clntWriteIV,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                   false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            clntWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            clntWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Create a new write cipher and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    CipherBox newWriteCipher() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        BulkCipher cipher = cipherSuite.cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        CipherBox box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            box = cipher.newCipher(protocolVersion, clntWriteKey, clntWriteIV,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                   true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            clntWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            clntWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            box = cipher.newCipher(protocolVersion, svrWriteKey, svrWriteIV,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                   true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            svrWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            svrWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Create a new read MAC and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    MAC newReadMAC() throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        MacAlg macAlg = cipherSuite.macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        MAC mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            mac = macAlg.newMac(protocolVersion, svrMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            svrMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            mac = macAlg.newMac(protocolVersion, clntMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            clntMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Create a new write MAC and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    MAC newWriteMAC() throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        MacAlg macAlg = cipherSuite.macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        MAC mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            mac = macAlg.newMac(protocolVersion, clntMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            clntMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            mac = macAlg.newMac(protocolVersion, svrMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            svrMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Returns true iff the handshake sequence is done, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * this freshly created session can become the current one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    boolean isDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return state == HandshakeMessage.ht_finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Returns the session which was created through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * handshake sequence ... should be called after isDone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    SSLSessionImpl getSession() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * This routine is fed SSL handshake records when they become available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * and processes messages found therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    void process_record(InputRecord r, boolean expectingFinished)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        checkThrown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * Store the incoming handshake data, then see if we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * now process any completed handshake messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        input.incomingRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * We don't need to create a separate delegatable task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * for finished messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if ((conn != null) || expectingFinished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            processLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            delegateTask(new PrivilegedExceptionAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                public Void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    processLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * On input, we hash messages one at a time since servers may need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * to access an intermediate hash to validate a CertificateVerify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Note that many handshake messages can come in one record (and often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * do, to reduce network resource utilization), and one message can also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * require multiple records (e.g. very large Certificate messages).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    void processLoop() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   503
        // need to read off 4 bytes at least to get the handshake
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   504
        // message type and length.
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   505
        while (input.available() >= 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            byte messageType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            int messageLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             * See if we can read the handshake message header, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             * then the entire handshake message.  If not, wait till
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
             * we can read and process an entire message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            input.mark(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            messageType = (byte)input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            messageLen = input.getInt24();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (input.available() < messageLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                input.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             * Process the messsage.  We require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             * that processMessage() consumes the entire message.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             * lieu of explicit error checks (how?!) we assume that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             * data will look like garbage on encoding/processing errors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
             * and that other protocol code will detect such errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
             * Note that digesting is normally deferred till after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
             * message has been processed, though to process at least the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             * client's Finished message (i.e. send the server's) we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
             * to acccelerate that digesting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
             * Also, note that hello request messages are never hashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
             * that includes the hello request header, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (messageType == HandshakeMessage.ht_hello_request) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                input.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                processMessage(messageType, messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                input.ignore(4 + messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                input.mark(messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                processMessage(messageType, messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                input.digestNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Returns true iff the handshaker has sent any messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Server kickstarting is not as neat as it should be; we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * need to create a new handshaker, this method lets us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * know if we should.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    boolean started() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return state >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Used to kickstart the negotiation ... either writing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * ClientHello or a HelloRequest as appropriate, whichever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * the subclass returns.  NOP if handshaking's already started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    void kickstart() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (state >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        HandshakeMessage m = getKickstartMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            m.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        m.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        state = m.messageType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Both client and server modes can start handshaking; but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * message they send to do so is different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    abstract HandshakeMessage getKickstartMessage() throws SSLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Client and Server side protocols are each driven though this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * call, which processes a single message and drives the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * side of the protocol state machine (depending on the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    abstract void processMessage(byte messageType, int messageLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Most alerts in the protocol relate to handshaking problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Alerts are detected as the connection reads data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    abstract void handshakeAlert(byte description) throws SSLProtocolException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Sends a change cipher spec message and updates the write side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * cipher state so that future messages use the just-negotiated spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    void sendChangeCipherSpec(Finished mesg, boolean lastMessage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        output.flush(); // i.e. handshake data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * The write cipher state is protected by the connection write lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * so we must grab it while making the change. We also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * make sure no writes occur between sending the ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * message, installing the new cipher state, and sending the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * Finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         * We already hold SSLEngine/SSLSocket "this" by virtue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * of this being called from the readRecord code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        OutputRecord r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            r = new OutputRecord(Record.ct_change_cipher_spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            r = new EngineOutputRecord(Record.ct_change_cipher_spec, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        r.write(1);     // single byte of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (conn != null) {
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   633
            conn.writeLock.lock();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   634
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                conn.writeRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                conn.changeWriteCiphers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                mesg.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                output.flush();
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   642
            } finally {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   643
                conn.writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            synchronized (engine.writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                engine.writeRecord((EngineOutputRecord)r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                engine.changeWriteCiphers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                mesg.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                if (lastMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    output.setFinishedMsg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Single access point to key calculation logic.  Given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * pre-master secret and the nonces from client and server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * produce all the keying material to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    void calculateKeys(SecretKey preMasterSecret, ProtocolVersion version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        SecretKey master = calculateMasterSecret(preMasterSecret, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        session.setMasterSecret(master);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        calculateConnectionKeys(master);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Calculate the master secret from its various components.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * used for key exchange by all cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * The master secret is the catenation of three MD5 hashes, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * consisting of the pre-master secret and a SHA1 hash.  Those three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * SHA1 hashes are of (different) constant strings, the pre-master
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * secret, and the nonces provided by the client and the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    private SecretKey calculateMasterSecret(SecretKey preMasterSecret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            ProtocolVersion requestedVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                (preMasterSecret, protocolVersion.major, protocolVersion.minor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                clnt_random.random_bytes, svr_random.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (debug != null && Debug.isOn("keygen")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            HexDumpEncoder      dump = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            System.out.println("SESSION KEYGEN:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            System.out.println("PreMaster Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            printHex(dump, preMasterSecret.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            // Nonces are dumped with connection keygen, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            // benefit to doing it twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        SecretKey masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            KeyGenerator kg = JsseJce.getKeyGenerator("SunTlsMasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            kg.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            masterSecret = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            // For RSA premaster secrets, do not signal a protocol error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            // due to the Bleichenbacher attack. See comments further down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            if (!preMasterSecret.getAlgorithm().equals("TlsRsaPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                System.out.println("RSA master secret generation error:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                System.out.println("Generating new random premaster secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            preMasterSecret = RSAClientKeyExchange.generateDummySecret(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            // recursive call with new premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return calculateMasterSecret(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // if no version check requested (client side handshake),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        // or version information is not available (not an RSA premaster secret),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // return master secret immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if ((requestedVersion == null) || !(masterSecret instanceof TlsMasterSecret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        TlsMasterSecret tlsKey = (TlsMasterSecret)masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        int major = tlsKey.getMajorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        int minor = tlsKey.getMinorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if ((major < 0) || (minor < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // check if the premaster secret version is ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // the specification says that it must be the maximum version supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        // by the client from its ClientHello message. However, many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        // implementations send the negotiated version, so accept both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // NOTE that we may be comparing two unsupported version numbers in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // the second case, which is why we cannot use object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        // equality in this special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        ProtocolVersion premasterVersion = ProtocolVersion.valueOf(major, minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        boolean versionMismatch = (premasterVersion != protocolVersion) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                  (premasterVersion.v != requestedVersion.v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (versionMismatch == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            // check passed, return key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // Due to the Bleichenbacher attack, do not signal a protocol error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // Generate a random premaster secret and continue with the handshake,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // which will fail when verifying the finished messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // For more information, see comments in PreMasterSecret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            System.out.println("RSA PreMasterSecret version error: expected"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                + protocolVersion + " or " + requestedVersion + ", decrypted: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                + premasterVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            System.out.println("Generating new random premaster secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        preMasterSecret = RSAClientKeyExchange.generateDummySecret(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // recursive call with new premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        return calculateMasterSecret(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Calculate the keys needed for this connection, once the session's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * master secret has been calculated.  Uses the master key and nonces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * the amount of keying material generated is a function of the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * suite that's been negotiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * This gets called both on the "full handshake" (where we exchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * a premaster secret and started a new session) as well as on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * "fast handshake" (where we just resumed a pre-existing session).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    void calculateConnectionKeys(SecretKey masterKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * For both the read and write sides of the protocol, we use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * master to generate MAC secrets and cipher keying material.  Block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * ciphers need initialization vectors, which we also generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * First we figure out how much keying material is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        int hashSize = cipherSuite.macAlg.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        boolean is_exportable = cipherSuite.exportable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        BulkCipher cipher = cipherSuite.cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        int keySize = cipher.keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        int ivSize = cipher.ivSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        int expandedKeySize = is_exportable ? cipher.expandedKeySize : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            (masterKey, protocolVersion.major, protocolVersion.minor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            clnt_random.random_bytes, svr_random.random_bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            cipher.algorithm, cipher.keySize, expandedKeySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            cipher.ivSize, hashSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            KeyGenerator kg = JsseJce.getKeyGenerator("SunTlsKeyMaterial");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            kg.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            TlsKeyMaterialSpec keySpec = (TlsKeyMaterialSpec)kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            clntWriteKey = keySpec.getClientCipherKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            svrWriteKey = keySpec.getServerCipherKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            clntWriteIV = keySpec.getClientIv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            svrWriteIV = keySpec.getServerIv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            clntMacSecret = keySpec.getClientMacKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            svrMacSecret = keySpec.getServerMacKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // Dump the connection keys as they're generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (debug != null && Debug.isOn("keygen")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            synchronized (System.out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                HexDumpEncoder  dump = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                System.out.println("CONNECTION KEYGEN:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                // Inputs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                System.out.println("Client Nonce:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                printHex(dump, clnt_random.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                System.out.println("Server Nonce:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                printHex(dump, svr_random.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                System.out.println("Master Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                printHex(dump, masterKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                // Outputs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                System.out.println("Client MAC write Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                printHex(dump, clntMacSecret.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                System.out.println("Server MAC write Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                printHex(dump, svrMacSecret.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                if (clntWriteKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    System.out.println("Client write key:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    printHex(dump, clntWriteKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    System.out.println("Server write key:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    printHex(dump, svrWriteKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    System.out.println("... no encryption keys used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                if (clntWriteIV != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    System.out.println("Client write IV:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    printHex(dump, clntWriteIV.getIV());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    System.out.println("Server write IV:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    printHex(dump, svrWriteIV.getIV());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    System.out.println("... no IV used for this cipher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private static void printHex(HexDumpEncoder dump, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            System.out.println("(key bytes not available)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                dump.encodeBuffer(bytes, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                // just for debugging, ignore this
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Throw an SSLException with the specified message and cause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Shorthand until a new SSLException constructor is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * This method never returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    static void throwSSLException(String msg, Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        SSLException e = new SSLException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        e.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Implement a simple task delegator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * We are currently implementing this as a single delegator, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * try for parallel tasks later.  Client Authentication could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * benefit from this, where ClientKeyExchange/CertificateVerify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * could be carried out in parallel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    class DelegatedTask<E> implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        private PrivilegedExceptionAction<E> pea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        DelegatedTask(PrivilegedExceptionAction<E> pea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            this.pea = pea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            synchronized (engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    AccessController.doPrivileged(pea, engine.getAcc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    thrown = pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                } catch (RuntimeException rte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    thrown = rte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                delegatedTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                taskDelegated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    private <T> void delegateTask(PrivilegedExceptionAction<T> pea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        delegatedTask = new DelegatedTask<T>(pea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        taskDelegated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        thrown = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    DelegatedTask getTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (!taskDelegated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            taskDelegated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            return delegatedTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * See if there are any tasks which need to be delegated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Locked by SSLEngine.this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    boolean taskOutstanding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return (delegatedTask != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * The previous caller failed for some reason, report back the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Exception.  We won't worry about Error's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * Locked by SSLEngine.this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    void checkThrown() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        synchronized (thrownLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if (thrown != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                String msg = thrown.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    msg = "Delegated task threw Exception/Error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                 * See what the underlying type of exception is.  We should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                 * throw the same thing.  Chain thrown to the new exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                Exception e = thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                thrown = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    throw (RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                        new RuntimeException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                } else if (e instanceof SSLHandshakeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    throw (SSLHandshakeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        new SSLHandshakeException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                } else if (e instanceof SSLKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    throw (SSLKeyException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        new SSLKeyException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                } else if (e instanceof SSLPeerUnverifiedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    throw (SSLPeerUnverifiedException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                        new SSLPeerUnverifiedException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                } else if (e instanceof SSLProtocolException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    throw (SSLProtocolException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        new SSLProtocolException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                     * If it's SSLException or any other Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                     * we'll wrap it in an SSLException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    throw (SSLException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        new SSLException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
}