jdk/src/share/classes/sun/security/rsa/RSASignature.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 7526 78a87adede1e
child 12685 8a448b5b9006
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7526
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * PKCS#1 RSA signatures with the various message digest algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This file contains an abstract base class with all the logic plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a nested static class for each of the message digest algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (see end of the file). We support MD2, MD5, SHA-1, SHA-256, SHA-384,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and SHA-512.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class RSASignature extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // we sign an ASN.1 SEQUENCE of AlgorithmId and digest
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    51
    // it has the form 30:xx:30:xx:[digestOID]:05:00:04:xx:[digest]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // this means the encoded length is (8 + digestOID.length + digest.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int baseLength = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // object identifier for the message digest algorithm used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final ObjectIdentifier digestOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // length of the encoded signature blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final int encodedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // message digest implementation we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // flag indicating whether the digest is reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean digestReset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // private key, if initialized for signing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private RSAPrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // public key, if initialized for verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private RSAPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // padding to use, set when the initSign/initVerify is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private RSAPadding padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Construct a new RSASignature. Used by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    RSASignature(String algorithm, ObjectIdentifier digestOID, int oidLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.digestOID = digestOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            md = MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        digestReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        encodedLength = baseLength + oidLength + md.getDigestLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // initialize for verification. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        RSAPublicKey rsaKey = (RSAPublicKey)RSAKeyFactory.toRSAKey(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.publicKey = rsaKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        initCommon(rsaKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // initialize for signing. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        engineInitSign(privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // initialize for signing. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throws InvalidKeyException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   106
        RSAPrivateKey rsaKey =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   107
            (RSAPrivateKey)RSAKeyFactory.toRSAKey(privateKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.privateKey = rsaKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        initCommon(rsaKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Init code common to sign and verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private void initCommon(RSAKey rsaKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        resetDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int keySize = RSACore.getByteLength(rsaKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            padding = RSAPadding.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                (RSAPadding.PAD_BLOCKTYPE_1, keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new InvalidKeyException(iape.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int maxDataSize = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (encodedLength > maxDataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                ("Key is too short for this signature algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Reset the message digest if it is not already reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private void resetDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (digestReset == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            digestReset = true;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Return the message digest value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private byte[] getDigestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        digestReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // update the signature with the plaintext data. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected void engineUpdate(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // update the signature with the plaintext data. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        md.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // update the signature with the plaintext data. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected void engineUpdate(ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // sign the data and return the signature. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        byte[] digest = getDigestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            byte[] encoded = encodeSignature(digestOID, digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            byte[] padded = padding.pad(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            byte[] encrypted = RSACore.rsa(padded, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return encrypted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new SignatureException("Could not sign data", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new SignatureException("Could not encode data", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // verify the data and return the result. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
7526
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   187
        if (sigBytes.length != RSACore.getByteLength(publicKey)) {
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   188
            throw new SignatureException("Signature length not correct: got " +
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   189
                    sigBytes.length + " but was expecting " +
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   190
                    RSACore.getByteLength(publicKey));
78a87adede1e 6896700: Validation of signatures succeed when it should fail
weijun
parents: 7043
diff changeset
   191
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        byte[] digest = getDigestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            byte[] decrypted = RSACore.rsa(sigBytes, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            byte[] unpadded = padding.unpad(decrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            byte[] decodedDigest = decodeSignature(digestOID, unpadded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return Arrays.equals(digest, decodedDigest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (javax.crypto.BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // occurs if the app has used the wrong RSA public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // or if sigBytes is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // return false rather than propagating the exception for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // compatibility/ease of use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new SignatureException("Signature encoding error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Encode the digest, return the to-be-signed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Also used by the PKCS#11 provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        new AlgorithmId(oid).encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        out.putOctetString(digest);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   218
        DerValue result =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   219
            new DerValue(DerValue.tag_Sequence, out.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return result.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Decode the signature data. Verify that the object identifier matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * and return the message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static byte[] decodeSignature(ObjectIdentifier oid, byte[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        DerInputStream in = new DerInputStream(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        DerValue[] values = in.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if ((values.length != 2) || (in.available() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new IOException("SEQUENCE length error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        AlgorithmId algId = AlgorithmId.parse(values[0]);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7526
diff changeset
   235
        if (algId.getOID().equals((Object)oid) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   236
            throw new IOException("ObjectIdentifier mismatch: "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   237
                + algId.getOID());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (algId.getEncodedParams() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new IOException("Unexpected AlgorithmId parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        byte[] digest = values[1].getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // set parameter, not supported. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        throw new UnsupportedOperationException("setParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    // get parameter, not supported. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        throw new UnsupportedOperationException("getParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // Nested class for MD2withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static final class MD2withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        public MD2withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            super("MD2", AlgorithmId.MD2_oid, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // Nested class for MD5withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static final class MD5withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        public MD5withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            super("MD5", AlgorithmId.MD5_oid, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // Nested class for SHA1withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public static final class SHA1withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public SHA1withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            super("SHA-1", AlgorithmId.SHA_oid, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    // Nested class for SHA256withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public static final class SHA256withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        public SHA256withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            super("SHA-256", AlgorithmId.SHA256_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // Nested class for SHA384withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public static final class SHA384withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        public SHA384withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            super("SHA-384", AlgorithmId.SHA384_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    // Nested class for SHA512withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public static final class SHA512withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        public SHA512withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            super("SHA-512", AlgorithmId.SHA512_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}