Search in sources :

Example 1 with ResourceResolver

use of org.apache.xml.security.utils.resolver.ResourceResolver in project xades4j by luisgoncalves.

the class OtherSignerTests method testSignUsingCustomResolver.

@Test
public void testSignUsingCustomResolver() throws Exception {
    System.out.println("signUsingCustomResolver");
    Document doc = getNewDocument();
    XadesSigner signer = new XadesBesSigningProfile(keyingProviderMy).newSigner();
    MyResolverSpi resolverSpi = new MyResolverSpi();
    SignedDataObjects dataObjs = new SignedDataObjects().withSignedDataObject(new DataObjectReference("xades4j://ref")).withResourceResolver(new ResourceResolver(resolverSpi));
    signer.sign(dataObjs, doc);
    assertEquals(1, resolverSpi.resolveCount);
}
Also used : ResourceResolver(org.apache.xml.security.utils.resolver.ResourceResolver) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 2 with ResourceResolver

use of org.apache.xml.security.utils.resolver.ResourceResolver in project xades4j by luisgoncalves.

the class XadesVerifierImpl method doCoreVerification.

private static void doCoreVerification(XMLSignature signature, SignatureSpecificVerificationOptions verificationOptions, X509Certificate validationCert) throws XAdES4jXMLSigException, InvalidSignatureException {
    List<ResourceResolver> resolvers = verificationOptions.getResolvers();
    if (!CollectionUtils.nullOrEmpty(resolvers)) {
        for (ResourceResolver resolver : resolvers) {
            signature.addResourceResolver(resolver);
        }
    }
    InputStream nullURIReferenceData = verificationOptions.getDataForAnonymousReference();
    if (nullURIReferenceData != null) {
        signature.addResourceResolver(new ResolverAnonymous(nullURIReferenceData));
    }
    try {
        if (signature.checkSignatureValue(validationCert)) {
            return;
        }
    } catch (XMLSignatureException ex) {
        throw new XAdES4jXMLSigException("Error verifying the signature", ex);
    }
    try {
        if (signature.getSignedInfo().verifyReferences()) // References are OK; this is a problem on the signature value
        // itself.
        {
            throw new SignatureValueException(signature);
        } else {
            // References are NOT OK; get the first invalid Reference.
            SignedInfo si = signature.getSignedInfo();
            for (int i = 0; i < si.getLength(); i++) {
                Reference r = si.item(i);
                if (!r.verify()) {
                    throw new ReferenceValueException(signature, r);
                }
            }
        }
    } catch (XMLSecurityException ex) {
        throw new XAdES4jXMLSigException("Error verifying the references", ex);
    }
}
Also used : XAdES4jXMLSigException(xades4j.XAdES4jXMLSigException) InputStream(java.io.InputStream) Reference(org.apache.xml.security.signature.Reference) ResourceResolver(org.apache.xml.security.utils.resolver.ResourceResolver) ResolverAnonymous(org.apache.xml.security.utils.resolver.implementations.ResolverAnonymous) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) SignedInfo(org.apache.xml.security.signature.SignedInfo)

Example 3 with ResourceResolver

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

the class RetrievalMethodResolver method resolveInput.

/**
 * Resolves the input from the given retrieval method
 * @return the input from the given retrieval method
 * @throws XMLSecurityException
 */
private static XMLSignatureInput resolveInput(RetrievalMethod rm, String baseURI, boolean secureValidation) throws XMLSecurityException {
    Attr uri = rm.getURIAttr();
    // Apply the transforms
    Transforms transforms = rm.getTransforms();
    ResourceResolver resRes = ResourceResolver.getInstance(uri, baseURI, secureValidation);
    XMLSignatureInput resource = resRes.resolve(uri, baseURI, secureValidation);
    if (transforms != null) {
        LOG.debug("We have Transforms");
        resource = transforms.performTransforms(resource);
    }
    return resource;
}
Also used : Transforms(org.apache.xml.security.transforms.Transforms) ResourceResolver(org.apache.xml.security.utils.resolver.ResourceResolver) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) Attr(org.w3c.dom.Attr)

Example 4 with ResourceResolver

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

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!
        LOG.debug("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(ex);
        }
        if (input != null) {
            LOG.debug("Managed to resolve URI \"{}\"", cr.getURI());
        } else {
            LOG.debug("Failed to resolve URI \"{}\"", cr.getURI());
        }
        // Lets see if there are any transforms
        Transforms transforms = cr.getTransforms();
        if (transforms != null) {
            LOG.debug("Have transforms in cipher reference");
            try {
                org.apache.xml.security.transforms.Transforms dsTransforms = transforms.getDSTransforms();
                dsTransforms.setSecureValidation(secureValidation);
                input = dsTransforms.performTransforms(input);
            } catch (TransformationException ex) {
                throw new XMLEncryptionException(ex);
            }
        }
        try {
            return input.getBytes();
        } catch (IOException ex) {
            throw new XMLEncryptionException(ex);
        } catch (CanonicalizationException ex) {
            throw new XMLEncryptionException(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");
    }
    LOG.debug("Encrypted octets:\n{}", base64EncodedEncryptedOctets);
    return Base64.getMimeDecoder().decode(base64EncodedEncryptedOctets);
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) Attr(org.w3c.dom.Attr) ResourceResolver(org.apache.xml.security.utils.resolver.ResourceResolver)

Example 5 with ResourceResolver

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

the class ResolverDirectHTTPTest method testBug40783.

@Test
public void testBug40783() throws Exception {
    Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
    Attr uri = doc.createAttribute("id");
    uri.setNodeValue("urn:ddd:uuu");
    doc.createElement("test").setAttributeNode(uri);
    try {
        ResourceResolver resolver = ResourceResolver.getInstance(uri, null, true);
        fail("No exception thrown, but resolver found: " + resolver);
    } catch (ResourceResolverException e) {
    // 
    }
}
Also used : ResourceResolver(org.apache.xml.security.utils.resolver.ResourceResolver) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.xml.security.utils.resolver.ResourceResolver)10 Attr (org.w3c.dom.Attr)7 Document (org.w3c.dom.Document)4 XMLSignatureInput (org.apache.xml.security.signature.XMLSignatureInput)3 ResourceResolverException (org.apache.xml.security.utils.resolver.ResourceResolverException)3 Reference (org.apache.xml.security.signature.Reference)2 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)2 Transforms (org.apache.xml.security.transforms.Transforms)2 ResolverAnonymous (org.apache.xml.security.utils.resolver.implementations.ResolverAnonymous)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 IdentityHashMap (java.util.IdentityHashMap)1 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)1 SignedInfo (org.apache.xml.security.signature.SignedInfo)1 TransformationException (org.apache.xml.security.transforms.TransformationException)1 Element (org.w3c.dom.Element)1