jdk/src/share/classes/sun/security/ssl/Handshaker.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 7990 57019dc81b66
child 9035 1255eb81cc2f
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
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.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    34
import java.security.AlgorithmConstraints;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.internal.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.internal.interfaces.TlsMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.ssl.HandshakeMessage.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    51
import static sun.security.ssl.CipherSuite.PRF.*;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Handshaker ... processes handshake records from an SSL V3.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * data stream, handling all the details of the handshake protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Note that the real protocol work is done in two subclasses, the  base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * class just provides the control flow and key generation framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
abstract class Handshaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    64
    // protocol version being established using this Handshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    ProtocolVersion protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    67
    // the currently active protocol version during a renegotiation
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    68
    ProtocolVersion     activeProtocolVersion;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
    69
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    70
    // security parameters for secure renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    71
    boolean             secureRenegotiation;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    72
    byte[]              clientVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    73
    byte[]              serverVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    74
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    75
    // Is it an initial negotiation  or a renegotiation?
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    76
    boolean                     isInitialHandshake;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    77
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    78
    // List of enabled protocols
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    79
    private ProtocolList        enabledProtocols;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    80
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    81
    // List of enabled CipherSuites
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    82
    private CipherSuiteList     enabledCipherSuites;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    83
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    84
    // The endpoint identification protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    85
    String              identificationProtocol;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    86
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    87
    // The cryptographic algorithm constraints
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    88
    private AlgorithmConstraints    algorithmConstraints = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    89
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    90
    // Local supported signature and algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    91
    Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    92
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    93
    // Peer supported signature and algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    94
    Collection<SignatureAndHashAlgorithm> peerSupportedSignAlgs;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    95
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    96
    /*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    97
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    98
    /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
    99
     * List of active protocols
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   100
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   101
     * Active protocols is a subset of enabled protocols, and will
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   102
     * contain only those protocols that have vaild cipher suites
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   103
     * enabled.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   104
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   105
    private ProtocolList       activeProtocols;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   106
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   107
    /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   108
     * List of active cipher suites
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   109
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   110
     * Active cipher suites is a subset of enabled cipher suites, and will
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   111
     * contain only those cipher suites available for the active protocols.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   112
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   113
    private CipherSuiteList    activeCipherSuites;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private boolean             isClient;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   116
    private boolean             needCertVerify;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    SSLSocketImpl               conn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    SSLEngineImpl               engine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    HandshakeHash               handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    HandshakeInStream           input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    HandshakeOutStream          output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int                         state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    SSLContextImpl              sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    RandomCookie                clnt_random, svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    SSLSessionImpl              session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // current CipherSuite. Never null, initially SSL_NULL_WITH_NULL_NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    CipherSuite         cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // current key exchange. Never null, initially K_NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    KeyExchange         keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /* True if this session is being resumed (fast handshake) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    boolean             resumingSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /* True if it's OK to start a new SSL session */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    boolean             enableNewSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // Temporary storage for the individual keys. Set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // calculateConnectionKeys() and cleared once the ciphers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private SecretKey clntWriteKey, svrWriteKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private IvParameterSpec clntWriteIV, svrWriteIV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private SecretKey clntMacSecret, svrMacSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Delegated task subsystem data structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * If thrown is set, we need to propagate this back immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * on entry into processMessage().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Data is protected by the SSLEngine.this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private volatile boolean taskDelegated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private volatile DelegatedTask delegatedTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private volatile Exception thrown = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // Could probably use a java.util.concurrent.atomic.AtomicReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // here instead of using this lock.  Consider changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private Object thrownLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   167
    // By default, disable the unsafe legacy session renegotiation
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   168
    static final boolean allowUnsafeRenegotiation = Debug.getBooleanProperty(
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   169
                    "sun.security.ssl.allowUnsafeRenegotiation", false);
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   170
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   171
    // For maximum interoperability and backward compatibility, RFC 5746
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   172
    // allows server (or client) to accept ClientHello (or ServerHello)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   173
    // message without the secure renegotiation_info extension or SCSV.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   174
    //
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   175
    // For maximum security, RFC 5746 also allows server (or client) to
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   176
    // reject such message with a fatal "handshake_failure" alert.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   177
    //
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   178
    // By default, allow such legacy hello messages.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   179
    static final boolean allowLegacyHelloMessages = Debug.getBooleanProperty(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   180
                    "sun.security.ssl.allowLegacyHelloMessages", true);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   181
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   182
    // need to dispose the object when it is invalidated
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   183
    boolean invalidated;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    Handshaker(SSLSocketImpl c, SSLContextImpl context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            ProtocolList enabledProtocols, boolean needCertVerify,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   187
            boolean isClient, ProtocolVersion activeProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   188
            boolean isInitialHandshake, boolean secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   189
            byte[] clientVerifyData, byte[] serverVerifyData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.conn = c;
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   191
        init(context, enabledProtocols, needCertVerify, isClient,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   192
            activeProtocolVersion, isInitialHandshake, secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   193
            clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    Handshaker(SSLEngineImpl engine, SSLContextImpl context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ProtocolList enabledProtocols, boolean needCertVerify,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   198
            boolean isClient, ProtocolVersion activeProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   199
            boolean isInitialHandshake, boolean secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   200
            byte[] clientVerifyData, byte[] serverVerifyData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this.engine = engine;
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   202
        init(context, enabledProtocols, needCertVerify, isClient,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   203
            activeProtocolVersion, isInitialHandshake, secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   204
            clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private void init(SSLContextImpl context, ProtocolList enabledProtocols,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   208
            boolean needCertVerify, boolean isClient,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   209
            ProtocolVersion activeProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   210
            boolean isInitialHandshake, boolean secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   211
            byte[] clientVerifyData, byte[] serverVerifyData) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   212
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   213
        if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   214
            System.out.println(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   215
                "Allow unsafe renegotiation: " + allowUnsafeRenegotiation +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   216
                "\nAllow legacy hello messages: " + allowLegacyHelloMessages +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   217
                "\nIs initial handshake: " + isInitialHandshake +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   218
                "\nIs secure renegotiation: " + secureRenegotiation);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   219
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.sslContext = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.isClient = isClient;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   223
        this.needCertVerify = needCertVerify;
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   224
        this.activeProtocolVersion = activeProtocolVersion;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   225
        this.isInitialHandshake = isInitialHandshake;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   226
        this.secureRenegotiation = secureRenegotiation;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   227
        this.clientVerifyData = clientVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   228
        this.serverVerifyData = serverVerifyData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        enableNewSession = true;
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   230
        invalidated = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        setCipherSuite(CipherSuite.C_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        setEnabledProtocols(enabledProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (conn != null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   236
            algorithmConstraints = new SSLAlgorithmConstraints(conn, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {        // engine != null
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   238
            algorithmConstraints = new SSLAlgorithmConstraints(engine, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // In addition to the connection state machine, controlling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // how the connection deals with the different sorts of records
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // that get sent (notably handshake transitions!), there's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // also a handshaking state machine that controls message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // sequencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // It's a convenient artifact of the protocol that this can,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // with only a couple of minor exceptions, be driven by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // type constant for the last message seen:  except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // client's cert verify, those constants are in a convenient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // order to drastically simplify state machine checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        //
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   255
        state = -2;  // initialized but not activated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Reroutes calls to the SSLSocket or SSLEngine (*SE).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * We could have also done it by extra classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * and letting them override, but this seemed much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * less involved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    void fatalSE(byte b, String diagnostic) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        fatalSE(b, diagnostic, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    void fatalSE(byte b, Throwable cause) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        fatalSE(b, null, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void fatalSE(byte b, String diagnostic, Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            conn.fatal(b, diagnostic, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            engine.fatal(b, diagnostic, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    void warningSE(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            conn.warning(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            engine.warning(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   290
    String getRawHostnameSE() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   291
        if (conn != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   292
            return conn.getRawHostname();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   293
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   294
            return engine.getPeerHost();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   295
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   296
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   297
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    String getHostSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return conn.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return engine.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    String getHostAddressSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return conn.getInetAddress().getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             * This is for caching only, doesn't matter that's is really
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
             * a hostname.  The main thing is that it doesn't do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
             * a reverse DNS lookup, potentially slowing things down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return engine.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    boolean isLoopbackSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return conn.getInetAddress().isLoopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    int getPortSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return conn.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return engine.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    int getLocalPortSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return conn.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    AccessControlContext getAccSE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return conn.getAcc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return engine.getAcc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private void setVersionSE(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            conn.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            engine.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Set the active protocol version and propagate it to the SSLSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * and our handshake streams. Called from ClientHandshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * and ServerHandshaker with the negotiated protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    void setVersion(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        setVersionSE(protocolVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   367
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        output.r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Set the enabled protocols. Called from the constructor or
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   373
     * SSLSocketImpl/SSLEngineImpl.setEnabledProtocols() (if the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   374
     * handshake is not yet in progress).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    void setEnabledProtocols(ProtocolList enabledProtocols) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   377
        activeCipherSuites = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   378
        activeProtocols = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   379
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        this.enabledProtocols = enabledProtocols;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   381
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   382
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   383
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   384
     * Set the enabled cipher suites. Called from
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   385
     * SSLSocketImpl/SSLEngineImpl.setEnabledCipherSuites() (if the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   386
     * handshake is not yet in progress).
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   387
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   388
    void setEnabledCipherSuites(CipherSuiteList enabledCipherSuites) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   389
        activeCipherSuites = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   390
        activeProtocols = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   391
        this.enabledCipherSuites = enabledCipherSuites;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   392
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   393
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   394
    /**
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   395
     * Set the algorithm constraints. Called from the constructor or
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   396
     * SSLSocketImpl/SSLEngineImpl.setAlgorithmConstraints() (if the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   397
     * handshake is not yet in progress).
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   398
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   399
    void setAlgorithmConstraints(AlgorithmConstraints algorithmConstraints) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   400
        activeCipherSuites = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   401
        activeProtocols = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   402
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   403
        this.algorithmConstraints =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   404
            new SSLAlgorithmConstraints(algorithmConstraints);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   405
        this.localSupportedSignAlgs = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   406
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   407
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   408
    Collection<SignatureAndHashAlgorithm> getLocalSupportedSignAlgs() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   409
        if (localSupportedSignAlgs == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   410
            localSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   411
                SignatureAndHashAlgorithm.getSupportedAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   412
                                                    algorithmConstraints);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   413
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   414
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   415
        return localSupportedSignAlgs;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   416
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   417
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   418
    void setPeerSupportedSignAlgs(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   419
            Collection<SignatureAndHashAlgorithm> algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   420
        peerSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   421
            new ArrayList<SignatureAndHashAlgorithm>(algorithms);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   422
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   423
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   424
    Collection<SignatureAndHashAlgorithm> getPeerSupportedSignAlgs() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   425
        return peerSupportedSignAlgs;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   426
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   427
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   428
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   429
    /**
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   430
     * Set the identification protocol. Called from the constructor or
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   431
     * SSLSocketImpl/SSLEngineImpl.setIdentificationProtocol() (if the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   432
     * handshake is not yet in progress).
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   433
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   434
    void setIdentificationProtocol(String protocol) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   435
        this.identificationProtocol = protocol;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   436
    }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   437
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   438
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   439
     * Prior to handshaking, activate the handshake and initialize the version,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   440
     * input stream and output stream.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   441
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   442
    void activate(ProtocolVersion helloVersion) throws IOException {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   443
        if (activeProtocols == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   444
            activeProtocols = getActiveProtocols();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   445
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   446
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   447
        if (activeProtocols.collection().isEmpty() ||
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   448
                activeProtocols.max.v == ProtocolVersion.NONE.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   449
            throw new SSLHandshakeException("No appropriate protocol");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   450
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   451
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   452
        if (activeCipherSuites == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   453
            activeCipherSuites = getActiveCipherSuites();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   454
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   455
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   456
        if (activeCipherSuites.collection().isEmpty()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   457
            throw new SSLHandshakeException("No appropriate cipher suite");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   458
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // temporary protocol version until the actual protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // is negotiated in the Hello exchange. This affects the record
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   462
        // version we sent with the ClientHello.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   463
        if (!isInitialHandshake) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   464
            protocolVersion = activeProtocolVersion;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   465
        } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   466
            protocolVersion = activeProtocols.max;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   467
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   469
        if (helloVersion == null || helloVersion.v == ProtocolVersion.NONE.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   470
            helloVersion = activeProtocols.helloVersion;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   471
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   473
        // We accumulate digests of the handshake messages so that
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   474
        // we can read/write CertificateVerify and Finished messages,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   475
        // getting assurance against some particular active attacks.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   476
        Set<String> localSupportedHashAlgorithms =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   477
            SignatureAndHashAlgorithm.getHashAlgorithmNames(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   478
                getLocalSupportedSignAlgs());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   479
        handshakeHash = new HandshakeHash(!isClient, needCertVerify,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   480
            localSupportedHashAlgorithms);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   481
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   482
        // Generate handshake input/output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        input = new HandshakeInStream(handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            output = new HandshakeOutStream(protocolVersion, helloVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                        handshakeHash, conn);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   487
            conn.getAppInputStream().r.setHandshakeHash(handshakeHash);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            conn.getAppInputStream().r.setHelloVersion(helloVersion);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   489
            conn.getAppOutputStream().r.setHelloVersion(helloVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            output = new HandshakeOutStream(protocolVersion, helloVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                        handshakeHash, engine);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   493
            engine.inputRecord.setHandshakeHash(handshakeHash);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   494
            engine.inputRecord.setHelloVersion(helloVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            engine.outputRecord.setHelloVersion(helloVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   498
        // move state to activated
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   499
        state = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Set cipherSuite and keyExchange to the given CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Does not perform any verification that this is a valid selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * this must be done before calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    void setCipherSuite(CipherSuite s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        this.cipherSuite = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        this.keyExchange = s.keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Check if the given ciphersuite is enabled and available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Does not check if the required server certificates are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   516
    boolean isNegotiable(CipherSuite s) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   517
        if (activeCipherSuites == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   518
            activeCipherSuites = getActiveCipherSuites();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   519
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   520
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   521
        return activeCipherSuites.contains(s) && s.isNegotiable();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   522
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   523
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   524
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   525
     * Check if the given protocol version is enabled and available.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   526
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   527
    boolean isNegotiable(ProtocolVersion protocolVersion) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   528
        if (activeProtocols == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   529
            activeProtocols = getActiveProtocols();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   530
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   531
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   532
        return activeProtocols.contains(protocolVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   533
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   534
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   535
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   536
     * Select a protocol version from the list. Called from
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   537
     * ServerHandshaker to negotiate protocol version.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   538
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   539
     * Return the lower of the protocol version suggested in the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   540
     * clien hello and the highest supported by the server.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   541
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   542
    ProtocolVersion selectProtocolVersion(ProtocolVersion protocolVersion) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   543
        if (activeProtocols == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   544
            activeProtocols = getActiveProtocols();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   545
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   546
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   547
        return activeProtocols.selectProtocolVersion(protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   551
     * Get the active cipher suites.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   552
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   553
     * In TLS 1.1, many weak or vulnerable cipher suites were obsoleted,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   554
     * such as TLS_RSA_EXPORT_WITH_RC4_40_MD5. The implementation MUST NOT
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   555
     * negotiate these cipher suites in TLS 1.1 or later mode.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   556
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   557
     * Therefore, when the active protocols only include TLS 1.1 or later,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   558
     * the client cannot request to negotiate those obsoleted cipher
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   559
     * suites.  That is, the obsoleted suites should not be included in the
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   560
     * client hello. So we need to create a subset of the enabled cipher
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   561
     * suites, the active cipher suites, which does not contain obsoleted
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   562
     * cipher suites of the minimum active protocol.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   563
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   564
     * Return empty list instead of null if no active cipher suites.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   565
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   566
    CipherSuiteList getActiveCipherSuites() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   567
        if (activeCipherSuites == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   568
            if (activeProtocols == null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   569
                activeProtocols = getActiveProtocols();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   570
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   571
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   572
            ArrayList<CipherSuite> suites = new ArrayList<>();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   573
            if (!(activeProtocols.collection().isEmpty()) &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   574
                    activeProtocols.min.v != ProtocolVersion.NONE.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   575
                for (CipherSuite suite : enabledCipherSuites.collection()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   576
                    if (suite.obsoleted > activeProtocols.min.v &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   577
                            suite.supported <= activeProtocols.max.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   578
                        if (algorithmConstraints.permits(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   579
                                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   580
                                suite.name, null)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   581
                            suites.add(suite);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   582
                        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   583
                    } else if (debug != null && Debug.isOn("verbose")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   584
                        if (suite.obsoleted <= activeProtocols.min.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   585
                            System.out.println(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   586
                                "Ignoring obsoleted cipher suite: " + suite);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   587
                        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   588
                            System.out.println(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   589
                                "Ignoring unsupported cipher suite: " + suite);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   590
                        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   591
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   592
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   593
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   594
            activeCipherSuites = new CipherSuiteList(suites);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   595
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   596
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   597
        return activeCipherSuites;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   598
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   599
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   600
    /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   601
     * Get the active protocol versions.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   602
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   603
     * In TLS 1.1, many weak or vulnerable cipher suites were obsoleted,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   604
     * such as TLS_RSA_EXPORT_WITH_RC4_40_MD5. The implementation MUST NOT
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   605
     * negotiate these cipher suites in TLS 1.1 or later mode.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   606
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   607
     * For example, if "TLS_RSA_EXPORT_WITH_RC4_40_MD5" is the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   608
     * only enabled cipher suite, the client cannot request TLS 1.1 or
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   609
     * later, even though TLS 1.1 or later is enabled.  We need to create a
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   610
     * subset of the enabled protocols, called the active protocols, which
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   611
     * contains protocols appropriate to the list of enabled Ciphersuites.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   612
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   613
     * Return empty list instead of null if no active protocol versions.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   614
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   615
    ProtocolList getActiveProtocols() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   616
        if (activeProtocols == null) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   617
            ArrayList<ProtocolVersion> protocols = new ArrayList<>(4);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   618
            for (ProtocolVersion protocol : enabledProtocols.collection()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   619
                boolean found = false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   620
                for (CipherSuite suite : enabledCipherSuites.collection()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   621
                    if (suite.isAvailable() && suite.obsoleted > protocol.v &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   622
                                               suite.supported <= protocol.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   623
                        if (algorithmConstraints.permits(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   624
                                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   625
                                suite.name, null)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   626
                            protocols.add(protocol);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   627
                            found = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   628
                            break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   629
                        } else if (debug != null && Debug.isOn("verbose")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   630
                            System.out.println(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   631
                                "Ignoring disabled cipher suite: " + suite +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   632
                                 " for " + protocol);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   633
                        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   634
                    } else if (debug != null && Debug.isOn("verbose")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   635
                        System.out.println(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   636
                            "Ignoring unsupported cipher suite: " + suite +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   637
                                 " for " + protocol);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   638
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   639
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   640
                if (!found && (debug != null) && Debug.isOn("handshake")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   641
                    System.out.println(
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   642
                        "No available cipher suite for " + protocol);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   643
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   644
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   645
            activeProtocols = new ProtocolList(protocols);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   646
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   647
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   648
        return activeProtocols;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   649
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   650
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   651
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   652
     * As long as handshaking has not activated, we can
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * change whether session creations are allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Callers should do their own checking if handshaking
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   656
     * has activated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    void setEnableSessionCreation(boolean newSessions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        enableNewSession = newSessions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Create a new read cipher and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    CipherBox newReadCipher() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        BulkCipher cipher = cipherSuite.cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        CipherBox box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            box = cipher.newCipher(protocolVersion, svrWriteKey, svrWriteIV,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   670
                                   sslContext.getSecureRandom(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            svrWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            svrWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            box = cipher.newCipher(protocolVersion, clntWriteKey, clntWriteIV,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   675
                                   sslContext.getSecureRandom(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            clntWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            clntWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Create a new write cipher and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    CipherBox newWriteCipher() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        BulkCipher cipher = cipherSuite.cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        CipherBox box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            box = cipher.newCipher(protocolVersion, clntWriteKey, clntWriteIV,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   690
                                   sslContext.getSecureRandom(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            clntWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            clntWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            box = cipher.newCipher(protocolVersion, svrWriteKey, svrWriteIV,
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   695
                                   sslContext.getSecureRandom(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            svrWriteKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            svrWriteIV = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Create a new read MAC and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    MAC newReadMAC() throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        MacAlg macAlg = cipherSuite.macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        MAC mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            mac = macAlg.newMac(protocolVersion, svrMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            svrMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            mac = macAlg.newMac(protocolVersion, clntMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            clntMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Create a new write MAC and return it to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    MAC newWriteMAC() throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        MacAlg macAlg = cipherSuite.macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        MAC mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            mac = macAlg.newMac(protocolVersion, clntMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            clntMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            mac = macAlg.newMac(protocolVersion, svrMacSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            svrMacSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Returns true iff the handshake sequence is done, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * this freshly created session can become the current one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    boolean isDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return state == HandshakeMessage.ht_finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * Returns the session which was created through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * handshake sequence ... should be called after isDone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    SSLSessionImpl getSession() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   753
     * Set the handshake session
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   754
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   755
    void setHandshakeSessionSE(SSLSessionImpl handshakeSession) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   756
        if (conn != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   757
            conn.setHandshakeSession(handshakeSession);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   758
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   759
            engine.setHandshakeSession(handshakeSession);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   760
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   761
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   762
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   763
    /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   764
     * Returns true if renegotiation is in use for this connection.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   765
     */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   766
    boolean isSecureRenegotiation() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   767
        return secureRenegotiation;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   768
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   769
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   770
    /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   771
     * Returns the verify_data from the Finished message sent by the client.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   772
     */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   773
    byte[] getClientVerifyData() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   774
        return clientVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   775
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   776
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   777
    /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   778
     * Returns the verify_data from the Finished message sent by the server.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   779
     */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   780
    byte[] getServerVerifyData() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   781
        return serverVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   782
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   783
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   784
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * This routine is fed SSL handshake records when they become available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * and processes messages found therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    void process_record(InputRecord r, boolean expectingFinished)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        checkThrown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         * Store the incoming handshake data, then see if we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * now process any completed handshake messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        input.incomingRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         * We don't need to create a separate delegatable task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * for finished messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if ((conn != null) || expectingFinished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            processLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            delegateTask(new PrivilegedExceptionAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                public Void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    processLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * On input, we hash messages one at a time since servers may need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * to access an intermediate hash to validate a CertificateVerify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Note that many handshake messages can come in one record (and often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * do, to reduce network resource utilization), and one message can also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * require multiple records (e.g. very large Certificate messages).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    void processLoop() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   826
        // need to read off 4 bytes at least to get the handshake
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   827
        // message type and length.
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 715
diff changeset
   828
        while (input.available() >= 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            byte messageType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            int messageLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
             * See if we can read the handshake message header, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
             * then the entire handshake message.  If not, wait till
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
             * we can read and process an entire message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            input.mark(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            messageType = (byte)input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            messageLen = input.getInt24();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (input.available() < messageLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                input.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
             * Process the messsage.  We require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
             * that processMessage() consumes the entire message.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
             * lieu of explicit error checks (how?!) we assume that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
             * data will look like garbage on encoding/processing errors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
             * and that other protocol code will detect such errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
             * Note that digesting is normally deferred till after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
             * message has been processed, though to process at least the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
             * client's Finished message (i.e. send the server's) we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
             * to acccelerate that digesting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
             * Also, note that hello request messages are never hashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
             * that includes the hello request header, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            if (messageType == HandshakeMessage.ht_hello_request) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                input.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                processMessage(messageType, messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                input.ignore(4 + messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                input.mark(messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                processMessage(messageType, messageLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                input.digestNow();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   876
     * Returns true iff the handshaker has been activated.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   877
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   878
     * In activated state, the handshaker may not send any messages out.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   879
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   880
    boolean activated() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   881
        return state >= -1;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   882
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   883
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   884
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Returns true iff the handshaker has sent any messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    boolean started() {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   888
        return state >= 0;  // 0: HandshakeMessage.ht_hello_request
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   889
                            // 1: HandshakeMessage.ht_client_hello
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * Used to kickstart the negotiation ... either writing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * ClientHello or a HelloRequest as appropriate, whichever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * the subclass returns.  NOP if handshaking's already started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    void kickstart() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (state >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   902
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        HandshakeMessage m = getKickstartMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            m.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        m.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        state = m.messageType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Both client and server modes can start handshaking; but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * message they send to do so is different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    abstract HandshakeMessage getKickstartMessage() throws SSLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Client and Server side protocols are each driven though this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * call, which processes a single message and drives the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * side of the protocol state machine (depending on the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    abstract void processMessage(byte messageType, int messageLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Most alerts in the protocol relate to handshaking problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Alerts are detected as the connection reads data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    abstract void handshakeAlert(byte description) throws SSLProtocolException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * Sends a change cipher spec message and updates the write side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * cipher state so that future messages use the just-negotiated spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    void sendChangeCipherSpec(Finished mesg, boolean lastMessage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        output.flush(); // i.e. handshake data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * The write cipher state is protected by the connection write lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         * so we must grab it while making the change. We also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         * make sure no writes occur between sending the ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         * message, installing the new cipher state, and sending the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         * Finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
         * We already hold SSLEngine/SSLSocket "this" by virtue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * of this being called from the readRecord code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        OutputRecord r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            r = new OutputRecord(Record.ct_change_cipher_spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            r = new EngineOutputRecord(Record.ct_change_cipher_spec, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        r.write(1);     // single byte of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        if (conn != null) {
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   964
            conn.writeLock.lock();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   965
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                conn.writeRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                conn.changeWriteCiphers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                mesg.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                output.flush();
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   973
            } finally {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   974
                conn.writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            synchronized (engine.writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                engine.writeRecord((EngineOutputRecord)r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                engine.changeWriteCiphers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                mesg.write(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                if (lastMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    output.setFinishedMsg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Single access point to key calculation logic.  Given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * pre-master secret and the nonces from client and server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * produce all the keying material to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    void calculateKeys(SecretKey preMasterSecret, ProtocolVersion version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        SecretKey master = calculateMasterSecret(preMasterSecret, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        session.setMasterSecret(master);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        calculateConnectionKeys(master);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * Calculate the master secret from its various components.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * used for key exchange by all cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * The master secret is the catenation of three MD5 hashes, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * consisting of the pre-master secret and a SHA1 hash.  Those three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * SHA1 hashes are of (different) constant strings, the pre-master
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * secret, and the nonces provided by the client and the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    private SecretKey calculateMasterSecret(SecretKey preMasterSecret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            ProtocolVersion requestedVersion) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1016
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (debug != null && Debug.isOn("keygen")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            HexDumpEncoder      dump = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            System.out.println("SESSION KEYGEN:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            System.out.println("PreMaster Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            printHex(dump, preMasterSecret.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            // Nonces are dumped with connection keygen, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            // benefit to doing it twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1029
        // What algs/params do we need to use?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1030
        String masterAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1031
        PRF prf;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1032
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1033
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1034
            masterAlg = "SunTls12MasterSecret";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1035
            prf = cipherSuite.prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1036
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1037
            masterAlg = "SunTlsMasterSecret";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1038
            prf = P_NONE;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1039
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1040
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1041
        String prfHashAlg = prf.getPRFHashAlg();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1042
        int prfHashLength = prf.getPRFHashLength();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1043
        int prfBlockSize = prf.getPRFBlockSize();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1044
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1045
        TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1046
                preMasterSecret, protocolVersion.major, protocolVersion.minor,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1047
                clnt_random.random_bytes, svr_random.random_bytes,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1048
                prfHashAlg, prfHashLength, prfBlockSize);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1049
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        SecretKey masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1052
            KeyGenerator kg = JsseJce.getKeyGenerator(masterAlg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            kg.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            masterSecret = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            // For RSA premaster secrets, do not signal a protocol error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            // due to the Bleichenbacher attack. See comments further down.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1058
            if (!preMasterSecret.getAlgorithm().equals(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1059
                    "TlsRsaPremasterSecret")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1062
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                System.out.println("RSA master secret generation error:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                System.out.println("Generating new random premaster secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1068
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1069
            if (requestedVersion != null) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1070
                preMasterSecret =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1071
                    RSAClientKeyExchange.generateDummySecret(requestedVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1072
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1073
                preMasterSecret =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1074
                    RSAClientKeyExchange.generateDummySecret(protocolVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1075
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1076
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            // recursive call with new premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            return calculateMasterSecret(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1081
        // if no version check requested (client side handshake), or version
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1082
        // information is not available (not an RSA premaster secret),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        // return master secret immediately.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1084
        if ((requestedVersion == null) ||
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1085
                !(masterSecret instanceof TlsMasterSecret)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1088
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1089
        // we have checked the ClientKeyExchange message when reading TLS
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1090
        // record, the following check is necessary to ensure that
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1091
        // JCE provider does not ignore the checking, or the previous
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1092
        // checking process bypassed the premaster secret version checking.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        TlsMasterSecret tlsKey = (TlsMasterSecret)masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        int major = tlsKey.getMajorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        int minor = tlsKey.getMinorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        if ((major < 0) || (minor < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        // check if the premaster secret version is ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        // the specification says that it must be the maximum version supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        // by the client from its ClientHello message. However, many
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        // implementations send the negotiated version, so accept both
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1104
        // for SSL v3.0 and TLS v1.0.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1105
        // NOTE that we may be comparing two unsupported version numbers, which
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1106
        // is why we cannot use object reference equality in this special case.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1107
        ProtocolVersion premasterVersion =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1108
                                    ProtocolVersion.valueOf(major, minor);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1109
        boolean versionMismatch = (premasterVersion.v != requestedVersion.v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1111
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1112
         * we never checked the client_version in server side
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1113
         * for TLS v1.0 and SSL v3.0. For compatibility, we
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1114
         * maintain this behavior.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1115
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1116
        if (versionMismatch && requestedVersion.v <= ProtocolVersion.TLS10.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1117
            versionMismatch = (premasterVersion.v != protocolVersion.v);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1118
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (versionMismatch == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            // check passed, return key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        // Due to the Bleichenbacher attack, do not signal a protocol error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        // Generate a random premaster secret and continue with the handshake,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        // which will fail when verifying the finished messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        // For more information, see comments in PreMasterSecret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            System.out.println("RSA PreMasterSecret version error: expected"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                + protocolVersion + " or " + requestedVersion + ", decrypted: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                + premasterVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            System.out.println("Generating new random premaster secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1135
        preMasterSecret =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1136
            RSAClientKeyExchange.generateDummySecret(requestedVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1137
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        // recursive call with new premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        return calculateMasterSecret(preMasterSecret, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Calculate the keys needed for this connection, once the session's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * master secret has been calculated.  Uses the master key and nonces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * the amount of keying material generated is a function of the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * suite that's been negotiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * This gets called both on the "full handshake" (where we exchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * a premaster secret and started a new session) as well as on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * "fast handshake" (where we just resumed a pre-existing session).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    void calculateConnectionKeys(SecretKey masterKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         * For both the read and write sides of the protocol, we use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * master to generate MAC secrets and cipher keying material.  Block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         * ciphers need initialization vectors, which we also generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
         * First we figure out how much keying material is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        int hashSize = cipherSuite.macAlg.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        boolean is_exportable = cipherSuite.exportable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        BulkCipher cipher = cipherSuite.cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        int expandedKeySize = is_exportable ? cipher.expandedKeySize : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1165
        // Which algs/params do we need to use?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1166
        String keyMaterialAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1167
        PRF prf;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1168
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1169
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1170
            keyMaterialAlg = "SunTls12KeyMaterial";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1171
            prf = cipherSuite.prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1172
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1173
            keyMaterialAlg = "SunTlsKeyMaterial";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1174
            prf = P_NONE;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1175
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1176
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1177
        String prfHashAlg = prf.getPRFHashAlg();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1178
        int prfHashLength = prf.getPRFHashLength();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1179
        int prfBlockSize = prf.getPRFBlockSize();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1180
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1181
        TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1182
            masterKey, protocolVersion.major, protocolVersion.minor,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            clnt_random.random_bytes, svr_random.random_bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            cipher.algorithm, cipher.keySize, expandedKeySize,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1185
            cipher.ivSize, hashSize,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1186
            prfHashAlg, prfHashLength, prfBlockSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1189
            KeyGenerator kg = JsseJce.getKeyGenerator(keyMaterialAlg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            kg.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            TlsKeyMaterialSpec keySpec = (TlsKeyMaterialSpec)kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            clntWriteKey = keySpec.getClientCipherKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            svrWriteKey = keySpec.getServerCipherKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1196
            // Return null if IVs are not supposed to be generated.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1197
            // e.g. TLS 1.1+.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            clntWriteIV = keySpec.getClientIv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            svrWriteIV = keySpec.getServerIv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            clntMacSecret = keySpec.getClientMacKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            svrMacSecret = keySpec.getServerMacKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        // Dump the connection keys as they're generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        if (debug != null && Debug.isOn("keygen")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            synchronized (System.out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                HexDumpEncoder  dump = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                System.out.println("CONNECTION KEYGEN:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                // Inputs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                System.out.println("Client Nonce:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                printHex(dump, clnt_random.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                System.out.println("Server Nonce:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                printHex(dump, svr_random.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                System.out.println("Master Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                printHex(dump, masterKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                // Outputs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                System.out.println("Client MAC write Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                printHex(dump, clntMacSecret.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                System.out.println("Server MAC write Secret:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                printHex(dump, svrMacSecret.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                if (clntWriteKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    System.out.println("Client write key:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    printHex(dump, clntWriteKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    System.out.println("Server write key:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    printHex(dump, svrWriteKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    System.out.println("... no encryption keys used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                if (clntWriteIV != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    System.out.println("Client write IV:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                    printHex(dump, clntWriteIV.getIV());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    System.out.println("Server write IV:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    printHex(dump, svrWriteIV.getIV());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                } else {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1245
                    if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1246
                        System.out.println(
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1247
                                "... no IV derived for this protocol");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1248
                    } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1249
                        System.out.println("... no IV used for this cipher");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1250
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    private static void printHex(HexDumpEncoder dump, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            System.out.println("(key bytes not available)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                dump.encodeBuffer(bytes, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                // just for debugging, ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * Throw an SSLException with the specified message and cause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Shorthand until a new SSLException constructor is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * This method never returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    static void throwSSLException(String msg, Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        SSLException e = new SSLException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        e.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * Implement a simple task delegator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * We are currently implementing this as a single delegator, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * try for parallel tasks later.  Client Authentication could
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * benefit from this, where ClientKeyExchange/CertificateVerify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * could be carried out in parallel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    class DelegatedTask<E> implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        private PrivilegedExceptionAction<E> pea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        DelegatedTask(PrivilegedExceptionAction<E> pea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            this.pea = pea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            synchronized (engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                    AccessController.doPrivileged(pea, engine.getAcc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    thrown = pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                } catch (RuntimeException rte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                    thrown = rte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                delegatedTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                taskDelegated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    private <T> void delegateTask(PrivilegedExceptionAction<T> pea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        delegatedTask = new DelegatedTask<T>(pea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        taskDelegated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        thrown = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    DelegatedTask getTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (!taskDelegated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            taskDelegated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            return delegatedTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * See if there are any tasks which need to be delegated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * Locked by SSLEngine.this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    boolean taskOutstanding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        return (delegatedTask != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * The previous caller failed for some reason, report back the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * Exception.  We won't worry about Error's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * Locked by SSLEngine.this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    void checkThrown() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        synchronized (thrownLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (thrown != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                String msg = thrown.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    msg = "Delegated task threw Exception/Error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                 * See what the underlying type of exception is.  We should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                 * throw the same thing.  Chain thrown to the new exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                Exception e = thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                thrown = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    throw (RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        new RuntimeException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                } else if (e instanceof SSLHandshakeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    throw (SSLHandshakeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                        new SSLHandshakeException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                } else if (e instanceof SSLKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    throw (SSLKeyException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                        new SSLKeyException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                } else if (e instanceof SSLPeerUnverifiedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    throw (SSLPeerUnverifiedException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                        new SSLPeerUnverifiedException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                } else if (e instanceof SSLProtocolException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    throw (SSLProtocolException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                        new SSLProtocolException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                     * If it's SSLException or any other Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                     * we'll wrap it in an SSLException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    throw (SSLException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                        new SSLException(msg).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
}