jdk/src/share/classes/sun/security/ssl/ProtocolVersion.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 7043 5e2d1edeb2c7
child 14664 e71aa0962e70
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2002, 2010, 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
 */
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    48
public final class ProtocolVersion implements Comparable<ProtocolVersion> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    50
    // The limit of maximum protocol version
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    51
    final static int LIMIT_MAX_VALUE = 0xFFFF;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    52
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    53
    // The limit of minimum protocol version
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    54
    final static int LIMIT_MIN_VALUE = 0x0000;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    55
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    56
    // Dummy protocol version value for invalid SSLSession
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    final static ProtocolVersion NONE = new ProtocolVersion(-1, "NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // If enabled, send/ accept SSLv2 hello messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    final static ProtocolVersion SSL20Hello = new ProtocolVersion(0x0002,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                                                "SSLv2Hello");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // SSL 3.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    final static ProtocolVersion SSL30 = new ProtocolVersion(0x0300, "SSLv3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // TLS 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    final static ProtocolVersion TLS10 = new ProtocolVersion(0x0301, "TLSv1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // TLS 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    final static ProtocolVersion TLS11 = new ProtocolVersion(0x0302, "TLSv1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    72
    // TLS 1.2
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    73
    final static ProtocolVersion TLS12 = new ProtocolVersion(0x0303, "TLSv1.2");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final boolean FIPS = SunJSSE.isFIPS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // minimum version we implement (SSL 3.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    final static ProtocolVersion MIN = FIPS ? TLS10 : SSL30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    80
    // maximum version we implement (TLS 1.2)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    81
    final static ProtocolVersion MAX = TLS12;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // ProtocolVersion to use by default (TLS 1.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    final static ProtocolVersion DEFAULT = TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Default version for hello messages (SSLv2Hello)
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    87
    final static ProtocolVersion DEFAULT_HELLO = FIPS ? TLS10 : SSL30;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // version in 16 bit MSB format as it appears in records and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // messages, i.e. 0x0301 for TLS 1.0
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    91
    public final int v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // major and minor version
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    94
    public final byte major, minor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // name used in JSSE (e.g. TLSv1 for TLS 1.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    final String name;
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 ProtocolVersion(int v, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.v = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        major = (byte)(v >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        minor = (byte)(v & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static ProtocolVersion valueOf(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (v == SSL30.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return SSL30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else if (v == TLS10.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return TLS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else if (v == TLS11.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return TLS11;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   115
        } else if (v == TLS12.v) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   116
            return TLS12;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else if (v == SSL20Hello.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return SSL20Hello;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int major = (v >>> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            int minor = v & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return new ProtocolVersion(v, "Unknown-" + major + "." + minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Return a ProtocolVersion with the specified major and minor version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * numbers. Never throws exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   130
    public static ProtocolVersion valueOf(int major, int minor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        major &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        minor &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int v = (major << 8) | minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return valueOf(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Return a ProtocolVersion for the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception IllegalArgumentException if name is null or does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * identify a supported protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static ProtocolVersion valueOf(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException("Protocol cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   147
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   148
        if (FIPS && (name.equals(SSL30.name) || name.equals(SSL20Hello.name))) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   149
            throw new IllegalArgumentException
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   150
                ("Only TLS 1.0 or later allowed in FIPS mode");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (name.equals(SSL30.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return SSL30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } else if (name.equals(TLS10.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return TLS10;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   157
        } else if (name.equals(TLS11.name)) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   158
            return TLS11;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   159
        } else if (name.equals(TLS12.name)) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   160
            return TLS12;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else if (name.equals(SSL20Hello.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return SSL20Hello;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new IllegalArgumentException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   172
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   173
     * Compares this object with the specified object for order.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   174
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   175
    public int compareTo(ProtocolVersion protocolVersion) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   176
        return this.v - protocolVersion.v;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   177
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}