jdk/src/windows/classes/sun/security/mscapi/RSASignature.java
author vinnie
Thu, 21 Apr 2011 19:05:29 +0100
changeset 9508 310b4f6c8e61
parent 9505 3bbc6ff8a905
child 9524 8417d0e74ac5
permissions -rw-r--r--
6732372: Some MSCAPI native methods not returning correct exceptions. Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
     2
 * Copyright (c) 2005, 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: 2596
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: 2596
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: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
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.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.InvalidAlgorithmParameterException;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
    34
import java.security.KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.SignatureSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.SignatureException;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    42
import java.math.BigInteger;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    43
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    44
import sun.security.rsa.RSAKeyFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * RSA signature implementation. Supports RSA signing using PKCS#1 v1.5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Objects should be instantiated by calling Signature.getInstance() using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * following algorithm names:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  . "SHA1withRSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  . "MD5withRSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . "MD2withRSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Note: RSA keys must be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
abstract class RSASignature extends java.security.SignatureSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // message digest implementation we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final MessageDigest messageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // flag indicating whether the digest is reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean needsReset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // the signing key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Key privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // the verification key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Key publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    RSASignature(String digestName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            messageDigest = MessageDigest.getInstance(digestName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static final class SHA1 extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public SHA1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            super("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final class MD5 extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public MD5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            super("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final class MD2 extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public MD2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            super("MD2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Initializes this signature object with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * public key for verification operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param publicKey the public key of the identity whose signature is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * going to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception InvalidKeyException if the key is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * encoded, parameters are missing, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void engineInitVerify(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // This signature accepts only RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ((key instanceof java.security.interfaces.RSAPublicKey) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new InvalidKeyException("Key type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        java.security.interfaces.RSAPublicKey rsaKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            (java.security.interfaces.RSAPublicKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if ((key instanceof sun.security.mscapi.RSAPublicKey) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // convert key to MSCAPI format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   131
            BigInteger modulus = rsaKey.getModulus();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   132
            BigInteger exponent =  rsaKey.getPublicExponent();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   133
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   134
            // Check against the local and global values to make sure
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   135
            // the sizes are ok.  Round up to the nearest byte.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   136
            RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   137
                exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   138
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   139
            byte[] modulusBytes = modulus.toByteArray();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   140
            byte[] exponentBytes = exponent.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // Adjust key length due to sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int keyBitLength = (modulusBytes[0] == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                ? (modulusBytes.length - 1) * 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                : modulusBytes.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            byte[] keyBlob = generatePublicKeyBlob(
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   148
                keyBitLength, modulusBytes, exponentBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   150
            try {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   151
                publicKey = importPublicKey(keyBlob, keyBitLength);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   152
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   153
            } catch (KeyStoreException e) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   154
                throw new InvalidKeyException(e);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   155
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            publicKey = (sun.security.mscapi.RSAPublicKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (needsReset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            messageDigest.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Initializes this signature object with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * private key for signing operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param privateKey the private key of the identity whose signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception InvalidKeyException if the key is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * encoded, parameters are missing, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected void engineInitSign(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // This signature accepts only RSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if ((key instanceof sun.security.mscapi.RSAPrivateKey) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new InvalidKeyException("Key type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        privateKey = (sun.security.mscapi.RSAPrivateKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   186
        // Check against the local and global values to make sure
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   187
        // the sizes are ok.  Round up to nearest byte.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   188
        RSAKeyFactory.checkKeyLengths(((privateKey.bitLength() + 7) & ~7),
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   189
            null, RSAKeyPairGenerator.KEY_SIZE_MIN,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   190
            RSAKeyPairGenerator.KEY_SIZE_MAX);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (needsReset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            messageDigest.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Updates the data to be signed or verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * using the specified byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param b the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected void engineUpdate(byte b) throws SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        messageDigest.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * specified array of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param b the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param off the offset to start from in the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param len the number of bytes to use, starting at offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        throws SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        messageDigest.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        needsReset = true;
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
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * specified ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    protected void engineUpdate(ByteBuffer input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        messageDigest.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Returns the signature bytes of all the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * updated so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * The format of the signature depends on the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * signature scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the signature bytes of the signing operation's result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * initialized properly or if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        byte[] hash = messageDigest.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Sign hash using MS Crypto APIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        byte[] result = signHash(hash, hash.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            messageDigest.getAlgorithm(), privateKey.getHCryptProvider(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            privateKey.getHCryptKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // Convert signature array from little endian to big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return convertEndianArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Convert array from big endian to little endian, or vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private byte[] convertEndianArray(byte[] byteArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (byteArray == null || byteArray.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return byteArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        byte [] retval = new byte[byteArray.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // make it big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int i=0;i < byteArray.length;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            retval[i] = byteArray[byteArray.length - i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Sign hash using Microsoft Crypto API with HCRYPTKEY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * The returned data is in little-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private native static byte[] signHash(byte[] hash, int hashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        String hashAlgorithm, long hCryptProv, long hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Verify a signed hash using Microsoft Crypto API with HCRYPTKEY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private native static boolean verifySignedHash(byte[] hash, int hashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        String hashAlgorithm, byte[] signature, int signatureSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        long hCryptProv, long hCryptKey) throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Verifies the passed-in signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param sigBytes the signature bytes to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return true if the signature was verified, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * initialized properly, the passed-in signature is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * encoded or of the wrong type, if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * process the input data provided, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    protected boolean engineVerify(byte[] sigBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        byte[] hash = messageDigest.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return verifySignedHash(hash, hash.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            messageDigest.getAlgorithm(), convertEndianArray(sigBytes),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            sigBytes.length, publicKey.getHCryptProvider(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            publicKey.getHCryptKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Sets the specified algorithm parameter to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * value. This method supplies a general-purpose mechanism through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * which it is possible to set the various parameters of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * A parameter may be any settable parameter for the algorithm, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * a parameter size, or a source of random bits for signature generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * (if appropriate), or an indication of whether or not to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * a specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param param the string identifier of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param value the parameter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @exception InvalidParameterException if <code>param</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * invalid parameter for this signature algorithm engine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * the parameter is already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * and cannot be set again, a security exception occurs, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @deprecated Replaced by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * engineSetParameter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throw new InvalidParameterException("Parameter not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Gets the value of the specified algorithm parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * This method supplies a general-purpose mechanism through which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * is possible to get the various parameters of this object. A parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * may be any settable parameter for the algorithm, such as a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * size, or  a source of random bits for signature generation (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * appropriate), or an indication of whether or not to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param param the string name of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return the object that represents the parameter value, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @exception InvalidParameterException if <code>param</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * invalid parameter for this engine, or another exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * trying to get this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        throw new InvalidParameterException("Parameter not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Generates a public-key BLOB from a key's components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents: 5506
diff changeset
   388
    // used by RSACipher
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents: 5506
diff changeset
   389
    static native byte[] generatePublicKeyBlob(
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   390
        int keyBitLength, byte[] modulus, byte[] publicExponent)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   391
            throws InvalidKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Imports a public-key BLOB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents: 5506
diff changeset
   396
    // used by RSACipher
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   397
    static native RSAPublicKey importPublicKey(byte[] keyBlob, int keySize)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   398
        throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}