jdk/src/share/classes/sun/security/ssl/SignatureAndHashAlgorithm.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 7990 57019dc81b66
child 9035 1255eb81cc2f
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:
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     1
/*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     4
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    10
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    15
 * accompanied this code).
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    16
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    20
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    23
 * questions.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    24
 */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    25
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    26
package sun.security.ssl;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    27
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    28
import java.security.AlgorithmConstraints;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    29
import java.security.CryptoPrimitive;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    30
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    31
import java.util.Set;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    32
import java.util.HashSet;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    33
import java.util.Map;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    34
import java.util.EnumSet;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    35
import java.util.TreeMap;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    36
import java.util.Collection;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    37
import java.util.Collections;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    38
import java.util.ArrayList;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    39
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    40
/**
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    41
 * Signature and hash algorithm.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    42
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    43
 * [RFC5246] The client uses the "signature_algorithms" extension to
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    44
 * indicate to the server which signature/hash algorithm pairs may be
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    45
 * used in digital signatures.  The "extension_data" field of this
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    46
 * extension contains a "supported_signature_algorithms" value.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    47
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    48
 *     enum {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    49
 *         none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    50
 *         sha512(6), (255)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    51
 *     } HashAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    52
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    53
 *     enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    54
 *       SignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    55
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    56
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    57
 *           HashAlgorithm hash;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    58
 *           SignatureAlgorithm signature;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    59
 *     } SignatureAndHashAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    60
 */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    61
