src/java.base/share/classes/sun/security/ssl/SignatureScheme.java
author xuelei
Mon, 25 Jun 2018 08:14:11 -0700
branchJDK-8145252-TLS13-branch
changeset 56806 32a737f51e37
parent 56804 63ab0dfe3dbb
permissions -rw-r--r--
Support RSASSS-PSS for TLS 1.2
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,
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56804
diff changeset
    79
                                    ProtocolVersion.PROTOCOLS_12_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,
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56804
diff changeset
    83
                                    ProtocolVersion.PROTOCOLS_12_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,
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56804
diff changeset
    87
                                    ProtocolVersion.PROTOCOLS_12_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,
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56804
diff changeset
    96
                                    ProtocolVersion.PROTOCOLS_12_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,
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56804
diff changeset
   100
                                    ProtocolVersion.PROTOCOLS_12_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,
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56804
diff changeset
   104
                                    ProtocolVersion.PROTOCOLS_12_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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   108
                                    "RSA", null, null, 511,
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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   122
                                    "DSA",
56542
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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   128
                                    "RSA", 511,
56542
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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   131
                                    "DSA",
56542
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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   137
                                    "RSA", null, null, 511,
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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   141
                                    "DSA",
56542
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",
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   144
                                    "RSA", 511,
56542
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.
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   161
    //
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   162
    // Note that some provider may use 511 bits for 512-bit strength RSA keys.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
    final int minimalKeySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
    final List<ProtocolVersion> supportedProtocols;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   165
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
    // Some signature schemes are supported in different versions for handshake
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
    // messages and certificates. This field holds the supported protocols
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
    // for handshake messages.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
    final List<ProtocolVersion> handshakeSupportedProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
    final boolean isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
    private static final String[] hashAlgorithms = new String[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
            "none",         "md5",      "sha1",     "sha224",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
            "sha256",       "sha384",   "sha512"
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
    private static final String[] signatureAlgorithms = new String[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
            "anonymous",    "rsa",      "dsa",      "ecdsa",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
    static enum SigAlgParamSpec {   // support RSASSA-PSS only now
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
        RSA_PSS_SHA256 ("SHA-256", 32),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
        RSA_PSS_SHA384 ("SHA-384", 48),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
        RSA_PSS_SHA512 ("SHA-512", 64);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
        final private AlgorithmParameterSpec parameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
        final boolean isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
        SigAlgParamSpec(String hash, int saltLength) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
            // See RFC 8017
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   191
            PSSParameterSpec pssParamSpec =
56710
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   192
                    new PSSParameterSpec(hash, "MGF1",
f4c7a97a1275 cleanup on SignatureScheme.java
xuelei
parents: 56706
diff changeset
   193
                            new MGF1ParameterSpec(hash), saltLength, 1);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
            boolean mediator = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
                Signature signer = JsseJce.getSignature("RSASSA-PSS");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
                signer.setParameter(pssParamSpec);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
            } catch (InvalidAlgorithmParameterException |
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
                    NoSuchAlgorithmException exp) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
                        "RSASSA-PSS signature with " + hash +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
                        " is not supported by the underlying providers", exp);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
                }
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
            this.isAvailable = mediator;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
            this.parameterSpec = mediator ? pssParamSpec : null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
        AlgorithmParameterSpec getParameterSpec() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
            return parameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
    // performance optimization
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   219
    private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
        Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   222
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   223
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   225
            ProtocolVersion[] supportedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
        this(id, name, algorithm, keyAlgorithm, -1, supportedProtocols);
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
            int minimalKeySize,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   232
            ProtocolVersion[] supportedProtocols) {
56706
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
                null, minimalKeySize, supportedProtocols);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
    private SignatureScheme(int id, String name,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   238
            String algorithm, String keyAlgorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
            SigAlgParamSpec signAlgParamSpec, int minimalKeySize,
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   240
            ProtocolVersion[] supportedProtocols) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   241
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   242
                signAlgParamSpec, null, minimalKeySize,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   243
                supportedProtocols, supportedProtocols);
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   244
    }
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
    private SignatureScheme(int id, String name,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   247
            String algorithm, String keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   248
            NamedGroup namedGroup,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   249
            ProtocolVersion[] supportedProtocols) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   250
        this(id, name, algorithm, keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   251
                null, namedGroup, -1,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   252
                supportedProtocols, supportedProtocols);
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   253
    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   254
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   255
    private SignatureScheme(int id, String name,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   256
            String algorithm, String keyAlgorithm,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   257
            SigAlgParamSpec signAlgParamSpec,
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   258
            NamedGroup namedGroup, int minimalKeySize,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
            ProtocolVersion[] supportedProtocols,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
            ProtocolVersion[] handshakeSupportedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
        this.id = id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
        this.name = name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   263
        this.algorithm = algorithm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
        this.keyAlgorithm = keyAlgorithm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
        this.signAlgParameter =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
            signAlgParamSpec != null ? signAlgParamSpec.parameterSpec : null;
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   267
        this.namedGroup = namedGroup;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
        this.minimalKeySize = minimalKeySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
        this.supportedProtocols = Arrays.asList(supportedProtocols);
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   270
        this.handshakeSupportedProtocols =
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   271
                Arrays.asList(handshakeSupportedProtocols);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
        boolean mediator = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
        if (signAlgParamSpec != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
            mediator = signAlgParamSpec.isAvailable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
                JsseJce.getSignature(algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
            } catch (Exception e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
                        "Signature algorithm, " + algorithm +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
                        ", is not supported by the underlying providers");
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
        if (mediator && ((id >> 8) & 0xFF) == 0x03) {   // SHA224
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
            // There are some problems to use SHA224 on Windows.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
            if (Security.getProvider("SunMSCAPI") != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
                mediator = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
        this.isAvailable = mediator;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
    static SignatureScheme valueOf(int id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
            if (ss.id == id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
                return ss;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
    static String nameOf(int id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
            if (ss.id == id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
                return ss.name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
        // Use TLS 1.2 style name for unknown signature scheme.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
        int hashId = ((id >> 8) & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
        int signId = (id & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
        String hashName = (hashId >= hashAlgorithms.length) ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
            "UNDEFINED-HASH(" + hashId + ")" : hashAlgorithms[hashId];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
        String signName = (signId >= signatureAlgorithms.length) ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
            "UNDEFINED-SIGNATURE(" + signId + ")" :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
            signatureAlgorithms[signId];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
        return signName + "_" + hashName;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
    // Return the size of a SignatureScheme structure in TLS record
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
    static int sizeInRecord() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
        return 2;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
    // Get local supported algorithm collection complying to algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
    // constraints.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
    static List<SignatureScheme> getSupportedAlgorithms(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
            AlgorithmConstraints constraints,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
            List<ProtocolVersion> activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
        List<SignatureScheme> supported = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
        for (SignatureScheme ss: SignatureScheme.values()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
            if (!ss.isAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
                continue;
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
            boolean isMatch = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
            for (ProtocolVersion pv : activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
                if (ss.supportedProtocols.contains(pv)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
                    isMatch = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
            if (isMatch) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
                if (constraints.permits(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
                        SIGNATURE_PRIMITIVE_SET, ss.algorithm, null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
                    supported.add(ss);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
                } else if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
                        SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
                    SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
                        "Ignore disabled signature sheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
            } else if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
                    SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
                SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
                    "Ignore inactive signature sheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
        return supported;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
    static List<SignatureScheme> getSupportedAlgorithms(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
            AlgorithmConstraints constraints,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
            ProtocolVersion protocolVersion, int[] algorithmIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
        List<SignatureScheme> supported = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
        for (int ssid : algorithmIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
            SignatureScheme ss = SignatureScheme.valueOf(ssid);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
            if (ss == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
                            "Unsupported signature scheme: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
                            SignatureScheme.nameOf(ssid));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
            } else if (ss.isAvailable &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
                    ss.supportedProtocols.contains(protocolVersion) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
                    constraints.permits(SIGNATURE_PRIMITIVE_SET,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
                           ss.algorithm, null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
                supported.add(ss);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
                            "Unsupported signature scheme: " + ss.name);
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
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
        return supported;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
    static SignatureScheme getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
            List<SignatureScheme> schemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
            SignatureScheme certScheme,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
            ProtocolVersion version) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
        for (SignatureScheme ss : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
            if (ss.isAvailable &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
            ss.handshakeSupportedProtocols.contains(version) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
            certScheme.keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
                return ss;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
    static SignatureScheme getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            List<SignatureScheme> schemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
            PrivateKey signingKey,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
            ProtocolVersion version) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
        String keyAlgorithm = signingKey.getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
        int keySize;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
        // 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
   424
        if (keyAlgorithm.equalsIgnoreCase("RSA") ||
0811eaea3cd4 Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents: 56710
diff changeset
   425
                keyAlgorithm.equalsIgnoreCase("RSASSA-PSS")) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
            keySize = KeyUtil.getKeySize(signingKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
            keySize = Integer.MAX_VALUE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
        for (SignatureScheme ss : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
            if (ss.isAvailable && (keySize >= ss.minimalKeySize) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   432
                ss.handshakeSupportedProtocols.contains(version) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   433
                keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) {
56706
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   434
                if (ss.namedGroup != null &&
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   435
                    ss.namedGroup.type == NamedGroupType.NAMED_GROUP_ECDHE) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   436
                    ECParameterSpec params =
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   437
                                ((ECPrivateKey)signingKey).getParams();
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   438
                    if (ss.namedGroup == NamedGroup.valueOf(params)) {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   439
                        return ss;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   440
                    }
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   441
                } else {
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   442
                    return ss;
a82a96b62d22 use right EC named group as signature algorithm
xuelei
parents: 56542
diff changeset
   443
                }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
    static String[] getAlgorithmNames(Collection<SignatureScheme> schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
        if (schemes != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
            ArrayList<String> names = new ArrayList<>(schemes.size());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
            for (SignatureScheme scheme : schemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
                names.add(scheme.algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
            return names.toArray(new String[0]);
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
        return new String[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   463
    Signature getSignature(Key key) throws NoSuchAlgorithmException,
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   464
            InvalidAlgorithmParameterException, InvalidKeyException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
        if (!isAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
        Signature signer = JsseJce.getSignature(algorithm);
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   470
        if (key instanceof PublicKey) {
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   471
            signer.initVerify((PublicKey)(key));
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   472
        } else {
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   473
            signer.initSign((PrivateKey)key);
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   474
        }
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   475
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   476
        // Important note:  Please don't set the parameters before signature
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   477
        // or verification initialization, so that the crypto provider can
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 56750
diff changeset
   478
        // be selected properly.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   479
        if (signAlgParameter != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   480
            signer.setParameter(signAlgParameter);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   481
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   482
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   483
        return signer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   484
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   485
}