1 /*
   2  * Copyright (c) 2002, 2015, 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 
  34 /**
  35  * A list of CipherSuites. Also maintains the lists of supported and
  36  * default ciphersuites and supports I/O from handshake streams.
  37  *
  38  * Instances of this class are immutable.
  39  *
  40  */
  41 final class CipherSuiteList {
  42 
  43     private final Collection<CipherSuite> cipherSuites;
  44     private String[] suiteNames;
  45 
  46     // flag indicating whether this list contains any ECC ciphersuites.
  47     // null if not yet checked.
  48     private volatile Boolean containsEC;
  49 
  50     // for use by buildAvailableCache() and
  51     // Handshaker.getKickstartMessage() only
  52     CipherSuiteList(Collection<CipherSuite> cipherSuites) {
  53         this.cipherSuites = cipherSuites;
  54     }
  55 
  56     /**
  57      * Create a CipherSuiteList with a single element.
  58      */
  59     CipherSuiteList(CipherSuite suite) {
  60         cipherSuites = new ArrayList<CipherSuite>(1);
  61         cipherSuites.add(suite);
  62     }
  63 
  64     /**
  65      * Construct a CipherSuiteList from a array of names. We don't bother
  66      * to eliminate duplicates.
  67      *
  68      * @exception IllegalArgumentException if the array or any of its elements
  69      * is null or if the ciphersuite name is unrecognized or unsupported
  70      * using currently installed providers.
  71      */
  72     CipherSuiteList(String[] names) {
  73         if (names == null) {
  74             throw new IllegalArgumentException("CipherSuites may not be null");
  75         }
  76         cipherSuites = new ArrayList<CipherSuite>(names.length);
  77         for (int i = 0; i < names.length; i++) {
  78             String suiteName = names[i];
  79             CipherSuite suite = CipherSuite.valueOf(suiteName);
  80             if (suite.isAvailable() == false) {
  81                 throw new IllegalArgumentException("Cannot support "
  82                     + suiteName + " with currently installed providers");
  83             }
  84             cipherSuites.add(suite);
  85         }
  86     }
  87 
  88     /**
  89      * Read a CipherSuiteList from a HandshakeInStream in V3 ClientHello
  90      * format. Does not check if the listed ciphersuites are known or
  91      * supported.
  92      */
  93     CipherSuiteList(HandshakeInStream in) throws IOException {
  94         byte[] bytes = in.getBytes16();
  95         if ((bytes.length & 1) != 0) {
  96             throw new SSLException("Invalid ClientHello message");
  97         }
  98         cipherSuites = new ArrayList<CipherSuite>(bytes.length >> 1);
  99         for (int i = 0; i < bytes.length; i += 2) {
 100             cipherSuites.add(CipherSuite.valueOf(bytes[i], bytes[i+1]));
 101         }
 102     }
 103 
 104     /**
 105      * Return whether this list contains the given CipherSuite.
 106      */
 107     boolean contains(CipherSuite suite) {
 108         return cipherSuites.contains(suite);
 109     }
 110 
 111     // Return whether this list contains any ECC ciphersuites
 112     boolean containsEC() {
 113         if (containsEC == null) {
 114             for (CipherSuite c : cipherSuites) {
 115                 switch (c.keyExchange) {
 116                 case K_ECDH_ECDSA:
 117                 case K_ECDH_RSA:
 118                 case K_ECDHE_ECDSA:
 119                 case K_ECDHE_RSA:
 120                 case K_ECDH_ANON:
 121                     containsEC = true;
 122                     return true;
 123                 default:
 124                     break;
 125                 }
 126             }
 127             containsEC = false;
 128         }
 129         return containsEC;
 130     }
 131 
 132     /**
 133      * Return an Iterator for the CipherSuites in this list.
 134      */
 135     Iterator<CipherSuite> iterator() {
 136         return cipherSuites.iterator();
 137     }
 138 
 139     /**
 140      * Return a reference to the internal Collection of CipherSuites.
 141      * The Collection MUST NOT be modified.
 142      */
 143     Collection<CipherSuite> collection() {
 144         return cipherSuites;
 145     }
 146 
 147     /**
 148      * Return the number of CipherSuites in this list.
 149      */
 150     int size() {
 151         return cipherSuites.size();
 152     }
 153 
 154     /**
 155      * Return an array with the names of the CipherSuites in this list.
 156      */
 157     synchronized String[] toStringArray() {
 158         if (suiteNames == null) {
 159             suiteNames = new String[cipherSuites.size()];
 160             int i = 0;
 161             for (CipherSuite c : cipherSuites) {
 162                 suiteNames[i++] = c.name;
 163             }
 164         }
 165         return suiteNames.clone();
 166     }
 167 
 168     @Override
 169     public String toString() {
 170         return cipherSuites.toString();
 171     }
 172 
 173     /**
 174      * Write this list to an HandshakeOutStream in V3 ClientHello format.
 175      */
 176     void send(HandshakeOutStream s) throws IOException {
 177         byte[] suiteBytes = new byte[cipherSuites.size() * 2];
 178         int i = 0;
 179         for (CipherSuite c : cipherSuites) {
 180             suiteBytes[i] = (byte)(c.id >> 8);
 181             suiteBytes[i+1] = (byte)c.id;
 182             i += 2;
 183         }
 184         s.putBytes16(suiteBytes);
 185     }
 186 }