com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException Java Examples

The following examples show how to use com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransformC14N11_WithComments.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
protected XMLSignatureInput enginePerformTransform(
    XMLSignatureInput input, OutputStream os, Transform transform
) throws CanonicalizationException {

    Canonicalizer11_WithComments c14n = new Canonicalizer11_WithComments();
    if (os != null) {
        c14n.setWriter(os);
    }

    byte[] result = null;
    result = c14n.engineCanonicalize(input);
    XMLSignatureInput output = new XMLSignatureInput(result);
    if (os != null) {
        output.setOutputStream(os);
    }
    return output;
}
 
Example #2
Source File: TransformC14N11_WithComments.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
protected XMLSignatureInput enginePerformTransform(
    XMLSignatureInput input, OutputStream os, Transform transform
) throws CanonicalizationException {

    Canonicalizer11_WithComments c14n = new Canonicalizer11_WithComments();
    if (os != null) {
        c14n.setWriter(os);
    }

    byte[] result = null;
    result = c14n.engineCanonicalize(input);
    XMLSignatureInput output = new XMLSignatureInput(result);
    if (os != null) {
        output.setOutputStream(os);
    }
    return output;
}
 
Example #3
Source File: SignedInfo.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #4
Source File: XMLSignatureInput.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the node set from input which was specified as the parameter of
 * {@link XMLSignatureInput} constructor
 * @param circumvent
 *
 * @return the node set
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws CanonicalizationException
 */
public Set<Node> getNodeSet(boolean circumvent) throws ParserConfigurationException,
    IOException, SAXException, CanonicalizationException {
    if (inputNodeSet != null) {
        return inputNodeSet;
    }
    if (inputOctetStreamProxy == null && subNode != null) {
        if (circumvent) {
            XMLUtils.circumventBug2650(XMLUtils.getOwnerDocument(subNode));
        }
        inputNodeSet = new LinkedHashSet<Node>();
        XMLUtils.getSet(subNode, inputNodeSet, excludeNode, excludeComments);
        return inputNodeSet;
    } else if (isOctetStream()) {
        convertToNodes();
        Set<Node> result = new LinkedHashSet<Node>();
        XMLUtils.getSet(subNode, result, null, false);
        return result;
    }

    throw new RuntimeException("getNodeSet() called but no input data present");
}
 
Example #5
Source File: SignedInfo.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #6
Source File: C14nHelper.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * This method throws an exception if the Attribute value contains
 * a relative URI.
 *
 * @param attr
 * @throws CanonicalizationException
 */
public static void assertNotRelativeNS(Attr attr) throws CanonicalizationException {
    if (attr == null) {
        return;
    }

    String nodeAttrName = attr.getNodeName();
    boolean definesDefaultNS = nodeAttrName.equals("xmlns");
    boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:");

    if ((definesDefaultNS || definesNonDefaultNS) && namespaceIsRelative(attr)) {
        String parentName = attr.getOwnerElement().getTagName();
        String attrValue = attr.getValue();
        Object exArgs[] = { parentName, nodeAttrName, attrValue };

        throw new CanonicalizationException(
            "c14n.Canonicalizer.RelativeNamespace", exArgs
        );
    }
}
 
Example #7
Source File: CanonicalizerPhysical.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes()) {
        return null;
    }

    // result will contain all the attrs declared directly on that element
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            result.add(attribute);
        }
    }

    return result.iterator();
}
 
Example #8
Source File: XMLSignatureInput.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Method toString
 * @inheritDoc
 */
public String toString() {
    if (isNodeSet()) {
        return "XMLSignatureInput/NodeSet/" + inputNodeSet.size()
               + " nodes/" + getSourceURI();
    }
    if (isElement()) {
        return "XMLSignatureInput/Element/" + subNode
            + " exclude "+ excludeNode + " comments:"
            + excludeComments +"/" + getSourceURI();
    }
    try {
        return "XMLSignatureInput/OctetStream/" + getBytes().length
               + " octets/" + getSourceURI();
    } catch (IOException iex) {
        return "XMLSignatureInput/OctetStream//" + getSourceURI();
    } catch (CanonicalizationException cex) {
        return "XMLSignatureInput/OctetStream//" + getSourceURI();
    }
}
 
Example #9
Source File: XMLSignatureInput.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the node set from input which was specified as the parameter of
 * {@link XMLSignatureInput} constructor
 * @param circumvent
 *
 * @return the node set
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws CanonicalizationException
 */
public Set<Node> getNodeSet(boolean circumvent) throws ParserConfigurationException,
    IOException, SAXException, CanonicalizationException {
    if (inputNodeSet != null) {
        return inputNodeSet;
    }
    if (inputOctetStreamProxy == null && subNode != null) {
        if (circumvent) {
            XMLUtils.circumventBug2650(XMLUtils.getOwnerDocument(subNode));
        }
        inputNodeSet = new LinkedHashSet<Node>();
        XMLUtils.getSet(subNode, inputNodeSet, excludeNode, excludeComments);
        return inputNodeSet;
    } else if (isOctetStream()) {
        convertToNodes();
        Set<Node> result = new LinkedHashSet<Node>();
        XMLUtils.getSet(subNode, result, null, false);
        return result;
    }

    throw new RuntimeException("getNodeSet() called but no input data present");
}
 
