src/java.base/share/classes/sun/security/ssl/RSASignature.java
author wetmore
Tue, 22 May 2018 16:36:02 -0700
branchJDK-8145252-TLS13-branch
changeset 56592 b1902b22005e
parent 56542 56aaa6cb3693
parent 50204 3195a713e24d
child 56711 ad4c1c488574
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Signature implementation for the SSL/TLS RSA Signature variant with both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * MD5 and SHA-1 MessageDigests. Used for explicit RSA server authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * (RSA signed server key exchange for RSA_EXPORT and DHE_RSA) and RSA client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * authentication (RSA signed certificate verify message).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * It conforms to the standard JCA Signature API. It is registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * SunJSSE provider to avoid more complicated getInstance() code and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * negative interaction with the JCA mechanisms for hardware providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The class should be instantiated via the getInstance() method in this class,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14664
diff changeset
    42
 * which returns the implementation from the preferred provider. The internal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * implementation allows the hashes to be explicitly set, which is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * for RSA client authentication. It can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * getInternalInstance() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class is not thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class RSASignature extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final Signature rawRsa;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    51
    private final MessageDigest mdMD5;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    52
    private final MessageDigest mdSHA;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public RSASignature() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        rawRsa = JsseJce.getSignature(JsseJce.SIGNATURE_RAWRSA);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    57
        this.mdMD5 = JsseJce.getMessageDigest("MD5");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    58
        this.mdSHA = JsseJce.getMessageDigest("SHA");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    62
     * Get an implementation for the RSA signature.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    63
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    64
     * Follows the standard JCA getInstance() model, so it return the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    65
     * implementation from the  provider with the highest precedence,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    66
     * which may be this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static Signature getInstance() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return JsseJce.getSignature(JsseJce.SIGNATURE_SSLRSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    73
     * Get an internal implementation for the RSA signature.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    74
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    75
     * Used for RSA client authentication, which needs the ability to set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    76
     * the digests to externally provided values via the setHashes() method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static Signature getInternalInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return Signature.getInstance(JsseJce.SIGNATURE_SSLRSA, "SunJSSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Set the MD5 and SHA hashes to the provided objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
    86
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static void setHashes(Signature sig, MessageDigest md5, MessageDigest sha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sig.setParameter("hashes", new MessageDigest[] {md5, sha});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    91
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throws InvalidKeyException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    94
        if (publicKey == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    95
            throw new InvalidKeyException("Public key must not be null");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    97
        mdMD5.reset();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    98
        mdSHA.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        rawRsa.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   102
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        engineInitSign(privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   108
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throws InvalidKeyException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   111
        if (privateKey == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   112
            throw new InvalidKeyException("Private key must not be null");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   113
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   114
        mdMD5.reset();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   115
        mdSHA.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        rawRsa.initSign(privateKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   119
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected void engineUpdate(byte b) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   121
        mdMD5.update(b);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   122
        mdSHA.update(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   125
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected void engineUpdate(byte[] b, int off, int len) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   127
        mdMD5.update(b, off, len);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   128
        mdSHA.update(b, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private byte[] getDigest() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            byte[] data = new byte[36];
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   134
            mdMD5.digest(data, 0, 16);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   135
            mdSHA.digest(data, 16, 20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new SignatureException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   143
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        rawRsa.update(getDigest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return rawRsa.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   149
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return engineVerify(sigBytes, 0, sigBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   154
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected boolean engineVerify(byte[] sigBytes, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        rawRsa.update(getDigest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return rawRsa.verify(sigBytes, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   161
    @Override
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   162
    @SuppressWarnings("deprecation")
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   163
    protected void engineSetParameter(String param,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   164
            Object value) throws InvalidParameterException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   165
        throw new InvalidParameterException("Parameters not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   168
    @Override
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   169
    protected void engineSetParameter(AlgorithmParameterSpec params)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   170
            throws InvalidAlgorithmParameterException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   171
        if (params != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   172
            throw new InvalidAlgorithmParameterException("No parameters accepted");
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   173
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   174
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   175
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   176
    @Override
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   177
    @SuppressWarnings("deprecation")
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   178
    protected Object engineGetParameter(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   179
            String param) throws InvalidParameterException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        throw new InvalidParameterException("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   183
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   184
    protected AlgorithmParameters engineGetParameters() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   185
        return null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   186
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}