src/java.base/share/classes/sun/security/ssl/SSLConfiguration.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
child 56702 75527e40bdfd
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.security.AccessControlContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.security.AccessController;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.security.AlgorithmConstraints;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    32
import java.security.PrivilegedActionException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import java.security.PrivilegedExceptionAction;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import java.util.ArrayList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import java.util.Collection;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import java.util.Collections;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
import java.util.HashMap;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import java.util.List;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
import java.util.function.BiFunction;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
import javax.crypto.KeyGenerator;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
import javax.net.ssl.HandshakeCompletedListener;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
import javax.net.ssl.SNIMatcher;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
import javax.net.ssl.SNIServerName;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
import javax.net.ssl.SSLEngine;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
import javax.net.ssl.SSLParameters;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
import javax.net.ssl.SSLSocket;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
import sun.security.ssl.SSLExtension.ClientExtensions;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
import sun.security.ssl.SSLExtension.ServerExtensions;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
/**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
 * SSL/(D)TLS configuration.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
class SSLConfiguration implements Cloneable {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
    // configurations with SSLParameters
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
    AlgorithmConstraints        algorithmConstraints;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
    List<ProtocolVersion>       enabledProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
    List<CipherSuite>           enabledCipherSuites;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
    ClientAuthType              clientAuthType;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
    String                      identificationProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
    List<SNIServerName>         serverNames;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
    Collection<SNIMatcher>      sniMatchers;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
    String[]                    applicationProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
    boolean                     preferLocalCipherSuites;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    64
    boolean                     enableRetransmissions;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
    int                         maximumPacketSize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
    // the maximum protocol version of enabled protocols
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
    ProtocolVersion             maximumProtocolVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
    // Configurations per SSLSocket or SSLEngine instance.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
    boolean                     isClientMode;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
    boolean                     enableSessionCreation;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
    // the application layer protocol negotiation configuration
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
    BiFunction<SSLSocket, List<String>, String> socketAPSelector;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
    BiFunction<SSLEngine, List<String>, String> engineAPSelector;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
    HashMap<HandshakeCompletedListener, AccessControlContext>
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
                                handshakeListeners;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
    boolean                     noSniExtension;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    82
    boolean                     noSniMatcher;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
    // To switch off the extended_master_secret extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
    static final boolean useExtendedMasterSecret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    86
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    87
    // Allow session resumption without Extended Master Secret extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
    static final boolean allowLegacyResumption =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
        Utilities.getBooleanProperty("jdk.tls.allowLegacyResumption", true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    90
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    91
    // Allow full handshake without Extended Master Secret extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
    static final boolean allowLegacyMasterSecret =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
        Utilities.getBooleanProperty("jdk.tls.allowLegacyMasterSecret", true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
    // Allow full handshake without Extended Master Secret extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
    static final boolean useCompatibilityMode = Utilities.getBooleanProperty(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
            "jdk.tls.client.useCompatibilityMode", true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
// TODO: Please remove after TLS 1.3 draft interop testing
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
// delete me
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
static int tls13VN = 0x0304;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
    // Is the extended_master_secret extension supported?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
    static {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
        boolean supportExtendedMasterSecret = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
            KeyGenerator kg =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   108
                JsseJce.getKeyGenerator("SunTlsExtendedMasterSecret");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
        } catch (NoSuchAlgorithmException nae) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
            supportExtendedMasterSecret = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
        if (supportExtendedMasterSecret) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
            useExtendedMasterSecret = Utilities.getBooleanProperty(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
                    "jdk.tls.useExtendedMasterSecret", true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   116
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   117
            useExtendedMasterSecret = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
// delete me
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
    tls13VN =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
        AccessController.doPrivileged(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
            new PrivilegedExceptionAction<Integer>() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
                @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
                public Integer run() throws Exception {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
                    return Integer.parseInt(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
                        System.getProperty("jdk.tls13.version", "0304"), 16);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
            });
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   131
} catch (PrivilegedActionException ex) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
    // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
}
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
    SSLConfiguration(SSLContextImpl sslContext, boolean isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   137
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
        // Configurations with SSLParameters, default values.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
        this.algorithmConstraints = SSLAlgorithmConstraints.DEFAULT;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
        this.enabledProtocols =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
                sslContext.getDefaultProtocolVersions(!isClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
        this.enabledCipherSuites =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
                sslContext.getDefaultCipherSuites(!isClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
        this.clientAuthType = ClientAuthType.CLIENT_AUTH_NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   146
        this.identificationProtocol = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
        this.serverNames = Collections.<SNIServerName>emptyList();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   148
        this.sniMatchers = Collections.<SNIMatcher>emptyList();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
        this.preferLocalCipherSuites = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
        this.applicationProtocols = new String[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   152
        this.enableRetransmissions = sslContext.isDTLS();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   153
        this.maximumPacketSize = 0;         // please reset it explicitly later
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   154
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
        this.maximumProtocolVersion = ProtocolVersion.NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   156
        for (ProtocolVersion pv : enabledProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   157
            if (pv.compareTo(maximumProtocolVersion) > 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
                this.maximumProtocolVersion = pv;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   159
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
        // Configurations per SSLSocket or SSLEngine instance.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
        this.isClientMode = isClientMode;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
        this.enableSessionCreation = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
        this.socketAPSelector = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
        this.engineAPSelector = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
        this.handshakeListeners = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
        this.noSniExtension = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
        this.noSniMatcher = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
    SSLParameters getSSLParameters() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
        SSLParameters params = new SSLParameters();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
        params.setAlgorithmConstraints(this.algorithmConstraints);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
        params.setProtocols(ProtocolVersion.toStringArray(enabledProtocols));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
        params.setCipherSuites(CipherSuite.namesOf(enabledCipherSuites));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
        switch (this.clientAuthType) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
            case CLIENT_AUTH_REQUIRED:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
                params.setNeedClientAuth(true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
                break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
            case CLIENT_AUTH_REQUESTED:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
                params.setWantClientAuth(true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
                break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
            default:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
                params.setWantClientAuth(false);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
        params.setEndpointIdentificationAlgorithm(this.identificationProtocol);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   191
        if (serverNames.isEmpty() && !noSniExtension) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   192
            // 'null' indicates none has been set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   193
            params.setServerNames(null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
            params.setServerNames(this.serverNames);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
        if (sniMatchers.isEmpty() && !noSniMatcher) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
            // 'null' indicates none has been set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
            params.setSNIMatchers(null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
            params.setSNIMatchers(this.sniMatchers);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
        params.setApplicationProtocols(this.applicationProtocols);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
        params.setUseCipherSuitesOrder(this.preferLocalCipherSuites);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
        params.setEnableRetransmissions(this.enableRetransmissions);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
        params.setMaximumPacketSize(this.maximumPacketSize);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
        return params;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
    void setSSLParameters(SSLParameters params) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
        AlgorithmConstraints ac = params.getAlgorithmConstraints();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
        if (ac != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
            this.algorithmConstraints = ac;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
        }   // otherwise, use the default value
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   219
        String[] sa = params.getCipherSuites();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
        if (sa != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
            this.enabledCipherSuites = CipherSuite.validValuesOf(sa);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   222
        }   // otherwise, use the default values
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   223
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
        sa = params.getProtocols();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   225
        if (sa != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
            this.enabledProtocols = ProtocolVersion.namesOf(sa);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
            this.maximumProtocolVersion = ProtocolVersion.NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
            for (ProtocolVersion pv : enabledProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
                if (pv.compareTo(maximumProtocolVersion) > 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   231
                    this.maximumProtocolVersion = pv;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   232
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   233
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   234
        }   // otherwise, use the default values
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
        if (params.getNeedClientAuth()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
            this.clientAuthType = ClientAuthType.CLIENT_AUTH_REQUIRED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   238
        } else if (params.getWantClientAuth()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
            this.clientAuthType = ClientAuthType.CLIENT_AUTH_REQUESTED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   240
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   241
            this.clientAuthType = ClientAuthType.CLIENT_AUTH_NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   242
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   243
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   244
        String s = params.getEndpointIdentificationAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   245
        if (s != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   246
            this.identificationProtocol = s;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   247
        }   // otherwise, use the default value
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   248
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   249
        List<SNIServerName> sniNames = params.getServerNames();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   250
        if (sniNames != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
            this.noSniExtension = sniNames.isEmpty();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
            this.serverNames = sniNames;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
        }   // null if none has been set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   254
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   255
        Collection<SNIMatcher> matchers = params.getSNIMatchers();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   256
        if (matchers != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
            this.noSniMatcher = matchers.isEmpty();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
            this.sniMatchers = matchers;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
        }   // null if none has been set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
        sa = params.getApplicationProtocols();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
        if (sa != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   263
            this.applicationProtocols = sa;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
        }   // otherwise, use the default values
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
        this.preferLocalCipherSuites = params.getUseCipherSuitesOrder();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
        this.enableRetransmissions = params.getEnableRetransmissions();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
        this.maximumPacketSize = params.getMaximumPacketSize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
    // SSLSocket only
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
    void addHandshakeCompletedListener(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
            HandshakeCompletedListener listener) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
        if (handshakeListeners == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
            handshakeListeners = new HashMap<>(4);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
        handshakeListeners.put(listener, AccessController.getContext());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
    // SSLSocket only
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
    void removeHandshakeCompletedListener(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
            HandshakeCompletedListener listener) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
        if (handshakeListeners == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
            throw new IllegalArgumentException("no listeners");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
        if (handshakeListeners.remove(listener) == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
            throw new IllegalArgumentException("listener not registered");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
        if (handshakeListeners.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
            handshakeListeners = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
     * Return true if the extension is available.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
    boolean isAvailable(SSLExtension extension) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
        for (ProtocolVersion protocolVersion : enabledProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
            if (extension.isAvailable(protocolVersion)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
                if (isClientMode ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
                        ClientExtensions.defaults.contains(extension) :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
                        ServerExtensions.defaults.contains(extension)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
                    return true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
        return false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
     * Return true if the extension is available for the specific protocol.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
    boolean isAvailable(SSLExtension extension,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
            ProtocolVersion protocolVersion) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
        return extension.isAvailable(protocolVersion) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
                (isClientMode ? ClientExtensions.defaults.contains(extension) :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
                                ServerExtensions.defaults.contains(extension));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
     * Get the enabled extensions for the specific handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
     * Used to consume handshake extensions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
    SSLExtension[] getEnabledExtensions(SSLHandshake handshakeType) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
        List<SSLExtension> extensions = new ArrayList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
        for (SSLExtension extension : SSLExtension.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
            if (extension.handshakeType == handshakeType) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
                if (isAvailable(extension)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
                    extensions.add(extension);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
        return extensions.toArray(new SSLExtension[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
     * Get the enabled extensions for the specific handshake message
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
     * and the specific protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
     * Used to produce handshake extensions after handshake protocol
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
     * version negotiation.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
    SSLExtension[] getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
            SSLHandshake handshakeType, ProtocolVersion protocolVersion) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
        List<SSLExtension> extensions = new ArrayList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
        for (SSLExtension extension : SSLExtension.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
            if (extension.handshakeType == handshakeType) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
                if (isAvailable(extension) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
                        extension.isAvailable(protocolVersion)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
                    extensions.add(extension);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
        return extensions.toArray(new SSLExtension[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
     * Get the enabled extensions for the specific handshake message
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
     * and the specific protocol versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
     * Used to produce ClientHello extensions before handshake protocol
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
     * version negotiation.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
    SSLExtension[] getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
            SSLHandshake handshakeType, List<ProtocolVersion> activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
        List<SSLExtension> extensions = new ArrayList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
        for (SSLExtension extension : SSLExtension.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
            if (extension.handshakeType == handshakeType) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
                if (!isAvailable(extension)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
                for (ProtocolVersion protocolVersion : activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
                    if (extension.isAvailable(protocolVersion)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
                        extensions.add(extension);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
                        break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
        return extensions.toArray(new SSLExtension[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
    @SuppressWarnings({"unchecked", "CloneDeclaresCloneNotSupported"})
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
    public Object clone() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
        // Note that only references to the configurations are copied.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
            SSLConfiguration config = (SSLConfiguration)super.clone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
            if (handshakeListeners != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
                config.handshakeListeners =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
                    (HashMap<HandshakeCompletedListener, AccessControlContext>)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
                            handshakeListeners.clone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
            return config;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
        } catch (CloneNotSupportedException cnse) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
            // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
        return null;    // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
}