final class SignatureAndHashAlgorithm {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    62
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    63
    // minimum priority for default enabled algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    64
    final static int SUPPORTED_ALG_PRIORITY_MAX_NUM = 0x00F0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    65
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    66
    // performance optimization
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    67
    private final static Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    68
                                    EnumSet.of(CryptoPrimitive.SIGNATURE);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    69
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    70
    // supported pairs of signature and hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    71
    private final static Map<Integer, SignatureAndHashAlgorithm> supportedMap;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    72
    private final static Map<Integer, SignatureAndHashAlgorithm> priorityMap;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    73
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    74
    // the hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    75
    private HashAlgorithm hash;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    76
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    77
    // the signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    78
    private SignatureAlgorithm signature;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    79
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    80
    // id in 16 bit MSB format, i.e. 0x0603 for SHA512withECDSA
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    81
    private int id;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    82
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    83
    // the standard algorithm name, for example "SHA512withECDSA"
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    84
    private String algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    85
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    86
    // Priority for the preference order. The lower the better.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    87
    //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    88
    // If the algorithm is unsupported, its priority should be bigger
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    89
    // than SUPPORTED_ALG_PRIORITY_MAX_NUM.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    90
    private int priority;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    91
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    92
    // constructor for supported algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    93
    private SignatureAndHashAlgorithm(HashAlgorithm hash,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    94
            SignatureAlgorithm signature, String algorithm, int priority) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    95
        this.hash = hash;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    96
        this.signature = signature;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    97
        this.algorithm = algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    98
        this.id = ((hash.value & 0xFF) << 8) | (signature.value & 0xFF);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
    99
        this.priority = priority;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   100
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   101
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   102
    // constructor for unsupported algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   103
    private SignatureAndHashAlgorithm(String algorithm, int id, int sequence) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   104
        this.hash = HashAlgorithm.valueOf((id >> 8) & 0xFF);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   105
        this.signature = SignatureAlgorithm.valueOf(id & 0xFF);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   106
        this.algorithm = algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   107
        this.id = id;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   108
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   109
        // add one more to the sequece number, in case that the number is zero
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   110
        this.priority = SUPPORTED_ALG_PRIORITY_MAX_NUM + sequence + 1;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   111
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   112
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   113
    // Note that we do not use the sequence argument for supported algorithms,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   114
    // so please don't sort by comparing the objects read from handshake
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   115
    // messages.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   116
    static SignatureAndHashAlgorithm valueOf(int hash,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   117
            int signature, int sequence) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   118
        hash &= 0xFF;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   119
        signature &= 0xFF;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   120
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   121
        int id = (hash << 8) | signature;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   122
        SignatureAndHashAlgorithm signAlg = supportedMap.get(id);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   123
        if (signAlg == null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   124
            // unsupported algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   125
            signAlg = new SignatureAndHashAlgorithm(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   126
                "Unknown (hash:0x" + Integer.toString(hash, 16) +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   127
                ", signature:0x" + Integer.toString(signature, 16) + ")",
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   128
                id, sequence);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   129
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   130
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   131
        return signAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   132
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   133
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   134
    int getHashValue() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   135
        return (id >> 8) & 0xFF;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   136
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   137
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   138
    int getSignatureValue() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   139
        return id & 0xFF;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   140
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   141
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   142
    String getAlgorithmName() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   143
        return algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   144
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   145
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   146
    // return the size of a SignatureAndHashAlgorithm structure in TLS record
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   147
    static int sizeInRecord() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   148
        return 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   149
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   150
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   151
    // Get local supported algorithm collection complying to
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   152
    // algorithm constraints
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   153
    static Collection<SignatureAndHashAlgorithm>
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   154
            getSupportedAlgorithms(AlgorithmConstraints constraints) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   155
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   156
        Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   157
        synchronized (priorityMap) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   158
            for (SignatureAndHashAlgorithm sigAlg : priorityMap.values()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   159
                if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   160
                        constraints.permits(SIGNATURE_PRIMITIVE_SET,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   161
                                sigAlg.algorithm, null)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   162
                    supported.add(sigAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   163
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   164
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   165
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   166
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   167
        return supported;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   168
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   169
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   170
    // Get supported algorithm collection from an untrusted collection
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   171
    static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   172
            Collection<SignatureAndHashAlgorithm> algorithms ) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   173
        Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   174
        for (SignatureAndHashAlgorithm sigAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   175
            if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   176
                supported.add(sigAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   177
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   178
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   179
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   180
        return supported;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   181
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   182
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   183
    static String[] getAlgorithmNames(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   184
            Collection<SignatureAndHashAlgorithm> algorithms) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   185
        ArrayList<String> algorithmNames = new ArrayList<>();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   186
        if (algorithms != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   187
            for (SignatureAndHashAlgorithm sigAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   188
                algorithmNames.add(sigAlg.algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   189
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   190
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   191
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   192
        String[] array = new String[algorithmNames.size()];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   193
        return algorithmNames.toArray(array);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   194
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   195
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   196
    static Set<String> getHashAlgorithmNames(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   197
            Collection<SignatureAndHashAlgorithm> algorithms) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   198
        Set<String> algorithmNames = new HashSet<>();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   199
        if (algorithms != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   200
            for (SignatureAndHashAlgorithm sigAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   201
                if (sigAlg.hash.value > 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   202
                    algorithmNames.add(sigAlg.hash.standardName);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   203
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   204
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   205
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   206
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   207
        return algorithmNames;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   208
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   209
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   210
    static String getHashAlgorithmName(SignatureAndHashAlgorithm algorithm) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   211
        return algorithm.hash.standardName;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   212
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   213
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   214
    private static void supports(HashAlgorithm hash,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   215
            SignatureAlgorithm signature, String algorithm, int priority) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   216
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   217
        SignatureAndHashAlgorithm pair =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   218
            new SignatureAndHashAlgorithm(hash, signature, algorithm, priority);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   219
        if (supportedMap.put(pair.id, pair) != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   220
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   221
                "Duplicate SignatureAndHashAlgorithm definition, id: " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   222
                pair.id);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   223
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   224
        if (priorityMap.put(pair.priority, pair) != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   225
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   226
                "Duplicate SignatureAndHashAlgorithm definition, priority: " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   227
                pair.priority);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   228
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   229
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   230
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   231
    static SignatureAndHashAlgorithm getPreferableAlgorithm(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   232
        Collection<SignatureAndHashAlgorithm> algorithms, String expected) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   233
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   234
        if (expected == null && !algorithms.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   235
            for (SignatureAndHashAlgorithm sigAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   236
                if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   237
                    return sigAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   238
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   239
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   240
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   241
            return null;  // no supported algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   242
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   243
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   244
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   245
        for (SignatureAndHashAlgorithm algorithm : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   246
            int signValue = algorithm.id & 0xFF;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   247
            if ((expected.equalsIgnoreCase("dsa") &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   248
                    signValue == SignatureAlgorithm.DSA.value) ||
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   249
                (expected.equalsIgnoreCase("rsa") &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   250
                    signValue == SignatureAlgorithm.RSA.value) ||
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   251
                (expected.equalsIgnoreCase("ecdsa") &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   252
                    signValue == SignatureAlgorithm.ECDSA.value) ||
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   253
                (expected.equalsIgnoreCase("ec") &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   254
                    signValue == SignatureAlgorithm.ECDSA.value)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   255
                return algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   256
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   257
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   258
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   259
        return null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   260
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   261
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   262
    static enum HashAlgorithm {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   263
        UNDEFINED("undefined",        "", -1),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   264
        NONE(          "none",    "NONE",  0),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   265
        MD5(            "md5",     "MD5",  1),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   266
        SHA1(          "sha1",   "SHA-1",  2),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   267
        SHA224(      "sha224", "SHA-224",  3),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   268
        SHA256(      "sha256", "SHA-256",  4),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   269
        SHA384(      "sha384", "SHA-384",  5),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   270
        SHA512(      "sha512", "SHA-512",  6);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   271
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   272
        final String name;  // not the standard signature algorithm name
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   273
                            // except the UNDEFINED, other names are defined
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   274
                            // by TLS 1.2 protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   275
        final String standardName; // the standard MessageDigest algorithm name
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   276
        final int value;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   277
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   278
        private HashAlgorithm(String name, String standardName, int value) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   279
            this.name = name;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   280
            this.standardName = standardName;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   281
            this.value = value;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   282
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   283
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   284
        static HashAlgorithm valueOf(int value) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   285
            HashAlgorithm algorithm = UNDEFINED;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   286
            switch (value) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   287
                case 0:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   288
                    algorithm = NONE;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   289
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   290
                case 1:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   291
                    algorithm = MD5;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   292
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   293
                case 2:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   294
                    algorithm = SHA1;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   295
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   296
                case 3:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   297
                    algorithm = SHA224;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   298
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   299
                case 4:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   300
                    algorithm = SHA256;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   301
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   302
                case 5:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   303
                    algorithm = SHA384;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   304
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   305
                case 6:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   306
                    algorithm = SHA512;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   307
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   308
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   309
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   310
            return algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   311
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   312
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   313
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   314
    static enum SignatureAlgorithm {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   315
        UNDEFINED("undefined", -1),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   316
        ANONYMOUS("anonymous",  0),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   317
        RSA(            "rsa",  1),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   318
        DSA(            "dsa",  2),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   319
        ECDSA(        "ecdsa",  3);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   320
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   321
        final String name;  // not the standard signature algorithm name
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   322
                            // except the UNDEFINED, other names are defined
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   323
                            // by TLS 1.2 protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   324
        final int value;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   325
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   326
        private SignatureAlgorithm(String name, int value) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   327
            this.name = name;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   328
            this.value = value;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   329
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   330
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   331
        static SignatureAlgorithm valueOf(int value) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   332
            SignatureAlgorithm algorithm = UNDEFINED;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   333
            switch (value) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   334
                case 0:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   335
                    algorithm = ANONYMOUS;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   336
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   337
                case 1:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   338
                    algorithm = RSA;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   339
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   340
                case 2:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   341
                    algorithm = DSA;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   342
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   343
                case 3:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   344
                    algorithm = ECDSA;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   345
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   346
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   347
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   348
            return algorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   349
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   350
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   351
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   352
    static {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   353
        supportedMap = Collections.synchronizedSortedMap(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   354
            new TreeMap<Integer, SignatureAndHashAlgorithm>());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   355
        priorityMap = Collections.synchronizedSortedMap(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   356
            new TreeMap<Integer, SignatureAndHashAlgorithm>());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   357
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   358
        synchronized (supportedMap) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   359
            int p = SUPPORTED_ALG_PRIORITY_MAX_NUM;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   360
            supports(HashAlgorithm.MD5,         SignatureAlgorithm.RSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   361
                    "MD5withRSA",           --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   362
            supports(HashAlgorithm.SHA1,        SignatureAlgorithm.DSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   363
                    "SHA1withDSA",          --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   364
            supports(HashAlgorithm.SHA1,        SignatureAlgorithm.RSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   365
                    "SHA1withRSA",          --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   366
            supports(HashAlgorithm.SHA1,        SignatureAlgorithm.ECDSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   367
                    "SHA1withECDSA",        --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   368
            supports(HashAlgorithm.SHA224,      SignatureAlgorithm.RSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   369
                    "SHA224withRSA",        --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   370
            supports(HashAlgorithm.SHA224,      SignatureAlgorithm.ECDSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   371
                    "SHA224withECDSA",      --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   372
            supports(HashAlgorithm.SHA256,      SignatureAlgorithm.RSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   373
                    "SHA256withRSA",        --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   374
            supports(HashAlgorithm.SHA256,      SignatureAlgorithm.ECDSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   375
                    "SHA256withECDSA",      --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   376
            supports(HashAlgorithm.SHA384,      SignatureAlgorithm.RSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   377
                    "SHA384withRSA",        --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   378
            supports(HashAlgorithm.SHA384,      SignatureAlgorithm.ECDSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   379
                    "SHA384withECDSA",      --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   380
            supports(HashAlgorithm.SHA512,      SignatureAlgorithm.RSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   381
                    "SHA512withRSA",        --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   382
            supports(HashAlgorithm.SHA512,      SignatureAlgorithm.ECDSA,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   383
                    "SHA512withECDSA",      --p);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   384
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   385
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   386
}
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents:
diff changeset
   387