jdk/src/share/classes/sun/security/ssl/CipherSuite.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 3957 c8fdb8fad795
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.IvParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.ssl.CipherSuite.KeyExchange.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import static sun.security.ssl.JsseJce.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * An SSL/TLS CipherSuite. Constants for the standard key exchange, cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and mac algorithms are also defined in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The CipherSuite class and the inner classes defined in this file roughly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * follow the type safe enum pattern described in Effective Java. This means:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  . instances are immutable, classes are final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  . there is a unique instance of every value, i.e. there are never two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *    instances representing the same CipherSuite, etc. This means equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    tests can be performed using == instead of equals() (although that works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *    as well). [A minor exception are *unsupported* CipherSuites read from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    handshake message, but this is usually irrelevant]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  . instances are obtained using the static valueOf() factory methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  . properties are defined as final variables and made available as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    package private variables without method accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *  . if the member variable allowed is false, the given algorithm is either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    unavailable or disabled at compile time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
final class CipherSuite implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // minimum priority for supported CipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    final static int SUPPORTED_SUITES_PRIORITY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // minimum priority for default enabled CipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    final static int DEFAULT_SUITES_PRIORITY = 300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Flag indicating if CipherSuite availability can change dynamically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // This is the case when we rely on a JCE cipher implementation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // may not be available in the installed JCE providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // It is true because we do not have a Java ECC implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    final static boolean DYNAMIC_AVAILABILITY = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final static boolean ALLOW_ECC = Debug.getBooleanProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ("com.sun.net.ssl.enableECC", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Map Integer(id) -> CipherSuite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // contains all known CipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final static Map<Integer,CipherSuite> idMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Map String(name) -> CipherSuite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // contains only supported CipherSuites (i.e. allowed == true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private final static Map<String,CipherSuite> nameMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // Protocol defined CipherSuite name, e.g. SSL_RSA_WITH_RC4_128_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // we use TLS_* only for new CipherSuites, still SSL_* for old ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // id in 16 bit MSB format, i.e. 0x0004 for SSL_RSA_WITH_RC4_128_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    final int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // priority for the internal default preference order. the higher the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // better. Each supported CipherSuite *must* have a unique priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Ciphersuites with priority >= DEFAULT_SUITES_PRIORITY are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    final int priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // key exchange, bulk cipher, and mac algorithms. See those classes below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    final KeyExchange keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    final BulkCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    final MacAlg macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // whether a CipherSuite qualifies as exportable under 512/40 bit rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    final boolean exportable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // true iff implemented and enabled at compile time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    final boolean allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private CipherSuite(String name, int id, int priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            KeyExchange keyExchange, BulkCipher cipher, boolean allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.priority = priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.keyExchange = keyExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.cipher = cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.exportable = cipher.exportable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (name.endsWith("_MD5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            macAlg = M_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else if (name.endsWith("_SHA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            macAlg = M_SHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } else if (name.endsWith("_NULL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            macAlg = M_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    ("Unknown MAC algorithm for ciphersuite " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        allowed &= keyExchange.allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        allowed &= cipher.allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.allowed = allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private CipherSuite(String name, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.priority = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.keyExchange = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.macAlg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.exportable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Return whether this CipherSuite is available for use. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * CipherSuite may be unavailable even if it is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * (i.e. allowed == true) if the required JCE cipher is not installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * In some configuration, this situation may change over time, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * CipherSuiteList.clearAvailableCache() before this method to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * the most current status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    boolean isAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return allowed && keyExchange.isAvailable() && cipher.isAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Compares CipherSuites based on their priority. Has the effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * sorting CipherSuites when put in a sorted collection, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * used by CipherSuiteList. Follows standard Comparable contract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Note that for unsupported CipherSuites parsed from a handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * message we violate the equals() contract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public int compareTo(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return ((CipherSuite)o).priority - priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns this.name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Return a CipherSuite for the given name. The returned CipherSuite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * is supported by this implementation but may not actually be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * currently useable. See isAvailable().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @exception IllegalArgumentException if the CipherSuite is unknown or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static CipherSuite valueOf(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IllegalArgumentException("Name must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   194
        CipherSuite c = nameMap.get(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ((c == null) || (c.allowed == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new IllegalArgumentException("Unsupported ciphersuite " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Return a CipherSuite with the given ID. A temporary object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * constructed if the ID is unknown. Use isAvailable() to verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the CipherSuite can actually be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    static CipherSuite valueOf(int id1, int id2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        id1 &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        id2 &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int id = (id1 << 8) | id2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        CipherSuite c = idMap.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            String h1 = Integer.toString(id1, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            String h2 = Integer.toString(id2, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            c = new CipherSuite("Unknown 0x" + h1 + ":0x" + h2, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // for use by CipherSuiteList only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    static Collection<CipherSuite> allowedCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return nameMap.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static void add(String name, int id, int priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            KeyExchange keyExchange, BulkCipher cipher, boolean allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        CipherSuite c = new CipherSuite(name, id, priority, keyExchange,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                        cipher, allowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (idMap.put(id, c) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new RuntimeException("Duplicate ciphersuite definition: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        + id + ", " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (c.allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (nameMap.put(name, c) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                throw new RuntimeException("Duplicate ciphersuite definition: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                            + id + ", " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private static void add(String name, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        CipherSuite c = new CipherSuite(name, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (idMap.put(id, c) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new RuntimeException("Duplicate ciphersuite definition: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                        + id + ", " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * An SSL/TLS key exchange algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static enum KeyExchange {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // key exchange algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        K_NULL       ("NULL",       false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        K_RSA        ("RSA",        true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        K_RSA_EXPORT ("RSA_EXPORT", true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        K_DH_RSA     ("DH_RSA",     false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        K_DH_DSS     ("DH_DSS",     false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        K_DHE_DSS    ("DHE_DSS",    true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        K_DHE_RSA    ("DHE_RSA",    true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        K_DH_ANON    ("DH_anon",    true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        K_ECDH_ECDSA ("ECDH_ECDSA",  ALLOW_ECC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        K_ECDH_RSA   ("ECDH_RSA",    ALLOW_ECC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        K_ECDHE_ECDSA("ECDHE_ECDSA", ALLOW_ECC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        K_ECDHE_RSA  ("ECDHE_RSA",   ALLOW_ECC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        K_ECDH_ANON  ("ECDH_anon",   ALLOW_ECC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Kerberos cipher suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        K_KRB5       ("KRB5", true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        K_KRB5_EXPORT("KRB5_EXPORT", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // name of the key exchange algorithm, e.g. DHE_DSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        final boolean allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        private final boolean alwaysAvailable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        KeyExchange(String name, boolean allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            this.allowed = allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.alwaysAvailable = allowed && (name.startsWith("EC") == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean isAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (alwaysAvailable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return allowed && JsseJce.isEcAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * An SSL/TLS bulk cipher algorithm. One instance per combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * cipher and key length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Also contains a factory method to obtain in initialized CipherBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * for this algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    final static class BulkCipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Map BulkCipher -> Boolean(available)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        private final static Map<BulkCipher,Boolean> availableCache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                            new HashMap<BulkCipher,Boolean>(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // descriptive name including key size, e.g. AES/128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        final String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // JCE cipher transformation string, e.g. AES/CBC/NoPadding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        final String transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // algorithm name, e.g. AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // supported and compile time enabled. Also see isAvailable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        final boolean allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // number of bytes of entropy in the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        final int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // length of the actual cipher key in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // for non-exportable ciphers, this is the same as keySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        final int expandedKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // size of the IV (also block size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        final int ivSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // exportable under 512/40 bit rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        final boolean exportable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        BulkCipher(String transformation, int keySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                int expandedKeySize, int ivSize, boolean allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            this.transformation = transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            this.algorithm = transformation.split("/")[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            this.description = this.algorithm + "/" + (keySize << 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            this.keySize = keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            this.ivSize = ivSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            this.allowed = allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            this.expandedKeySize = expandedKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            this.exportable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        BulkCipher(String transformation, int keySize, int ivSize, boolean allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            this.transformation = transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            this.algorithm = transformation.split("/")[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            this.description = this.algorithm + "/" + (keySize << 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            this.keySize = keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            this.ivSize = ivSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            this.allowed = allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            this.expandedKeySize = keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            this.exportable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * Return an initialized CipherBox for this BulkCipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * IV must be null for stream ciphers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * @exception NoSuchAlgorithmException if anything goes wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        CipherBox newCipher(ProtocolVersion version, SecretKey key, IvParameterSpec iv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                boolean encrypt) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return CipherBox.newCipherBox(version, this, key, iv, encrypt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * Test if this bulk cipher is available. For use by CipherSuite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * Currently all supported ciphers except AES are always available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * via the JSSE internal implementations. We also assume AES/128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * is always available since it is shipped with the SunJCE provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * However, AES/256 is unavailable when the default JCE policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * jurisdiction files are installed because of key length restrictions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        boolean isAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (allowed == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (this == B_AES_256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                return isAvailable(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // always available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // for use by CipherSuiteList.clearAvailableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        static synchronized void clearAvailableCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (DYNAMIC_AVAILABILITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                availableCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        private static synchronized boolean isAvailable(BulkCipher cipher) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   398
            Boolean b = availableCache.get(cipher);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    SecretKey key = new SecretKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                            (new byte[cipher.expandedKeySize], cipher.algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    IvParameterSpec iv = new IvParameterSpec(new byte[cipher.ivSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    cipher.newCipher(ProtocolVersion.DEFAULT, key, iv, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    b = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    b = Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                availableCache.put(cipher, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return b.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * An SSL/TLS key MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Also contains a factory method to obtain in initialized MAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * for this algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    final static class MacAlg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // descriptive name, e.g. MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // size of the MAC value (and MAC key) in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        MacAlg(String name, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * Return an initialized MAC for this MacAlg. ProtocolVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * must either be SSL30 (SSLv3 custom MAC) or TLS10 (std. HMAC).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * @exception NoSuchAlgorithmException if anything goes wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        MAC newMac(ProtocolVersion protocolVersion, SecretKey secret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return new MAC(this, protocolVersion, secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    // export strength ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    final static BulkCipher B_NULL    = new BulkCipher("NULL",     0,  0, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    final static BulkCipher B_RC4_40  = new BulkCipher(CIPHER_RC4, 5, 16, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    final static BulkCipher B_RC2_40  = new BulkCipher("RC2",      5, 16, 8, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    final static BulkCipher B_DES_40  = new BulkCipher(CIPHER_DES, 5,  8, 8, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    // domestic strength ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    final static BulkCipher B_RC4_128 = new BulkCipher(CIPHER_RC4,  16,  0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    final static BulkCipher B_DES     = new BulkCipher(CIPHER_DES,   8,  8, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    final static BulkCipher B_3DES    = new BulkCipher(CIPHER_3DES, 24,  8, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    final static BulkCipher B_IDEA    = new BulkCipher("IDEA",      16,  8, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    final static BulkCipher B_AES_128 = new BulkCipher(CIPHER_AES,  16, 16, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    final static BulkCipher B_AES_256 = new BulkCipher(CIPHER_AES,  32, 16, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // MACs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    final static MacAlg M_NULL = new MacAlg("NULL", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    final static MacAlg M_MD5  = new MacAlg("MD5", 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    final static MacAlg M_SHA  = new MacAlg("SHA", 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        idMap = new HashMap<Integer,CipherSuite>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        nameMap = new HashMap<String,CipherSuite>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        final boolean F = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        final boolean T = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // N: ciphersuites only allowed if we are not in FIPS mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        final boolean N = (SunJSSE.isFIPS() == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
add("SSL_NULL_WITH_NULL_NULL",                0x0000,   1, K_NULL,       B_NULL,    F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // Definition of the CipherSuites that are enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // They are listed in preference order, most preferred first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int p = DEFAULT_SUITES_PRIORITY * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
add("SSL_RSA_WITH_RC4_128_MD5",              0x0004, --p, K_RSA,        B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
add("SSL_RSA_WITH_RC4_128_SHA",              0x0005, --p, K_RSA,        B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
add("TLS_RSA_WITH_AES_128_CBC_SHA",          0x002f, --p, K_RSA,        B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
add("TLS_RSA_WITH_AES_256_CBC_SHA",          0x0035, --p, K_RSA,        B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
add("TLS_ECDH_ECDSA_WITH_RC4_128_SHA",       0xC002, --p, K_ECDH_ECDSA, B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
add("TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA",   0xC004, --p, K_ECDH_ECDSA, B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
add("TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA",   0xC005, --p, K_ECDH_ECDSA, B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
add("TLS_ECDH_RSA_WITH_RC4_128_SHA",         0xC00C, --p, K_ECDH_RSA,   B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
add("TLS_ECDH_RSA_WITH_AES_128_CBC_SHA",     0xC00E, --p, K_ECDH_RSA,   B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
add("TLS_ECDH_RSA_WITH_AES_256_CBC_SHA",     0xC00F, --p, K_ECDH_RSA,   B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
add("TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",      0xC007, --p, K_ECDHE_ECDSA,B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
add("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",  0xC009, --p, K_ECDHE_ECDSA,B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
add("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",  0xC00A, --p, K_ECDHE_ECDSA,B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
add("TLS_ECDHE_RSA_WITH_RC4_128_SHA",        0xC011, --p, K_ECDHE_RSA,  B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
add("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",    0xC013, --p, K_ECDHE_RSA,  B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
add("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",    0xC014, --p, K_ECDHE_RSA,  B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
add("TLS_DHE_RSA_WITH_AES_128_CBC_SHA",      0x0033, --p, K_DHE_RSA,    B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
add("TLS_DHE_RSA_WITH_AES_256_CBC_SHA",      0x0039, --p, K_DHE_RSA,    B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
add("TLS_DHE_DSS_WITH_AES_128_CBC_SHA",      0x0032, --p, K_DHE_DSS,    B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
add("TLS_DHE_DSS_WITH_AES_256_CBC_SHA",      0x0038, --p, K_DHE_DSS,    B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
add("SSL_RSA_WITH_3DES_EDE_CBC_SHA",         0x000a, --p, K_RSA,        B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
add("TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",  0xC003, --p, K_ECDH_ECDSA, B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
add("TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA",    0xC00D, --p, K_ECDH_RSA,   B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
add("TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA", 0xC008, --p, K_ECDHE_ECDSA,B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
add("TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",   0xC012, --p, K_ECDHE_RSA,  B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
add("SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA",     0x0016, --p, K_DHE_RSA,    B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
add("SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA",     0x0013, --p, K_DHE_DSS,    B_3DES,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
add("SSL_RSA_WITH_DES_CBC_SHA",              0x0009, --p, K_RSA,        B_DES,     N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
add("SSL_DHE_RSA_WITH_DES_CBC_SHA",          0x0015, --p, K_DHE_RSA,    B_DES,     N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
add("SSL_DHE_DSS_WITH_DES_CBC_SHA",          0x0012, --p, K_DHE_DSS,    B_DES,     N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
add("SSL_RSA_EXPORT_WITH_RC4_40_MD5",        0x0003, --p, K_RSA_EXPORT, B_RC4_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
add("SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",     0x0008, --p, K_RSA_EXPORT, B_DES_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
add("SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", 0x0014, --p, K_DHE_RSA,    B_DES_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
add("SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", 0x0011, --p, K_DHE_DSS,    B_DES_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // Definition of the CipherSuites that are supported but not enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // They are listed in preference order, preferred first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        p = DEFAULT_SUITES_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
// Anonymous key exchange and the NULL ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
add("SSL_RSA_WITH_NULL_MD5",                 0x0001, --p, K_RSA,        B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
add("SSL_RSA_WITH_NULL_SHA",                 0x0002, --p, K_RSA,        B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
add("TLS_ECDH_ECDSA_WITH_NULL_SHA",          0xC001, --p, K_ECDH_ECDSA, B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
add("TLS_ECDH_RSA_WITH_NULL_SHA",            0xC00B, --p, K_ECDH_RSA,   B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
add("TLS_ECDHE_ECDSA_WITH_NULL_SHA",         0xC006, --p, K_ECDHE_ECDSA,B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
add("TLS_ECDHE_RSA_WITH_NULL_SHA",           0xC010, --p, K_ECDHE_RSA,  B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
add("SSL_DH_anon_WITH_RC4_128_MD5",          0x0018, --p, K_DH_ANON,    B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
add("TLS_DH_anon_WITH_AES_128_CBC_SHA",      0x0034, --p, K_DH_ANON,    B_AES_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
add("TLS_DH_anon_WITH_AES_256_CBC_SHA",      0x003a, --p, K_DH_ANON,    B_AES_256, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
add("SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",     0x001b, --p, K_DH_ANON,    B_3DES,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
add("SSL_DH_anon_WITH_DES_CBC_SHA",          0x001a, --p, K_DH_ANON,    B_DES,     N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
add("TLS_ECDH_anon_WITH_RC4_128_SHA",        0xC016, --p, K_ECDH_ANON,  B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
add("TLS_ECDH_anon_WITH_AES_128_CBC_SHA",    0xC018, --p, K_ECDH_ANON,  B_AES_128, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
add("TLS_ECDH_anon_WITH_AES_256_CBC_SHA",    0xC019, --p, K_ECDH_ANON,  B_AES_256, T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
add("TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",   0xC017, --p, K_ECDH_ANON,  B_3DES,    T);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
add("SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",    0x0017, --p, K_DH_ANON,    B_RC4_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
add("SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", 0x0019, --p, K_DH_ANON,    B_DES_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
add("TLS_ECDH_anon_WITH_NULL_SHA",           0xC015, --p, K_ECDH_ANON,  B_NULL,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
// Supported Kerberos ciphersuites from RFC2712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
add("TLS_KRB5_WITH_RC4_128_SHA",             0x0020, --p, K_KRB5,        B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
add("TLS_KRB5_WITH_RC4_128_MD5",             0x0024, --p, K_KRB5,        B_RC4_128, N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
add("TLS_KRB5_WITH_3DES_EDE_CBC_SHA",        0x001f, --p, K_KRB5,        B_3DES,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
add("TLS_KRB5_WITH_3DES_EDE_CBC_MD5",        0x0023, --p, K_KRB5,        B_3DES,    N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
add("TLS_KRB5_WITH_DES_CBC_SHA",             0x001e, --p, K_KRB5,        B_DES,     N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
add("TLS_KRB5_WITH_DES_CBC_MD5",             0x0022, --p, K_KRB5,        B_DES,     N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
add("TLS_KRB5_EXPORT_WITH_RC4_40_SHA",       0x0028, --p, K_KRB5_EXPORT, B_RC4_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
add("TLS_KRB5_EXPORT_WITH_RC4_40_MD5",       0x002b, --p, K_KRB5_EXPORT, B_RC4_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
add("TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA",   0x0026, --p, K_KRB5_EXPORT, B_DES_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
add("TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5",   0x0029, --p, K_KRB5_EXPORT, B_DES_40,  N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // Register the names of a few additional CipherSuites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // Makes them show up as names instead of numbers in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // the debug output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // remaining unsupported ciphersuites defined in RFC2246.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        add("SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5",      0x0006);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        add("SSL_RSA_WITH_IDEA_CBC_SHA",               0x0007);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        add("SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA",    0x000b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        add("SSL_DH_DSS_WITH_DES_CBC_SHA",             0x000c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        add("SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA",        0x000d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        add("SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA",    0x000e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        add("SSL_DH_RSA_WITH_DES_CBC_SHA",             0x000f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        add("SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA",        0x0010);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // SSL 3.0 Fortezza ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        add("SSL_FORTEZZA_DMS_WITH_NULL_SHA",          0x001c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        add("SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA",  0x001d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // 1024/56 bit exportable ciphersuites from expired internet draft
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        add("SSL_RSA_EXPORT1024_WITH_DES_CBC_SHA",     0x0062);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        add("SSL_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA", 0x0063);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        add("SSL_RSA_EXPORT1024_WITH_RC4_56_SHA",      0x0064);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        add("SSL_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA",  0x0065);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        add("SSL_DHE_DSS_WITH_RC4_128_SHA",            0x0066);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // Netscape old and new SSL 3.0 FIPS ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // see http://www.mozilla.org/projects/security/pki/nss/ssl/fips-ssl-ciphersuites.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        add("NETSCAPE_RSA_FIPS_WITH_3DES_EDE_CBC_SHA", 0xffe0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        add("NETSCAPE_RSA_FIPS_WITH_DES_CBC_SHA",      0xffe1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        add("SSL_RSA_FIPS_WITH_DES_CBC_SHA",           0xfefe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        add("SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA",      0xfeff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // Unsupported Kerberos cipher suites from RFC 2712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        add("TLS_KRB5_WITH_IDEA_CBC_SHA",              0x0021);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        add("TLS_KRB5_WITH_IDEA_CBC_MD5",              0x0025);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        add("TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA",     0x0027);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        add("TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5",     0x002a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // ciphersuite SSL_NULL_WITH_NULL_NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    final static CipherSuite C_NULL = CipherSuite.valueOf(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
}