RSASignature.java revision 0
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. 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
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any 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;
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;
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 *
0N/A * . "SHA1withRSA"
0N/A * . "MD5withRSA"
0N/A * . "MD2withRSA"
0N/A *
0N/A * Note: RSA keys must be at least 512 bits long
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
0N/A // flag indicating whether the digest is 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
0N/A
0N/A RSASignature(String digestName) {
0N/A
0N/A try {
0N/A messageDigest = MessageDigest.getInstance(digestName);
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
0N/A public static final class SHA1 extends RSASignature {
0N/A public SHA1() {
0N/A super("SHA1");
0N/A }
0N/A }
0N/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
0N/A /**
0N/A * Initializes this signature object with the specified
0N/A * public key for verification operations.
0N/A *
0N/A * @param publicKey the public key of the identity whose signature is
0N/A * going to be verified.
0N/A *
0N/A * @exception InvalidKeyException if the key is improperly
0N/A * encoded, parameters are missing, and so on.
0N/A */
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
0N/A byte[] modulusBytes = rsaKey.getModulus().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(
0N/A keyBitLength, modulusBytes,
0N/A rsaKey.getPublicExponent().toByteArray());
0N/A
0N/A publicKey = importPublicKey(keyBlob, keyBitLength);
0N/A
0N/A } else {
0N/A publicKey = (sun.security.mscapi.RSAPublicKey) key;
0N/A }
0N/A
0N/A if (needsReset) {
0N/A messageDigest.reset();
0N/A needsReset = false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Initializes this signature object with the specified
0N/A * private key for signing operations.
0N/A *
0N/A * @param privateKey the private key of the identity whose signature
0N/A * will be generated.
0N/A *
0N/A * @exception InvalidKeyException if the key is improperly
0N/A * encoded, parameters are missing, and so on.
0N/A */
0N/A protected void engineInitSign(PrivateKey key)
0N/A 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
0N/A // Determine byte length from bit length
0N/A int keySize = (privateKey.bitLength() + 7) >> 3;
0N/A
0N/A if (keySize < 64)
0N/A throw new InvalidKeyException(
0N/A "RSA keys must be at least 512 bits long");
0N/A
0N/A if (needsReset) {
0N/A messageDigest.reset();
0N/A needsReset = false;
0N/A }
0N/A }
0N/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
0N/A byte[] hash = messageDigest.digest();
0N/A needsReset = false;
0N/A
0N/A // Sign hash using MS Crypto APIs
0N/A
0N/A byte[] result = signHash(hash, hash.length,
0N/A messageDigest.getAlgorithm(), 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 */
0N/A private native static byte[] signHash(byte[] hash, int hashSize,
0N/A 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 {
0N/A byte[] hash = messageDigest.digest();
0N/A needsReset = false;
0N/A
0N/A return verifySignedHash(hash, hash.length,
0N/A messageDigest.getAlgorithm(), 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 */
0N/A private native byte[] generatePublicKeyBlob(
0N/A int keyBitLength, byte[] modulus, byte[] publicExponent);
0N/A
0N/A /**
0N/A * Imports a public-key BLOB.
0N/A */
0N/A private native RSAPublicKey importPublicKey(byte[] keyBlob, int keySize);
0N/A}