src/java.base/share/classes/sun/security/ssl/RSASignature.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 50204 3195a713e24d
child 53734 cb1642ccc732
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
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;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    51
    private final MessageDigest mdMD5;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
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);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    57
        this.mdMD5 = JsseJce.getMessageDigest("MD5");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
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
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    62
     * Get an implementation for the RSA signature.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    63
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    64
     * Follows the standard JCA getInstance() model, so it return the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    65
     * implementation from the  provider with the highest precedence,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
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
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    72
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throws InvalidKeyException {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    75
        if (publicKey == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    76
            throw new InvalidKeyException("Public key must not be null");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    77
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    78
        mdMD5.reset();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    79
        mdSHA.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        rawRsa.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    83
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        engineInitSign(privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    89
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throws InvalidKeyException {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    92
        if (privateKey == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    93
            throw new InvalidKeyException("Private key must not be null");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    94
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    95
        mdMD5.reset();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
    96
        mdSHA.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        rawRsa.initSign(privateKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   100
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected void engineUpdate(byte b) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   102
        mdMD5.update(b);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   103
        mdSHA.update(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   106
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected void engineUpdate(byte[] b, int off, int len) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   108
        mdMD5.update(b, off, len);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   109
        mdSHA.update(b, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private byte[] getDigest() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            byte[] data = new byte[36];
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   115
            mdMD5.digest(data, 0, 16);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   116
            mdSHA.digest(data, 16, 20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new SignatureException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   124
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        rawRsa.update(getDigest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return rawRsa.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   130
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return engineVerify(sigBytes, 0, sigBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   135
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected boolean engineVerify(byte[] sigBytes, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        rawRsa.update(getDigest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return rawRsa.verify(sigBytes, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   142
    @Override
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   143
    @SuppressWarnings("deprecation")
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   144
    protected void engineSetParameter(String param,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   145
            Object value) throws InvalidParameterException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   146
        throw new InvalidParameterException("Parameters not supported");
2
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   150
    protected void engineSetParameter(AlgorithmParameterSpec params)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   151
            throws InvalidAlgorithmParameterException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   152
        if (params != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   153
            throw new InvalidAlgorithmParameterException("No parameters accepted");
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   154
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   155
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   156
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   157
    @Override
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   158
    @SuppressWarnings("deprecation")
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   159
    protected Object engineGetParameter(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 50204
diff changeset
   160
            String param) throws InvalidParameterException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throw new InvalidParameterException("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   164
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   165
    protected AlgorithmParameters engineGetParameters() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   166
        return null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   167
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}