jdk/src/share/classes/sun/security/ssl/ProtocolVersion.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4236 02f52c723b79
child 7039 6464c8e62a18
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     2
 * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Type safe enum for an SSL/TLS protocol version. Instances are obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * using the static factory methods or by referencing the static members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * in this class. Member variables are final and can be accessed without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * accessor methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * There is only ever one instance per supported protocol version, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * means == can be used for comparision instead of equals() if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Checks for a particular version number should generally take this form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * if (protocolVersion.v >= ProtocolVersion.TLS10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   // TLS 1.0 code goes here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   // SSL 3.0 code here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since   1.4.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    48
public final class ProtocolVersion {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // dummy protocol version value for invalid SSLSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    final static ProtocolVersion NONE = new ProtocolVersion(-1, "NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // If enabled, send/ accept SSLv2 hello messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    final static ProtocolVersion SSL20Hello = new ProtocolVersion(0x0002,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                                                "SSLv2Hello");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // SSL 3.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    final static ProtocolVersion SSL30 = new ProtocolVersion(0x0300, "SSLv3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // TLS 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    final static ProtocolVersion TLS10 = new ProtocolVersion(0x0301, "TLSv1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // TLS 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // not supported yet, but added for better readability of the debug trace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    final static ProtocolVersion TLS11 = new ProtocolVersion(0x0302, "TLSv1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final boolean FIPS = SunJSSE.isFIPS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // minimum version we implement (SSL 3.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    final static ProtocolVersion MIN = FIPS ? TLS10 : SSL30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // maximum version we implement (TLS 1.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    final static ProtocolVersion MAX = TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // ProtocolVersion to use by default (TLS 1.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    final static ProtocolVersion DEFAULT = TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Default version for hello messages (SSLv2Hello)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    final static ProtocolVersion DEFAULT_HELLO = FIPS ? TLS10 : SSL20Hello;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // version in 16 bit MSB format as it appears in records and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // messages, i.e. 0x0301 for TLS 1.0
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    83
    public final int v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // major and minor version
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    86
    public final byte major, minor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // name used in JSSE (e.g. TLSv1 for TLS 1.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private ProtocolVersion(int v, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.v = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        major = (byte)(v >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        minor = (byte)(v & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static ProtocolVersion valueOf(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (v == SSL30.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return SSL30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else if (v == TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } else if (v == TLS11.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return TLS11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else if (v == SSL20Hello.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return SSL20Hello;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int major = (v >>> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int minor = v & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return new ProtocolVersion(v, "Unknown-" + major + "." + minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Return a ProtocolVersion with the specified major and minor version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * numbers. Never throws exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   120
    public static ProtocolVersion valueOf(int major, int minor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        major &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        minor &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int v = (major << 8) | minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return valueOf(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Return a ProtocolVersion for the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @exception IllegalArgumentException if name is null or does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * identify a supported protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static ProtocolVersion valueOf(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new IllegalArgumentException("Protocol cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (FIPS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (name.equals(TLS10.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    ("Only TLS 1.0 allowed in FIPS mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (name.equals(SSL30.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return SSL30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else if (name.equals(TLS10.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else if (name.equals(SSL20Hello.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return SSL20Hello;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IllegalArgumentException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}