jdk/src/java.base/share/classes/sun/security/ssl/ProtocolList.java
author xuelei
Tue, 02 Jun 2015 04:01:04 +0000
changeset 30904 ec0224270f90
parent 25859 3317bb8137f4
permissions -rw-r--r--
8043758: Datagram Transport Layer Security (DTLS) Reviewed-by: jnimeh, weijun, mullan, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2015, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A list of ProtocolVersions. Also maintains the list of supported protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Instances of this class are immutable. Some member variables are final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and can be accessed directly without method accessors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since   1.4.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
final class ProtocolList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    40
    // the sorted protocol version list
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    41
    private final ArrayList<ProtocolVersion> protocols;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private String[] protocolNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // the minimum and maximum ProtocolVersions in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    final ProtocolVersion min, max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // the format for the hello version to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    final ProtocolVersion helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    ProtocolList(String[] names) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    52
        this(convert(names));
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    53
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    54
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    55
    ProtocolList(ArrayList<ProtocolVersion> versions) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    56
        this.protocols = versions;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    57
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    58
        if ((protocols.size() == 1) &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    59
                protocols.contains(ProtocolVersion.SSL20Hello)) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    60
            throw new IllegalArgumentException("SSLv2Hello cannot be " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    61
                "enabled unless at least one other supported version " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    62
                "is also enabled.");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    63
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    64
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    65
        if (protocols.size() != 0) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    66
            Collections.sort(protocols);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    67
            min = protocols.get(0);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    68
            max = protocols.get(protocols.size() - 1);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    69
            helloVersion = protocols.get(0);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    70
        } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    71
            min = ProtocolVersion.NONE;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    72
            max = ProtocolVersion.NONE;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    73
            helloVersion = ProtocolVersion.NONE;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    74
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    75
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    76
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    77
    private static ArrayList<ProtocolVersion> convert(String[] names) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (names == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new IllegalArgumentException("Protocols may not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    81
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9275
diff changeset
    82
        ArrayList<ProtocolVersion> versions = new ArrayList<>(names.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i = 0; i < names.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ProtocolVersion version = ProtocolVersion.valueOf(names[i]);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    85
            if (versions.contains(version) == false) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    86
                versions.add(version);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    89
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
    90
        return versions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Return whether this list contains the specified protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * SSLv2Hello is not a real protocol version we support, we always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * return false for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    boolean contains(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (protocolVersion == ProtocolVersion.SSL20Hello) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return protocols.contains(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   106
     * Return a reference to the internal Collection of CipherSuites.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   107
     * The Collection MUST NOT be modified.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   108
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   109
    Collection<ProtocolVersion> collection() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   110
        return protocols;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   111
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   112
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   113
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   114
     * Select a protocol version from the list.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   115
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   116
     * Return the lower of the protocol version of that suggested by
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   117
     * the <code>protocolVersion</code> and the highest version of this
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   118
     * protocol list, or null if no protocol version is available.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   119
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   120
     * The method is used by TLS server to negotiated the protocol
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   121
     * version between client suggested protocol version in the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   122
     * client hello and protocol versions supported by the server.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   123
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   124
    ProtocolVersion selectProtocolVersion(ProtocolVersion protocolVersion) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   125
        ProtocolVersion selectedVersion = null;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   126
        for (ProtocolVersion pv : protocols) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   127
            if (pv.compareTo(protocolVersion) > 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   128
                break;      // Safe to break here as this.protocols is sorted,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   129
                            // and DTLS and SSL/TLS protocols are not mixed.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   130
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   131
            selectedVersion = pv;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   132
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   133
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   134
        return selectedVersion;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   135
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   136
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   137
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Return an array with the names of the ProtocolVersions in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    synchronized String[] toStringArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (protocolNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            protocolNames = new String[protocols.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            for (ProtocolVersion version : protocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                protocolNames[i++] = version.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   148
        return protocolNames.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   151
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return protocols.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}