Monday 27 July 2015

Xml Signature Validation Failed

If you are getting below Exception while validating XML Signature

javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID 







Now Digital Signature Policy has been change. 
Suppose you have below xml :

Node:

<test Id="my_271692"> 

Signature: 
<Reference URI="#my_271692"> 

In your code if you do:
XMLSignatureFactory factory = XMLSignatureFactory
                    .getInstance("DOM");
            javax.xml.crypto.dsig.XMLSignature signature = factory
                    .unmarshalXMLSignature(valContext);
  valid = signature.validate(valContext); 


Last line will throw exception: 
Now have to add below line to make it work:
DOMValidateContext valContext="...........

NodeList nl = doc.getElementsByTagNameNS(
               namespace, "test"); 

Element el = (Element) (n2.item(0));
            valContext.setIdAttributeNS(el, namespace, "Id"); 








Here I'm passing null because xmlns is not there for test tag. otherwise pass the namespace.
It will work fine.


 

1 comment: