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