0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
661N/A * Copyright 1999-2007 The Apache Software Foundation.
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A *
0N/A */
0N/Apackage com.sun.org.apache.xml.internal.security.algorithms.implementations;
0N/A
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.Key;
0N/Aimport java.security.NoSuchProviderException;
0N/Aimport java.security.PrivateKey;
0N/Aimport java.security.PublicKey;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.Signature;
0N/Aimport java.security.SignatureException;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/Aimport com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
0N/Aimport com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi;
0N/Aimport com.sun.org.apache.xml.internal.security.signature.XMLSignature;
0N/Aimport com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
0N/A
0N/A/**
0N/A *
661N/A * @author $Author: mullan $
0N/A */
0N/Apublic abstract class SignatureBaseRSA extends SignatureAlgorithmSpi {
0N/A
661N/A /** {@link java.util.logging} logging facility */
0N/A static java.util.logging.Logger log =
661N/A java.util.logging.Logger.getLogger
661N/A (SignatureBaseRSA.class.getName());
661N/A
661N/A /** @inheritDoc */
661N/A public abstract String engineGetURI();
661N/A
661N/A /** Field algorithm */
661N/A private java.security.Signature _signatureAlgorithm = null;
661N/A
661N/A /**
661N/A * Constructor SignatureRSA
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureBaseRSA() throws XMLSignatureException {
661N/A
661N/A String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
661N/A
661N/A if (log.isLoggable(java.util.logging.Level.FINE))
661N/A log.log(java.util.logging.Level.FINE, "Created SignatureRSA using " + algorithmID);
661N/A String provider=JCEMapper.getProviderId();
661N/A try {
661N/A if (provider==null) {
661N/A this._signatureAlgorithm = Signature.getInstance(algorithmID);
661N/A } else {
661N/A this._signatureAlgorithm = Signature.getInstance(algorithmID,provider);
661N/A }
661N/A } catch (java.security.NoSuchAlgorithmException ex) {
661N/A Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };
661N/A
661N/A throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
661N/A } catch (NoSuchProviderException ex) {
661N/A Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };
661N/A
661N/A throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected void engineSetParameter(AlgorithmParameterSpec params)
661N/A throws XMLSignatureException {
661N/A
661N/A try {
661N/A this._signatureAlgorithm.setParameter(params);
661N/A } catch (InvalidAlgorithmParameterException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected boolean engineVerify(byte[] signature)
661N/A throws XMLSignatureException {
661N/A
661N/A try {
661N/A return this._signatureAlgorithm.verify(signature);
661N/A } catch (SignatureException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected void engineInitVerify(Key publicKey) throws XMLSignatureException {
661N/A
661N/A if (!(publicKey instanceof PublicKey)) {
661N/A String supplied = publicKey.getClass().getName();
661N/A String needed = PublicKey.class.getName();
661N/A Object exArgs[] = { supplied, needed };
661N/A
661N/A throw new XMLSignatureException
661N/A ("algorithms.WrongKeyForThisOperation", exArgs);
661N/A }
661N/A
661N/A try {
661N/A this._signatureAlgorithm.initVerify((PublicKey) publicKey);
661N/A } catch (InvalidKeyException ex) {
661N/A // reinstantiate Signature object to work around bug in JDK
661N/A // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
661N/A Signature sig = this._signatureAlgorithm;
661N/A try {
661N/A this._signatureAlgorithm = Signature.getInstance
661N/A (_signatureAlgorithm.getAlgorithm());
661N/A } catch (Exception e) {
661N/A // this shouldn't occur, but if it does, restore previous
661N/A // Signature
661N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
661N/A log.log(java.util.logging.Level.FINE, "Exception when reinstantiating Signature:" + e);
661N/A }
661N/A this._signatureAlgorithm = sig;
661N/A }
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected byte[] engineSign() throws XMLSignatureException {
661N/A try {
661N/A return this._signatureAlgorithm.sign();
661N/A } catch (SignatureException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected void engineInitSign(Key privateKey, SecureRandom secureRandom)
661N/A throws XMLSignatureException {
661N/A
661N/A if (!(privateKey instanceof PrivateKey)) {
661N/A String supplied = privateKey.getClass().getName();
661N/A String needed = PrivateKey.class.getName();
661N/A Object exArgs[] = { supplied, needed };
661N/A
661N/A throw new XMLSignatureException
661N/A ("algorithms.WrongKeyForThisOperation", exArgs);
661N/A }
661N/A
661N/A try {
661N/A this._signatureAlgorithm.initSign
661N/A ((PrivateKey) privateKey, secureRandom);
661N/A } catch (InvalidKeyException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected void engineInitSign(Key privateKey) throws XMLSignatureException {
661N/A
661N/A if (!(privateKey instanceof PrivateKey)) {
661N/A String supplied = privateKey.getClass().getName();
661N/A String needed = PrivateKey.class.getName();
661N/A Object exArgs[] = { supplied, needed };
661N/A
661N/A throw new XMLSignatureException
661N/A ("algorithms.WrongKeyForThisOperation", exArgs);
661N/A }
661N/A
661N/A try {
661N/A this._signatureAlgorithm.initSign((PrivateKey) privateKey);
661N/A } catch (InvalidKeyException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected void engineUpdate(byte[] input) throws XMLSignatureException {
661N/A try {
661N/A this._signatureAlgorithm.update(input);
661N/A } catch (SignatureException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
661N/A
661N/A /** @inheritDoc */
661N/A protected void engineUpdate(byte input) throws XMLSignatureException {
661N/A try {
661N/A this._signatureAlgorithm.update(input);
661N/A } catch (SignatureException ex) {
661N/A throw new XMLSignatureException("empty", ex);
661N/A }
661N/A }
0N/A
0N/A /** @inheritDoc */
661N/A protected void engineUpdate(byte buf[], int offset, int len)
661N/A throws XMLSignatureException {
661N/A try {
661N/A this._signatureAlgorithm.update(buf, offset, len);
661N/A } catch (SignatureException ex) {
661N/A throw new XMLSignatureException("empty", ex);
0N/A }
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A protected String engineGetJCEAlgorithmString() {
661N/A return this._signatureAlgorithm.getAlgorithm();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A protected String engineGetJCEProviderName() {
661N/A return this._signatureAlgorithm.getProvider().getName();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A protected void engineSetHMACOutputLength(int HMACOutputLength)
661N/A throws XMLSignatureException {
661N/A throw new XMLSignatureException
661N/A ("algorithms.HMACOutputLengthOnlyForHMAC");
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A protected void engineInitSign(
661N/A Key signingKey, AlgorithmParameterSpec algorithmParameterSpec)
661N/A throws XMLSignatureException {
661N/A throw new XMLSignatureException(
661N/A "algorithms.CannotUseAlgorithmParameterSpecOnRSA");
661N/A }
0N/A
661N/A /**
661N/A * Class SignatureRSASHA1
661N/A *
661N/A * @author $Author: mullan $
661N/A * @version $Revision: 1.5 $
661N/A */
661N/A public static class SignatureRSASHA1 extends SignatureBaseRSA {
0N/A
661N/A /**
661N/A * Constructor SignatureRSASHA1
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureRSASHA1() throws XMLSignatureException {
661N/A super();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A public String engineGetURI() {
661N/A return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
661N/A }
661N/A }
0N/A
661N/A /**
661N/A * Class SignatureRSASHA256
661N/A *
661N/A * @author $Author: mullan $
661N/A * @version $Revision: 1.5 $
661N/A */
661N/A public static class SignatureRSASHA256 extends SignatureBaseRSA {
0N/A
661N/A /**
661N/A * Constructor SignatureRSASHA256
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureRSASHA256() throws XMLSignatureException {
661N/A super();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A public String engineGetURI() {
661N/A return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;
661N/A }
661N/A }
0N/A
661N/A /**
661N/A * Class SignatureRSASHA384
661N/A *
661N/A * @author $Author: mullan $
661N/A * @version $Revision: 1.5 $
661N/A */
661N/A public static class SignatureRSASHA384 extends SignatureBaseRSA {
0N/A
661N/A /**
661N/A * Constructor SignatureRSASHA384
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureRSASHA384() throws XMLSignatureException {
661N/A super();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A public String engineGetURI() {
661N/A return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;
661N/A }
661N/A }
0N/A
661N/A /**
661N/A * Class SignatureRSASHA512
661N/A *
661N/A * @author $Author: mullan $
661N/A * @version $Revision: 1.5 $
661N/A */
661N/A public static class SignatureRSASHA512 extends SignatureBaseRSA {
0N/A
661N/A /**
661N/A * Constructor SignatureRSASHA512
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureRSASHA512() throws XMLSignatureException {
661N/A super();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A public String engineGetURI() {
661N/A return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;
661N/A }
661N/A }
0N/A
661N/A /**
661N/A * Class SignatureRSARIPEMD160
661N/A *
661N/A * @author $Author: mullan $
661N/A * @version $Revision: 1.5 $
661N/A */
661N/A public static class SignatureRSARIPEMD160 extends SignatureBaseRSA {
0N/A
661N/A /**
661N/A * Constructor SignatureRSARIPEMD160
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureRSARIPEMD160() throws XMLSignatureException {
661N/A super();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A public String engineGetURI() {
661N/A return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;
661N/A }
661N/A }
0N/A
661N/A /**
661N/A * Class SignatureRSAMD5
661N/A *
661N/A * @author $Author: mullan $
661N/A * @version $Revision: 1.5 $
661N/A */
661N/A public static class SignatureRSAMD5 extends SignatureBaseRSA {
0N/A
661N/A /**
661N/A * Constructor SignatureRSAMD5
661N/A *
661N/A * @throws XMLSignatureException
661N/A */
661N/A public SignatureRSAMD5() throws XMLSignatureException {
661N/A super();
661N/A }
0N/A
661N/A /** @inheritDoc */
661N/A public String engineGetURI() {
661N/A return XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5;
661N/A }
661N/A }
0N/A}