Search in sources :

Example 6 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class DOMReference method transform.

private byte[] transform(Data dereferencedData, XMLCryptoContext context) throws XMLSignatureException {
    if (md == null) {
        try {
            md = MessageDigest.getInstance(((DOMDigestMethod) digestMethod).getMessageDigestAlgorithm());
        } catch (NoSuchAlgorithmException nsae) {
            throw new XMLSignatureException(nsae);
        }
    }
    md.reset();
    DigesterOutputStream dos;
    Boolean cache = (Boolean) context.getProperty("javax.xml.crypto.dsig.cacheReference");
    if (cache != null && cache.booleanValue()) {
        this.derefData = copyDerefData(dereferencedData);
        dos = new DigesterOutputStream(md, true);
    } else {
        dos = new DigesterOutputStream(md);
    }
    OutputStream os = null;
    Data data = dereferencedData;
    try {
        os = new UnsyncBufferedOutputStream(dos);
        for (int i = 0, size = transforms.size(); i < size; i++) {
            DOMTransform transform = (DOMTransform) transforms.get(i);
            if (i < size - 1) {
                data = transform.transform(data, context);
            } else {
                data = transform.transform(data, context, os);
            }
        }
        if (data != null) {
            XMLSignatureInput xi;
            // explicitly use C14N 1.1 when generating signature
            // first check system property, then context property
            boolean c14n11 = useC14N11;
            String c14nalg = CanonicalizationMethod.INCLUSIVE;
            if (context instanceof XMLSignContext) {
                if (!c14n11) {
                    Boolean prop = (Boolean) context.getProperty("com.sun.org.apache.xml.internal.security.useC14N11");
                    c14n11 = (prop != null && prop.booleanValue());
                    if (c14n11) {
                        c14nalg = "http://www.w3.org/2006/12/xml-c14n11";
                    }
                } else {
                    c14nalg = "http://www.w3.org/2006/12/xml-c14n11";
                }
            }
            if (data instanceof ApacheData) {
                xi = ((ApacheData) data).getXMLSignatureInput();
            } else if (data instanceof OctetStreamData) {
                xi = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
            } else if (data instanceof NodeSetData) {
                TransformService spi = null;
                if (provider == null) {
                    spi = TransformService.getInstance(c14nalg, "DOM");
                } else {
                    try {
                        spi = TransformService.getInstance(c14nalg, "DOM", provider);
                    } catch (NoSuchAlgorithmException nsae) {
                        spi = TransformService.getInstance(c14nalg, "DOM");
                    }
                }
                data = spi.transform(data, context);
                xi = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
            } else {
                throw new XMLSignatureException("unrecognized Data type");
            }
            if (context instanceof XMLSignContext && c14n11 && !xi.isOctetStream() && !xi.isOutputStreamSet()) {
                TransformService spi = null;
                if (provider == null) {
                    spi = TransformService.getInstance(c14nalg, "DOM");
                } else {
                    try {
                        spi = TransformService.getInstance(c14nalg, "DOM", provider);
                    } catch (NoSuchAlgorithmException nsae) {
                        spi = TransformService.getInstance(c14nalg, "DOM");
                    }
                }
                DOMTransform t = new DOMTransform(spi);
                Element transformsElem = null;
                String dsPrefix = DOMUtils.getSignaturePrefix(context);
                if (allTransforms.isEmpty()) {
                    transformsElem = DOMUtils.createElement(refElem.getOwnerDocument(), "Transforms", XMLSignature.XMLNS, dsPrefix);
                    refElem.insertBefore(transformsElem, DOMUtils.getFirstChildElement(refElem));
                } else {
                    transformsElem = DOMUtils.getFirstChildElement(refElem);
                }
                t.marshal(transformsElem, dsPrefix, (DOMCryptoContext) context);
                allTransforms.add(t);
                xi.updateOutputStream(os, true);
            } else {
                xi.updateOutputStream(os);
            }
        }
        os.flush();
        if (cache != null && cache.booleanValue()) {
            this.dis = dos.getInputStream();
        }
        return dos.getDigestValue();
    } catch (NoSuchAlgorithmException e) {
        throw new XMLSignatureException(e);
    } catch (TransformException e) {
        throw new XMLSignatureException(e);
    } catch (MarshalException e) {
        throw new XMLSignatureException(e);
    } catch (IOException e) {
        throw new XMLSignatureException(e);
    } catch (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException e) {
        throw new XMLSignatureException(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                throw new XMLSignatureException(e);
            }
        }
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                throw new XMLSignatureException(e);
            }
        }
    }
}
Also used : DigesterOutputStream(org.jcp.xml.dsig.internal.DigesterOutputStream) UnsyncBufferedOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream) Element(org.w3c.dom.Element) UnsyncBufferedOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) DigesterOutputStream(org.jcp.xml.dsig.internal.DigesterOutputStream)

Example 7 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class DOMReference method copyDerefData.

private static Data copyDerefData(Data dereferencedData) {
    if (dereferencedData instanceof ApacheData) {
        // need to make a copy of the Data
        ApacheData ad = (ApacheData) dereferencedData;
        XMLSignatureInput xsi = ad.getXMLSignatureInput();
        if (xsi.isNodeSet()) {
            try {
                final Set<Node> s = xsi.getNodeSet();
                return new NodeSetData() {

                    public Iterator iterator() {
                        return s.iterator();
                    }
                };
            } catch (Exception e) {
                // log a warning
                log.log(java.util.logging.Level.WARNING, "cannot cache dereferenced data: " + e);
                return null;
            }
        } else if (xsi.isElement()) {
            return new DOMSubTreeData(xsi.getSubNode(), xsi.isExcludeComments());
        } else if (xsi.isOctetStream() || xsi.isByteArray()) {
            try {
                return new OctetStreamData(xsi.getOctetStream(), xsi.getSourceURI(), xsi.getMIMEType());
            } catch (IOException ioe) {
                // log a warning
                log.log(java.util.logging.Level.WARNING, "cannot cache dereferenced data: " + ioe);
                return null;
            }
        }
    }
    return dereferencedData;
}
Also used : Node(org.w3c.dom.Node) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) URISyntaxException(java.net.URISyntaxException) Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)

Example 8 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class TransformC14NWithComments method enginePerformTransform.

/** @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;
}
Also used : XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) Canonicalizer20010315WithComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315WithComments)

Example 9 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class TransformC14N method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws CanonicalizationException {
    Canonicalizer20010315OmitComments c14n = new Canonicalizer20010315OmitComments();
    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;
}
Also used : Canonicalizer20010315OmitComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315OmitComments) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)

Example 10 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class TransformC14N11 method enginePerformTransform.

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;
}
Also used : Canonicalizer11_OmitComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer11_OmitComments) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)

Aggregations

XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)24 Element (org.w3c.dom.Element)11 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)6 IOException (java.io.IOException)6 Node (org.w3c.dom.Node)6 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)5 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)4 ResourceResolver (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Attr (org.w3c.dom.Attr)4 SAXException (org.xml.sax.SAXException)4 Base64DecodingException (com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)3 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URISyntaxException (java.net.URISyntaxException)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)2 RetrievalMethod (com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod)2 Transform (com.sun.org.apache.xml.internal.security.transforms.Transform)2 InclusiveNamespaces (com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces)2