Search in sources :

Example 1 with XMLSignatureInput

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

the class ResolverFragment method engineResolveURI.

/**
     * Method engineResolve
     *
     * @inheritDoc
     * @param uri
     * @param baseURI
     */
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
    Document doc = context.attr.getOwnerElement().getOwnerDocument();
    Node selectedElem = null;
    if (context.uriToResolve.equals("")) {
        /*
             * Identifies the node-set (minus any comment nodes) of the XML
             * resource containing the signature
             */
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "ResolverFragment with empty URI (means complete document)");
        }
        selectedElem = doc;
    } else {
        /*
             * URI="#chapter1"
             * Identifies a node-set containing the element with ID attribute
             * value 'chapter1' of the XML resource containing the signature.
             * XML Signature (and its applications) modify this node-set to
             * include the element plus all descendants including namespaces and
             * attributes -- but not comments.
             */
        String id = context.uriToResolve.substring(1);
        selectedElem = doc.getElementById(id);
        if (selectedElem == null) {
            Object[] exArgs = { id };
            throw new ResourceResolverException("signature.Verification.MissingID", exArgs, context.attr, context.baseUri);
        }
        if (context.secureValidation) {
            Element start = context.attr.getOwnerDocument().getDocumentElement();
            if (!XMLUtils.protectAgainstWrappingAttack(start, id)) {
                Object[] exArgs = { id };
                throw new ResourceResolverException("signature.Verification.MultipleIDs", exArgs, context.attr, context.baseUri);
            }
        }
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "Try to catch an Element with ID " + id + " and Element was " + selectedElem);
        }
    }
    XMLSignatureInput result = new XMLSignatureInput(selectedElem);
    result.setExcludeComments(true);
    result.setMIMEType("text/xml");
    if (context.baseUri != null && context.baseUri.length() > 0) {
        result.setSourceURI(context.baseUri.concat(context.uriToResolve));
    } else {
        result.setSourceURI(context.uriToResolve);
    }
    return result;
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) Document(org.w3c.dom.Document)

Example 2 with XMLSignatureInput

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

the class ResolverLocalFilesystem method engineResolveURI.

/**
     * @inheritDoc
     */
@Override
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
    try {
        // calculate new URI
        URI uriNew = getNewURI(context.uriToResolve, context.baseUri);
        String fileName = ResolverLocalFilesystem.translateUriToFilename(uriNew.toString());
        FileInputStream inputStream = new FileInputStream(fileName);
        XMLSignatureInput result = new XMLSignatureInput(inputStream);
        result.setSourceURI(uriNew.toString());
        return result;
    } catch (Exception e) {
        throw new ResourceResolverException("generic.EmptyMessage", e, context.attr, context.baseUri);
    }
}
Also used : XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)

Example 3 with XMLSignatureInput

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

the class ResolverXPointer method engineResolveURI.

/**
     * @inheritDoc
     */
@Override
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
    Node resultNode = null;
    Document doc = context.attr.getOwnerElement().getOwnerDocument();
    if (isXPointerSlash(context.uriToResolve)) {
        resultNode = doc;
    } else if (isXPointerId(context.uriToResolve)) {
        String id = getXPointerId(context.uriToResolve);
        resultNode = doc.getElementById(id);
        if (context.secureValidation) {
            Element start = context.attr.getOwnerDocument().getDocumentElement();
            if (!XMLUtils.protectAgainstWrappingAttack(start, id)) {
                Object[] exArgs = { id };
                throw new ResourceResolverException("signature.Verification.MultipleIDs", exArgs, context.attr, context.baseUri);
            }
        }
        if (resultNode == null) {
            Object[] exArgs = { id };
            throw new ResourceResolverException("signature.Verification.MissingID", exArgs, context.attr, context.baseUri);
        }
    }
    XMLSignatureInput result = new XMLSignatureInput(resultNode);
    result.setMIMEType("text/xml");
    if (context.baseUri != null && context.baseUri.length() > 0) {
        result.setSourceURI(context.baseUri.concat(context.uriToResolve));
    } else {
        result.setSourceURI(context.uriToResolve);
    }
    return result;
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) Document(org.w3c.dom.Document)

Example 4 with XMLSignatureInput

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

the class ApacheCanonicalizer method transform.

public Data transform(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (data == null) {
        throw new NullPointerException("data must not be null");
    }
    if (os == null) {
        throw new NullPointerException("output stream must not be null");
    }
    if (ownerDoc == null) {
        throw new TransformException("transform must be marshalled");
    }
    if (apacheTransform == null) {
        try {
            apacheTransform = new Transform(ownerDoc, getAlgorithm(), transformElem.getChildNodes());
            apacheTransform.setElement(transformElem, xc.getBaseURI());
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Created transform for algorithm: " + getAlgorithm());
            }
        } catch (Exception ex) {
            throw new TransformException("Couldn't find Transform for: " + getAlgorithm(), ex);
        }
    }
    XMLSignatureInput in;
    if (data instanceof ApacheData) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "ApacheData = true");
        }
        in = ((ApacheData) data).getXMLSignatureInput();
    } else if (data instanceof NodeSetData) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "isNodeSet() = true");
        }
        if (data instanceof DOMSubTreeData) {
            DOMSubTreeData subTree = (DOMSubTreeData) data;
            in = new XMLSignatureInput(subTree.getRoot());
            in.setExcludeComments(subTree.excludeComments());
        } else {
            @SuppressWarnings("unchecked") Set<Node> nodeSet = Utils.toNodeSet(((NodeSetData) data).iterator());
            in = new XMLSignatureInput(nodeSet);
        }
    } else {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "isNodeSet() = false");
        }
        try {
            in = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } catch (Exception ex) {
            throw new TransformException(ex);
        }
    }
    try {
        in = apacheTransform.performTransform(in, os);
        if (!in.isNodeSet() && !in.isElement()) {
            return null;
        }
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception ex) {
        throw new TransformException(ex);
    }
}
Also used : Set(java.util.Set) TransformException(javax.xml.crypto.dsig.TransformException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) Transform(com.sun.org.apache.xml.internal.security.transforms.Transform) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException)

Example 5 with XMLSignatureInput

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

the class ApacheTransform method transformIt.

private Data transformIt(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (ownerDoc == null) {
        throw new TransformException("transform must be marshalled");
    }
    if (apacheTransform == null) {
        try {
            apacheTransform = new Transform(ownerDoc, getAlgorithm(), transformElem.getChildNodes());
            apacheTransform.setElement(transformElem, xc.getBaseURI());
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Created transform for algorithm: " + getAlgorithm());
            }
        } catch (Exception ex) {
            throw new TransformException("Couldn't find Transform for: " + getAlgorithm(), ex);
        }
    }
    if (Utils.secureValidation(xc)) {
        String algorithm = getAlgorithm();
        if (Policy.restrictAlg(algorithm)) {
            throw new TransformException("Transform " + algorithm + " is forbidden when secure validation is enabled");
        }
    }
    XMLSignatureInput in;
    if (data instanceof ApacheData) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "ApacheData = true");
        }
        in = ((ApacheData) data).getXMLSignatureInput();
    } else if (data instanceof NodeSetData) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "isNodeSet() = true");
        }
        if (data instanceof DOMSubTreeData) {
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "DOMSubTreeData = true");
            }
            DOMSubTreeData subTree = (DOMSubTreeData) data;
            in = new XMLSignatureInput(subTree.getRoot());
            in.setExcludeComments(subTree.excludeComments());
        } else {
            @SuppressWarnings("unchecked") Set<Node> nodeSet = Utils.toNodeSet(((NodeSetData) data).iterator());
            in = new XMLSignatureInput(nodeSet);
        }
    } else {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "isNodeSet() = false");
        }
        try {
            in = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } catch (Exception ex) {
            throw new TransformException(ex);
        }
    }
    try {
        if (os != null) {
            in = apacheTransform.performTransform(in, os);
            if (!in.isNodeSet() && !in.isElement()) {
                return null;
            }
        } else {
            in = apacheTransform.performTransform(in);
        }
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception ex) {
        throw new TransformException(ex);
    }
}
Also used : Set(java.util.Set) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) Transform(com.sun.org.apache.xml.internal.security.transforms.Transform) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

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