src/java.base/share/classes/sun/security/rsa/RSASignature.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56592 b1902b22005e
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 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.rsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    33
import java.security.spec.AlgorithmParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    35
import sun.security.rsa.RSAUtil.KeyType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    40
 * PKCS#1 v1.5 RSA signatures with the various message digest algorithms.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This file contains an abstract base class with all the logic plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a nested static class for each of the message digest algorithms
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
    43
 * (see end of the file). We support MD2, MD5, SHA-1, SHA-224, SHA-256,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    44
 * SHA-384, SHA-512, SHA-512/224, and SHA-512/256.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public abstract class RSASignature extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // we sign an ASN.1 SEQUENCE of AlgorithmId and digest
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    52
    // it has the form 30:xx:30:xx:[digestOID]:05:00:04:xx:[digest]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // this means the encoded length is (8 + digestOID.length + digest.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int baseLength = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // object identifier for the message digest algorithm used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final ObjectIdentifier digestOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // length of the encoded signature blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final int encodedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // message digest implementation we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // flag indicating whether the digest is reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean digestReset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // private key, if initialized for signing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private RSAPrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // public key, if initialized for verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private RSAPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // padding to use, set when the initSign/initVerify is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private RSAPadding padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Construct a new RSASignature. Used by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    RSASignature(String algorithm, ObjectIdentifier digestOID, int oidLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.digestOID = digestOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            md = MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        digestReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        encodedLength = baseLength + oidLength + md.getDigestLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // initialize for verification. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    90
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        RSAPublicKey rsaKey = (RSAPublicKey)RSAKeyFactory.toRSAKey(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.publicKey = rsaKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        initCommon(rsaKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // initialize for signing. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        engineInitSign(privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // initialize for signing. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   107
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throws InvalidKeyException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   110
        RSAPrivateKey rsaKey =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   111
            (RSAPrivateKey)RSAKeyFactory.toRSAKey(privateKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.privateKey = rsaKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        initCommon(rsaKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Init code common to sign and verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private void initCommon(RSAKey rsaKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throws InvalidKeyException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   122
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   123
            RSAUtil.checkParamsAgainstType(KeyType.RSA, rsaKey.getParams());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   124
        } catch (ProviderException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   125
            throw new InvalidKeyException("Invalid key for RSA signatures", e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   126
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        resetDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int keySize = RSACore.getByteLength(rsaKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            padding = RSAPadding.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                (RSAPadding.PAD_BLOCKTYPE_1, keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new InvalidKeyException(iape.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int maxDataSize = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (encodedLength > maxDataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                ("Key is too short for this signature algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Reset the message digest if it is not already reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private void resetDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (digestReset == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            digestReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Return the message digest value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private byte[] getDigestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        digestReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // update the signature with the plaintext data. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   161
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected void engineUpdate(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // update the signature with the plaintext data. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   168
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        md.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // update the signature with the plaintext data. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   176
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected void engineUpdate(ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // sign the data and return the signature. See JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   183
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected byte[] engineSign() throws SignatureException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   185
        if (privateKey == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   186
            throw new SignatureException("Missing private key");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   187
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        byte[] digest = getDigestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            byte[] encoded = encodeSignature(digestOID, digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            byte[] padded = padding.pad(encoded);
29915
88af03f531f0 8071726: Better RSA optimizations
valeriep
parents: 25859
diff changeset
   192
            byte[] encrypted = RSACore.rsa(padded, privateKey, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return encrypted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new SignatureException("Could not sign data", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new SignatureException("Could not encode data", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // verify the data and return the result. See JCA doc
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   202
    // should be reset to the state after engineInitVerify call.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   203
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   205
        if (publicKey == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   206
            throw new SignatureException("Missing public key");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   207
        }
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   208
        try {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   209
            if (sigBytes.length != RSACore.getByteLength(publicKey)) {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   210
                throw new SignatureException("Signature length not correct: got " +
7526
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   211
                    sigBytes.length + " but was expecting " +
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   212
                    RSACore.getByteLength(publicKey));
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   213
            }
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   214
            byte[] digest = getDigestValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            byte[] decrypted = RSACore.rsa(sigBytes, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            byte[] unpadded = padding.unpad(decrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            byte[] decodedDigest = decodeSignature(digestOID, unpadded);
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 31426
diff changeset
   218
            return MessageDigest.isEqual(digest, decodedDigest);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (javax.crypto.BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            // occurs if the app has used the wrong RSA public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // or if sigBytes is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // return false rather than propagating the exception for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // compatibility/ease of use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new SignatureException("Signature encoding error", e);
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   227
        } finally {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 31695
diff changeset
   228
            resetDigest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Encode the digest, return the to-be-signed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Also used by the PKCS#11 provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        new AlgorithmId(oid).encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        out.putOctetString(digest);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   241
        DerValue result =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   242
            new DerValue(DerValue.tag_Sequence, out.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return result.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Decode the signature data. Verify that the object identifier matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * and return the message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 41205
diff changeset
   250
    public static byte[] decodeSignature(ObjectIdentifier oid, byte[] sig)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throws IOException {
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 41205
diff changeset
   252
        // Enforce strict DER checking for signatures
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 41205
diff changeset
   253
        DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        DerValue[] values = in.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if ((values.length != 2) || (in.available() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new IOException("SEQUENCE length error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        AlgorithmId algId = AlgorithmId.parse(values[0]);
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 29915
diff changeset
   259
        if (algId.getOID().equals(oid) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   260
            throw new IOException("ObjectIdentifier mismatch: "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   261
                + algId.getOID());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (algId.getEncodedParams() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new IOException("Unexpected AlgorithmId parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        byte[] digest = values[1].getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // set parameter, not supported. See JCA doc
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 12685
diff changeset
   271
    @Deprecated
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   272
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throw new UnsupportedOperationException("setParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   278
    // See JCA doc
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   279
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   280
    protected void engineSetParameter(AlgorithmParameterSpec params)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   281
        throws InvalidAlgorithmParameterException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   282
        if (params != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   283
            throw new InvalidAlgorithmParameterException("No parameters accepted");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   284
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   285
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   286
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // get parameter, not supported. See JCA doc
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 12685
diff changeset
   288
    @Deprecated
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   289
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        throw new UnsupportedOperationException("getParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   295
    // See JCA doc
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   296
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   297
    protected AlgorithmParameters engineGetParameters() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   298
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   299
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // Nested class for MD2withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public static final class MD2withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        public MD2withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            super("MD2", AlgorithmId.MD2_oid, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // Nested class for MD5withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public static final class MD5withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public MD5withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            super("MD5", AlgorithmId.MD5_oid, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    // Nested class for SHA1withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public static final class SHA1withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        public SHA1withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            super("SHA-1", AlgorithmId.SHA_oid, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   322
    // Nested class for SHA224withRSA signatures
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   323
    public static final class SHA224withRSA extends RSASignature {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   324
        public SHA224withRSA() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   325
            super("SHA-224", AlgorithmId.SHA224_oid, 11);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   326
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   327
    }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   328
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    // Nested class for SHA256withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public static final class SHA256withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public SHA256withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            super("SHA-256", AlgorithmId.SHA256_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    // Nested class for SHA384withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static final class SHA384withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        public SHA384withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            super("SHA-384", AlgorithmId.SHA384_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // Nested class for SHA512withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public static final class SHA512withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public SHA512withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            super("SHA-512", AlgorithmId.SHA512_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   350
    // Nested class for SHA512/224withRSA signatures
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   351
    public static final class SHA512_224withRSA extends RSASignature {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   352
        public SHA512_224withRSA() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   353
            super("SHA-512/224", AlgorithmId.SHA512_224_oid, 11);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   354
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   355
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   356
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   357
    // Nested class for SHA512/256withRSA signatures
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   358
    public static final class SHA512_256withRSA extends RSASignature {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   359
        public SHA512_256withRSA() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   360
            super("SHA-512/256", AlgorithmId.SHA512_256_oid, 11);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   361
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   362
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}