/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* PKCS#1 RSA signatures with the various message digest algorithms.
* This file contains an abstract base class with all the logic plus
* a nested static class for each of the message digest algorithms
* (see end of the file). We support MD2, MD5, SHA-1, SHA-256, SHA-384,
* and SHA-512.
*
* @since 1.5
* @author Andreas Sterbenz
*/
// we sign an ASN.1 SEQUENCE of AlgorithmId and digest
// it has the form 30:xx:30:xx:[digestOID]:05:00:04:xx:[digest]
// this means the encoded length is (8 + digestOID.length + digest.length)
// object identifier for the message digest algorithm used
// length of the encoded signature blob
private final int encodedLength;
// message digest implementation we use
// flag indicating whether the digest is reset
private boolean digestReset;
// private key, if initialized for signing
// public key, if initialized for verifying
// padding to use, set when the initSign/initVerify is called
/**
* Construct a new RSASignature. Used by subclasses.
*/
try {
} catch (NoSuchAlgorithmException e) {
throw new ProviderException(e);
}
digestReset = true;
}
// initialize for verification. See JCA doc
throws InvalidKeyException {
this.privateKey = null;
}
// initialize for signing. See JCA doc
throws InvalidKeyException {
}
// initialize for signing. See JCA doc
throws InvalidKeyException {
this.privateKey = rsaKey;
}
/**
* Init code common to sign and verify.
*/
throws InvalidKeyException {
resetDigest();
try {
} catch (InvalidAlgorithmParameterException iape) {
}
if (encodedLength > maxDataSize) {
throw new InvalidKeyException
("Key is too short for this signature algorithm");
}
}
/**
* Reset the message digest if it is not already reset.
*/
private void resetDigest() {
if (digestReset == false) {
digestReset = true;
}
}
/**
* Return the message digest value.
*/
private byte[] getDigestValue() {
digestReset = true;
}
// update the signature with the plaintext data. See JCA doc
digestReset = false;
}
// update the signature with the plaintext data. See JCA doc
throws SignatureException {
digestReset = false;
}
// update the signature with the plaintext data. See JCA doc
digestReset = false;
}
// sign the data and return the signature. See JCA doc
byte[] digest = getDigestValue();
try {
return encrypted;
} catch (GeneralSecurityException e) {
throw new SignatureException("Could not sign data", e);
} catch (IOException e) {
throw new SignatureException("Could not encode data", e);
}
}
// verify the data and return the result. See JCA doc
throw new SignatureException("Signature length not correct: got " +
}
byte[] digest = getDigestValue();
try {
// occurs if the app has used the wrong RSA public key
// or if sigBytes is invalid
// return false rather than propagating the exception for
// compatibility/ease of use
return false;
} catch (GeneralSecurityException e) {
throw new SignatureException("Signature verification failed", e);
} catch (IOException e) {
throw new SignatureException("Signature encoding error", e);
}
}
/**
* Encode the digest, return the to-be-signed data.
* Also used by the PKCS#11 provider.
*/
throws IOException {
return result.toByteArray();
}
/**
* Decode the signature data. Verify that the object identifier matches
* and return the message digest.
*/
throws IOException {
throw new IOException("SEQUENCE length error");
}
throw new IOException("ObjectIdentifier mismatch: "
}
throw new IOException("Unexpected AlgorithmId parameters");
}
return digest;
}
// set parameter, not supported. See JCA doc
throws InvalidParameterException {
throw new UnsupportedOperationException("setParameter() not supported");
}
// get parameter, not supported. See JCA doc
throws InvalidParameterException {
throw new UnsupportedOperationException("getParameter() not supported");
}
// Nested class for MD2withRSA signatures
public MD2withRSA() {
}
}
// Nested class for MD5withRSA signatures
public MD5withRSA() {
}
}
// Nested class for SHA1withRSA signatures
public SHA1withRSA() {
}
}
// Nested class for SHA256withRSA signatures
public SHA256withRSA() {
}
}
// Nested class for SHA384withRSA signatures
public SHA384withRSA() {
}
}
// Nested class for SHA512withRSA signatures
public SHA512withRSA() {
}
}
}