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