| 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
package com.sun.org.apache.xml.internal.security.algorithms.implementations;  | 
 | 
 | 
 | 
import java.security.InvalidAlgorithmParameterException;  | 
 | 
import java.security.InvalidKeyException;  | 
 | 
import java.security.Key;  | 
 | 
import java.security.NoSuchProviderException;  | 
 | 
import java.security.PrivateKey;  | 
 | 
import java.security.PublicKey;  | 
 | 
import java.security.SecureRandom;  | 
 | 
import java.security.Signature;  | 
 | 
import java.security.SignatureException;  | 
 | 
import java.security.spec.AlgorithmParameterSpec;  | 
 | 
 | 
 | 
import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;  | 
 | 
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi;  | 
 | 
import com.sun.org.apache.xml.internal.security.signature.XMLSignature;  | 
 | 
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;  | 
 | 
 | 
 | 
public abstract class SignatureBaseRSA extends SignatureAlgorithmSpi { | 
 | 
 | 
 | 
    private static final com.sun.org.slf4j.internal.Logger LOG =  | 
 | 
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(SignatureBaseRSA.class);  | 
 | 
 | 
 | 
      | 
 | 
    public abstract String engineGetURI();  | 
 | 
 | 
 | 
      | 
 | 
    private Signature signatureAlgorithm;  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public SignatureBaseRSA() throws XMLSignatureException { | 
 | 
        String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());  | 
 | 
 | 
 | 
        LOG.debug("Created SignatureRSA using {}", algorithmID); | 
 | 
        String provider = JCEMapper.getProviderId();  | 
 | 
        try { | 
 | 
            if (provider == null) { | 
 | 
                this.signatureAlgorithm = Signature.getInstance(algorithmID);  | 
 | 
            } else { | 
 | 
                this.signatureAlgorithm = Signature.getInstance(algorithmID, provider);  | 
 | 
            }  | 
 | 
        } catch (java.security.NoSuchAlgorithmException ex) { | 
 | 
            Object[] exArgs = { algorithmID, ex.getLocalizedMessage() }; | 
 | 
 | 
 | 
            throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); | 
 | 
        } catch (NoSuchProviderException ex) { | 
 | 
            Object[] exArgs = { algorithmID, ex.getLocalizedMessage() }; | 
 | 
 | 
 | 
            throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineSetParameter(AlgorithmParameterSpec params)  | 
 | 
        throws XMLSignatureException { | 
 | 
        try { | 
 | 
            this.signatureAlgorithm.setParameter(params);  | 
 | 
        } catch (InvalidAlgorithmParameterException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected boolean engineVerify(byte[] signature) throws XMLSignatureException { | 
 | 
        try { | 
 | 
            return this.signatureAlgorithm.verify(signature);  | 
 | 
        } catch (SignatureException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineInitVerify(Key publicKey) throws XMLSignatureException { | 
 | 
        if (!(publicKey instanceof PublicKey)) { | 
 | 
            String supplied = null;  | 
 | 
            if (publicKey != null) { | 
 | 
                supplied = publicKey.getClass().getName();  | 
 | 
            }  | 
 | 
            String needed = PublicKey.class.getName();  | 
 | 
            Object exArgs[] = { supplied, needed }; | 
 | 
 | 
 | 
            throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs); | 
 | 
        }  | 
 | 
 | 
 | 
        try { | 
 | 
            this.signatureAlgorithm.initVerify((PublicKey) publicKey);  | 
 | 
        } catch (InvalidKeyException ex) { | 
 | 
            // reinstantiate Signature object to work around bug in JDK  | 
 | 
              | 
 | 
            Signature sig = this.signatureAlgorithm;  | 
 | 
            try { | 
 | 
                this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm());  | 
 | 
            } catch (Exception e) { | 
 | 
                // this shouldn't occur, but if it does, restore previous  | 
 | 
                  | 
 | 
                LOG.debug("Exception when reinstantiating Signature: {}", e); | 
 | 
                this.signatureAlgorithm = sig;  | 
 | 
            }  | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected byte[] engineSign() throws XMLSignatureException { | 
 | 
        try { | 
 | 
            return this.signatureAlgorithm.sign();  | 
 | 
        } catch (SignatureException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineInitSign(Key privateKey, SecureRandom secureRandom)  | 
 | 
        throws XMLSignatureException { | 
 | 
        if (!(privateKey instanceof PrivateKey)) { | 
 | 
            String supplied = null;  | 
 | 
            if (privateKey != null) { | 
 | 
                supplied = privateKey.getClass().getName();  | 
 | 
            }  | 
 | 
            String needed = PrivateKey.class.getName();  | 
 | 
            Object exArgs[] = { supplied, needed }; | 
 | 
 | 
 | 
            throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs); | 
 | 
        }  | 
 | 
 | 
 | 
        try { | 
 | 
            if (secureRandom == null) { | 
 | 
                this.signatureAlgorithm.initSign((PrivateKey) privateKey);  | 
 | 
            } else { | 
 | 
                this.signatureAlgorithm.initSign((PrivateKey) privateKey, secureRandom);  | 
 | 
            }  | 
 | 
        } catch (InvalidKeyException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineInitSign(Key privateKey) throws XMLSignatureException { | 
 | 
        engineInitSign(privateKey, (SecureRandom)null);  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineUpdate(byte[] input) throws XMLSignatureException { | 
 | 
        try { | 
 | 
            this.signatureAlgorithm.update(input);  | 
 | 
        } catch (SignatureException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineUpdate(byte input) throws XMLSignatureException { | 
 | 
        try { | 
 | 
            this.signatureAlgorithm.update(input);  | 
 | 
        } catch (SignatureException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineUpdate(byte buf[], int offset, int len) throws XMLSignatureException { | 
 | 
        try { | 
 | 
            this.signatureAlgorithm.update(buf, offset, len);  | 
 | 
        } catch (SignatureException ex) { | 
 | 
            throw new XMLSignatureException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected String engineGetJCEAlgorithmString() { | 
 | 
        return this.signatureAlgorithm.getAlgorithm();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected String engineGetJCEProviderName() { | 
 | 
        return this.signatureAlgorithm.getProvider().getName();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineSetHMACOutputLength(int HMACOutputLength)  | 
 | 
        throws XMLSignatureException { | 
 | 
        throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC"); | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    protected void engineInitSign(  | 
 | 
        Key signingKey, AlgorithmParameterSpec algorithmParameterSpec  | 
 | 
    ) throws XMLSignatureException { | 
 | 
        throw new XMLSignatureException("algorithms.CannotUseAlgorithmParameterSpecOnRSA"); | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA1 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA1() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA224 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA224() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA224;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA256 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA256() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA384 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA384() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA512 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA512() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSARIPEMD160 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSARIPEMD160() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSAMD5 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSAMD5() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA1MGF1 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA1MGF1() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1_MGF1;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA224MGF1 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA224MGF1() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA224_MGF1;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA256MGF1 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA256MGF1() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256_MGF1;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA384MGF1 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA384MGF1() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384_MGF1;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public static class SignatureRSASHA512MGF1 extends SignatureBaseRSA { | 
 | 
 | 
 | 
          | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
         */  | 
 | 
        public SignatureRSASHA512MGF1() throws XMLSignatureException { | 
 | 
            super();  | 
 | 
        }  | 
 | 
 | 
 | 
          | 
 | 
        public String engineGetURI() { | 
 | 
            return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512_MGF1;  | 
 | 
        }  | 
 | 
    }  | 
 | 
}  |