Search in sources :

Example 1 with Canonicalizer

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

the class SignedInfo method reparseSignedInfoElem.

private static Element reparseSignedInfoElem(Element element) throws XMLSecurityException {
    /*
         * If a custom canonicalizationMethod is used, canonicalize
         * ds:SignedInfo, reparse it into a new document
         * and replace the original not-canonicalized ds:SignedInfo by
         * the re-parsed canonicalized one.
         */
    Element c14nMethod = XMLUtils.getNextElement(element.getFirstChild());
    String c14nMethodURI = c14nMethod.getAttributeNS(null, Constants._ATT_ALGORITHM);
    if (!(c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS))) {
        // so reparse the SignedInfo to be sure
        try {
            Canonicalizer c14nizer = Canonicalizer.getInstance(c14nMethodURI);
            byte[] c14nizedBytes = c14nizer.canonicalizeSubtree(element);
            javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
            javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
            Document newdoc = db.parse(new ByteArrayInputStream(c14nizedBytes));
            Node imported = element.getOwnerDocument().importNode(newdoc.getDocumentElement(), true);
            element.getParentNode().replaceChild(imported, element);
            return (Element) imported;
        } catch (ParserConfigurationException ex) {
            throw new XMLSecurityException("empty", ex);
        } catch (IOException ex) {
            throw new XMLSecurityException("empty", ex);
        } catch (SAXException ex) {
            throw new XMLSecurityException("empty", ex);
        }
    }
    return element;
}
Also used : Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Canonicalizer(com.sun.org.apache.xml.internal.security.c14n.Canonicalizer)

Example 2 with Canonicalizer

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

the class SignedInfo method getCanonicalizedOctetStream.

/**
     * Returns getCanonicalizedOctetStream
     *
     * @return the canonicalization result octet stream of <code>SignedInfo</code> element
     * @throws CanonicalizationException
     * @throws InvalidCanonicalizerException
     * @throws XMLSecurityException
     */
public byte[] getCanonicalizedOctetStream() throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
    if (this.c14nizedBytes == null) {
        Canonicalizer c14nizer = Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
        this.c14nizedBytes = c14nizer.canonicalizeSubtree(this.constructionElement);
    }
    // make defensive copy
    return this.c14nizedBytes.clone();
}
Also used : Canonicalizer(com.sun.org.apache.xml.internal.security.c14n.Canonicalizer)

Example 3 with Canonicalizer

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

the class SignedInfo method signInOctetStream.

/**
     * Output the C14n stream to the given OutputStream.
     * @param os
     * @throws CanonicalizationException
     * @throws InvalidCanonicalizerException
     * @throws XMLSecurityException
     */
public void signInOctetStream(OutputStream os) throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
    if (this.c14nizedBytes == null) {
        Canonicalizer c14nizer = Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
        c14nizer.setWriter(os);
        String inclusiveNamespaces = this.getInclusiveNamespaces();
        if (inclusiveNamespaces == null) {
            c14nizer.canonicalizeSubtree(this.constructionElement);
        } else {
            c14nizer.canonicalizeSubtree(this.constructionElement, inclusiveNamespaces);
        }
    } else {
        try {
            os.write(this.c14nizedBytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : IOException(java.io.IOException) Canonicalizer(com.sun.org.apache.xml.internal.security.c14n.Canonicalizer)

Aggregations

Canonicalizer (com.sun.org.apache.xml.internal.security.c14n.Canonicalizer)3 IOException (java.io.IOException)2 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1