0N/A/*
3909N/A * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.ssl;
0N/A
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * A list of ProtocolVersions. Also maintains the list of supported protocols.
0N/A * Instances of this class are immutable. Some member variables are final
0N/A * and can be accessed directly without method accessors.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.4.1
0N/A */
0N/Afinal class ProtocolList {
0N/A
2998N/A // the sorted protocol version list
2998N/A private final ArrayList<ProtocolVersion> protocols;
2998N/A
0N/A private String[] protocolNames;
0N/A
0N/A // the minimum and maximum ProtocolVersions in this list
0N/A final ProtocolVersion min, max;
0N/A
0N/A // the format for the hello version to use
0N/A final ProtocolVersion helloVersion;
0N/A
0N/A ProtocolList(String[] names) {
2998N/A this(convert(names));
2998N/A }
2998N/A
2998N/A ProtocolList(ArrayList<ProtocolVersion> versions) {
2998N/A this.protocols = versions;
2998N/A
2998N/A if ((protocols.size() == 1) &&
2998N/A protocols.contains(ProtocolVersion.SSL20Hello)) {
2998N/A throw new IllegalArgumentException("SSLv2Hello cannot be " +
2998N/A "enabled unless at least one other supported version " +
2998N/A "is also enabled.");
2998N/A }
2998N/A
2998N/A if (protocols.size() != 0) {
2998N/A Collections.sort(protocols);
2998N/A min = protocols.get(0);
2998N/A max = protocols.get(protocols.size() - 1);
2998N/A helloVersion = protocols.get(0);
2998N/A } else {
2998N/A min = ProtocolVersion.NONE;
2998N/A max = ProtocolVersion.NONE;
2998N/A helloVersion = ProtocolVersion.NONE;
2998N/A }
2998N/A }
2998N/A
2998N/A private static ArrayList<ProtocolVersion> convert(String[] names) {
0N/A if (names == null) {
0N/A throw new IllegalArgumentException("Protocols may not be null");
0N/A }
2998N/A
3401N/A ArrayList<ProtocolVersion> versions = new ArrayList<>(3);
0N/A for (int i = 0; i < names.length; i++ ) {
0N/A ProtocolVersion version = ProtocolVersion.valueOf(names[i]);
2998N/A if (versions.contains(version) == false) {
2998N/A versions.add(version);
0N/A }
0N/A }
2998N/A
2998N/A return versions;
0N/A }
0N/A
0N/A /**
0N/A * Return whether this list contains the specified protocol version.
0N/A * SSLv2Hello is not a real protocol version we support, we always
0N/A * return false for it.
0N/A */
0N/A boolean contains(ProtocolVersion protocolVersion) {
0N/A if (protocolVersion == ProtocolVersion.SSL20Hello) {
0N/A return false;
0N/A }
0N/A return protocols.contains(protocolVersion);
0N/A }
0N/A
0N/A /**
2998N/A * Return a reference to the internal Collection of CipherSuites.
2998N/A * The Collection MUST NOT be modified.
2998N/A */
2998N/A Collection<ProtocolVersion> collection() {
2998N/A return protocols;
2998N/A }
2998N/A
2998N/A /**
2998N/A * Select a protocol version from the list.
2998N/A *
2998N/A * Return the lower of the protocol version of that suggested by
2998N/A * the <code>protocolVersion</code> and the highest version of this
2998N/A * protocol list, or null if no protocol version is available.
2998N/A *
2998N/A * The method is used by TLS server to negotiated the protocol
2998N/A * version between client suggested protocol version in the
2998N/A * client hello and protocol versions supported by the server.
2998N/A */
2998N/A ProtocolVersion selectProtocolVersion(ProtocolVersion protocolVersion) {
2998N/A ProtocolVersion selectedVersion = null;
2998N/A for (ProtocolVersion pv : protocols) {
2998N/A if (pv.v > protocolVersion.v) {
2998N/A break; // Safe to break here as this.protocols is sorted
2998N/A }
2998N/A selectedVersion = pv;
2998N/A }
2998N/A
2998N/A return selectedVersion;
2998N/A }
2998N/A
2998N/A /**
0N/A * Return an array with the names of the ProtocolVersions in this list.
0N/A */
0N/A synchronized String[] toStringArray() {
0N/A if (protocolNames == null) {
0N/A protocolNames = new String[protocols.size()];
0N/A int i = 0;
0N/A for (ProtocolVersion version : protocols) {
0N/A protocolNames[i++] = version.name;
0N/A }
0N/A }
28N/A return protocolNames.clone();
0N/A }
0N/A
0N/A public String toString() {
0N/A return protocols.toString();
0N/A }
0N/A}