src/java.base/share/classes/sun/security/ssl/CipherSuiteList.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/CipherSuiteList.java@b1b45177051b
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: 39563
diff changeset
     2
 * Copyright (c) 2002, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.SSLException;
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    33
import static sun.security.ssl.NamedGroupType.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A list of CipherSuites. Also maintains the lists of supported and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * default ciphersuites and supports I/O from handshake streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
final class CipherSuiteList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final Collection<CipherSuite> cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String[] suiteNames;
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    46
    private final EnumSet<NamedGroupType> groupsTypes =
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    47
            EnumSet.noneOf(NamedGroupType.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    49
    // for use by buildAvailableCache() and
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    50
    // Handshaker.getKickstartMessage() only
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    51
    CipherSuiteList(Collection<CipherSuite> cipherSuites) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.cipherSuites = cipherSuites;
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    53
        for (CipherSuite suite : cipherSuites) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    54
            updateGroupTypes(suite);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    55
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Create a CipherSuiteList with a single element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    CipherSuiteList(CipherSuite suite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        cipherSuites = new ArrayList<CipherSuite>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        cipherSuites.add(suite);
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    64
        updateGroupTypes(suite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Construct a CipherSuiteList from a array of names. We don't bother
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * to eliminate duplicates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @exception IllegalArgumentException if the array or any of its elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * is null or if the ciphersuite name is unrecognized or unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * using currently installed providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    CipherSuiteList(String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (names == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new IllegalArgumentException("CipherSuites may not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        cipherSuites = new ArrayList<CipherSuite>(names.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            String suiteName = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            CipherSuite suite = CipherSuite.valueOf(suiteName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (suite.isAvailable() == false) {
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 25859
diff changeset
    84
                throw new IllegalArgumentException("Cannot support "
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 25859
diff changeset
    85
                    + suiteName + " with currently installed providers");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            cipherSuites.add(suite);
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
    88
            updateGroupTypes(suite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Read a CipherSuiteList from a HandshakeInStream in V3 ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * format. Does not check if the listed ciphersuites are known or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    CipherSuiteList(HandshakeInStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        byte[] bytes = in.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if ((bytes.length & 1) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new SSLException("Invalid ClientHello message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        cipherSuites = new ArrayList<CipherSuite>(bytes.length >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < bytes.length; i += 2) {
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   104
            CipherSuite suite = CipherSuite.valueOf(bytes[i], bytes[i+1]);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   105
            cipherSuites.add(suite);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   106
            updateGroupTypes(suite);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   107
        }
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   108
    }
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   109
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   110
    // Please don't use this method except constructors.
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   111
    private void updateGroupTypes(CipherSuite cipherSuite) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   112
        if (cipherSuite.keyExchange != null && (!cipherSuite.exportable)) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   113
            NamedGroupType groupType = cipherSuite.keyExchange.groupType;
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   114
            if ((groupType != NAMED_GROUP_NONE) &&
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   115
                    (!groupsTypes.contains(groupType))) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   116
                groupsTypes.add(groupType);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   117
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Return whether this list contains the given CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    boolean contains(CipherSuite suite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return cipherSuites.contains(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   128
    // Return whether this list contains cipher suites of a named group type.
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   129
    boolean contains(NamedGroupType groupType) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 39563
diff changeset
   130
        return groupsTypes.contains(groupType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Return an Iterator for the CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    Iterator<CipherSuite> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return cipherSuites.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Return a reference to the internal Collection of CipherSuites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The Collection MUST NOT be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    Collection<CipherSuite> collection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Return the number of CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return cipherSuites.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Return an array with the names of the CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    synchronized String[] toStringArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (suiteNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            suiteNames = new String[cipherSuites.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                suiteNames[i++] = c.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return suiteNames.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 9275
diff changeset
   169
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return cipherSuites.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Write this list to an HandshakeOutStream in V3 ClientHello format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        byte[] suiteBytes = new byte[cipherSuites.size() * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            suiteBytes[i] = (byte)(c.id >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            suiteBytes[i+1] = (byte)c.id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        s.putBytes16(suiteBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}