src/java.base/share/classes/sun/security/ssl/SignatureScheme.java
author xuelei
Tue, 12 Jun 2018 22:15:10 -0700
branchJDK-8145252-TLS13-branch
changeset 56750 2b4ae319412b
parent 56738 0811eaea3cd4
child 56804 63ab0dfe3dbb
permissions -rw-r--r--
Enable RSASSA-PSS for TLS 1.2, and socket close checking
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
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    73
    //
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    74
    // The minimalKeySize is calculated as (See RFC 8017 for details):
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    75
    //     hash length + salt length + 16
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
    RSA_PSS_RSAE_SHA256     (0x0804, "rsa_pss_rsae_sha256",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
                                    "RSASSA-PSS", "RSA",
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    78
                                    SigAlgParamSpec.RSA_PSS_SHA256, 528,
56750
2b4ae319412b Enable RSASSA-PSS for TLS 1.2, and socket close checking
xuelei
parents: 56738
diff changeset
    79
                                    ProtocolVersion.PROTOCOLS_TO_13),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
    RSA_PSS_RSAE_SHA384     (0x0805, "rsa_pss_rsae_sha384",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
                                    "RSASSA-PSS", "RSA",
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    82
                                    SigAlgParamSpec.RSA_PSS_SHA384, 784,
56750
2b4ae319412b Enable RSASSA-PSS for TLS 1.2, and socket close checking
xuelei
parents: 56738
diff changeset
    83
                                    ProtocolVersion.PROTOCOLS_TO_13),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
    RSA_PSS_RSAE_SHA512     (0x0806, "rsa_pss_rsae_sha512",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
                                    "RSASSA-PSS", "RSA",
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    86
                                    SigAlgParamSpec.RSA_PSS_SHA512, 1040,
56750
2b4ae319412b Enable RSASSA-PSS for TLS 1.2, and socket close checking
xuelei
parents: 56738
diff changeset
    87
                                    ProtocolVersion.PROTOCOLS_TO_13),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
    // RSASSA-PSS algorithms with public key OID RSASSA-PSS
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    90
    //
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    91
    // The minimalKeySize is calculated as (See RFC 8017 for details):
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    92
    //     hash length + salt length + 16
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
    RSA_PSS_PSS_SHA256      (0x0809, "rsa_pss_pss_sha256",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
                                    "RSASSA-PSS", "RSASSA-PSS",
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    95
                                    SigAlgParamSpec.RSA_PSS_SHA256, 528,
56750
2b4ae319412b Enable RSASSA-PSS for TLS 1.2, and socket close checking
xuelei
parents: 56738
diff changeset
    96
                                    ProtocolVersion.PROTOCOLS_TO_13),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
    RSA_PSS_PSS_SHA384      (0x080A, "rsa_pss_pss_sha384",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
                                    "RSASSA-PSS", "RSASSA-PSS",
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
    99
                                    SigAlgParamSpec.RSA_PSS_SHA384, 784,
56750
2b4ae319412b Enable RSASSA-PSS for TLS 1.2, and socket close checking
xuelei
parents: 56738
diff changeset
   100
                                    ProtocolVersion.PROTOCOLS_TO_13),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
    RSA_PSS_PSS_SHA512      (0x080B, "rsa_pss_pss_sha512",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
                                    "RSASSA-PSS", "RSASSA-PSS",
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
   103
                                    SigAlgParamSpec.RSA_PSS_SHA512, 1040,
56750
2b4ae319412b Enable RSASSA-PSS for TLS 1.2, and socket close checking
xuelei
parents: 56738
diff changeset
   104
                                    ProtocolVersion.PROTOCOLS_TO_13),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
    // RSASSA-PKCS1-v1_5 algorithms
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
    RSA_PKCS1_SHA256        (0x0401, "rsa_pkcs1_sha256", "SHA256withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   108
                                    "RSA", null, null, 512,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
    RSA_PKCS1_SHA384        (0x0501, "rsa_pkcs1_sha384", "SHA384withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   112
                                    "RSA", null, null, 768,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
    RSA_PKCS1_SHA512        (0x0601, "rsa_pkcs1_sha512", "SHA512withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   116
                                    "RSA", null, null, 768,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   117
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
    // Legacy algorithms
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
    DSA_SHA256              (0x0402, "dsa_sha256", "SHA256withDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
                                    "dsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
    ECDSA_SHA224            (0x0303, "ecdsa_sha224", "SHA224withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
                                    "EC",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
    RSA_SHA224              (0x0301, "rsa_sha224", "SHA224withRSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
                                    "rsa", 768,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
    DSA_SHA224              (0x0302, "dsa_sha224", "SHA224withDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   131
                                    "dsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
    ECDSA_SHA1              (0x0203, "ecdsa_sha1", "SHA1withECDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
                                    "EC",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
                                    ProtocolVersion.PROTOCOLS_TO_13),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
    RSA_PKCS1_SHA1          (0x0201, "rsa_pkcs1_sha1", "SHA1withRSA",
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   137
                                    "rsa", null, null, 512,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
                                    ProtocolVersion.PROTOCOLS_TO_13,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
    DSA_SHA1                (0x0202, "dsa_sha1", "SHA1withDSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
                                    "dsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
                                    ProtocolVersion.PROTOCOLS_TO_12),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
    RSA_MD5                 (0x0101, "rsa_md5", "MD5withRSA",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
                                    "rsa", 512,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
                                    ProtocolVersion.PROTOCOLS_TO_12);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   146
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
    final int id;                       // hash + signature
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   148
    final String name;                  // literal name
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
    private final String algorithm;     // signature algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
    final String keyAlgorithm;          // signature key algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
    private final AlgorithmParameterSpec signAlgParameter;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   152
    private final NamedGroup namedGroup;    // associated named group
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   153
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   154
    // The minimal required key size in bits.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
    //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   156
    // Only need to check RSA algorithm at present. RSA keys of 512 bits
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   157
    // have been shown to be practically breakable, it does not make much
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
    // sense to use the strong hash algorithm for keys whose key size less
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   159
    // than 512 bits.  So it is not necessary to calculate the minimal
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
    // required key size exactly for a hash algorithm.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
    final int minimalKeySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
    final List<ProtocolVersion> supportedProtocols;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   163
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
    // Some signature schemes are supported in different versions for handshake
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
    // messages and certificates. This field holds the supported protocols
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
    // for handshake messages.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
    final List<ProtocolVersion> handshakeSupportedProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
    final boolean isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
    private static final String[] hashAlgorithms = new String[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
            "none",         "md5",      "sha1",     "sha224",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
            "sha256",       "sha384",   "sha512"
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
    private static final String[] signatureAlgorithms = new String[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
            "anonymous",    "rsa",      "dsa",      "ecdsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
    static enum SigAlgParamSpec {   // support RSASSA-PSS only now
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
        RSA_PSS_SHA256 ("SHA-256", 32),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
        RSA_PSS_SHA384 ("SHA-384", 48),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
        RSA_PSS_SHA512 ("SHA-512", 64);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
        final private AlgorithmParameterSpec parameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
        final boolean isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
        SigAlgParamSpec(String hash, int saltLength) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
            // See RFC 8017
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
            PSSParameterSpec pssParamSpec =
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   190
                    new PSSParameterSpec(hash, "MGF1",
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   191
                            new MGF1ParameterSpec(hash), saltLength, 1);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   192
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   193
            boolean mediator = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
                Signature signer = JsseJce.getSignature("RSASSA-PSS");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
                signer.setParameter(pssParamSpec);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
            } catch (InvalidAlgorithmParameterException |
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
                    NoSuchAlgorithmException exp) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
                        "RSASSA-PSS signature with " + hash +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
                        " is not supported by the underlying providers", exp);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
            this.isAvailable = mediator;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
            this.parameterSpec = mediator ? pssParamSpec : null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
        AlgorithmParameterSpec getParameterSpec() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
            return parameterSpec;
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
    // performance optimization
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
    private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
        Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
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
            ProtocolVersion[] supportedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
        this(id, name, algorithm, keyAlgorithm, -1, supportedProtocols);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   225
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
            int minimalKeySize,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
            ProtocolVersion[] supportedProtocols) {
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   231
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   232
                null, minimalKeySize, supportedProtocols);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   233
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   234
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
            SigAlgParamSpec signAlgParamSpec, int minimalKeySize,
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   238
            ProtocolVersion[] supportedProtocols) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   239
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   240
                signAlgParamSpec, null, minimalKeySize,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   241
                supportedProtocols, supportedProtocols);
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   242
    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   243
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   244
    private SignatureScheme(int id, String name,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   245
            String algorithm, String keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   246
            NamedGroup namedGroup,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   247
            ProtocolVersion[] supportedProtocols) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   248
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   249
                null, namedGroup, -1,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   250
                supportedProtocols, supportedProtocols);
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   251
    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   252
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   253
    private SignatureScheme(int id, String name,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   254
            String algorithm, String keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   255
            SigAlgParamSpec signAlgParamSpec,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   256
            NamedGroup namedGroup, int minimalKeySize,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
            ProtocolVersion[] supportedProtocols,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
            ProtocolVersion[] handshakeSupportedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
        this.id = id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
        this.name = name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
        this.algorithm = algorithm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
        this.keyAlgorithm = keyAlgorithm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   263
        this.signAlgParameter =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
            signAlgParamSpec != null ? signAlgParamSpec.parameterSpec : null;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   265
        this.namedGroup = namedGroup;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
        this.minimalKeySize = minimalKeySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
        this.supportedProtocols = Arrays.asList(supportedProtocols);
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   268
        this.handshakeSupportedProtocols =
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   269
                Arrays.asList(handshakeSupportedProtocols);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
        boolean mediator = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
        if (signAlgParamSpec != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
            mediator = signAlgParamSpec.isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
                JsseJce.getSignature(algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
            } catch (Exception e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
                        "Signature algorithm, " + algorithm +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
                        ", is not supported by the underlying providers");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
            }
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
        if (mediator && ((id >> 8) & 0xFF) == 0x03) {   // SHA224
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
            // There are some problems to use SHA224 on Windows.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
            if (Security.getProvider("SunMSCAPI") != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
        this.isAvailable = mediator;
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
    static SignatureScheme valueOf(int id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
            if (ss.id == id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
                return ss;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
        return null;
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
    static String nameOf(int id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
            if (ss.id == id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
                return ss.name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
        // Use TLS 1.2 style name for unknown signature scheme.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
        int hashId = ((id >> 8) & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
        int signId = (id & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
        String hashName = (hashId >= hashAlgorithms.length) ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
            "UNDEFINED-HASH(" + hashId + ")" : hashAlgorithms[hashId];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
        String signName = (signId >= signatureAlgorithms.length) ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
            "UNDEFINED-SIGNATURE(" + signId + ")" :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
            signatureAlgorithms[signId];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
        return signName + "_" + hashName;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
    // Return the size of a SignatureScheme structure in TLS record
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
    static int sizeInRecord() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
        return 2;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
    // Get local supported algorithm collection complying to algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
    // constraints.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
    static List<SignatureScheme> getSupportedAlgorithms(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
            AlgorithmConstraints constraints,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
            List<ProtocolVersion> activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
        List<SignatureScheme> supported = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
            if (!ss.isAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
                continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
            boolean isMatch = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
            for (ProtocolVersion pv : activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
                if (ss.supportedProtocols.contains(pv)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
                    isMatch = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
            if (isMatch) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
                if (constraints.permits(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
                        SIGNATURE_PRIMITIVE_SET, ss.algorithm, null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
                    supported.add(ss);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
                } else if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
                        SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
                    SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
                        "Ignore disabled signature sheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
            } else if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
                    SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
                SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
                    "Ignore inactive signature sheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
        return supported;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
    static List<SignatureScheme> getSupportedAlgorithms(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
            AlgorithmConstraints constraints,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
            ProtocolVersion protocolVersion, int[] algorithmIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
        List<SignatureScheme> supported = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
        for (int ssid : algorithmIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
            SignatureScheme ss = SignatureScheme.valueOf(ssid);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
            if (ss == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
                            "Unsupported signature scheme: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                            SignatureScheme.nameOf(ssid));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
            } else if (ss.isAvailable &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
                    ss.supportedProtocols.contains(protocolVersion) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
                    constraints.permits(SIGNATURE_PRIMITIVE_SET,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
                           ss.algorithm, null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
                supported.add(ss);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
                            "Unsupported signature scheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
        return supported;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
    static SignatureScheme getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
            List<SignatureScheme> schemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
            SignatureScheme certScheme,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
            ProtocolVersion version) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
        for (SignatureScheme ss : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
            if (ss.isAvailable &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
            ss.handshakeSupportedProtocols.contains(version) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
            certScheme.keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
                return ss;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
    static SignatureScheme getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
            List<SignatureScheme> schemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
            PrivateKey signingKey,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            ProtocolVersion version) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
        String keyAlgorithm = signingKey.getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
        int keySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
        // Only need to check RSA algorithm at present.
56738
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
   422
        if (keyAlgorithm.equalsIgnoreCase("RSA") ||
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
   423
                keyAlgorithm.equalsIgnoreCase("RSASSA-PSS")) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
            keySize = KeyUtil.getKeySize(signingKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   425
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
            keySize = Integer.MAX_VALUE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
        for (SignatureScheme ss : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
            if (ss.isAvailable && (keySize >= ss.minimalKeySize) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
                ss.handshakeSupportedProtocols.contains(version) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
                keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) {
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   432
                if (ss.namedGroup != null &&
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   433
                    ss.namedGroup.type == NamedGroupType.NAMED_GROUP_ECDHE) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   434
                    ECParameterSpec params =
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   435
                                ((ECPrivateKey)signingKey).getParams();
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   436
                    if (ss.namedGroup == NamedGroup.valueOf(params)) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   437
                        return ss;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   438
                    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   439
                } else {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   440
                    return ss;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   441
                }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   443
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
        return null;
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
    static String[] getAlgorithmNames(Collection<SignatureScheme> schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
        if (schemes != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
            ArrayList<String> names = new ArrayList<>(schemes.size());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
            for (SignatureScheme scheme : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
                names.add(scheme.algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
            return names.toArray(new String[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   458
        return new String[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   459
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
    Signature getSignature() throws NoSuchAlgorithmException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
            InvalidAlgorithmParameterException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   463
        if (!isAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
        Signature signer = JsseJce.getSignature(algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
        if (signAlgParameter != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
            signer.setParameter(signAlgParameter);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   470
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   471
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   472
        return signer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   473
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   474
}