Search in sources :

Example 6 with ResourceResolverException

use of com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException in project jdk8u_jdk by JetBrains.

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

Example 7 with ResourceResolverException

use of com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException in project jdk8u_jdk by JetBrains.

the class XMLCipherInput method getDecryptBytes.

/**
     * Internal method to get bytes in decryption mode
     * @return the decrypted bytes
     * @throws XMLEncryptionException
     */
private byte[] getDecryptBytes() throws XMLEncryptionException {
    String base64EncodedEncryptedOctets = null;
    if (cipherData.getDataType() == CipherData.REFERENCE_TYPE) {
        // Fun time!
        if (logger.isLoggable(java.util.logging.Level.FINE)) {
            logger.log(java.util.logging.Level.FINE, "Found a reference type CipherData");
        }
        CipherReference cr = cipherData.getCipherReference();
        // Need to wrap the uri in an Attribute node so that we can
        // Pass to the resource resolvers
        Attr uriAttr = cr.getURIAsAttr();
        XMLSignatureInput input = null;
        try {
            ResourceResolver resolver = ResourceResolver.getInstance(uriAttr, null, secureValidation);
            input = resolver.resolve(uriAttr, null, secureValidation);
        } catch (ResourceResolverException ex) {
            throw new XMLEncryptionException("empty", ex);
        }
        if (input != null) {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Managed to resolve URI \"" + cr.getURI() + "\"");
            }
        } else {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Failed to resolve URI \"" + cr.getURI() + "\"");
            }
        }
        // Lets see if there are any transforms
        Transforms transforms = cr.getTransforms();
        if (transforms != null) {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Have transforms in cipher reference");
            }
            try {
                com.sun.org.apache.xml.internal.security.transforms.Transforms dsTransforms = transforms.getDSTransforms();
                dsTransforms.setSecureValidation(secureValidation);
                input = dsTransforms.performTransforms(input);
            } catch (TransformationException ex) {
                throw new XMLEncryptionException("empty", ex);
            }
        }
        try {
            return input.getBytes();
        } catch (IOException ex) {
            throw new XMLEncryptionException("empty", ex);
        } catch (CanonicalizationException ex) {
            throw new XMLEncryptionException("empty", ex);
        }
    // retrieve the cipher text
    } else if (cipherData.getDataType() == CipherData.VALUE_TYPE) {
        base64EncodedEncryptedOctets = cipherData.getCipherValue().getValue();
    } else {
        throw new XMLEncryptionException("CipherData.getDataType() returned unexpected value");
    }
    if (logger.isLoggable(java.util.logging.Level.FINE)) {
        logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets);
    }
    try {
        return Base64.decode(base64EncodedEncryptedOctets);
    } catch (Base64DecodingException bde) {
        throw new XMLEncryptionException("empty", bde);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) Attr(org.w3c.dom.Attr) Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException) ResourceResolver(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)

Example 8 with ResourceResolverException

use of com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException in project jdk8u_jdk by JetBrains.

the class ResolverDirectHTTP method engineResolveURI.

/**
     * Method resolve
     *
     * @param uri
     * @param baseURI
     *
     * @throws ResourceResolverException
     * @return
     * $todo$ calculate the correct URI from the attribute and the baseURI
     */
@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;
        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.encode(password.getBytes("ISO-8859-1"));
                // set authentication property in the http header
                urlConnection.setRequestProperty("Authorization", "Basic " + encodedPassword);
            }
        }
        String mimeType = urlConnection.getHeaderField("Content-Type");
        InputStream inputStream = urlConnection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int read = 0;
        int summarized = 0;
        while ((read = inputStream.read(buf)) >= 0) {
            baos.write(buf, 0, read);
            summarized += read;
        }
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "Fetched " + summarized + " bytes from URI " + uriNew.toString());
        }
        XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray());
        result.setSourceURI(uriNew.toString());
        result.setMIMEType(mimeType);
        return result;
    } catch (URISyntaxException ex) {
        throw new ResourceResolverException("generic.EmptyMessage", ex, context.attr, context.baseUri);
    } catch (MalformedURLException ex) {
        throw new ResourceResolverException("generic.EmptyMessage", ex, context.attr, context.baseUri);
    } catch (IOException ex) {
        throw new ResourceResolverException("generic.EmptyMessage", ex, context.attr, context.baseUri);
    } catch (IllegalArgumentException e) {
        throw new ResourceResolverException("generic.EmptyMessage", e, context.attr, context.baseUri);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)8 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)5 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)3 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)3 IOException (java.io.IOException)3 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)2 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)2 Transforms (com.sun.org.apache.xml.internal.security.transforms.Transforms)2 ResourceResolver (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Attr (org.w3c.dom.Attr)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 Base64DecodingException (com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)1 Transform (com.sun.org.apache.xml.internal.security.transforms.Transform)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1