0N/A/*
2998N/A * Copyright (c) 2002, 2010, 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/A/**
0N/A * Type safe enum for an SSL/TLS protocol version. Instances are obtained
0N/A * using the static factory methods or by referencing the static members
0N/A * in this class. Member variables are final and can be accessed without
0N/A * accessor methods.
0N/A *
0N/A * There is only ever one instance per supported protocol version, this
0N/A * means == can be used for comparision instead of equals() if desired.
0N/A *
0N/A * Checks for a particular version number should generally take this form:
0N/A *
0N/A * if (protocolVersion.v >= ProtocolVersion.TLS10) {
0N/A * // TLS 1.0 code goes here
0N/A * } else {
0N/A * // SSL 3.0 code here
0N/A * }
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.4.1
0N/A */
2998N/Apublic final class ProtocolVersion implements Comparable<ProtocolVersion> {
0N/A
2998N/A // The limit of maximum protocol version
2998N/A final static int LIMIT_MAX_VALUE = 0xFFFF;
2998N/A
3002N/A // The limit of minimum protocol version
3002N/A final static int LIMIT_MIN_VALUE = 0x0000;
3002N/A
2998N/A // Dummy protocol version value for invalid SSLSession
0N/A final static ProtocolVersion NONE = new ProtocolVersion(-1, "NONE");
0N/A
0N/A // If enabled, send/ accept SSLv2 hello messages
0N/A final static ProtocolVersion SSL20Hello = new ProtocolVersion(0x0002,
0N/A "SSLv2Hello");
0N/A
0N/A // SSL 3.0
0N/A final static ProtocolVersion SSL30 = new ProtocolVersion(0x0300, "SSLv3");
0N/A
0N/A // TLS 1.0
0N/A final static ProtocolVersion TLS10 = new ProtocolVersion(0x0301, "TLSv1");
0N/A
0N/A // TLS 1.1
0N/A final static ProtocolVersion TLS11 = new ProtocolVersion(0x0302, "TLSv1.1");
0N/A
2998N/A // TLS 1.2
2998N/A final static ProtocolVersion TLS12 = new ProtocolVersion(0x0303, "TLSv1.2");
2998N/A
0N/A private static final boolean FIPS = SunJSSE.isFIPS();
0N/A
0N/A // minimum version we implement (SSL 3.0)
0N/A final static ProtocolVersion MIN = FIPS ? TLS10 : SSL30;
0N/A
3002N/A // maximum version we implement (TLS 1.2)
3002N/A final static ProtocolVersion MAX = TLS12;
0N/A
0N/A // ProtocolVersion to use by default (TLS 1.0)
0N/A final static ProtocolVersion DEFAULT = TLS10;
0N/A
0N/A // Default version for hello messages (SSLv2Hello)
2998N/A final static ProtocolVersion DEFAULT_HELLO = FIPS ? TLS10 : SSL30;
0N/A
0N/A // version in 16 bit MSB format as it appears in records and
0N/A // messages, i.e. 0x0301 for TLS 1.0
1870N/A public final int v;
0N/A
0N/A // major and minor version
1870N/A public final byte major, minor;
0N/A
0N/A // name used in JSSE (e.g. TLSv1 for TLS 1.0)
0N/A final String name;
0N/A
0N/A // private
0N/A private ProtocolVersion(int v, String name) {
0N/A this.v = v;
0N/A this.name = name;
0N/A major = (byte)(v >>> 8);
0N/A minor = (byte)(v & 0xff);
0N/A }
0N/A
0N/A // private
0N/A private static ProtocolVersion valueOf(int v) {
0N/A if (v == SSL30.v) {
0N/A return SSL30;
0N/A } else if (v == TLS10.v) {
0N/A return TLS10;
0N/A } else if (v == TLS11.v) {
0N/A return TLS11;
2998N/A } else if (v == TLS12.v) {
2998N/A return TLS12;
0N/A } else if (v == SSL20Hello.v) {
0N/A return SSL20Hello;
0N/A } else {
0N/A int major = (v >>> 8) & 0xff;
0N/A int minor = v & 0xff;
0N/A return new ProtocolVersion(v, "Unknown-" + major + "." + minor);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return a ProtocolVersion with the specified major and minor version
0N/A * numbers. Never throws exceptions.
0N/A */
1870N/A public static ProtocolVersion valueOf(int major, int minor) {
0N/A major &= 0xff;
0N/A minor &= 0xff;
0N/A int v = (major << 8) | minor;
0N/A return valueOf(v);
0N/A }
0N/A
0N/A /**
0N/A * Return a ProtocolVersion for the given name.
0N/A *
0N/A * @exception IllegalArgumentException if name is null or does not
0N/A * identify a supported protocol
0N/A */
0N/A static ProtocolVersion valueOf(String name) {
0N/A if (name == null) {
0N/A throw new IllegalArgumentException("Protocol cannot be null");
0N/A }
2998N/A
2998N/A if (FIPS && (name.equals(SSL30.name) || name.equals(SSL20Hello.name))) {
2998N/A throw new IllegalArgumentException
2998N/A ("Only TLS 1.0 or later allowed in FIPS mode");
0N/A }
2998N/A
0N/A if (name.equals(SSL30.name)) {
0N/A return SSL30;
0N/A } else if (name.equals(TLS10.name)) {
0N/A return TLS10;
2998N/A } else if (name.equals(TLS11.name)) {
2998N/A return TLS11;
2998N/A } else if (name.equals(TLS12.name)) {
2998N/A return TLS12;
0N/A } else if (name.equals(SSL20Hello.name)) {
0N/A return SSL20Hello;
0N/A } else {
0N/A throw new IllegalArgumentException(name);
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A return name;
0N/A }
0N/A
2998N/A /**
2998N/A * Compares this object with the specified object for order.
2998N/A */
2998N/A public int compareTo(ProtocolVersion protocolVersion) {
2998N/A return this.v - protocolVersion.v;
2998N/A }
0N/A}