jdk/src/share/classes/sun/security/rsa/RSASignature.java
author xuelei
Mon, 01 Nov 2010 22:02:35 -0700
changeset 7043 5e2d1edeb2c7
parent 5506 202f599c92aa
child 7526 78a87adede1e
permissions -rw-r--r--
6916074: Add support for TLS 1.2 6985179: To support Server Name Indication extension for JSSE client Summary: Introduces the algorithm constraints to support signature and hash algorithm selection. Includes contributions from wetmore and weijung. Reviewed-by: wetmore, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, 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.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * PKCS#1 RSA signatures with the various message digest algorithms.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * (see end of the file). We support MD2, MD5, SHA-1, SHA-256, SHA-384,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and SHA-512.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        RSAPublicKey rsaKey = (RSAPublicKey)RSAKeyFactory.toRSAKey(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.publicKey = rsaKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        initCommon(rsaKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // initialize for signing. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        engineInitSign(privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // initialize for signing. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throws InvalidKeyException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   107
        RSAPrivateKey rsaKey =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   108
            (RSAPrivateKey)RSAKeyFactory.toRSAKey(privateKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.privateKey = rsaKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        initCommon(rsaKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Init code common to sign and verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private void initCommon(RSAKey rsaKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        resetDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int keySize = RSACore.getByteLength(rsaKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            padding = RSAPadding.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                (RSAPadding.PAD_BLOCKTYPE_1, keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new InvalidKeyException(iape.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int maxDataSize = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (encodedLength > maxDataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                ("Key is too short for this signature algorithm");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Reset the message digest if it is not already reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private void resetDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (digestReset == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            digestReset = true;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Return the message digest value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private byte[] getDigestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        digestReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // update the signature with the plaintext data. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected void engineUpdate(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // update the signature with the plaintext data. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        md.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // update the signature with the plaintext data. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected void engineUpdate(ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        digestReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // sign the data and return the signature. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        byte[] digest = getDigestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            byte[] encoded = encodeSignature(digestOID, digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            byte[] padded = padding.pad(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            byte[] encrypted = RSACore.rsa(padded, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return encrypted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new SignatureException("Could not sign data", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new SignatureException("Could not encode data", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // verify the data and return the result. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
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[] decrypted = RSACore.rsa(sigBytes, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            byte[] unpadded = padding.unpad(decrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            byte[] decodedDigest = decodeSignature(digestOID, unpadded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return Arrays.equals(digest, decodedDigest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (javax.crypto.BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // occurs if the app has used the wrong RSA public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // or if sigBytes is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // return false rather than propagating the exception for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // compatibility/ease of use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new SignatureException("Signature verification failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new SignatureException("Signature encoding error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Encode the digest, return the to-be-signed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Also used by the PKCS#11 provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        new AlgorithmId(oid).encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        out.putOctetString(digest);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   216
        DerValue result =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   217
            new DerValue(DerValue.tag_Sequence, out.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return result.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Decode the signature data. Verify that the object identifier matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * and return the message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static byte[] decodeSignature(ObjectIdentifier oid, byte[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        DerInputStream in = new DerInputStream(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        DerValue[] values = in.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if ((values.length != 2) || (in.available() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new IOException("SEQUENCE length error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        AlgorithmId algId = AlgorithmId.parse(values[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (algId.getOID().equals(oid) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   234
            throw new IOException("ObjectIdentifier mismatch: "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   235
                + algId.getOID());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (algId.getEncodedParams() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new IOException("Unexpected AlgorithmId parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        byte[] digest = values[1].getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    // set parameter, not supported. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throw new UnsupportedOperationException("setParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // get parameter, not supported. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        throw new UnsupportedOperationException("getParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // Nested class for MD2withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static final class MD2withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        public MD2withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            super("MD2", AlgorithmId.MD2_oid, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // Nested class for MD5withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public static final class MD5withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        public MD5withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            super("MD5", AlgorithmId.MD5_oid, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // Nested class for SHA1withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public static final class SHA1withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        public SHA1withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            super("SHA-1", AlgorithmId.SHA_oid, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // Nested class for SHA256withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public static final class SHA256withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public SHA256withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            super("SHA-256", AlgorithmId.SHA256_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // Nested class for SHA384withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public static final class SHA384withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public SHA384withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            super("SHA-384", AlgorithmId.SHA384_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Nested class for SHA512withRSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static final class SHA512withRSA extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        public SHA512withRSA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            super("SHA-512", AlgorithmId.SHA512_oid, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}