Search in sources :

Example 1 with ReferenceNodeSetData

use of org.apache.xml.security.signature.reference.ReferenceNodeSetData in project santuario-java by apache.

the class InteropTestBase method checkReferences.

private void checkReferences(XMLSignature xmlSignature) throws Exception {
    SignedInfo signedInfo = xmlSignature.getSignedInfo();
    assertTrue(signedInfo.getLength() > 0);
    for (int i = 0; i < signedInfo.getLength(); i++) {
        Reference reference = signedInfo.item(i);
        assertNotNull(reference);
        ReferenceData referenceData = reference.getReferenceData();
        assertNotNull(referenceData);
        if (referenceData instanceof ReferenceNodeSetData) {
            Iterator<Node> iter = ((ReferenceNodeSetData) referenceData).iterator();
            assertTrue(iter.hasNext());
            boolean found = false;
            while (iter.hasNext()) {
                Node n = iter.next();
                if (n instanceof Element) {
                    found = true;
                    break;
                }
            }
            assertTrue(found);
        } else if (referenceData instanceof ReferenceOctetStreamData) {
            assertNotNull(((ReferenceOctetStreamData) referenceData).getOctetStream());
        }
    }
}
Also used : ReferenceData(org.apache.xml.security.signature.reference.ReferenceData) ReferenceOctetStreamData(org.apache.xml.security.signature.reference.ReferenceOctetStreamData) Reference(org.apache.xml.security.signature.Reference) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ReferenceNodeSetData(org.apache.xml.security.signature.reference.ReferenceNodeSetData) SignedInfo(org.apache.xml.security.signature.SignedInfo)

Example 2 with ReferenceNodeSetData

use of org.apache.xml.security.signature.reference.ReferenceNodeSetData in project santuario-java by apache.

the class SignatureReferenceTest method testSigningVerifyingReference.

@org.junit.Test
public void testSigningVerifyingReference() throws Throwable {
    Document doc = getOriginalDocument();
    XMLSignature signature = signDocument(doc);
    PublicKey pubKey = getPublicKey();
    assertTrue(signature.checkSignatureValue(pubKey));
    // Check the reference(s)
    SignedInfo signedInfo = signature.getSignedInfo();
    assertTrue(signedInfo.getLength() == 1);
    Reference reference = signedInfo.item(0);
    ReferenceData referenceData = reference.getReferenceData();
    assertNotNull(referenceData);
    assertTrue(referenceData instanceof ReferenceNodeSetData);
    // Test the cached Element
    Element referenceElement = (Element) ((ReferenceNodeSetData) referenceData).iterator().next();
    assertNotNull(referenceElement);
    assertTrue("root".equals(referenceElement.getLocalName()));
    Element originalElement = (Element) doc.getElementsByTagNameNS("http://ns.example.org/", "root").item(0);
    assertNotNull(originalElement);
    assertEquals(referenceElement, originalElement);
}
Also used : ReferenceData(org.apache.xml.security.signature.reference.ReferenceData) XMLSignature(org.apache.xml.security.signature.XMLSignature) PublicKey(java.security.PublicKey) Reference(org.apache.xml.security.signature.Reference) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ReferenceNodeSetData(org.apache.xml.security.signature.reference.ReferenceNodeSetData) SignedInfo(org.apache.xml.security.signature.SignedInfo)

Example 3 with ReferenceNodeSetData

use of org.apache.xml.security.signature.reference.ReferenceNodeSetData in project santuario-java by apache.

the class Reference method cacheDereferencedElement.

/**
 * Store the dereferenced Element(s) so that it/they can be retrieved later.
 */
private void cacheDereferencedElement(XMLSignatureInput input) {
    if (input.isNodeSet()) {
        try {
            final Set<Node> s = input.getNodeSet();
            referenceData = new ReferenceNodeSetData() {

                public Iterator<Node> iterator() {
                    return new Iterator<Node>() {

                        Iterator<Node> sIterator = s.iterator();

                        @Override
                        public boolean hasNext() {
                            return sIterator.hasNext();
                        }

                        @Override
                        public Node next() {
                            return sIterator.next();
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            };
        } catch (Exception e) {
            // LOG a warning
            LOG.warn("cannot cache dereferenced data: " + e);
        }
    } else if (input.isElement()) {
        referenceData = new ReferenceSubTreeData(input.getSubNode(), input.isExcludeComments());
    } else if (input.isOctetStream() || input.isByteArray()) {
        try {
            referenceData = new ReferenceOctetStreamData(input.getOctetStream(), input.getSourceURI(), input.getMIMEType());
        } catch (IOException ioe) {
            // LOG a warning
            LOG.warn("cannot cache dereferenced data: " + ioe);
        }
    }
}
Also used : ReferenceOctetStreamData(org.apache.xml.security.signature.reference.ReferenceOctetStreamData) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) IOException(java.io.IOException) ReferenceNodeSetData(org.apache.xml.security.signature.reference.ReferenceNodeSetData) InvalidTransformException(org.apache.xml.security.transforms.InvalidTransformException) TransformationException(org.apache.xml.security.transforms.TransformationException) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) ReferenceSubTreeData(org.apache.xml.security.signature.reference.ReferenceSubTreeData)

Aggregations

ReferenceNodeSetData (org.apache.xml.security.signature.reference.ReferenceNodeSetData)3 Reference (org.apache.xml.security.signature.Reference)2 SignedInfo (org.apache.xml.security.signature.SignedInfo)2 ReferenceData (org.apache.xml.security.signature.reference.ReferenceData)2 ReferenceOctetStreamData (org.apache.xml.security.signature.reference.ReferenceOctetStreamData)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 IOException (java.io.IOException)1 PublicKey (java.security.PublicKey)1 Iterator (java.util.Iterator)1 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)1 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 XMLSignature (org.apache.xml.security.signature.XMLSignature)1 ReferenceSubTreeData (org.apache.xml.security.signature.reference.ReferenceSubTreeData)1 InvalidTransformException (org.apache.xml.security.transforms.InvalidTransformException)1 TransformationException (org.apache.xml.security.transforms.TransformationException)1 ResourceResolverException (org.apache.xml.security.utils.resolver.ResourceResolverException)1 Document (org.w3c.dom.Document)1