jdk/src/java.base/share/classes/sun/security/ssl/ServerHandshaker.java
author asmotrak
Mon, 02 Mar 2015 12:56:22 -0800
changeset 31689 1201792aa3a3
parent 31538 0981099a3e54
child 31695 4d10942c9a7b
permissions -rw-r--r--
8043201: Deprecate RC4 in SunJSSE provider Reviewed-by: xuelei, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
     2
 * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.ECParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
    41
import sun.security.util.KeyUtil;
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
    42
import sun.security.util.LegacyAlgorithmConstraints;
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
    43
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.ssl.HandshakeMessage.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.ssl.CipherSuite.*;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    46
import sun.security.ssl.SignatureAndHashAlgorithm.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import static sun.security.ssl.CipherSuite.KeyExchange.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ServerHandshaker does the protocol handshaking from the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * of view of a server.  It is driven asychronously by handshake messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * as delivered by the parent Handshaker class, and also uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * common functionality (e.g. key generation) that is provided there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
final class ServerHandshaker extends Handshaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // is the server going to require the client to authenticate?
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
    60
    private ClientAuthType      doClientAuth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // our authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private X509Certificate[]   certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private PrivateKey          privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
17189
9f2ae085280b 8005523: Unbound krb5 for TLS
weijun
parents: 16100
diff changeset
    66
    private Object              serviceCreds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // flag to check for clientCertificateVerify message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean             needClientVerify = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * For exportable ciphersuites using non-exportable key sizes, we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * ephemeral RSA keys. We could also do anonymous RSA in the same way
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * but there are no such ciphersuites currently defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private PrivateKey          tempPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private PublicKey           tempPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * For anonymous and ephemeral Diffie-Hellman key exchange, we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * ephemeral Diffie-Hellman keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private DHCrypt dh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Helper for ECDH based key exchanges
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private ECDHCrypt ecdh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // version request by the client in its ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // we remember it for the RSA premaster secret version check
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private ProtocolVersion clientRequestedVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private SupportedEllipticCurvesExtension supportedCurves;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    94
    // the preferable signature algorithm used by ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    95
    SignatureAndHashAlgorithm preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    96
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
    97
    // Flag to use smart ephemeral DH key which size matches the corresponding
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
    98
    // authentication key
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
    99
    private static final boolean useSmartEphemeralDHKeys;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   100
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   101
    // Flag to use legacy ephemeral DH key which size is 512 bits for
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   102
    // exportable cipher suites, and 768 bits for others
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   103
    private static final boolean useLegacyEphemeralDHKeys;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   104
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   105
    // The customized ephemeral DH key size for non-exportable cipher suites.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   106
    private static final int customizedDHKeySize;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   107
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
   108
    // legacy algorithm constraints
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
   109
    private static final AlgorithmConstraints legacyAlgorithmConstraints =
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
   110
            new LegacyAlgorithmConstraints(
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
   111
                    LegacyAlgorithmConstraints.PROPERTY_TLS_LEGACY_ALGS,
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
   112
                    new SSLAlgorithmDecomposer());
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
   113
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   114
    static {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   115
        String property = AccessController.doPrivileged(
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   116
                    new GetPropertyAction("jdk.tls.ephemeralDHKeySize"));
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   117
        if (property == null || property.length() == 0) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   118
            useLegacyEphemeralDHKeys = false;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   119
            useSmartEphemeralDHKeys = false;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   120
            customizedDHKeySize = -1;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   121
        } else if ("matched".equals(property)) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   122
            useLegacyEphemeralDHKeys = false;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   123
            useSmartEphemeralDHKeys = true;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   124
            customizedDHKeySize = -1;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   125
        } else if ("legacy".equals(property)) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   126
            useLegacyEphemeralDHKeys = true;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   127
            useSmartEphemeralDHKeys = false;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   128
            customizedDHKeySize = -1;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   129
        } else {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   130
            useLegacyEphemeralDHKeys = false;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   131
            useSmartEphemeralDHKeys = false;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   132
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   133
            try {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   134
                customizedDHKeySize = Integer.parseUnsignedInt(property);
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   135
                if (customizedDHKeySize < 1024 || customizedDHKeySize > 2048) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   136
                    throw new IllegalArgumentException(
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   137
                        "Customized DH key size should be positive integer " +
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   138
                        "between 1024 and 2048 bits, inclusive");
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   139
                }
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   140
            } catch (NumberFormatException nfe) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   141
                throw new IllegalArgumentException(
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   142
                        "Invalid system property jdk.tls.ephemeralDHKeySize");
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   143
            }
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   144
        }
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   145
    }
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
   146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Constructor ... use the keys found in the auth context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    ServerHandshaker(SSLSocketImpl socket, SSLContextImpl context,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   151
            ProtocolList enabledProtocols, ClientAuthType clientAuth,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   152
            ProtocolVersion activeProtocolVersion, boolean isInitialHandshake,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   153
            boolean secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   154
            byte[] clientVerifyData, byte[] serverVerifyData) {
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        super(socket, context, enabledProtocols,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   157
                (clientAuth != ClientAuthType.CLIENT_AUTH_NONE), false,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   158
                activeProtocolVersion, isInitialHandshake, secureRenegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   159
                clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        doClientAuth = clientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Constructor ... use the keys found in the auth context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    ServerHandshaker(SSLEngineImpl engine, SSLContextImpl context,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   167
            ProtocolList enabledProtocols, ClientAuthType clientAuth,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   168
            ProtocolVersion activeProtocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   169
            boolean isInitialHandshake, boolean secureRenegotiation,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   170
            byte[] clientVerifyData, byte[] serverVerifyData,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   171
            boolean isDTLS) {
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        super(engine, context, enabledProtocols,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   174
                (clientAuth != ClientAuthType.CLIENT_AUTH_NONE), false,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   175
                activeProtocolVersion, isInitialHandshake, secureRenegotiation,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   176
                clientVerifyData, serverVerifyData, isDTLS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        doClientAuth = clientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * whether client authentication is required.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   185
    void setClientAuth(ClientAuthType clientAuth) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        doClientAuth = clientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * This routine handles all the server side handshake messages, one at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * a time.  Given the message type (and in some cases the pending cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * spec) it parses the type-specific message.  Then it calls a function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * that handles that specific message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * It updates the state machine as each message is processed, and writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * responses as needed using the connection in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   198
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    void processMessage(byte type, int message_len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   201
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   202
        // check the handshake state
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   203
        handshakeState.check(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            case HandshakeMessage.ht_client_hello:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   207
                ClientHello ch = new ClientHello(input, message_len, isDTLS);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   208
                handshakeState.update(ch, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                 * send it off for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                this.clientHello(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            case HandshakeMessage.ht_certificate:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   217
                if (doClientAuth == ClientAuthType.CLIENT_AUTH_NONE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    fatalSE(Alerts.alert_unexpected_message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                "client sent unsolicited cert chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   222
                CertificateMsg certificateMsg = new CertificateMsg(input);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   223
                handshakeState.update(certificateMsg, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   224
                this.clientCertificate(certificateMsg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            case HandshakeMessage.ht_client_key_exchange:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                SecretKey preMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                case K_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                case K_RSA_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                     * The client's pre-master secret is decrypted using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                     * either the server's normal private RSA key, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                     * temporary one used for non-export or signing-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                     * certificates/keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                     */
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   238
                    RSAClientKeyExchange pms = new RSAClientKeyExchange(
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   239
                            protocolVersion, clientRequestedVersion,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   240
                            sslContext.getSecureRandom(), input,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   241
                            message_len, privateKey);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   242
                    handshakeState.update(pms, resumingSession);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    preMasterSecret = this.clientKeyExchange(pms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                     * The pre-master secret is derived using the normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                     * Diffie-Hellman calculation.   Note that the main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                     * protocol difference in these five flavors is in how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                     * the ServerKeyExchange message was constructed!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                     */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   254
                    DHClientKeyExchange dhcke = new DHClientKeyExchange(input);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   255
                    handshakeState.update(dhcke, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   256
                    preMasterSecret = this.clientKeyExchange(dhcke);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                case K_ECDH_ANON:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   263
                    ECDHClientKeyExchange ecdhcke =
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   264
                                    new ECDHClientKeyExchange(input);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   265
                    handshakeState.update(ecdhcke, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   266
                    preMasterSecret = this.clientKeyExchange(ecdhcke);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                default:
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   269
                    ClientKeyExchangeService p =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   270
                            ClientKeyExchangeService.find(keyExchange.name);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   271
                    if (p == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   272
                        throw new SSLProtocolException
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   273
                                ("Unrecognized key exchange: " + keyExchange);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   274
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   275
                    byte[] encodedTicket = input.getBytes16();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   276
                    input.getBytes16();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   277
                    byte[] secret = input.getBytes16();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   278
                    ClientKeyExchange cke = p.createServerExchange(protocolVersion,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   279
                            clientRequestedVersion,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   280
                            sslContext.getSecureRandom(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   281
                            encodedTicket,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   282
                            secret,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   283
                            this.getAccSE(), serviceCreds);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   284
                    handshakeState.update(cke, resumingSession);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   285
                    preMasterSecret = this.clientKeyExchange(cke);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   286
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                // All keys are calculated from the premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                // and the exchanged nonces in the same way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                calculateKeys(preMasterSecret, clientRequestedVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            case HandshakeMessage.ht_certificate_verify:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   297
                CertificateVerify cvm =
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   298
                        new CertificateVerify(input,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   299
                            localSupportedSignAlgs, protocolVersion);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   300
                handshakeState.update(cvm, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   301
                this.clientCertificateVerify(cvm);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   302
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            case HandshakeMessage.ht_finished:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   306
                Finished cfm =
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   307
                    new Finished(protocolVersion, input, cipherSuite);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   308
                handshakeState.update(cfm, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   309
                this.clientFinished(cfm);
28550
003089aca6b9 8057555: Less cryptic cipher suite management
ascarpino
parents: 28059
diff changeset
   310
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        "Illegal server handshake msg, " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * ClientHello presents the server with a bunch of options, to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * server replies with a ServerHello listing the ones which this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * will use.  If needed, it also writes its Certificate plus in some cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * a ServerKeyExchange message.  It may also write a CertificateRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * to elicit a client certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * All these messages are terminated by a ServerHelloDone message.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * most cases, all this can be sent in a single Record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void clientHello(ClientHello mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   335
18283
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   336
        // Reject client initiated renegotiation?
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   337
        //
18570
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   338
        // If server side should reject client-initiated renegotiation,
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   339
        // send an alert_handshake_failure fatal alert, not a no_renegotiation
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   340
        // warning alert (no_renegotiation must be a warning: RFC 2246).
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   341
        // no_renegotiation might seem more natural at first, but warnings
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   342
        // are not appropriate because the sending party does not know how
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   343
        // the receiving party will behave.  This state must be treated as
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   344
        // a fatal server condition.
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   345
        //
9078dc28a187 8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents: 18283
diff changeset
   346
        // This will not have any impact on server initiated renegotiation.
18283
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   347
        if (rejectClientInitiatedRenego && !isInitialHandshake &&
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   348
                !serverHelloRequested) {
18283
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   349
            fatalSE(Alerts.alert_handshake_failure,
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   350
                "Client initiated renegotiation is not allowed");
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   351
        }
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   352
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   353
        // check the server name indication if required
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   354
        ServerNameExtension clientHelloSNIExt = (ServerNameExtension)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   355
                    mesg.extensions.get(ExtensionType.EXT_SERVER_NAME);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   356
        if (!sniMatchers.isEmpty()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   357
            // we do not reject client without SNI extension
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   358
            if (clientHelloSNIExt != null &&
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   359
                        !clientHelloSNIExt.isMatched(sniMatchers)) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   360
                fatalSE(Alerts.alert_unrecognized_name,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   361
                    "Unrecognized server name indication");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   362
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   363
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   364
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   365
        // Does the message include security renegotiation indication?
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   366
        boolean renegotiationIndicated = false;
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   367
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   368
        // check the TLS_EMPTY_RENEGOTIATION_INFO_SCSV
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   369
        CipherSuiteList cipherSuites = mesg.getCipherSuites();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   370
        if (cipherSuites.contains(CipherSuite.C_SCSV)) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   371
            renegotiationIndicated = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   372
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   373
                secureRenegotiation = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   374
            } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   375
                // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   376
                if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   377
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   378
                        "The SCSV is present in a secure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   379
                } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   380
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   381
                        "The SCSV is present in a insecure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   382
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   383
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   384
        }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   385
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   386
        // check the "renegotiation_info" extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   387
        RenegotiationInfoExtension clientHelloRI = (RenegotiationInfoExtension)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   388
                    mesg.extensions.get(ExtensionType.EXT_RENEGOTIATION_INFO);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   389
        if (clientHelloRI != null) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   390
            renegotiationIndicated = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   391
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   392
                // verify the length of the "renegotiated_connection" field
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   393
                if (!clientHelloRI.isEmpty()) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   394
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   395
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   396
                        "The renegotiation_info field is not empty");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   397
                }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   398
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   399
                secureRenegotiation = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   400
            } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   401
                if (!secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   402
                    // unexpected RI extension for insecure renegotiation,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   403
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   404
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   405
                        "The renegotiation_info is present in a insecure " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   406
                        "renegotiation");
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   407
                }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   408
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   409
                // verify the client_verify_data value
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   410
                if (!Arrays.equals(clientVerifyData,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   411
                                clientHelloRI.getRenegotiatedConnection())) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   412
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   413
                        "Incorrect verify data in ClientHello " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   414
                        "renegotiation_info message");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   415
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   416
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   417
        } else if (!isInitialHandshake && secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   418
           // if the connection's "secure_renegotiation" flag is set to TRUE
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   419
           // and the "renegotiation_info" extension is not present, abort
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   420
           // the handshake.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   421
            fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   422
                        "Inconsistent secure renegotiation indication");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   423
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   424
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   425
        // if there is no security renegotiation indication or the previous
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   426
        // handshake is insecure.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   427
        if (!renegotiationIndicated || !secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   428
            if (isInitialHandshake) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   429
                if (!allowLegacyHelloMessages) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   430
                    // abort the handshake with a fatal handshake_failure alert
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   431
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   432
                        "Failed to negotiate the use of secure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   433
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   434
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   435
                // continue with legacy ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   436
                if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   437
                    System.out.println("Warning: No renegotiation " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   438
                        "indication in ClientHello, allow legacy ClientHello");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   439
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   440
            } else if (!allowUnsafeRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   441
                // abort the handshake
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   442
                if (activeProtocolVersion.useTLS10PlusSpec()) {
18283
f842a42076b9 7188658: Add possibility to disable client initiated renegotiation
xuelei
parents: 17189
diff changeset
   443
                    // respond with a no_renegotiation warning
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   444
                    warningSE(Alerts.alert_no_renegotiation);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   445
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   446
                    // invalidate the handshake so that the caller can
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   447
                    // dispose this object.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   448
                    invalidated = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   449
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   450
                    // If there is still unread block in the handshake
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   451
                    // input stream, it would be truncated with the disposal
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   452
                    // and the next handshake message will become incomplete.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   453
                    //
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   454
                    // However, according to SSL/TLS specifications, no more
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   455
                    // handshake message could immediately follow ClientHello
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   456
                    // or HelloRequest. But in case of any improper messages,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   457
                    // we'd better check to ensure there is no remaining bytes
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   458
                    // in the handshake input stream.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   459
                    if (input.available() > 0) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   460
                        fatalSE(Alerts.alert_unexpected_message,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   461
                            "ClientHello followed by an unexpected  " +
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   462
                            "handshake message");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   463
                    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   464
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   465
                    return;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   466
                } else {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   467
                    // For SSLv3, send the handshake_failure fatal error.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   468
                    // Note that SSLv3 does not define a no_renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   469
                    // alert like TLSv1. However we cannot ignore the message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   470
                    // simply, otherwise the other side was waiting for a
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   471
                    // response that would never come.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   472
                    fatalSE(Alerts.alert_handshake_failure,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   473
                        "Renegotiation is not allowed");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   474
                }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   475
            } else {   // !isInitialHandshake && allowUnsafeRenegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   476
                // continue with unsafe renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   477
                if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   478
                    System.out.println(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   479
                            "Warning: continue with insecure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   480
                }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   481
            }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   482
        }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 4236
diff changeset
   483
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   484
        // check the "max_fragment_length" extension
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   485
        MaxFragmentLengthExtension maxFragLenExt = (MaxFragmentLengthExtension)
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   486
                    mesg.extensions.get(ExtensionType.EXT_MAX_FRAGMENT_LENGTH);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   487
        if ((maxFragLenExt != null) && (maximumPacketSize != 0)) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   488
            // Not yet consider the impact of IV/MAC/padding.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   489
            int estimatedMaxFragSize = maximumPacketSize;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   490
            if (isDTLS) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   491
                estimatedMaxFragSize -= DTLSRecord.headerSize;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   492
            } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   493
                estimatedMaxFragSize -= SSLRecord.headerSize;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   494
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   495
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   496
            if (maxFragLenExt.getMaxFragLen() > estimatedMaxFragSize) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   497
                // For better interoperability, abort the maximum fragment
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   498
                // length negotiation, rather than terminate the connection
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   499
                // with a fatal alert.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   500
                maxFragLenExt = null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   501
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   502
                // fatalSE(Alerts.alert_illegal_parameter,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   503
                //         "Not an allowed max_fragment_length value");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   504
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   505
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   506
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   507
        // cookie exchange
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   508
        if (isDTLS) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   509
             HelloCookieManager hcMgr = sslContext.getHelloCookieManager();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   510
             if ((mesg.cookie == null) || (mesg.cookie.length == 0) ||
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   511
                    (!hcMgr.isValid(mesg))) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   512
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   513
                //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   514
                // Perform cookie exchange for DTLS handshaking if no cookie
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   515
                // or the cookie is invalid in the ClientHello message.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   516
                //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   517
                HelloVerifyRequest m0 = new HelloVerifyRequest(hcMgr, mesg);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   518
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   519
                if (debug != null && Debug.isOn("handshake")) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   520
                    m0.print(System.out);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   521
                }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   522
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   523
                m0.write(output);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   524
                handshakeState.update(m0, resumingSession);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   525
                output.flush();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   526
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   527
                return;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   528
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   529
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * FIRST, construct the ServerHello using the options and priorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * from the ClientHello.  Update the (pending) cipher spec as we do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * so, and save the client's version to protect against rollback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * There are a bunch of minor tasks here, and one major one: deciding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         * if the short or the full handshake sequence will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        ServerHello m1 = new ServerHello();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        clientRequestedVersion = mesg.protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   544
        // select a proper protocol version.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   545
        ProtocolVersion selectedVersion =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   546
               selectProtocolVersion(clientRequestedVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   547
        if (selectedVersion == null ||
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   548
                selectedVersion.v == ProtocolVersion.SSL20Hello.v) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                "Client requested protocol " + clientRequestedVersion +
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   551
                " not enabled or not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   553
7804
c59149ba3780 6996367: improve HandshakeHash
weijun
parents: 7043
diff changeset
   554
        handshakeHash.protocolDetermined(selectedVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        setVersion(selectedVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        m1.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // random ... save client and server values for later use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        // in computing the master secret (from pre-master secret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        // and thence the other crypto keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // NOTE:  this use of three inputs to generating _each_ set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // of ciphers slows things down, but it does increase the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        // security since each connection in the session can hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        // its own authenticated (and strong) keys.  One could make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // creation of a session a rare thing...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        clnt_random = mesg.clnt_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        svr_random = new RandomCookie(sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        m1.svr_random = svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        session = null; // forget about the current session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        // Here we go down either of two paths:  (a) the fast one, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // the client's asked to rejoin an existing session, and the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // permits this; (b) the other one, where a new session is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (mesg.sessionId.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            // client is trying to resume a session, let's see...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            SSLSessionImpl previous = ((SSLSessionContextImpl)sslContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        .engineGetServerSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        .get(mesg.sessionId.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // Check if we can use the fast path, resuming a session.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            // can do so iff we have a valid record for that session, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // the cipher suite for that session was on the list which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            // client requested, and if we're not forgetting any needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            // authentication on the part of the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (previous != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                resumingSession = previous.isRejoinable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    ProtocolVersion oldVersion = previous.getProtocolVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    // cannot resume session with different version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    if (oldVersion != protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        resumingSession = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   604
                // cannot resume session with different server name indication
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   605
                if (resumingSession) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   606
                    List<SNIServerName> oldServerNames =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   607
                            previous.getRequestedServerNames();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   608
                    if (clientHelloSNIExt != null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   609
                        if (!clientHelloSNIExt.isIdentical(oldServerNames)) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   610
                            resumingSession = false;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   611
                        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   612
                    } else if (!oldServerNames.isEmpty()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   613
                        resumingSession = false;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   614
                    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   615
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   616
                    if (!resumingSession &&
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   617
                            debug != null && Debug.isOn("handshake")) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   618
                        System.out.println(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   619
                            "The requested server name indication " +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   620
                            "is not identical to the previous one");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   621
                    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   622
                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   623
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if (resumingSession &&
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   625
                        (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        previous.getPeerPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    } catch (SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        resumingSession = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                // validate subject identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    CipherSuite suite = previous.getSuite();
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   636
                    ClientKeyExchangeService p =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   637
                            ClientKeyExchangeService.find(suite.keyExchange.name);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   638
                    if (p != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        Principal localPrincipal = previous.getLocalPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   641
                        if (p.isRelated(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   642
                                false, getAccSE(), localPrincipal)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   643
                            if (debug != null && Debug.isOn("session"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   644
                                System.out.println("Subject can" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   645
                                        " provide creds for princ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            resumingSession = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            if (debug != null && Debug.isOn("session"))
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   649
                                System.out.println("Subject cannot" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   650
                                        " provide creds for princ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    CipherSuite suite = previous.getSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    // verify that the ciphersuite from the cached session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    // is in the list of client requested ciphersuites and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    // we have it enabled
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   660
                    if ((isNegotiable(suite) == false) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                            (mesg.getCipherSuites().contains(suite) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        resumingSession = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                        // everything looks ok, set the ciphersuite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        // this should be done last when we are sure we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        // will resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        setCipherSuite(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    session = previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    if (debug != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        (Debug.isOn("handshake") || Debug.isOn("session"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        System.out.println("%% Resuming " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   679
        }   // else client did not try to resume
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        // If client hasn't specified a session we can resume, start a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // new one and choose its cipher suite and compression options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        // Unless new session creation is disabled for this connection!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if (!enableNewSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                throw new SSLException("Client did not resume a session");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   690
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   691
            supportedCurves = (SupportedEllipticCurvesExtension)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   692
                        mesg.extensions.get(ExtensionType.EXT_ELLIPTIC_CURVES);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   693
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   694
            // We only need to handle the "signature_algorithm" extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   695
            // for full handshakes and TLS 1.2 or later.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   696
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   697
                SignatureAlgorithmsExtension signAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   698
                    (SignatureAlgorithmsExtension)mesg.extensions.get(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   699
                                    ExtensionType.EXT_SIGNATURE_ALGORITHMS);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   700
                if (signAlgs != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   701
                    Collection<SignatureAndHashAlgorithm> peerSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   702
                                            signAlgs.getSignAlgorithms();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   703
                    if (peerSignAlgs == null || peerSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   704
                        throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   705
                            "No peer supported signature algorithms");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   706
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   707
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   708
                    Collection<SignatureAndHashAlgorithm>
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   709
                        supportedPeerSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   710
                            SignatureAndHashAlgorithm.getSupportedAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   711
                                                            peerSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   712
                    if (supportedPeerSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   713
                        throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   714
                            "No supported signature and hash algorithm " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   715
                            "in common");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   716
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   717
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   718
                    setPeerSupportedSignAlgs(supportedPeerSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   719
                } // else, need to use peer implicit supported signature algs
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   720
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   721
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   722
            session = new SSLSessionImpl(protocolVersion, CipherSuite.C_NULL,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   723
                        getLocalSupportedSignAlgs(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   724
                        sslContext.getSecureRandom(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   725
                        getHostAddressSE(), getPortSE());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   726
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   727
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   728
                if (peerSupportedSignAlgs != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   729
                    session.setPeerSupportedSignatureAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   730
                            peerSupportedSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   731
                }   // else, we will set the implicit peer supported signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   732
                    // algorithms in chooseCipherSuite()
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   733
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   734
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   735
            // set the server name indication in the session
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   736
            List<SNIServerName> clientHelloSNI =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   737
                    Collections.<SNIServerName>emptyList();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   738
            if (clientHelloSNIExt != null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   739
                clientHelloSNI = clientHelloSNIExt.getServerNames();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   740
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   741
            session.setRequestedServerNames(clientHelloSNI);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   742
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   743
            // set the handshake session
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   744
            setHandshakeSessionSE(session);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   745
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   746
            // choose cipher suite and corresponding private key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            chooseCipherSuite(mesg);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   748
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   749
            session.setSuite(cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            session.setLocalPrivateKey(privateKey);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   751
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            // chooseCompression(mesg);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   753
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   754
            // set the negotiated maximum fragment in the session
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   755
            //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   756
            // The protocol version and cipher suite have been negotiated
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   757
            // in previous processes.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   758
            if (maxFragLenExt != null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   759
                int maxFragLen = maxFragLenExt.getMaxFragLen();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   760
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   761
                // More check of the requested "max_fragment_length" extension.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   762
                if (maximumPacketSize != 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   763
                    int estimatedMaxFragSize = cipherSuite.calculatePacketSize(
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   764
                            maxFragLen, protocolVersion, isDTLS);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   765
                    if (estimatedMaxFragSize > maximumPacketSize) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   766
                        // For better interoperability, abort the maximum
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   767
                        // fragment length negotiation, rather than terminate
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   768
                        // the connection with a fatal alert.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   769
                        maxFragLenExt = null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   770
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   771
                        // fatalSE(Alerts.alert_illegal_parameter,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   772
                        //         "Not an allowed max_fragment_length value");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   773
                    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   774
                }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   775
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   776
                if (maxFragLenExt != null) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   777
                    session.setNegotiatedMaxFragSize(maxFragLen);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   778
                }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   779
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   780
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   781
            session.setMaximumPacketSize(maximumPacketSize);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   782
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   783
            // set the handshake session
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   784
            setHandshakeSessionSE(session);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   785
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   786
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   787
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   788
            handshakeHash.setFinishedAlg(cipherSuite.prfAlg.getPRFHashAlg());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        m1.cipherSuite = cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        m1.sessionId = session.getSessionId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        m1.compression_method = session.getCompression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   795
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   796
            // For ServerHellos that are initial handshakes, then the
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   797
            // "renegotiated_connection" field in "renegotiation_info"
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   798
            // extension is of zero length.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   799
            //
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   800
            // For ServerHellos that are renegotiating, this field contains
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   801
            // the concatenation of client_verify_data and server_verify_data.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   802
            //
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   803
            // Note that for initial handshakes, both the clientVerifyData
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   804
            // variable and serverVerifyData variable are of zero length.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   805
            HelloExtension serverHelloRI = new RenegotiationInfoExtension(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   806
                                        clientVerifyData, serverVerifyData);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   807
            m1.extensions.add(serverHelloRI);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   808
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   809
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   810
        if (!sniMatchers.isEmpty() && clientHelloSNIExt != null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   811
            // When resuming a session, the server MUST NOT include a
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   812
            // server_name extension in the server hello.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   813
            if (!resumingSession) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   814
                ServerNameExtension serverHelloSNI = new ServerNameExtension();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   815
                m1.extensions.add(serverHelloSNI);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   816
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   817
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 12874
diff changeset
   818
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   819
        if ((maxFragLenExt != null) && !resumingSession) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   820
            // When resuming a session, the server MUST NOT include a
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   821
            // max_fragment_length extension in the server hello.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   822
            //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   823
            // Otherwise, use the same value as the requested extension.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   824
            m1.extensions.add(maxFragLenExt);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   825
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   826
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            m1.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            System.out.println("Cipher suite:  " + session.getSuite());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        m1.write(output);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   832
        handshakeState.update(m1, resumingSession);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // If we are resuming a session, we finish writing handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        // messages right now and then finish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            calculateConnectionKeys(session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            sendChangeCipherAndFinish(false);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   841
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   842
            // expecting the final ChangeCipherSpec and Finished messages
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   843
            expectingFinishFlightSE();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   844
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * SECOND, write the server Certificate(s) if we need to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * NOTE:  while an "anonymous RSA" mode is explicitly allowed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * the protocol, we can't support it since all of the SSL flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * defined in the protocol spec are explicitly stated to require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * using RSA certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         */
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   857
        if (ClientKeyExchangeService.find(cipherSuite.keyExchange.name) != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   858
            // No external key exchange provider needs a cert now.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        } else if ((keyExchange != K_DH_ANON) && (keyExchange != K_ECDH_ANON)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                throw new RuntimeException("no certificates");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            CertificateMsg m2 = new CertificateMsg(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
             * Set local certs in the SSLSession, output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
             * debug info, and then actually write to the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            session.setLocalCertificates(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                m2.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            m2.write(output);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   875
            handshakeState.update(m2, resumingSession);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // XXX has some side effects with OS TCP buffering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            // leave it out for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            // let client verify chain in the meantime...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            // output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                throw new RuntimeException("anonymous keyexchange with certs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * THIRD, the ServerKeyExchange message ... iff it's needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * It's usually needed unless there's an encryption-capable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * RSA cert, or a D-H cert.  The notable exception is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * exportable ciphers used with big RSA keys need to downgrade
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * to use short RSA keys, even when the key/cert encrypts OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        ServerKeyExchange m3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        case K_RSA:
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   900
            // no server key exchange for RSA ciphersuites
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            m3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        case K_RSA_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            if (JsseJce.getRSAKeyLength(certs[0].getPublicKey()) > 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    m3 = new RSA_ServerKeyExchange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                        tempPublicKey, privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        clnt_random, svr_random,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    privateKey = tempPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    m3 = null; // make compiler happy
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   913
                    throw new SSLException(
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   914
                            "Error generating RSA server key exchange", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                // RSA_EXPORT with short key, don't need ServerKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                m3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        case K_DHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        case K_DHE_DSS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                m3 = new DH_ServerKeyExchange(dh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    clnt_random.random_bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    svr_random.random_bytes,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   928
                    sslContext.getSecureRandom(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   929
                    preferableSignatureAlgorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   930
                    protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                m3 = null; // make compiler happy
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   933
                throw new SSLException(
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   934
                        "Error generating DH server key exchange", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        case K_DH_ANON:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   938
            m3 = new DH_ServerKeyExchange(dh, protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                m3 = new ECDH_ServerKeyExchange(ecdh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    clnt_random.random_bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    svr_random.random_bytes,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   948
                    sslContext.getSecureRandom(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   949
                    preferableSignatureAlgorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   950
                    protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                m3 = null; // make compiler happy
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   953
                throw new SSLException(
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   954
                        "Error generating ECDH server key exchange", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            // ServerKeyExchange not used for fixed ECDH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            m3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        default:
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   963
            ClientKeyExchangeService p =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   964
                    ClientKeyExchangeService.find(keyExchange.name);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   965
            if (p != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   966
                // No external key exchange provider needs a cert now.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   967
                m3 = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   968
                break;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   969
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            throw new RuntimeException("internal error: " + keyExchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if (m3 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                m3.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            m3.write(output);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   977
            handshakeState.update(m3, resumingSession);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // FOURTH, the CertificateRequest message.  The details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // the message can be affected by the key exchange algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        // in use.  For example, certs with fixed Diffie-Hellman keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        // are only useful with the DH_DSS and DH_RSA key exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        // algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        // Needed only if server requires client to authenticate self.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        // Illegal for anonymous flavors, so we need to check that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        //
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   990
        // No external key exchange provider needs a cert now.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   991
        if (doClientAuth != ClientAuthType.CLIENT_AUTH_NONE &&
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   992
                keyExchange != K_DH_ANON && keyExchange != K_ECDH_ANON &&
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
   993
                ClientKeyExchangeService.find(keyExchange.name) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            CertificateRequest m4;
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30905
diff changeset
   996
            X509Certificate[] caCerts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   998
            Collection<SignatureAndHashAlgorithm> localSignAlgs = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
   999
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1000
                // We currently use all local upported signature and hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1001
                // algorithms. However, to minimize the computation cost
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1002
                // of requested hash algorithms, we may use a restricted
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1003
                // set of signature algorithms in the future.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1004
                localSignAlgs = getLocalSupportedSignAlgs();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1005
                if (localSignAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1006
                    throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1007
                            "No supported signature algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1008
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1009
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1010
                Set<String> localHashAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1011
                    SignatureAndHashAlgorithm.getHashAlgorithmNames(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1012
                        localSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1013
                if (localHashAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1014
                    throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1015
                            "No supported signature algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1016
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1017
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1018
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            caCerts = sslContext.getX509TrustManager().getAcceptedIssuers();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1020
            m4 = new CertificateRequest(caCerts, keyExchange,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1021
                                            localSignAlgs, protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                m4.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            m4.write(output);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1027
            handshakeState.update(m4, resumingSession);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
         * FIFTH, say ServerHelloDone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        ServerHelloDone m5 = new ServerHelloDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            m5.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        m5.write(output);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1039
        handshakeState.update(m5, resumingSession);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         * Flush any buffered messages so the client will see them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * Ideally, all the messages above go in a single network level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         * message to the client.  Without big Certificate chains, it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
         * going to be the common case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Choose cipher suite from among those supported by client. Sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * the cipherSuite and keyExchange variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    private void chooseCipherSuite(ClientHello mesg) throws IOException {
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1055
        CipherSuiteList prefered;
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1056
        CipherSuiteList proposed;
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1057
        if (preferLocalCipherSuites) {
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1058
            prefered = getActiveCipherSuites();
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1059
            proposed = mesg.getCipherSuites();
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1060
        } else {
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1061
            prefered = mesg.getCipherSuites();
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1062
            proposed = getActiveCipherSuites();
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1063
        }
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1064
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1065
        List<CipherSuite> legacySuites = new ArrayList<>();
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1066
        for (CipherSuite suite : prefered.collection()) {
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1067
            if (isNegotiable(proposed, suite) == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            }
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1070
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1071
            if (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1072
                if ((suite.keyExchange == K_DH_ANON) ||
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1073
                    (suite.keyExchange == K_ECDH_ANON)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            }
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1077
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1078
            if (!legacyAlgorithmConstraints.permits(null, suite.name, null)) {
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1079
                legacySuites.add(suite);
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1080
                continue;
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1081
            }
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1082
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            if (trySetCipherSuite(suite) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1088
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1089
        for (CipherSuite suite : legacySuites) {
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1090
            if (trySetCipherSuite(suite)) {
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1091
                return;
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1092
            }
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1093
        }
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 31538
diff changeset
  1094
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 18570
diff changeset
  1095
        fatalSE(Alerts.alert_handshake_failure, "no cipher suites in common");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Set the given CipherSuite, if possible. Return the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * The call succeeds if the CipherSuite is available and we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * the necessary certificates to complete the handshake. We don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * check if the CipherSuite is actually enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * If successful, this method also generates ephemeral keys if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * required for this ciphersuite. This may take some time, so this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * method should only be called if you really want to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1109
     * This method is called from chooseCipherSuite() in this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    boolean trySetCipherSuite(CipherSuite suite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
         * If we're resuming a session we know we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
         * support this key exchange algorithm and in fact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
         * have already cached the result of it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
         * the session state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1122
        if (suite.isNegotiable() == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1126
        // must not negotiate the obsoleted weak cipher suites.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1127
        if (protocolVersion.obsoletes(suite)) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1128
            return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1129
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1130
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1131
        // must not negotiate unsupported cipher suites.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1132
        if (!protocolVersion.supports(suite)) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1133
            return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1134
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        KeyExchange keyExchange = suite.keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        // null out any existing references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        dh = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        tempPrivateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        tempPublicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1145
        Collection<SignatureAndHashAlgorithm> supportedSignAlgs = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1146
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1147
            if (peerSupportedSignAlgs != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1148
                supportedSignAlgs = peerSupportedSignAlgs;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1149
            } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1150
                SignatureAndHashAlgorithm algorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1151
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1152
                // we may optimize the performance
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1153
                switch (keyExchange) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1154
                    // If the negotiated key exchange algorithm is one of
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1155
                    // (RSA, DHE_RSA, DH_RSA, RSA_PSK, ECDH_RSA, ECDHE_RSA),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1156
                    // behave as if client had sent the value {sha1,rsa}.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1157
                    case K_RSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1158
                    case K_DHE_RSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1159
                    case K_DH_RSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1160
                    // case K_RSA_PSK:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1161
                    case K_ECDH_RSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1162
                    case K_ECDHE_RSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1163
                        algorithm = SignatureAndHashAlgorithm.valueOf(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1164
                                HashAlgorithm.SHA1.value,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1165
                                SignatureAlgorithm.RSA.value, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1166
                        break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1167
                    // If the negotiated key exchange algorithm is one of
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1168
                    // (DHE_DSS, DH_DSS), behave as if the client had
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1169
                    // sent the value {sha1,dsa}.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1170
                    case K_DHE_DSS:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1171
                    case K_DH_DSS:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1172
                        algorithm = SignatureAndHashAlgorithm.valueOf(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1173
                                HashAlgorithm.SHA1.value,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1174
                                SignatureAlgorithm.DSA.value, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1175
                        break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1176
                    // If the negotiated key exchange algorithm is one of
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1177
                    // (ECDH_ECDSA, ECDHE_ECDSA), behave as if the client
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1178
                    // had sent value {sha1,ecdsa}.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1179
                    case K_ECDH_ECDSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1180
                    case K_ECDHE_ECDSA:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1181
                        algorithm = SignatureAndHashAlgorithm.valueOf(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1182
                                HashAlgorithm.SHA1.value,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1183
                                SignatureAlgorithm.ECDSA.value, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1184
                        break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1185
                    default:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1186
                        // no peer supported signature algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1187
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1188
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1189
                if (algorithm == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1190
                    supportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1191
                        Collections.<SignatureAndHashAlgorithm>emptySet();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1192
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1193
                    supportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1194
                        new ArrayList<SignatureAndHashAlgorithm>(1);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1195
                    supportedSignAlgs.add(algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1196
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1197
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1198
                // Sets the peer supported signature algorithm to use in KM
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1199
                // temporarily.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1200
                session.setPeerSupportedSignatureAlgorithms(supportedSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1201
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1202
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1203
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        switch (keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        case K_RSA:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1206
            // need RSA certs for authentication
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1207
            if (setupPrivateKeyAndChain("RSA") == false) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1208
                return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1209
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1210
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        case K_RSA_EXPORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            // need RSA certs for authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            if (setupPrivateKeyAndChain("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1217
            try {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1218
               if (JsseJce.getRSAKeyLength(certs[0].getPublicKey()) > 512) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1219
                    if (!setupEphemeralRSAKeys(suite.exportable)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1220
                        return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1221
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1222
               }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1223
            } catch (RuntimeException e) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1224
                // could not determine keylength, ignore key
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1225
                return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1226
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1227
            break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1228
        case K_DHE_RSA:
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1229
            // need RSA certs for authentication
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1230
            if (setupPrivateKeyAndChain("RSA") == false) {
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1231
                return false;
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1232
            }
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1233
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1234
            // get preferable peer signature algorithm for server key exchange
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1235
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1236
                preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1237
                    SignatureAndHashAlgorithm.getPreferableAlgorithm(
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1238
                                        supportedSignAlgs, "RSA", privateKey);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1239
                if (preferableSignatureAlgorithm == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1242
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1243
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1244
            setupEphemeralDHKeys(suite.exportable, privateKey);
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1245
            break;
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1246
        case K_ECDHE_RSA:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1247
            // need RSA certs for authentication
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1248
            if (setupPrivateKeyAndChain("RSA") == false) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1249
                return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1250
            }
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1251
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1252
            // get preferable peer signature algorithm for server key exchange
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1253
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1254
                preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1255
                    SignatureAndHashAlgorithm.getPreferableAlgorithm(
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 9499
diff changeset
  1256
                                        supportedSignAlgs, "RSA", privateKey);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1257
                if (preferableSignatureAlgorithm == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1260
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1261
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1262
            if (setupEphemeralECDHKeys() == false) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1263
                return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1264
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        case K_DHE_DSS:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1267
            // get preferable peer signature algorithm for server key exchange
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1268
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1269
                preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1270
                    SignatureAndHashAlgorithm.getPreferableAlgorithm(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1271
                                                supportedSignAlgs, "DSA");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1272
                if (preferableSignatureAlgorithm == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1273
                    return false;
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
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1276
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            // need DSS certs for authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            if (setupPrivateKeyAndChain("DSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            }
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1281
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1282
            setupEphemeralDHKeys(suite.exportable, privateKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        case K_ECDHE_ECDSA:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1285
            // get preferable peer signature algorithm for server key exchange
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1286
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1287
                preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1288
                    SignatureAndHashAlgorithm.getPreferableAlgorithm(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1289
                                            supportedSignAlgs, "ECDSA");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1290
                if (preferableSignatureAlgorithm == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1291
                    return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1292
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1293
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            // need EC cert signed using EC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            if (setupPrivateKeyAndChain("EC_EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            if (setupEphemeralECDHKeys() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            // need EC cert signed using RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            if (setupPrivateKeyAndChain("EC_RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            setupStaticECDHKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            // need EC cert signed using EC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            if (setupPrivateKeyAndChain("EC_EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            setupStaticECDHKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        case K_DH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            // no certs needed for anonymous
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1319
            setupEphemeralDHKeys(suite.exportable, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            // no certs needed for anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            if (setupEphemeralECDHKeys() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        default:
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1328
            ClientKeyExchangeService p =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1329
                    ClientKeyExchangeService.find(keyExchange.name);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1330
            if (p == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1331
                // internal error, unknown key exchange
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1332
                throw new RuntimeException("Unrecognized cipherSuite: " + suite);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1333
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1334
            // need service creds
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1335
            if (serviceCreds == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1336
                AccessControlContext acc = getAccSE();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1337
                serviceCreds = p.getServiceCreds(acc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1338
                if (serviceCreds != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1339
                    if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1340
                        System.out.println("Using serviceCreds");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1341
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1342
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1343
                if (serviceCreds == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1344
                    return false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1345
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1346
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1347
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        setCipherSuite(suite);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1350
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1351
        // set the peer implicit supported signature algorithms
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1352
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1353
            if (peerSupportedSignAlgs == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1354
                setPeerSupportedSignAlgs(supportedSignAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1355
                // we had alreay update the session
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1356
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1357
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * Get some "ephemeral" RSA keys for this context. This means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * generating them if it's not already been done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * Note that we currently do not implement any ciphersuites that use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * strong ephemeral RSA. (We do not support the EXPORT1024 ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * and standard RSA ciphersuites prohibit ephemeral mode for some reason)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * This means that export is always true and 512 bit keys are generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    private boolean setupEphemeralRSAKeys(boolean export) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        KeyPair kp = sslContext.getEphemeralKeyManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                        getRSAKeyPair(export, sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        if (kp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            tempPublicKey = kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            tempPrivateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Acquire some "ephemeral" Diffie-Hellman  keys for this handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * We don't reuse these, for improved forward secrecy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     */
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1386
    private void setupEphemeralDHKeys(boolean export, Key key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        /*
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1388
         * 768 bits ephemeral DH private keys were used to be used in
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1389
         * ServerKeyExchange except that exportable ciphers max out at 512
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1390
         * bits modulus values. We still adhere to this behavior in legacy
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1391
         * mode (system property "jdk.tls.ephemeralDHKeySize" is defined
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1392
         * as "legacy").
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1393
         *
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1394
         * Old JDK (JDK 7 and previous) releases don't support DH keys bigger
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1395
         * than 1024 bits. We have to consider the compatibility requirement.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1396
         * 1024 bits DH key is always used for non-exportable cipher suites
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1397
         * in default mode (system property "jdk.tls.ephemeralDHKeySize"
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1398
         * is not defined).
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1399
         *
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1400
         * However, if applications want more stronger strength, setting
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1401
         * system property "jdk.tls.ephemeralDHKeySize" to "matched"
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1402
         * is a workaround to use ephemeral DH key which size matches the
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1403
         * corresponding authentication key. For example, if the public key
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1404
         * size of an authentication certificate is 2048 bits, then the
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1405
         * ephemeral DH key size should be 2048 bits accordingly unless
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1406
         * the cipher suite is exportable.  This key sizing scheme keeps
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1407
         * the cryptographic strength consistent between authentication
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1408
         * keys and key-exchange keys.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1409
         *
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1410
         * Applications may also want to customize the ephemeral DH key size
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1411
         * to a fixed length for non-exportable cipher suites. This can be
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1412
         * approached by setting system property "jdk.tls.ephemeralDHKeySize"
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1413
         * to a valid positive integer between 1024 and 2048 bits, inclusive.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1414
         *
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1415
         * Note that the minimum acceptable key size is 1024 bits except
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1416
         * exportable cipher suites or legacy mode.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1417
         *
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1418
         * Note that the maximum acceptable key size is 2048 bits because
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1419
         * DH keys bigger than 2048 are not always supported by underlying
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1420
         * JCE providers.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1421
         *
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1422
         * Note that per RFC 2246, the key size limit of DH is 512 bits for
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1423
         * exportable cipher suites.  Because of the weakness, exportable
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1424
         * cipher suites are deprecated since TLS v1.1 and they are not
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1425
         * enabled by default in Oracle provider. The legacy behavior is
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1426
         * reserved and 512 bits DH key is always used for exportable
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1427
         * cipher suites.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         */
20539
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1429
        int keySize = export ? 512 : 1024;           // default mode
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1430
        if (!export) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1431
            if (useLegacyEphemeralDHKeys) {          // legacy mode
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1432
                keySize = 768;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1433
            } else if (useSmartEphemeralDHKeys) {    // matched mode
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1434
                if (key != null) {
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1435
                    int ks = KeyUtil.getKeySize(key);
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1436
                    // Note that SunJCE provider only supports 2048 bits DH
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1437
                    // keys bigger than 1024.  Please DON'T use value other
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1438
                    // than 1024 and 2048 at present.  We may improve the
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1439
                    // underlying providers and key size here in the future.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1440
                    //
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1441
                    // keySize = ks <= 1024 ? 1024 : (ks >= 2048 ? 2048 : ks);
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1442
                    keySize = ks <= 1024 ? 1024 : 2048;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1443
                } // Otherwise, anonymous cipher suites, 1024-bit is used.
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1444
            } else if (customizedDHKeySize > 0) {    // customized mode
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1445
                keySize = customizedDHKeySize;
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1446
            }
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1447
        }
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1448
cbff16f695c6 6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents: 19823
diff changeset
  1449
        dh = new DHCrypt(keySize, sslContext.getSecureRandom());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    // Setup the ephemeral ECDH parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    // If we cannot continue because we do not support any of the curves that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    // the client requested, return false. Otherwise (all is well), return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    private boolean setupEphemeralECDHKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        if (supportedCurves != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            // if the client sent the supported curves extension, pick the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            // first one that we support;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            for (int curveId : supportedCurves.curveIds()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                if (SupportedEllipticCurvesExtension.isSupported(curveId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    index = curveId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                // no match found, cannot use this ciphersuite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            // pick our preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            index = SupportedEllipticCurvesExtension.DEFAULT.curveIds()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        String oid = SupportedEllipticCurvesExtension.getCurveOid(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        ecdh = new ECDHCrypt(oid, sslContext.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    private void setupStaticECDHKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        // don't need to check whether the curve is supported, already done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        // in setupPrivateKeyAndChain().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        ecdh = new ECDHCrypt(privateKey, certs[0].getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * Retrieve the server key and certificate for the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * from the KeyManager and set the instance variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * @return true if successful, false if not available or invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    private boolean setupPrivateKeyAndChain(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        X509ExtendedKeyManager km = sslContext.getX509KeyManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            alias = km.chooseServerAlias(algorithm, null, conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            alias = km.chooseEngineServerAlias(algorithm, null, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        PrivateKey tempPrivateKey = km.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        if (tempPrivateKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        X509Certificate[] tempCerts = km.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        if ((tempCerts == null) || (tempCerts.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        String keyAlgorithm = algorithm.split("_")[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        PublicKey publicKey = tempCerts[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        if ((tempPrivateKey.getAlgorithm().equals(keyAlgorithm) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                || (publicKey.getAlgorithm().equals(keyAlgorithm) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        // For ECC certs, check whether we support the EC domain parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        // If the client sent a SupportedEllipticCurves ClientHello extension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        // check against that too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        if (keyAlgorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            if (publicKey instanceof ECPublicKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            ECParameterSpec params = ((ECPublicKey)publicKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            int index = SupportedEllipticCurvesExtension.getCurveIndex(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            if (SupportedEllipticCurvesExtension.isSupported(index) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            if ((supportedCurves != null) && !supportedCurves.contains(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        this.privateKey = tempPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        this.certs = tempCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1537
    /*
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1538
     * Returns premaster secret for external key exchange services.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     */
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1540
    private SecretKey clientKeyExchange(ClientKeyExchange mesg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        // Record the principals involved in exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        session.setPeerPrincipal(mesg.getPeerPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        session.setLocalPrincipal(mesg.getLocalPrincipal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1551
        return mesg.clientKeyExchange();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * Diffie Hellman key exchange is used when the server presented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * D-H parameters in its certificate (signed using RSA or DSS/DSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * or else the server presented no certificate but sent D-H params
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * in a ServerKeyExchange message.  Use of D-H is specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * cipher suite chosen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * The message optionally contains the client's D-H public key (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * it wasn't not sent in a client certificate).  As always with D-H,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * if a client and a server have each other's D-H public keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * they use common algorithm parameters, they have a shared key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * that's derived via the D-H calculation.  That key becomes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * pre-master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    private SecretKey clientKeyExchange(DHClientKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 16071
diff changeset
  1574
        return dh.getAgreedSecret(mesg.getClientPublicKey(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    private SecretKey clientKeyExchange(ECDHClientKeyExchange mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        return ecdh.getAgreedSecret(mesg.getEncodedPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * Client wrote a message to verify the certificate it sent earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * Note that this certificate isn't involved in key exchange.  Client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * authentication messages are included in the checksums used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * validate the handshake (e.g. Finished messages).  Other than that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * the _exact_ identity of the client is less fundamental to protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * security than its role in selecting keys via the pre-master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    private void clientCertificateVerify(CertificateVerify mesg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1602
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1603
            SignatureAndHashAlgorithm signAlg =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1604
                mesg.getPreferableSignatureAlgorithm();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1605
            if (signAlg == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1606
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1607
                        "Illegal CertificateVerify message");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1608
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1609
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1610
            String hashAlg =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1611
                SignatureAndHashAlgorithm.getHashAlgorithmName(signAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1612
            if (hashAlg == null || hashAlg.length() == 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1613
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1614
                        "No supported hash algorithm");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1615
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1616
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1617
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            PublicKey publicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                session.getPeerCertificates()[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            boolean valid = mesg.verify(protocolVersion, handshakeHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                                        publicKey, session.getMasterSecret());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            if (valid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                fatalSE(Alerts.alert_bad_certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                            "certificate verify message signature error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            fatalSE(Alerts.alert_bad_certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                "certificate verify format error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        // reset the flag for clientCertificateVerify message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        needClientVerify = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * Client writes "finished" at the end of its handshake, after cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * spec is changed.   We verify it and then send ours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * When we're resuming a session, we'll have already sent our own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * Finished message so just the verification is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    private void clientFinished(Finished mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
         * Verify if client did send the certificate when client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
         * authentication was required, otherwise server should not proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
         */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1654
        if (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
           // get X500Principal of the end-entity certificate for X509-based
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 30904
diff changeset
  1656
           // ciphersuites, or Kerberos principal for Kerberos ciphersuites, etc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
           session.getPeerPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * Verify if client did send clientCertificateVerify message following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         * the client Certificate, otherwise server should not proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        if (needClientVerify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                        "client did not send certificate verify message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
         * Verify the client's message with the "before" digest of messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
         * and forget about continuing to use that digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1673
        boolean verified = mesg.verify(handshakeHash, Finished.CLIENT,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1674
            session.getMasterSecret());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        if (!verified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            fatalSE(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                        "client 'finished' message doesn't verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            // NOTREACHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1683
         * save client verify data for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1684
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1685
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1686
            clientVerifyData = mesg.getVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1687
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1688
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1689
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
         * OK, it verified.  If we're doing the full handshake, add that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
         * "Finished" message to the hash of handshake messages, then send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
         * the change_cipher_spec and Finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        if (!resumingSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            sendChangeCipherAndFinish(true);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1696
        } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1697
            handshakeFinished = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         * Update the session cache only after the handshake completed, else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         * we're open to an attack against a partially completed handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        session.setLastAccessedTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        if (!resumingSession && session.isRejoinable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            ((SSLSessionContextImpl)sslContext.engineGetServerSessionContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                .put(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    "%% Cached server session: " + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        } else if (!resumingSession &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                "%% Didn't cache non-resumable server session: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                + session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * Compute finished message with the "server" digest (and then forget
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * about that digest, it can't be used again).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    private void sendChangeCipherAndFinish(boolean finishedTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1727
        // Reload if this message has been reserved.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1728
        handshakeHash.reload();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        Finished mesg = new Finished(protocolVersion, handshakeHash,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1731
            Finished.SERVER, session.getMasterSecret(), cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
         * Send the change_cipher_spec record; then our Finished handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
         * message will be the last handshake message.  Flush, and now we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
         * are ready for application data!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        sendChangeCipherSpec(mesg, finishedTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1741
         * save server verify data for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1742
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1743
        if (secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1744
            serverVerifyData = mesg.getVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1745
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * Returns a HelloRequest message to kickstart renegotiations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1752
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    HandshakeMessage getKickstartMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        return new HelloRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * Fault detected during handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1761
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    void handshakeAlert(byte description) throws SSLProtocolException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        String message = Alerts.alertDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            System.out.println("SSL -- handshake alert:  "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         * It's ok to get a no_certificate alert from a client of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         * we *requested* authentication information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         * However, if we *required* it, then this is not acceptable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         * Anyone calling getPeerCertificates() on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         * session will get an SSLPeerUnverifiedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        if ((description == Alerts.alert_no_certificate) &&
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1780
                (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUESTED)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        throw new SSLProtocolException("handshake alert: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * RSA key exchange is normally used.  The client encrypts a "pre-master
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * secret" with the server's public key, from the Certificate (or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * ServerKeyExchange) message that was sent to it by the server.  That's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * decrypted using the private key before we get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1793
    private SecretKey clientKeyExchange(RSAClientKeyExchange mesg)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1794
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        return mesg.preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * Verify the certificate sent by the client. We'll only get one if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * sent a CertificateRequest to request client authentication. If we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * are in TLS mode, the client may send a message with no certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * to indicate it does not have an appropriate chain. (In SSLv3 mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * it would send a no certificate alert).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    private void clientCertificate(CertificateMsg mesg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        if (debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            mesg.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        X509Certificate[] peerCerts = mesg.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        if (peerCerts.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
             * If the client authentication is only *REQUESTED* (e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
             * not *REQUIRED*, this is an acceptable condition.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
             */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 28550
diff changeset
  1821
            if (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUESTED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                fatalSE(Alerts.alert_bad_certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    "null cert chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        // ask the trust manager to verify the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        X509TrustManager tm = sslContext.getX509TrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            // find out the types of client authentication used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            PublicKey key = peerCerts[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            String keyAlgorithm = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            String authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            if (keyAlgorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                authType = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            } else if (keyAlgorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                authType = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            } else if (keyAlgorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                authType = "EC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                // unknown public key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                authType = "UNKNOWN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            if (tm instanceof X509ExtendedTrustManager) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1849
                if (conn != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1850
                    ((X509ExtendedTrustManager)tm).checkClientTrusted(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1851
                        peerCerts.clone(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                        authType,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1853
                        conn);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1854
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1855
                    ((X509ExtendedTrustManager)tm).checkClientTrusted(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1856
                        peerCerts.clone(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1857
                        authType,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1858
                        engine);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1859
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1861
                // Unlikely to happen, because we have wrapped the old
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1862
                // X509TrustManager with the new X509ExtendedTrustManager.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1863
                throw new CertificateException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1864
                    "Improper X509TrustManager implementation");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            // This will throw an exception, so include the original error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            fatalSE(Alerts.alert_certificate_unknown, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        // set the flag for clientCertificateVerify message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        needClientVerify = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        session.setPeerCertificates(peerCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
}