Search in sources :

Example 11 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class CanonicalizerBase method engineCanonicalizeSubTree.

/**
     * Canonicalizes a Subtree node.
     *
     * @param rootNode
     *            the root of the subtree to canonicalize
     * @param excludeNode
     *            a node to be excluded from the canonicalize operation
     * @return The canonicalize stream.
     * @throws CanonicalizationException
     */
protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode) throws CanonicalizationException {
    this.excludeNode = excludeNode;
    try {
        NameSpaceSymbTable ns = new NameSpaceSymbTable();
        int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
        if (rootNode != null && Node.ELEMENT_NODE == rootNode.getNodeType()) {
            //Fills the nssymbtable with the definitions of the parent of the root subnode
            getParentNameSpaces((Element) rootNode, ns);
            nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
        }
        this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel);
        this.writer.flush();
        if (this.writer instanceof ByteArrayOutputStream) {
            byte[] result = ((ByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((ByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
            byte[] result = ((UnsyncByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((UnsyncByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else {
            this.writer.close();
        }
        return null;
    } catch (UnsupportedEncodingException ex) {
        throw new CanonicalizationException("empty", ex);
    } catch (IOException ex) {
        throw new CanonicalizationException("empty", ex);
    }
}
Also used : UnsyncByteArrayOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsyncByteArrayOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream) IOException(java.io.IOException)

Example 12 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Reference method getNodesetBeforeFirstCanonicalization.

/**
     * This method returns the XMLSignatureInput which represents the node set before
     * some kind of canonicalization is applied for the first time.
     * @return Gets a the node doing everything till the first c14n is needed
     *
     * @throws XMLSignatureException
     */
public XMLSignatureInput getNodesetBeforeFirstCanonicalization() throws XMLSignatureException {
    try {
        XMLSignatureInput input = this.getContentsBeforeTransformation();
        cacheDereferencedElement(input);
        XMLSignatureInput output = input;
        Transforms transforms = this.getTransforms();
        if (transforms != null) {
            doTransforms: for (int i = 0; i < transforms.getLength(); i++) {
                Transform t = transforms.item(i);
                String uri = t.getURI();
                if (uri.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_WITH_COMMENTS)) {
                    break doTransforms;
                }
                output = t.performTransform(output, null);
            }
            output.setSourceURI(input.getSourceURI());
        }
        return output;
    } catch (IOException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (ResourceResolverException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (CanonicalizationException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (TransformationException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException("empty", ex);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) Transforms(com.sun.org.apache.xml.internal.security.transforms.Transforms) IOException(java.io.IOException) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) Transform(com.sun.org.apache.xml.internal.security.transforms.Transform) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 13 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class TransformC14NExclusive method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws CanonicalizationException {
    try {
        String inclusiveNamespaces = null;
        if (transformObject.length(InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES) == 1) {
            Element inclusiveElement = XMLUtils.selectNode(transformObject.getElement().getFirstChild(), InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES, 0);
            inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement, transformObject.getBaseURI()).getInclusiveNamespaces();
        }
        Canonicalizer20010315ExclOmitComments c14n = new Canonicalizer20010315ExclOmitComments();
        if (os != null) {
            c14n.setWriter(os);
        }
        byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
        XMLSignatureInput output = new XMLSignatureInput(result);
        if (os != null) {
            output.setOutputStream(os);
        }
        return output;
    } catch (XMLSecurityException ex) {
        throw new CanonicalizationException("empty", ex);
    }
}
Also used : CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Element(org.w3c.dom.Element) InclusiveNamespaces(com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) Canonicalizer20010315ExclOmitComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclOmitComments) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 14 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class TransformC14NExclusiveWithComments method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws CanonicalizationException {
    try {
        String inclusiveNamespaces = null;
        if (transformObject.length(InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES) == 1) {
            Element inclusiveElement = XMLUtils.selectNode(transformObject.getElement().getFirstChild(), InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES, 0);
            inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement, transformObject.getBaseURI()).getInclusiveNamespaces();
        }
        Canonicalizer20010315ExclWithComments c14n = new Canonicalizer20010315ExclWithComments();
        if (os != null) {
            c14n.setWriter(os);
        }
        byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
        XMLSignatureInput output = new XMLSignatureInput(result);
        return output;
    } catch (XMLSecurityException ex) {
        throw new CanonicalizationException("empty", ex);
    }
}
Also used : Canonicalizer20010315ExclWithComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclWithComments) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Element(org.w3c.dom.Element) InclusiveNamespaces(com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 15 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class XMLSignature method sign.

/**
     * Digests all References in the SignedInfo, calculates the signature value
     * and sets it in the SignatureValue Element.
     *
     * @param signingKey the {@link java.security.PrivateKey} or
     * {@link javax.crypto.SecretKey} that is used to sign.
     * @throws XMLSignatureException
     */
public void sign(Key signingKey) throws XMLSignatureException {
    if (signingKey instanceof PublicKey) {
        throw new IllegalArgumentException(I18n.translate("algorithms.operationOnlyVerification"));
    }
    try {
        //Create a SignatureAlgorithm object
        SignedInfo si = this.getSignedInfo();
        SignatureAlgorithm sa = si.getSignatureAlgorithm();
        OutputStream so = null;
        try {
            // initialize SignatureAlgorithm for signing
            sa.initSign(signingKey);
            // generate digest values for all References in this SignedInfo
            si.generateDigestValues();
            so = new UnsyncBufferedOutputStream(new SignerOutputStream(sa));
            // get the canonicalized bytes from SignedInfo
            si.signInOctetStream(so);
        } catch (XMLSecurityException ex) {
            throw ex;
        } finally {
            if (so != null) {
                try {
                    so.close();
                } catch (IOException ex) {
                    if (log.isLoggable(java.util.logging.Level.FINE)) {
                        log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
                    }
                }
            }
        }
        // set them on the SignatureValue element
        this.setSignatureValueElement(sa.sign());
    } catch (XMLSignatureException ex) {
        throw ex;
    } catch (CanonicalizationException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException("empty", ex);
    }
}
Also used : SignerOutputStream(com.sun.org.apache.xml.internal.security.utils.SignerOutputStream) PublicKey(java.security.PublicKey) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) SignerOutputStream(com.sun.org.apache.xml.internal.security.utils.SignerOutputStream) UnsyncBufferedOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream) OutputStream(java.io.OutputStream) SignatureAlgorithm(com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm) IOException(java.io.IOException) UnsyncBufferedOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Aggregations

CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)17 Attr (org.w3c.dom.Attr)9 Node (org.w3c.dom.Node)7 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)6 IOException (java.io.IOException)6 NamedNodeMap (org.w3c.dom.NamedNodeMap)6 Element (org.w3c.dom.Element)5 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)4 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)4 UnsyncByteArrayOutputStream (com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)3 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)3 OutputStream (java.io.OutputStream)3 Transforms (com.sun.org.apache.xml.internal.security.transforms.Transforms)2 InclusiveNamespaces (com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 SignatureAlgorithm (com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm)1 Canonicalizer20010315ExclOmitComments (com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclOmitComments)1