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