blob: 11aacd5150450677e985ecbad8343fea2e08b942 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2007 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.ssl;
27
28/**
29 * Type safe enum for an SSL/TLS protocol version. Instances are obtained
30 * using the static factory methods or by referencing the static members
31 * in this class. Member variables are final and can be accessed without
32 * accessor methods.
33 *
34 * There is only ever one instance per supported protocol version, this
35 * means == can be used for comparision instead of equals() if desired.
36 *
37 * Checks for a particular version number should generally take this form:
38 *
39 * if (protocolVersion.v >= ProtocolVersion.TLS10) {
40 * // TLS 1.0 code goes here
41 * } else {
42 * // SSL 3.0 code here
43 * }
44 *
45 * @author Andreas Sterbenz
46 * @since 1.4.1
47 */
48final class ProtocolVersion {
49
50 // dummy protocol version value for invalid SSLSession
51 final static ProtocolVersion NONE = new ProtocolVersion(-1, "NONE");
52
53 // If enabled, send/ accept SSLv2 hello messages
54 final static ProtocolVersion SSL20Hello = new ProtocolVersion(0x0002,
55 "SSLv2Hello");
56
57 // SSL 3.0
58 final static ProtocolVersion SSL30 = new ProtocolVersion(0x0300, "SSLv3");
59
60 // TLS 1.0
61 final static ProtocolVersion TLS10 = new ProtocolVersion(0x0301, "TLSv1");
62
63 // TLS 1.1
64 // not supported yet, but added for better readability of the debug trace
65 final static ProtocolVersion TLS11 = new ProtocolVersion(0x0302, "TLSv1.1");
66
67 private static final boolean FIPS = SunJSSE.isFIPS();
68
69 // minimum version we implement (SSL 3.0)
70 final static ProtocolVersion MIN = FIPS ? TLS10 : SSL30;
71
72 // maximum version we implement (TLS 1.0)
73 final static ProtocolVersion MAX = TLS10;
74
75 // ProtocolVersion to use by default (TLS 1.0)
76 final static ProtocolVersion DEFAULT = TLS10;
77
78 // Default version for hello messages (SSLv2Hello)
79 final static ProtocolVersion DEFAULT_HELLO = FIPS ? TLS10 : SSL20Hello;
80
81 // version in 16 bit MSB format as it appears in records and
82 // messages, i.e. 0x0301 for TLS 1.0
83 final int v;
84
85 // major and minor version
86 final byte major, minor;
87
88 // name used in JSSE (e.g. TLSv1 for TLS 1.0)
89 final String name;
90
91 // private
92 private ProtocolVersion(int v, String name) {
93 this.v = v;
94 this.name = name;
95 major = (byte)(v >>> 8);
96 minor = (byte)(v & 0xff);
97 }
98
99 // private
100 private static ProtocolVersion valueOf(int v) {
101 if (v == SSL30.v) {
102 return SSL30;
103 } else if (v == TLS10.v) {
104 return TLS10;
105 } else if (v == TLS11.v) {
106 return TLS11;
107 } else if (v == SSL20Hello.v) {
108 return SSL20Hello;
109 } else {
110 int major = (v >>> 8) & 0xff;
111 int minor = v & 0xff;
112 return new ProtocolVersion(v, "Unknown-" + major + "." + minor);
113 }
114 }
115
116 /**
117 * Return a ProtocolVersion with the specified major and minor version
118 * numbers. Never throws exceptions.
119 */
120 static ProtocolVersion valueOf(int major, int minor) {
121 major &= 0xff;
122 minor &= 0xff;
123 int v = (major << 8) | minor;
124 return valueOf(v);
125 }
126
127 /**
128 * Return a ProtocolVersion for the given name.
129 *
130 * @exception IllegalArgumentException if name is null or does not
131 * identify a supported protocol
132 */
133 static ProtocolVersion valueOf(String name) {
134 if (name == null) {
135 throw new IllegalArgumentException("Protocol cannot be null");
136 }
137 if (FIPS) {
138 if (name.equals(TLS10.name)) {
139 return TLS10;
140 } else {
141 throw new IllegalArgumentException
142 ("Only TLS 1.0 allowed in FIPS mode");
143 }
144 }
145 if (name.equals(SSL30.name)) {
146 return SSL30;
147 } else if (name.equals(TLS10.name)) {
148 return TLS10;
149 } else if (name.equals(SSL20Hello.name)) {
150 return SSL20Hello;
151 } else {
152 throw new IllegalArgumentException(name);
153 }
154 }
155
156 public String toString() {
157 return name;
158 }
159
160}