src/java.base/share/classes/sun/security/ssl/CipherSuiteList.java
changeset 47216 71c04702a3d5
parent 45064 b1b45177051b
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 
       
    27 package sun.security.ssl;
       
    28 
       
    29 import java.io.*;
       
    30 import java.util.*;
       
    31 
       
    32 import javax.net.ssl.SSLException;
       
    33 import static sun.security.ssl.NamedGroupType.*;
       
    34 
       
    35 /**
       
    36  * A list of CipherSuites. Also maintains the lists of supported and
       
    37  * default ciphersuites and supports I/O from handshake streams.
       
    38  *
       
    39  * Instances of this class are immutable.
       
    40  *
       
    41  */
       
    42 final class CipherSuiteList {
       
    43 
       
    44     private final Collection<CipherSuite> cipherSuites;
       
    45     private String[] suiteNames;
       
    46     private final EnumSet<NamedGroupType> groupsTypes =
       
    47             EnumSet.noneOf(NamedGroupType.class);
       
    48 
       
    49     // for use by buildAvailableCache() and
       
    50     // Handshaker.getKickstartMessage() only
       
    51     CipherSuiteList(Collection<CipherSuite> cipherSuites) {
       
    52         this.cipherSuites = cipherSuites;
       
    53         for (CipherSuite suite : cipherSuites) {
       
    54             updateGroupTypes(suite);
       
    55         }
       
    56     }
       
    57 
       
    58     /**
       
    59      * Create a CipherSuiteList with a single element.
       
    60      */
       
    61     CipherSuiteList(CipherSuite suite) {
       
    62         cipherSuites = new ArrayList<CipherSuite>(1);
       
    63         cipherSuites.add(suite);
       
    64         updateGroupTypes(suite);
       
    65     }
       
    66 
       
    67     /**
       
    68      * Construct a CipherSuiteList from a array of names. We don't bother
       
    69      * to eliminate duplicates.
       
    70      *
       
    71      * @exception IllegalArgumentException if the array or any of its elements
       
    72      * is null or if the ciphersuite name is unrecognized or unsupported
       
    73      * using currently installed providers.
       
    74      */
       
    75     CipherSuiteList(String[] names) {
       
    76         if (names == null) {
       
    77             throw new IllegalArgumentException("CipherSuites may not be null");
       
    78         }
       
    79         cipherSuites = new ArrayList<CipherSuite>(names.length);
       
    80         for (int i = 0; i < names.length; i++) {
       
    81             String suiteName = names[i];
       
    82             CipherSuite suite = CipherSuite.valueOf(suiteName);
       
    83             if (suite.isAvailable() == false) {
       
    84                 throw new IllegalArgumentException("Cannot support "
       
    85                     + suiteName + " with currently installed providers");
       
    86             }
       
    87             cipherSuites.add(suite);
       
    88             updateGroupTypes(suite);
       
    89         }
       
    90     }
       
    91 
       
    92     /**
       
    93      * Read a CipherSuiteList from a HandshakeInStream in V3 ClientHello
       
    94      * format. Does not check if the listed ciphersuites are known or
       
    95      * supported.
       
    96      */
       
    97     CipherSuiteList(HandshakeInStream in) throws IOException {
       
    98         byte[] bytes = in.getBytes16();
       
    99         if ((bytes.length & 1) != 0) {
       
   100             throw new SSLException("Invalid ClientHello message");
       
   101         }
       
   102         cipherSuites = new ArrayList<CipherSuite>(bytes.length >> 1);
       
   103         for (int i = 0; i < bytes.length; i += 2) {
       
   104             CipherSuite suite = CipherSuite.valueOf(bytes[i], bytes[i+1]);
       
   105             cipherSuites.add(suite);
       
   106             updateGroupTypes(suite);
       
   107         }
       
   108     }
       
   109 
       
   110     // Please don't use this method except constructors.
       
   111     private void updateGroupTypes(CipherSuite cipherSuite) {
       
   112         if (cipherSuite.keyExchange != null && (!cipherSuite.exportable)) {
       
   113             NamedGroupType groupType = cipherSuite.keyExchange.groupType;
       
   114             if ((groupType != NAMED_GROUP_NONE) &&
       
   115                     (!groupsTypes.contains(groupType))) {
       
   116                 groupsTypes.add(groupType);
       
   117             }
       
   118         }
       
   119     }
       
   120 
       
   121     /**
       
   122      * Return whether this list contains the given CipherSuite.
       
   123      */
       
   124     boolean contains(CipherSuite suite) {
       
   125         return cipherSuites.contains(suite);
       
   126     }
       
   127 
       
   128     // Return whether this list contains cipher suites of a named group type.
       
   129     boolean contains(NamedGroupType groupType) {
       
   130         return groupsTypes.contains(groupType);
       
   131     }
       
   132 
       
   133     /**
       
   134      * Return an Iterator for the CipherSuites in this list.
       
   135      */
       
   136     Iterator<CipherSuite> iterator() {
       
   137         return cipherSuites.iterator();
       
   138     }
       
   139 
       
   140     /**
       
   141      * Return a reference to the internal Collection of CipherSuites.
       
   142      * The Collection MUST NOT be modified.
       
   143      */
       
   144     Collection<CipherSuite> collection() {
       
   145         return cipherSuites;
       
   146     }
       
   147 
       
   148     /**
       
   149      * Return the number of CipherSuites in this list.
       
   150      */
       
   151     int size() {
       
   152         return cipherSuites.size();
       
   153     }
       
   154 
       
   155     /**
       
   156      * Return an array with the names of the CipherSuites in this list.
       
   157      */
       
   158     synchronized String[] toStringArray() {
       
   159         if (suiteNames == null) {
       
   160             suiteNames = new String[cipherSuites.size()];
       
   161             int i = 0;
       
   162             for (CipherSuite c : cipherSuites) {
       
   163                 suiteNames[i++] = c.name;
       
   164             }
       
   165         }
       
   166         return suiteNames.clone();
       
   167     }
       
   168 
       
   169     @Override
       
   170     public String toString() {
       
   171         return cipherSuites.toString();
       
   172     }
       
   173 
       
   174     /**
       
   175      * Write this list to an HandshakeOutStream in V3 ClientHello format.
       
   176      */
       
   177     void send(HandshakeOutStream s) throws IOException {
       
   178         byte[] suiteBytes = new byte[cipherSuites.size() * 2];
       
   179         int i = 0;
       
   180         for (CipherSuite c : cipherSuites) {
       
   181             suiteBytes[i] = (byte)(c.id >> 8);
       
   182             suiteBytes[i+1] = (byte)c.id;
       
   183             i += 2;
       
   184         }
       
   185         s.putBytes16(suiteBytes);
       
   186     }
       
   187 }