jdk/src/java.base/share/classes/sun/security/ssl/CipherSuiteList.java
author xuelei
Wed, 23 Mar 2016 12:25:08 +0000
changeset 36661 044bf6a5474a
parent 34826 4bbdce2630f8
child 39563 1449ed425710
permissions -rw-r--r--
8149017: Delayed provider selection broken in RSA client key exchange Reviewed-by: coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A list of CipherSuites. Also maintains the lists of supported and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * default ciphersuites and supports I/O from handshake streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
final class CipherSuiteList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final Collection<CipherSuite> cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String[] suiteNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // flag indicating whether this list contains any ECC ciphersuites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // null if not yet checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private volatile Boolean containsEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    50
    // for use by buildAvailableCache() and
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    51
    // Handshaker.getKickstartMessage() only
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    52
    CipherSuiteList(Collection<CipherSuite> cipherSuites) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.cipherSuites = cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Create a CipherSuiteList with a single element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    CipherSuiteList(CipherSuite suite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        cipherSuites = new ArrayList<CipherSuite>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        cipherSuites.add(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Construct a CipherSuiteList from a array of names. We don't bother
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * to eliminate duplicates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @exception IllegalArgumentException if the array or any of its elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * is null or if the ciphersuite name is unrecognized or unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * using currently installed providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    CipherSuiteList(String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (names == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new IllegalArgumentException("CipherSuites may not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        cipherSuites = new ArrayList<CipherSuite>(names.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            String suiteName = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            CipherSuite suite = CipherSuite.valueOf(suiteName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (suite.isAvailable() == false) {
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 25859
diff changeset
    81
                throw new IllegalArgumentException("Cannot support "
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 25859
diff changeset
    82
                    + suiteName + " with currently installed providers");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            cipherSuites.add(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Read a CipherSuiteList from a HandshakeInStream in V3 ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * format. Does not check if the listed ciphersuites are known or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    CipherSuiteList(HandshakeInStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        byte[] bytes = in.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if ((bytes.length & 1) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new SSLException("Invalid ClientHello message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        cipherSuites = new ArrayList<CipherSuite>(bytes.length >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < bytes.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            cipherSuites.add(CipherSuite.valueOf(bytes[i], bytes[i+1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Return whether this list contains the given CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    boolean contains(CipherSuite suite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return cipherSuites.contains(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Return whether this list contains any ECC ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    boolean containsEC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (containsEC == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                switch (c.keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    containsEC = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            containsEC = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return containsEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Return an Iterator for the CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    Iterator<CipherSuite> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return cipherSuites.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Return a reference to the internal Collection of CipherSuites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The Collection MUST NOT be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    Collection<CipherSuite> collection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Return the number of CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return cipherSuites.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Return an array with the names of the CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    synchronized String[] toStringArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (suiteNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            suiteNames = new String[cipherSuites.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                suiteNames[i++] = c.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return suiteNames.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 9275
diff changeset
   168
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return cipherSuites.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Write this list to an HandshakeOutStream in V3 ClientHello format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        byte[] suiteBytes = new byte[cipherSuites.size() * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            suiteBytes[i] = (byte)(c.id >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            suiteBytes[i+1] = (byte)c.id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        s.putBytes16(suiteBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}