0N/A/*
4589N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.mscapi;
0N/A
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.security.PublicKey;
0N/Aimport java.security.PrivateKey;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.InvalidParameterException;
0N/Aimport java.security.InvalidAlgorithmParameterException;
4111N/Aimport java.security.KeyStoreException;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/Aimport java.security.ProviderException;
0N/Aimport java.security.MessageDigest;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.Signature;
0N/Aimport java.security.SignatureSpi;
0N/Aimport java.security.SignatureException;
1111N/Aimport java.math.BigInteger;
1111N/A
1111N/Aimport sun.security.rsa.RSAKeyFactory;
0N/A
0N/A/**
0N/A * RSA signature implementation. Supports RSA signing using PKCS#1 v1.5 padding.
0N/A *
0N/A * Objects should be instantiated by calling Signature.getInstance() using the
0N/A * following algorithm names:
0N/A *
4136N/A * . "NONEwithRSA"
0N/A * . "SHA1withRSA"
4127N/A * . "SHA256withRSA"
4127N/A * . "SHA384withRSA"
4127N/A * . "SHA512withRSA"
0N/A * . "MD5withRSA"
0N/A * . "MD2withRSA"
0N/A *
4136N/A * NOTE: RSA keys must be at least 512 bits long.
4136N/A *
4136N/A * NOTE: NONEwithRSA must be supplied with a pre-computed message digest.
4136N/A * Only the following digest algorithms are supported: MD5, SHA-1,
4136N/A * SHA-256, SHA-384, SHA-512 and a special-purpose digest algorithm
4136N/A * which is a concatenation of SHA-1 and MD5 digests.
0N/A *
0N/A * @since 1.6
0N/A * @author Stanley Man-Kit Ho
0N/A */
0N/Aabstract class RSASignature extends java.security.SignatureSpi
0N/A{
0N/A // message digest implementation we use
0N/A private final MessageDigest messageDigest;
0N/A
4127N/A // message digest name
4136N/A private String messageDigestAlgorithm;
4127N/A
4127N/A // flag indicating whether the digest has been reset
0N/A private boolean needsReset;
0N/A
0N/A // the signing key
0N/A private Key privateKey = null;
0N/A
0N/A // the verification key
0N/A private Key publicKey = null;
0N/A
4136N/A /**
4136N/A * Constructs a new RSASignature. Used by Raw subclass.
4136N/A */
4136N/A RSASignature() {
4136N/A messageDigest = null;
4136N/A messageDigestAlgorithm = null;
4136N/A }
0N/A
4127N/A /**
4127N/A * Constructs a new RSASignature. Used by subclasses.
4127N/A */
0N/A RSASignature(String digestName) {
0N/A
0N/A try {
0N/A messageDigest = MessageDigest.getInstance(digestName);
4127N/A // Get the digest's canonical name
4127N/A messageDigestAlgorithm = messageDigest.getAlgorithm();
0N/A
0N/A } catch (NoSuchAlgorithmException e) {
0N/A throw new ProviderException(e);
0N/A }
0N/A
0N/A needsReset = false;
0N/A }
0N/A
4136N/A // Nested class for NONEwithRSA signatures
4136N/A public static final class Raw extends RSASignature {
4136N/A
4136N/A // the longest supported digest is 512 bits (SHA-512)
4136N/A private static final int RAW_RSA_MAX = 64;
4136N/A
4136N/A private final byte[] precomputedDigest;
4136N/A private int offset = 0;
4136N/A
4136N/A public Raw() {
4136N/A precomputedDigest = new byte[RAW_RSA_MAX];
4136N/A }
4136N/A
4136N/A // Stores the precomputed message digest value.
4136N/A @Override
4136N/A protected void engineUpdate(byte b) throws SignatureException {
4136N/A if (offset >= precomputedDigest.length) {
4136N/A offset = RAW_RSA_MAX + 1;
4136N/A return;
4136N/A }
4136N/A precomputedDigest[offset++] = b;
4136N/A }
4136N/A
4136N/A // Stores the precomputed message digest value.
4136N/A @Override
4136N/A protected void engineUpdate(byte[] b, int off, int len)
4136N/A throws SignatureException {
4136N/A if (offset + len > precomputedDigest.length) {
4136N/A offset = RAW_RSA_MAX + 1;
4136N/A return;
4136N/A }
4136N/A System.arraycopy(b, off, precomputedDigest, offset, len);
4136N/A offset += len;
4136N/A }
4136N/A
4136N/A // Stores the precomputed message digest value.
4136N/A @Override
4136N/A protected void engineUpdate(ByteBuffer byteBuffer) {
4136N/A int len = byteBuffer.remaining();
4136N/A if (len <= 0) {
4136N/A return;
4136N/A }
4136N/A if (offset + len > precomputedDigest.length) {
4136N/A offset = RAW_RSA_MAX + 1;
4136N/A return;
4136N/A }
4136N/A byteBuffer.get(precomputedDigest, offset, len);
4136N/A offset += len;
4136N/A }
4136N/A
4136N/A @Override
4136N/A protected void resetDigest(){
4136N/A offset = 0;
4136N/A }
4136N/A
4136N/A // Returns the precomputed message digest value.
4136N/A @Override
4136N/A protected byte[] getDigestValue() throws SignatureException {
4136N/A if (offset > RAW_RSA_MAX) {
4136N/A throw new SignatureException("Message digest is too long");
4136N/A }
4136N/A
4136N/A // Determine the digest algorithm from the digest length
4136N/A if (offset == 20) {
4136N/A setDigestName("SHA1");
4136N/A } else if (offset == 36) {
4136N/A setDigestName("SHA1+MD5");
4136N/A } else if (offset == 32) {
4136N/A setDigestName("SHA-256");
4136N/A } else if (offset == 48) {
4136N/A setDigestName("SHA-384");
4136N/A } else if (offset == 64) {
4136N/A setDigestName("SHA-512");
4136N/A } else if (offset == 16) {
4136N/A setDigestName("MD5");
4136N/A } else {
4136N/A throw new SignatureException(
4136N/A "Message digest length is not supported");
4136N/A }
4136N/A
4136N/A byte[] result = new byte[offset];
4136N/A System.arraycopy(precomputedDigest, 0, result, 0, offset);
4136N/A offset = 0;
4136N/A
4136N/A return result;
4136N/A }
4136N/A }
4136N/A
0N/A public static final class SHA1 extends RSASignature {
0N/A public SHA1() {
0N/A super("SHA1");
0N/A }
0N/A }
0N/A
4127N/A public static final class SHA256 extends RSASignature {
4127N/A public SHA256() {
4127N/A super("SHA-256");
4127N/A }
4127N/A }
4127N/A
4127N/A public static final class SHA384 extends RSASignature {
4127N/A public SHA384() {
4127N/A super("SHA-384");
4127N/A }
4127N/A }
4127N/A
4127N/A public static final class SHA512 extends RSASignature {
4127N/A public SHA512() {
4127N/A super("SHA-512");
4127N/A }
4127N/A }
4127N/A
0N/A public static final class MD5 extends RSASignature {
0N/A public MD5() {
0N/A super("MD5");
0N/A }
0N/A }
0N/A
0N/A public static final class MD2 extends RSASignature {
0N/A public MD2() {
0N/A super("MD2");
0N/A }
0N/A }
0N/A
4127N/A // initialize for signing. See JCA doc
0N/A protected void engineInitVerify(PublicKey key)
0N/A throws InvalidKeyException
0N/A {
0N/A // This signature accepts only RSAPublicKey
0N/A if ((key instanceof java.security.interfaces.RSAPublicKey) == false) {
0N/A throw new InvalidKeyException("Key type not supported");
0N/A }
0N/A
0N/A java.security.interfaces.RSAPublicKey rsaKey =
0N/A (java.security.interfaces.RSAPublicKey) key;
0N/A
0N/A if ((key instanceof sun.security.mscapi.RSAPublicKey) == false) {
0N/A
0N/A // convert key to MSCAPI format
0N/A
1111N/A BigInteger modulus = rsaKey.getModulus();
1111N/A BigInteger exponent = rsaKey.getPublicExponent();
1111N/A
1111N/A // Check against the local and global values to make sure
1111N/A // the sizes are ok. Round up to the nearest byte.
1111N/A RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
1111N/A exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX);
1111N/A
1111N/A byte[] modulusBytes = modulus.toByteArray();
1111N/A byte[] exponentBytes = exponent.toByteArray();
0N/A
0N/A // Adjust key length due to sign bit
0N/A int keyBitLength = (modulusBytes[0] == 0)
0N/A ? (modulusBytes.length - 1) * 8
0N/A : modulusBytes.length * 8;
0N/A
0N/A byte[] keyBlob = generatePublicKeyBlob(
1111N/A keyBitLength, modulusBytes, exponentBytes);
0N/A
4111N/A try {
4111N/A publicKey = importPublicKey(keyBlob, keyBitLength);
4111N/A
4111N/A } catch (KeyStoreException e) {
4111N/A throw new InvalidKeyException(e);
4111N/A }
0N/A
0N/A } else {
0N/A publicKey = (sun.security.mscapi.RSAPublicKey) key;
0N/A }
0N/A
4127N/A this.privateKey = null;
4127N/A resetDigest();
0N/A }
0N/A
4127N/A // initialize for signing. See JCA doc
4127N/A protected void engineInitSign(PrivateKey key) throws InvalidKeyException
0N/A {
0N/A // This signature accepts only RSAPrivateKey
0N/A if ((key instanceof sun.security.mscapi.RSAPrivateKey) == false) {
0N/A throw new InvalidKeyException("Key type not supported");
0N/A }
0N/A privateKey = (sun.security.mscapi.RSAPrivateKey) key;
0N/A
1111N/A // Check against the local and global values to make sure
1111N/A // the sizes are ok. Round up to nearest byte.
4589N/A RSAKeyFactory.checkKeyLengths(((privateKey.length() + 7) & ~7),
1111N/A null, RSAKeyPairGenerator.KEY_SIZE_MIN,
1111N/A RSAKeyPairGenerator.KEY_SIZE_MAX);
0N/A
4127N/A this.publicKey = null;
4127N/A resetDigest();
4127N/A }
4127N/A
4127N/A /**
4127N/A * Resets the message digest if needed.
4127N/A */
4136N/A protected void resetDigest() {
0N/A if (needsReset) {
0N/A messageDigest.reset();
0N/A needsReset = false;
0N/A }
0N/A }
0N/A
4136N/A protected byte[] getDigestValue() throws SignatureException {
4127N/A needsReset = false;
4127N/A return messageDigest.digest();
4127N/A }
4127N/A
4136N/A protected void setDigestName(String name) {
4136N/A messageDigestAlgorithm = name;
4136N/A }
4136N/A
0N/A /**
0N/A * Updates the data to be signed or verified
0N/A * using the specified byte.
0N/A *
0N/A * @param b the byte to use for the update.
0N/A *
0N/A * @exception SignatureException if the engine is not initialized
0N/A * properly.
0N/A */
0N/A protected void engineUpdate(byte b) throws SignatureException
0N/A {
0N/A messageDigest.update(b);
0N/A needsReset = true;
0N/A }
0N/A
0N/A /**
0N/A * Updates the data to be signed or verified, using the
0N/A * specified array of bytes, starting at the specified offset.
0N/A *
0N/A * @param b the array of bytes
0N/A * @param off the offset to start from in the array of bytes
0N/A * @param len the number of bytes to use, starting at offset
0N/A *
0N/A * @exception SignatureException if the engine is not initialized
0N/A * properly
0N/A */
0N/A protected void engineUpdate(byte[] b, int off, int len)
0N/A throws SignatureException
0N/A {
0N/A messageDigest.update(b, off, len);
0N/A needsReset = true;
0N/A }
0N/A
0N/A /**
0N/A * Updates the data to be signed or verified, using the
0N/A * specified ByteBuffer.
0N/A *
0N/A * @param input the ByteBuffer
0N/A */
0N/A protected void engineUpdate(ByteBuffer input)
0N/A {
0N/A messageDigest.update(input);
0N/A needsReset = true;
0N/A }
0N/A
0N/A /**
0N/A * Returns the signature bytes of all the data
0N/A * updated so far.
0N/A * The format of the signature depends on the underlying
0N/A * signature scheme.
0N/A *
0N/A * @return the signature bytes of the signing operation's result.
0N/A *
0N/A * @exception SignatureException if the engine is not
0N/A * initialized properly or if this signature algorithm is unable to
0N/A * process the input data provided.
0N/A */
0N/A protected byte[] engineSign() throws SignatureException {
0N/A
4127N/A byte[] hash = getDigestValue();
0N/A
4136N/A // Omit the hash OID when generating a Raw signature
4136N/A boolean noHashOID = this instanceof Raw;
4136N/A
0N/A // Sign hash using MS Crypto APIs
0N/A
4136N/A byte[] result = signHash(noHashOID, hash, hash.length,
4127N/A messageDigestAlgorithm, privateKey.getHCryptProvider(),
0N/A privateKey.getHCryptKey());
0N/A
0N/A // Convert signature array from little endian to big endian
0N/A return convertEndianArray(result);
0N/A }
0N/A
0N/A /**
0N/A * Convert array from big endian to little endian, or vice versa.
0N/A */
0N/A private byte[] convertEndianArray(byte[] byteArray)
0N/A {
0N/A if (byteArray == null || byteArray.length == 0)
0N/A return byteArray;
0N/A
0N/A byte [] retval = new byte[byteArray.length];
0N/A
0N/A // make it big endian
0N/A for (int i=0;i < byteArray.length;i++)
0N/A retval[i] = byteArray[byteArray.length - i - 1];
0N/A
0N/A return retval;
0N/A }
0N/A
0N/A /**
0N/A * Sign hash using Microsoft Crypto API with HCRYPTKEY.
0N/A * The returned data is in little-endian.
0N/A */
4136N/A private native static byte[] signHash(boolean noHashOID, byte[] hash,
4136N/A int hashSize, String hashAlgorithm, long hCryptProv, long hCryptKey)
0N/A throws SignatureException;
0N/A
0N/A /**
0N/A * Verify a signed hash using Microsoft Crypto API with HCRYPTKEY.
0N/A */
0N/A private native static boolean verifySignedHash(byte[] hash, int hashSize,
0N/A String hashAlgorithm, byte[] signature, int signatureSize,
0N/A long hCryptProv, long hCryptKey) throws SignatureException;
0N/A
0N/A /**
0N/A * Verifies the passed-in signature.
0N/A *
0N/A * @param sigBytes the signature bytes to be verified.
0N/A *
0N/A * @return true if the signature was verified, false if not.
0N/A *
0N/A * @exception SignatureException if the engine is not
0N/A * initialized properly, the passed-in signature is improperly
0N/A * encoded or of the wrong type, if this signature algorithm is unable to
0N/A * process the input data provided, etc.
0N/A */
0N/A protected boolean engineVerify(byte[] sigBytes)
0N/A throws SignatureException
0N/A {
4127N/A byte[] hash = getDigestValue();
0N/A
0N/A return verifySignedHash(hash, hash.length,
4127N/A messageDigestAlgorithm, convertEndianArray(sigBytes),
0N/A sigBytes.length, publicKey.getHCryptProvider(),
0N/A publicKey.getHCryptKey());
0N/A }
0N/A
0N/A /**
0N/A * Sets the specified algorithm parameter to the specified
0N/A * value. This method supplies a general-purpose mechanism through
0N/A * which it is possible to set the various parameters of this object.
0N/A * A parameter may be any settable parameter for the algorithm, such as
0N/A * a parameter size, or a source of random bits for signature generation
0N/A * (if appropriate), or an indication of whether or not to perform
0N/A * a specific but optional computation. A uniform algorithm-specific
0N/A * naming scheme for each parameter is desirable but left unspecified
0N/A * at this time.
0N/A *
0N/A * @param param the string identifier of the parameter.
0N/A *
0N/A * @param value the parameter value.
0N/A *
0N/A * @exception InvalidParameterException if <code>param</code> is an
0N/A * invalid parameter for this signature algorithm engine,
0N/A * the parameter is already set
0N/A * and cannot be set again, a security exception occurs, and so on.
0N/A *
0N/A * @deprecated Replaced by {@link
0N/A * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
0N/A * engineSetParameter}.
0N/A */
0N/A protected void engineSetParameter(String param, Object value)
0N/A throws InvalidParameterException
0N/A {
0N/A throw new InvalidParameterException("Parameter not supported");
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Gets the value of the specified algorithm parameter.
0N/A * This method supplies a general-purpose mechanism through which it
0N/A * is possible to get the various parameters of this object. A parameter
0N/A * may be any settable parameter for the algorithm, such as a parameter
0N/A * size, or a source of random bits for signature generation (if
0N/A * appropriate), or an indication of whether or not to perform a
0N/A * specific but optional computation. A uniform algorithm-specific
0N/A * naming scheme for each parameter is desirable but left unspecified
0N/A * at this time.
0N/A *
0N/A * @param param the string name of the parameter.
0N/A *
0N/A * @return the object that represents the parameter value, or null if
0N/A * there is none.
0N/A *
0N/A * @exception InvalidParameterException if <code>param</code> is an
0N/A * invalid parameter for this engine, or another exception occurs while
0N/A * trying to get this parameter.
0N/A *
0N/A * @deprecated
0N/A */
0N/A protected Object engineGetParameter(String param)
0N/A throws InvalidParameterException
0N/A {
0N/A throw new InvalidParameterException("Parameter not supported");
0N/A }
0N/A
0N/A /**
0N/A * Generates a public-key BLOB from a key's components.
0N/A */
4108N/A // used by RSACipher
4108N/A static native byte[] generatePublicKeyBlob(
4111N/A int keyBitLength, byte[] modulus, byte[] publicExponent)
4111N/A throws InvalidKeyException;
0N/A
0N/A /**
0N/A * Imports a public-key BLOB.
0N/A */
4108N/A // used by RSACipher
4111N/A static native RSAPublicKey importPublicKey(byte[] keyBlob, int keySize)
4111N/A throws KeyStoreException;
0N/A}