Example #10
Source File: CanonicalizerPhysical.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes()) {
        return null;
    }

    // result will contain all the attrs declared directly on that element
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            result.add(attribute);
        }
    }

    return result.iterator();
}
 
Example #11
Source File: TransformC14N11_WithComments.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
protected XMLSignatureInput enginePerformTransform(
    XMLSignatureInput input, OutputStream os, Transform transform
) throws CanonicalizationException {

    Canonicalizer11_WithComments c14n = new Canonicalizer11_WithComments();
    if (os != null) {
        c14n.setWriter(os);
    }

    byte[] result = null;
    result = c14n.engineCanonicalize(input);
    XMLSignatureInput output = new XMLSignatureInput(result);
    if (os != null) {
        output.setOutputStream(os);
    }
    return output;
}
 
Example #12
Source File: TransformC14NWithComments.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/** @inheritDoc */
protected XMLSignatureInput enginePerformTransform(
    XMLSignatureInput input, OutputStream os, Transform transformObject
) throws CanonicalizationException {

    Canonicalizer20010315WithComments c14n = new Canonicalizer20010315WithComments();
    if (os != null) {
        c14n.setWriter(os);
    }

    byte[] result = null;
    result = c14n.engineCanonicalize(input);
    XMLSignatureInput output = new XMLSignatureInput(result);
    if (os != null) {
        output.setOutputStream(os);
    }
    return output;
}
 
Example #13
Source File: XMLSignatureInput.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Method toString
 * @inheritDoc
 */
public String toString() {
    if (isNodeSet()) {
        return "XMLSignatureInput/NodeSet/" + inputNodeSet.size()
               + " nodes/" + getSourceURI();
    }
    if (isElement()) {
        return "XMLSignatureInput/Element/" + subNode
            + " exclude "+ excludeNode + " comments:"
            + excludeComments +"/" + getSourceURI();
    }
    try {
        return "XMLSignatureInput/OctetStream/" + getBytes().length
               + " octets/" + getSourceURI();
    } catch (IOException iex) {
        return "XMLSignatureInput/OctetStream//" + getSourceURI();
    } catch (CanonicalizationException cex) {
        return "XMLSignatureInput/OctetStream//" + getSourceURI();
    }
}
 
Example #14
Source File: C14nHelper.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This method throws an exception if the Attribute value contains
 * a relative URI.
 *
 * @param attr
 * @throws CanonicalizationException
 */
public static void assertNotRelativeNS(Attr attr) throws CanonicalizationException {
    if (attr == null) {
        return;
    }

    String nodeAttrName = attr.getNodeName();
    boolean definesDefaultNS = nodeAttrName.equals("xmlns");
    boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:");

    if ((definesDefaultNS || definesNonDefaultNS) && namespaceIsRelative(attr)) {
        String parentName = attr.getOwnerElement().getTagName();
        String attrValue = attr.getValue();
        Object exArgs[] = { parentName, nodeAttrName, attrValue };

        throw new CanonicalizationException(
            "c14n.Canonicalizer.RelativeNamespace", exArgs
        );
    }
}
 
Example #15
Source File: RetrievalMethodResolver.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private static X509Certificate getRawCertificate(XMLSignatureInput resource)
    throws CanonicalizationException, IOException, CertificateException {
    byte inputBytes[] = resource.getBytes();
    // if the resource stores a raw certificate, we have to handle it
    CertificateFactory certFact =
        CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
    X509Certificate cert = (X509Certificate)
        certFact.generateCertificate(new ByteArrayInputStream(inputBytes));
    return cert;
}
 
Example #16
Source File: C14nHelper.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * This method throws a CanonicalizationException if the supplied Document
 * is not able to be traversed using a TreeWalker.
 *
 * @param document
 * @throws CanonicalizationException
 */
public static void checkTraversability(Document document)
    throws CanonicalizationException {
    if (!document.isSupported("Traversal", "2.0")) {
        Object exArgs[] = {document.getImplementation().getClass().getName() };

        throw new CanonicalizationException(
            "c14n.Canonicalizer.TraversalNotSupported", exArgs
        );
    }
}
 
Example #17
Source File: C14nHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method throws a CanonicalizationException if the supplied Document
 * is not able to be traversed using a TreeWalker.
 *
 * @param document
 * @throws CanonicalizationException
 */
public static void checkTraversability(Document document)
    throws CanonicalizationException {
    if (!document.isSupported("Traversal", "2.0")) {
        Object exArgs[] = {document.getImplementation().getClass().getName() };

        throw new CanonicalizationException(
            "c14n.Canonicalizer.TraversalNotSupported", exArgs
        );
    }
}
 
