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