jdk/src/java.base/share/classes/sun/security/ssl/CipherSuiteList.java
author xuelei
Wed, 22 Apr 2015 05:09:54 +0000
changeset 31712 e4d5230193da
parent 25859 3317bb8137f4
child 34826 4bbdce2630f8
permissions -rw-r--r--
8076328: Enforce key exchange constraints Reviewed-by: wetmore, igerasim, ahgross, asmotrak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14664
diff changeset
     2
 * Copyright (c) 2002, 2012, 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
        // refresh available cache once if a CipherSuite is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // (maybe new JCE providers have been installed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        boolean refreshed = false;
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (refreshed == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    // clear the cache so that the isAvailable() call below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    // does a full check
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    clearAvailableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    refreshed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                // still missing?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (suite.isAvailable() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    throw new IllegalArgumentException("Cannot support "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        + suiteName + " with currently installed providers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            cipherSuites.add(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Read a CipherSuiteList from a HandshakeInStream in V3 ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * format. Does not check if the listed ciphersuites are known or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    CipherSuiteList(HandshakeInStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        byte[] bytes = in.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if ((bytes.length & 1) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new SSLException("Invalid ClientHello message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        cipherSuites = new ArrayList<CipherSuite>(bytes.length >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 0; i < bytes.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            cipherSuites.add(CipherSuite.valueOf(bytes[i], bytes[i+1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Return whether this list contains the given CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    boolean contains(CipherSuite suite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return cipherSuites.contains(suite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // Return whether this list contains any ECC ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    boolean containsEC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (containsEC == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                switch (c.keyExchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                case K_ECDH_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                case K_ECDH_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                case K_ECDHE_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                case K_ECDHE_RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                case K_ECDH_ANON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    containsEC = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            containsEC = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return containsEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Return an Iterator for the CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    Iterator<CipherSuite> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return cipherSuites.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Return a reference to the internal Collection of CipherSuites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The Collection MUST NOT be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    Collection<CipherSuite> collection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Return the number of CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return cipherSuites.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Return an array with the names of the CipherSuites in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    synchronized String[] toStringArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (suiteNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            suiteNames = new String[cipherSuites.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                suiteNames[i++] = c.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return suiteNames.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 9275
diff changeset
   180
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return cipherSuites.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Write this list to an HandshakeOutStream in V3 ClientHello format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        byte[] suiteBytes = new byte[cipherSuites.size() * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (CipherSuite c : cipherSuites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            suiteBytes[i] = (byte)(c.id >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            suiteBytes[i+1] = (byte)c.id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        s.putBytes16(suiteBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Clear cache of available ciphersuites. If we support all ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * internally, there is no need to clear the cache and calling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static synchronized void clearAvailableCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (CipherSuite.DYNAMIC_AVAILABILITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            CipherSuite.BulkCipher.clearAvailableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            JsseJce.clearEcAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}