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