Search in sources :

Example 21 with XMLSignatureException

use of org.apache.xml.security.signature.XMLSignatureException in project santuario-java by apache.

the class SignatureBaseRSA method engineInitVerify.

/**
 * {@inheritDoc}
 */
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
        // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
        Signature sig = this.signatureAlgorithm;
        try {
            this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm());
        } catch (Exception e) {
            // this shouldn't occur, but if it does, restore previous
            // Signature
            LOG.debug("Exception when reinstantiating Signature: {}", e);
            this.signatureAlgorithm = sig;
        }
        throw new XMLSignatureException(ex);
    }
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) XMLSignature(org.apache.xml.security.signature.XMLSignature) InvalidKeyException(java.security.InvalidKeyException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException) SignatureException(java.security.SignatureException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException)

Example 22 with XMLSignatureException

use of org.apache.xml.security.signature.XMLSignatureException in project santuario-java by apache.

the class IntegrityHmac method engineInitVerify.

/**
 * Proxy method for {@link java.security.Signature#initVerify(java.security.PublicKey)}
 * which is executed on the internal {@link java.security.Signature} object.
 *
 * @param secretKey
 * @throws XMLSignatureException
 */
protected void engineInitVerify(Key secretKey) throws XMLSignatureException {
    if (!(secretKey instanceof SecretKey)) {
        String supplied = null;
        if (secretKey != null) {
            supplied = secretKey.getClass().getName();
        }
        String needed = SecretKey.class.getName();
        Object[] exArgs = { supplied, needed };
        throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs);
    }
    try {
        this.macAlgorithm.init(secretKey);
    } catch (InvalidKeyException ex) {
        // reinstantiate Mac object to work around bug in JDK
        // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
        Mac mac = this.macAlgorithm;
        try {
            this.macAlgorithm = Mac.getInstance(macAlgorithm.getAlgorithm());
        } catch (Exception e) {
            // this shouldn't occur, but if it does, restore previous Mac
            LOG.debug("Exception when reinstantiating Mac: {}", e);
            this.macAlgorithm = mac;
        }
        throw new XMLSignatureException(ex);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidKeyException(java.security.InvalidKeyException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException) Mac(javax.crypto.Mac) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidKeyException(java.security.InvalidKeyException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException)

Example 23 with XMLSignatureException

use of org.apache.xml.security.signature.XMLSignatureException in project santuario-java by apache.

the class IntegrityHmac method engineInitSign.

/**
 * Method engineInitSign
 *
 * @param secretKey
 * @param algorithmParameterSpec
 * @throws XMLSignatureException
 */
protected void engineInitSign(Key secretKey, AlgorithmParameterSpec algorithmParameterSpec) throws XMLSignatureException {
    if (!(secretKey instanceof SecretKey)) {
        String supplied = null;
        if (secretKey != null) {
            supplied = secretKey.getClass().getName();
        }
        String needed = SecretKey.class.getName();
        Object[] exArgs = { supplied, needed };
        throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs);
    }
    try {
        if (algorithmParameterSpec == null) {
            this.macAlgorithm.init(secretKey);
        } else {
            this.macAlgorithm.init(secretKey, algorithmParameterSpec);
        }
    } catch (InvalidKeyException ex) {
        throw new XMLSignatureException(ex);
    } catch (InvalidAlgorithmParameterException ex) {
        throw new XMLSignatureException(ex);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidKeyException(java.security.InvalidKeyException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException)

Example 24 with XMLSignatureException

use of org.apache.xml.security.signature.XMLSignatureException in project santuario-java by apache.

the class IntegrityHmac method engineVerify.

/**
 * Proxy method for {@link java.security.Signature#verify(byte[])}
 * which is executed on the internal {@link java.security.Signature} object.
 *
 * @param signature
 * @return true if the signature is correct
 * @throws XMLSignatureException
 */
protected boolean engineVerify(byte[] signature) throws XMLSignatureException {
    try {
        if (this.HMACOutputLengthSet && this.HMACOutputLength < getDigestLength()) {
            LOG.debug("HMACOutputLength must not be less than {}", getDigestLength());
            Object[] exArgs = { String.valueOf(getDigestLength()) };
            throw new XMLSignatureException("algorithms.HMACOutputLengthMin", exArgs);
        } else {
            byte[] completeResult = this.macAlgorithm.doFinal();
            return MessageDigestAlgorithm.isEqual(completeResult, signature);
        }
    } catch (IllegalStateException ex) {
        throw new XMLSignatureException(ex);
    }
}
Also used : XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException)

Example 25 with XMLSignatureException

use of org.apache.xml.security.signature.XMLSignatureException in project santuario-java by apache.

the class SignatureECDSA method engineInitVerify.

/**
 * {@inheritDoc}
 */
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
        // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
        Signature sig = this.signatureAlgorithm;
        try {
            this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm());
        } catch (Exception e) {
            // this shouldn't occur, but if it does, restore previous
            // Signature
            LOG.debug("Exception when reinstantiating Signature: {}", e);
            this.signatureAlgorithm = sig;
        }
        throw new XMLSignatureException(ex);
    }
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) XMLSignature(org.apache.xml.security.signature.XMLSignature) InvalidKeyException(java.security.InvalidKeyException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException)

Aggregations

XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)28 InvalidKeyException (java.security.InvalidKeyException)8 XMLSignature (org.apache.xml.security.signature.XMLSignature)8 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)5 X509Certificate (java.security.cert.X509Certificate)5 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5 PrivateKey (java.security.PrivateKey)4 Transforms (org.apache.xml.security.transforms.Transforms)4 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)3 NoSuchProviderException (java.security.NoSuchProviderException)3 PublicKey (java.security.PublicKey)3 Signature (java.security.Signature)3 SignatureException (java.security.SignatureException)3 Reference (org.apache.xml.security.signature.Reference)3 OfflineResolver (org.apache.xml.security.test.dom.utils.resolver.OfflineResolver)3 ResourceResolverSpi (org.apache.xml.security.utils.resolver.ResourceResolverSpi)3 UnsupportedAlgorithmException (xades4j.UnsupportedAlgorithmException)3 IOException (java.io.IOException)2