Example #18
Source File: Canonicalizer11.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void circumventBugIfNeeded(XMLSignatureInput input)
    throws CanonicalizationException, ParserConfigurationException,
    IOException, SAXException {
    if (!input.isNeedsToBeExpanded()) {
        return;
    }
    Document doc = null;
    if (input.getSubNode() != null) {
        doc = XMLUtils.getOwnerDocument(input.getSubNode());
    } else {
        doc = XMLUtils.getOwnerDocument(input.getNodeSet());
    }
    XMLUtils.circumventBug2650(doc);
}
 
Example #19
Source File: TransformC14NExclusiveWithComments.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #20
Source File: RetrievalMethodResolver.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static X509Certificate getRawCertificate(XMLSignatureInput resource)
    throws CanonicalizationException, IOException, CertificateException {
    byte inputBytes[] = resource.getBytes();
    // if the resource stores a raw certificate, we have to handle it
    CertificateFactory certFact =
        CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
    X509Certificate cert = (X509Certificate)
        certFact.generateCertificate(new ByteArrayInputStream(inputBytes));
    return cert;
}
 
Example #21
Source File: Canonicalizer20010315Excl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void circumventBugIfNeeded(XMLSignatureInput input)
    throws CanonicalizationException, ParserConfigurationException,
           IOException, SAXException {
    if (!input.isNeedsToBeExpanded() || inclusiveNSSet.isEmpty() || inclusiveNSSet.isEmpty()) {
        return;
    }
    Document doc = null;
    if (input.getSubNode() != null) {
        doc = XMLUtils.getOwnerDocument(input.getSubNode());
    } else {
        doc = XMLUtils.getOwnerDocument(input.getNodeSet());
    }
    XMLUtils.circumventBug2650(doc);
}
 
Example #22
Source File: TransformC14N11.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
protected XMLSignatureInput enginePerformTransform(
    XMLSignatureInput input, OutputStream os, Transform transform
) throws CanonicalizationException {
    Canonicalizer11_OmitComments c14n = new Canonicalizer11_OmitComments();
    if (os != null) {
        c14n.setWriter(os);
    }
    byte[] result = null;
    result = c14n.engineCanonicalize(input);
    XMLSignatureInput output = new XMLSignatureInput(result);
    if (os != null) {
        output.setOutputStream(os);
    }
    return output;
}
 
Example #23
Source File: TransformC14NExclusive.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #24
Source File: TransformC14NExclusive.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #25
Source File: SignedInfo.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #26
Source File: Canonicalizer20010315Excl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void circumventBugIfNeeded(XMLSignatureInput input)
    throws CanonicalizationException, ParserConfigurationException,
           IOException, SAXException {
    if (!input.isNeedsToBeExpanded() || inclusiveNSSet.isEmpty() || inclusiveNSSet.isEmpty()) {
        return;
    }
    Document doc = null;
    if (input.getSubNode() != null) {
        doc = XMLUtils.getOwnerDocument(input.getSubNode());
    } else {
        doc = XMLUtils.getOwnerDocument(input.getNodeSet());
    }
    XMLUtils.circumventBug2650(doc);
}
 
Example #27
Source File: TransformC14NExclusiveWithComments.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #28
Source File: XMLSignatureInput.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void convertToNodes() throws CanonicalizationException,
    ParserConfigurationException, IOException, SAXException {
    if (dfactory == null) {
        dfactory = DocumentBuilderFactory.newInstance();
        dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
        dfactory.setValidating(false);
        dfactory.setNamespaceAware(true);
    }
    DocumentBuilder db = dfactory.newDocumentBuilder();
    // select all nodes, also the comments.
    try {
        db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils.IgnoreAllErrorHandler());

        Document doc = db.parse(this.getOctetStream());
        this.subNode = doc;
    } catch (SAXException ex) {
        // if a not-wellformed nodeset exists, put a container around it...
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        baos.write("<container>".getBytes("UTF-8"));
        baos.write(this.getBytes());
        baos.write("</container>".getBytes("UTF-8"));

        byte result[] = baos.toByteArray();
        Document document = db.parse(new ByteArrayInputStream(result));
        this.subNode = document.getDocumentElement().getFirstChild().getFirstChild();
    } finally {
        if (this.inputOctetStreamProxy != null) {
            this.inputOctetStreamProxy.close();
        }
        this.inputOctetStreamProxy = null;
        this.bytes = null;
    }
}
 
Example #29
Source File: TransformC14N11.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected XMLSignatureInput enginePerformTransform(
    XMLSignatureInput input, OutputStream os, Transform transform
) throws CanonicalizationException {
    Canonicalizer11_OmitComments c14n = new Canonicalizer11_OmitComments();
    if (os != null) {
        c14n.setWriter(os);
    }
    byte[] result = null;
    result = c14n.engineCanonicalize(input);
    XMLSignatureInput output = new XMLSignatureInput(result);
    if (os != null) {
        output.setOutputStream(os);
    }
    return output;
}
 
Example #30
Source File: TransformC14NExclusive.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
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);
    }
}