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: Cannot resolve element with ID 

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


<test Id="my_271692"> 

<Reference URI="#my_271692"> 

In your code if you do:
XMLSignatureFactory factory = XMLSignatureFactory
            javax.xml.crypto.dsig.XMLSignature signature = factory
  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: