Search in sources :

Example 11 with ResourceResolverException

use of org.apache.xml.security.utils.resolver.ResourceResolverException in project santuario-java by apache.

the class Reference method getContentsBeforeTransformation.

/**
 * Returns the XMLSignatureInput which is created by de-referencing the URI attribute.
 * @return the XMLSignatureInput of the source of this reference
 * @throws ReferenceNotInitializedException If the resolver found any
 * problem resolving the reference
 */
public XMLSignatureInput getContentsBeforeTransformation() throws ReferenceNotInitializedException {
    try {
        Attr uriAttr = getElement().getAttributeNodeNS(null, Constants._ATT_URI);
        ResourceResolver resolver = ResourceResolver.getInstance(uriAttr, this.baseURI, this.manifest.getPerManifestResolvers(), secureValidation);
        resolver.addProperties(this.manifest.getResolverProperties());
        return resolver.resolve(uriAttr, this.baseURI, secureValidation);
    } catch (ResourceResolverException ex) {
        throw new ReferenceNotInitializedException(ex);
    }
}
Also used : ResourceResolver(org.apache.xml.security.utils.resolver.ResourceResolver) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) Attr(org.w3c.dom.Attr)

Example 12 with ResourceResolverException

use of org.apache.xml.security.utils.resolver.ResourceResolverException in project santuario-java by apache.

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) {
            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) || uri.equals(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N11_WITH_COMMENTS)) {
                    break;
                }
                output = t.performTransform(output, null);
            }
            output.setSourceURI(input.getSourceURI());
        }
        return output;
    } catch (IOException ex) {
        throw new XMLSignatureException(ex);
    } catch (ResourceResolverException ex) {
        throw new XMLSignatureException(ex);
    } catch (CanonicalizationException ex) {
        throw new XMLSignatureException(ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new XMLSignatureException(ex);
    } catch (TransformationException ex) {
        throw new XMLSignatureException(ex);
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException(ex);
    }
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) Transforms(org.apache.xml.security.transforms.Transforms) IOException(java.io.IOException) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) Transform(org.apache.xml.security.transforms.Transform) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 13 with ResourceResolverException

use of org.apache.xml.security.utils.resolver.ResourceResolverException in project santuario-java by apache.

the class ResolverDirectHTTP method engineResolveURI.

/**
 * {@inheritDoc}
 */
@Override
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
    try {
        // calculate new URI
        URI uriNew = getNewURI(context.uriToResolve, context.baseUri);
        URL url = uriNew.toURL();
        URLConnection urlConnection = openConnection(url);
        // check if Basic authentication is required
        String auth = urlConnection.getHeaderField("WWW-Authenticate");
        if (auth != null && auth.startsWith("Basic")) {
            // do http basic authentication
            String user = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpBasicUser]);
            String pass = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpBasicPass]);
            if (user != null && pass != null) {
                urlConnection = openConnection(url);
                String password = user + ":" + pass;
                String encodedPassword = Base64.getMimeEncoder().encodeToString(password.getBytes(StandardCharsets.ISO_8859_1));
                // set authentication property in the http header
                urlConnection.setRequestProperty("Authorization", "Basic " + encodedPassword);
            }
        }
        String mimeType = urlConnection.getHeaderField("Content-Type");
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream inputStream = urlConnection.getInputStream()) {
            byte[] buf = new byte[4096];
            int read = 0;
            int summarized = 0;
            while ((read = inputStream.read(buf)) >= 0) {
                baos.write(buf, 0, read);
                summarized += read;
            }
            LOG.debug("Fetched {} bytes from URI {}", summarized, uriNew.toString());
            XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray());
            result.setSecureValidation(context.secureValidation);
            result.setSourceURI(uriNew.toString());
            result.setMIMEType(mimeType);
            return result;
        }
    } catch (URISyntaxException ex) {
        throw new ResourceResolverException(ex, context.uriToResolve, context.baseUri, "generic.EmptyMessage");
    } catch (MalformedURLException ex) {
        throw new ResourceResolverException(ex, context.uriToResolve, context.baseUri, "generic.EmptyMessage");
    } catch (IOException ex) {
        throw new ResourceResolverException(ex, context.uriToResolve, context.baseUri, "generic.EmptyMessage");
    } catch (IllegalArgumentException e) {
        throw new ResourceResolverException(e, context.uriToResolve, context.baseUri, "generic.EmptyMessage");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 14 with ResourceResolverException

use of org.apache.xml.security.utils.resolver.ResourceResolverException in project santuario-java by apache.

the class ResolverFragment method engineResolveURI.

/**
 * {@inheritDoc}
 */
@Override
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
             */
        LOG.debug("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.uriToResolve, 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.uriToResolve, context.baseUri);
            }
        }
        LOG.debug("Try to catch an Element with ID {} and Element was {}", id, selectedElem);
    }
    XMLSignatureInput result = new XMLSignatureInput(selectedElem);
    result.setSecureValidation(context.secureValidation);
    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(org.apache.xml.security.signature.XMLSignatureInput) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) Document(org.w3c.dom.Document)

Aggregations

ResourceResolverException (org.apache.xml.security.utils.resolver.ResourceResolverException)14 XMLSignatureInput (org.apache.xml.security.signature.XMLSignatureInput)8 Attr (org.w3c.dom.Attr)5 Document (org.w3c.dom.Document)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)3 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)3 TransformationException (org.apache.xml.security.transforms.TransformationException)3 ResourceResolver (org.apache.xml.security.utils.resolver.ResourceResolver)3 Test (org.junit.Test)3 Node (org.w3c.dom.Node)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)2 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)2 Transforms (org.apache.xml.security.transforms.Transforms)2 ResourceResolverContext (org.apache.xml.security.utils.resolver.ResourceResolverContext)2 ResolverDirectHTTP (org.apache.xml.security.utils.resolver.implementations.ResolverDirectHTTP)2 Ignore (org.junit.Ignore)2 Element (org.w3c.dom.Element)2