src/java.base/share/classes/sun/security/ssl/SignatureScheme.java
author xuelei
Fri, 08 Jun 2018 12:48:32 -0700
branchJDK-8145252-TLS13-branch
changeset 56710 f4c7a97a1275
parent 56706 a82a96b62d22
child 56738 0811eaea3cd4
permissions -rw-r--r--
cleanup on SignatureScheme.java
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.security.*;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    29
import java.security.interfaces.ECPrivateKey;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    31
import java.security.spec.ECParameterSpec;
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
    32
import java.security.spec.MGF1ParameterSpec;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import java.security.spec.PSSParameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import java.util.ArrayList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import java.util.Arrays;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import java.util.Collection;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
import java.util.Collections;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import java.util.EnumSet;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
import java.util.LinkedList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
import java.util.List;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
import java.util.Set;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    42
import sun.security.ssl.SupportedGroupsExtension.NamedGroup;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    43
import sun.security.ssl.SupportedGroupsExtension.NamedGroupType;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
import sun.security.util.KeyUtil;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
enum SignatureScheme {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
    // EdDSA algorithms
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
    ED25519                 (0x0807, "ed25519", "ed25519",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
                                    "ed25519",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
    ED448                   (0x0808, "ed448", "ed448",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
                                    "ed448",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
    // ECDSA algorithms
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
    ECDSA_SECP256R1_SHA256  (0x0403, "ecdsa_secp256r1_sha256",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
                                    "SHA256withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
                                    "EC",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    59
                                    NamedGroup.SECP256_R1,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
                                    ProtocolVersion.PROTOCOLS_TO_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
    ECDSA_SECP384R1_SHA384  (0x0503, "ecdsa_secp384r1_sha384",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
                                    "SHA384withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
                                    "EC",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    64
                                    NamedGroup.SECP384_R1,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
                                    ProtocolVersion.PROTOCOLS_TO_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
    ECDSA_SECP512R1_SHA512  (0x0603, "ecdsa_secp512r1_sha512",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
                                    "SHA512withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
                                    "EC",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
    69
                                    NamedGroup.SECP521_R1,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
                                    ProtocolVersion.PROTOCOLS_TO_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
    // RSASSA-PSS algorithms with public key OID rsaEncryption
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
    RSA_PSS_RSAE_SHA256     (0x0804, "rsa_pss_rsae_sha256",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
                                    "RSASSA-PSS", "RSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
                                    SigAlgParamSpec.RSA_PSS_SHA256, 512,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
    RSA_PSS_RSAE_SHA384     (0x0805, "rsa_pss_rsae_sha384",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
                                    "RSASSA-PSS", "RSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
                                    SigAlgParamSpec.RSA_PSS_SHA384, 768,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
    RSA_PSS_RSAE_SHA512     (0x0806, "rsa_pss_rsae_sha512",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    82
                                    "RSASSA-PSS", "RSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
                                    SigAlgParamSpec.RSA_PSS_SHA512, 768,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    86
    // RSASSA-PSS algorithms with public key OID RSASSA-PSS
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    87
    RSA_PSS_PSS_SHA256      (0x0809, "rsa_pss_pss_sha256",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
                                    "RSASSA-PSS", "RSASSA-PSS",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
                                    SigAlgParamSpec.RSA_PSS_SHA256, 512,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    90
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    91
    RSA_PSS_PSS_SHA384      (0x080A, "rsa_pss_pss_sha384",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
                                    "RSASSA-PSS", "RSASSA-PSS",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
                                    SigAlgParamSpec.RSA_PSS_SHA384, 768,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
    RSA_PSS_PSS_SHA512      (0x080B, "rsa_pss_pss_sha512",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
                                    "RSASSA-PSS", "RSASSA-PSS",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
                                    SigAlgParamSpec.RSA_PSS_SHA512, 768,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
                                    ProtocolVersion.PROTOCOLS_OF_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
    // RSASSA-PKCS1-v1_5 algorithms
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
    RSA_PKCS1_SHA256        (0x0401, "rsa_pkcs1_sha256", "SHA256withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   102
                                    "RSA", null, null, 512,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
    RSA_PKCS1_SHA384        (0x0501, "rsa_pkcs1_sha384", "SHA384withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   106
                                    "RSA", null, null, 768,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   108
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
    RSA_PKCS1_SHA512        (0x0601, "rsa_pkcs1_sha512", "SHA512withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   110
                                    "RSA", null, null, 768,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
    // Legacy algorithms
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
    DSA_SHA256              (0x0402, "dsa_sha256", "SHA256withDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   116
                                    "dsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   117
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
    ECDSA_SHA224            (0x0303, "ecdsa_sha224", "SHA224withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
                                    "EC",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
    RSA_SHA224              (0x0301, "rsa_sha224", "SHA224withRSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
                                    "rsa", 768,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
    DSA_SHA224              (0x0302, "dsa_sha224", "SHA224withDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
                                    "dsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
    ECDSA_SHA1              (0x0203, "ecdsa_sha1", "SHA1withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
                                    "EC",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
                                    ProtocolVersion.PROTOCOLS_TO_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
    RSA_PKCS1_SHA1          (0x0201, "rsa_pkcs1_sha1", "SHA1withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   131
                                    "rsa", null, null, 512,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
    DSA_SHA1                (0x0202, "dsa_sha1", "SHA1withDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
                                    "dsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   137
    RSA_MD5                 (0x0101, "rsa_md5", "MD5withRSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
                                    "rsa", 512,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
                                    ProtocolVersion.PROTOCOLS_TO_12);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
    final int id;                       // hash + signature
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
    final String name;                  // literal name
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
    private final String algorithm;     // signature algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
    final String keyAlgorithm;          // signature key algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
    private final AlgorithmParameterSpec signAlgParameter;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   146
    private final NamedGroup namedGroup;    // associated named group
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   148
    // The minimal required key size in bits.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
    //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
    // Only need to check RSA algorithm at present. RSA keys of 512 bits
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
    // have been shown to be practically breakable, it does not make much
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   152
    // sense to use the strong hash algorithm for keys whose key size less
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   153
    // than 512 bits.  So it is not necessary to calculate the minimal
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   154
    // required key size exactly for a hash algorithm.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
    final int minimalKeySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   156
    final List<ProtocolVersion> supportedProtocols;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   157
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
    // Some signature schemes are supported in different versions for handshake
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   159
    // messages and certificates. This field holds the supported protocols
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
    // for handshake messages.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
    final List<ProtocolVersion> handshakeSupportedProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
    final boolean isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
    private static final String[] hashAlgorithms = new String[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
            "none",         "md5",      "sha1",     "sha224",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
            "sha256",       "sha384",   "sha512"
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
    private static final String[] signatureAlgorithms = new String[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
            "anonymous",    "rsa",      "dsa",      "ecdsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
    static enum SigAlgParamSpec {   // support RSASSA-PSS only now
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
        RSA_PSS_SHA256 ("SHA-256", 32),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
        RSA_PSS_SHA384 ("SHA-384", 48),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
        RSA_PSS_SHA512 ("SHA-512", 64);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
        final private AlgorithmParameterSpec parameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
        final boolean isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
        SigAlgParamSpec(String hash, int saltLength) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
            // See RFC 8017
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
            PSSParameterSpec pssParamSpec =
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   184
                    new PSSParameterSpec(hash, "MGF1",
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   185
                            new MGF1ParameterSpec(hash), saltLength, 1);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
            boolean mediator = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
                Signature signer = JsseJce.getSignature("RSASSA-PSS");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
                signer.setParameter(pssParamSpec);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   191
            } catch (InvalidAlgorithmParameterException |
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   192
                    NoSuchAlgorithmException exp) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   193
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
                        "RSASSA-PSS signature with " + hash +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
                        " is not supported by the underlying providers", exp);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
            this.isAvailable = mediator;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
            this.parameterSpec = mediator ? pssParamSpec : null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
        AlgorithmParameterSpec getParameterSpec() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
            return parameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
    // performance optimization
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
    private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
        Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
            ProtocolVersion[] supportedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
        this(id, name, algorithm, keyAlgorithm, -1, supportedProtocols);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   219
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   222
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   223
            int minimalKeySize,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
            ProtocolVersion[] supportedProtocols) {
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   225
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   226
                null, minimalKeySize, supportedProtocols);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   231
            SigAlgParamSpec signAlgParamSpec, int minimalKeySize,
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   232
            ProtocolVersion[] supportedProtocols) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   233
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   234
                signAlgParamSpec, null, minimalKeySize,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   235
                supportedProtocols, supportedProtocols);
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   236
    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   237
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   238
    private SignatureScheme(int id, String name,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   239
            String algorithm, String keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   240
            NamedGroup namedGroup,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   241
            ProtocolVersion[] supportedProtocols) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   242
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   243
                null, namedGroup, -1,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   244
                supportedProtocols, supportedProtocols);
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   245
    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   246
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   247
    private SignatureScheme(int id, String name,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   248
            String algorithm, String keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   249
            SigAlgParamSpec signAlgParamSpec,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   250
            NamedGroup namedGroup, int minimalKeySize,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
            ProtocolVersion[] supportedProtocols,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
            ProtocolVersion[] handshakeSupportedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
        this.id = id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   254
        this.name = name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   255
        this.algorithm = algorithm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   256
        this.keyAlgorithm = keyAlgorithm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
        this.signAlgParameter =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
            signAlgParamSpec != null ? signAlgParamSpec.parameterSpec : null;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   259
        this.namedGroup = namedGroup;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
        this.minimalKeySize = minimalKeySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
        this.supportedProtocols = Arrays.asList(supportedProtocols);
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   262
        this.handshakeSupportedProtocols =
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   263
                Arrays.asList(handshakeSupportedProtocols);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
        boolean mediator = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
        if (signAlgParamSpec != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
            mediator = signAlgParamSpec.isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
                JsseJce.getSignature(algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
            } catch (Exception e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
                        "Signature algorithm, " + algorithm +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
                        ", is not supported by the underlying providers");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
        if (mediator && ((id >> 8) & 0xFF) == 0x03) {   // SHA224
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
            // There are some problems to use SHA224 on Windows.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
            if (Security.getProvider("SunMSCAPI") != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
        this.isAvailable = mediator;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
    static SignatureScheme valueOf(int id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
            if (ss.id == id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
                return ss;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
    static String nameOf(int id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
            if (ss.id == id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
                return ss.name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
        // Use TLS 1.2 style name for unknown signature scheme.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
        int hashId = ((id >> 8) & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
        int signId = (id & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
        String hashName = (hashId >= hashAlgorithms.length) ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
            "UNDEFINED-HASH(" + hashId + ")" : hashAlgorithms[hashId];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
        String signName = (signId >= signatureAlgorithms.length) ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
            "UNDEFINED-SIGNATURE(" + signId + ")" :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
            signatureAlgorithms[signId];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
        return signName + "_" + hashName;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
    // Return the size of a SignatureScheme structure in TLS record
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
    static int sizeInRecord() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
        return 2;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
    // Get local supported algorithm collection complying to algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
    // constraints.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
    static List<SignatureScheme> getSupportedAlgorithms(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
            AlgorithmConstraints constraints,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
            List<ProtocolVersion> activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
        List<SignatureScheme> supported = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
            if (!ss.isAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
                continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
            boolean isMatch = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
            for (ProtocolVersion pv : activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
                if (ss.supportedProtocols.contains(pv)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
                    isMatch = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
            if (isMatch) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
                if (constraints.permits(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
                        SIGNATURE_PRIMITIVE_SET, ss.algorithm, null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
                    supported.add(ss);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
                } else if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
                        SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
                    SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
                        "Ignore disabled signature sheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
            } else if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
                    SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
                SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
                    "Ignore inactive signature sheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
        return supported;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
    static List<SignatureScheme> getSupportedAlgorithms(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
            AlgorithmConstraints constraints,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
            ProtocolVersion protocolVersion, int[] algorithmIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
        List<SignatureScheme> supported = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
        for (int ssid : algorithmIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
            SignatureScheme ss = SignatureScheme.valueOf(ssid);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
            if (ss == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
                            "Unsupported signature scheme: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
                            SignatureScheme.nameOf(ssid));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
            } else if (ss.isAvailable &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
                    ss.supportedProtocols.contains(protocolVersion) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
                    constraints.permits(SIGNATURE_PRIMITIVE_SET,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
                           ss.algorithm, null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                supported.add(ss);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
                            "Unsupported signature scheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
        return supported;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
    static SignatureScheme getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
            List<SignatureScheme> schemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
            SignatureScheme certScheme,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
            ProtocolVersion version) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
        for (SignatureScheme ss : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
            if (ss.isAvailable &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
            ss.handshakeSupportedProtocols.contains(version) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
            certScheme.keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
                return ss;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
    static SignatureScheme getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
            List<SignatureScheme> schemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
            PrivateKey signingKey,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
            ProtocolVersion version) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
        String keyAlgorithm = signingKey.getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
        int keySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
        // Only need to check RSA algorithm at present.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
        if (keyAlgorithm.equalsIgnoreCase("rsa")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            keySize = KeyUtil.getKeySize(signingKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
            keySize = Integer.MAX_VALUE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
        for (SignatureScheme ss : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
            if (ss.isAvailable && (keySize >= ss.minimalKeySize) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
                ss.handshakeSupportedProtocols.contains(version) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
                keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) {
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   425
                if (ss.namedGroup != null &&
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   426
                    ss.namedGroup.type == NamedGroupType.NAMED_GROUP_ECDHE) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   427
                    ECParameterSpec params =
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   428
                                ((ECPrivateKey)signingKey).getParams();
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   429
                    if (ss.namedGroup == NamedGroup.valueOf(params)) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   430
                        return ss;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   431
                    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   432
                } else {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   433
                    return ss;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   434
                }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   435
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   436
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   437
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   438
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   439
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   440
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   441
    static String[] getAlgorithmNames(Collection<SignatureScheme> schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
        if (schemes != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   443
            ArrayList<String> names = new ArrayList<>(schemes.size());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
            for (SignatureScheme scheme : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
                names.add(scheme.algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
            return names.toArray(new String[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
        return new String[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
    Signature getSignature() throws NoSuchAlgorithmException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
            InvalidAlgorithmParameterException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
        if (!isAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   458
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   459
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
        Signature signer = JsseJce.getSignature(algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
        if (signAlgParameter != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
            signer.setParameter(signAlgParameter);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   463
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
        return signer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
}