Skip to main content

URIReferenceException with "file:/..." URI type

1 reply [Last post]
segnilape
Offline
Joined: 2007-06-05

Dear all

I've got a
"Exception in thread "main" javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: java.lang.NullPointerException"
which occurs at the line below.
-------
signature.sign(dsc);
-------

The cause of the problem is the use of a URI-type reference: if I use a "file:/..." one, Exception occurs, if I use a "http://..." one, then there is no Exception
--------
Reference ref_1 = xsfac.newReference("file:/C:/temp/myPix.jpg",xsfac.newDigestMethod(DigestMethod.SHA1, null));
Reference ref_2 = xsfac.newReference("file:///C:/temp/myPix.jpg",xsfac.newDigestMethod(DigestMethod.SHA1, null));
Reference ref_3 = xsfac.newReference("http://www.mycompany.com/data/myPix.jpg", xsfac.newDigestMEthod(DigestMethod.SHA1,null));
--------

Below is the full code

import javax.xml.crypto.dsig.XMLSignatureFactory;
public class GenSig{
XMLSignatureFactory xsfac = XMLSignatureFactory.getInstance("DOM");
Reference ref_1 = xsfac.newReference("file:/C:/temp/myPix.jpg",xsfac.newDigestMethod(DigestMethod.SHA1, null));
Reference ref_2 = xsfac.newReference("file:///C:/temp/myPix.jpg",xsfac.newDigestMethod(DigestMethod.SHA1, null));
Reference ref_3 = xsfac.newReference("http://www.mycompany.com/data/myPix.jpg", xsfac.newDigestMEthod(DigestMethod.SHA1,null));

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();

SignedInfo si = fac.newSignedInfo(
fac.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
(C14NMethodParameterSpec)null),
fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null),
Collections.singletonList(ref_1));

KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(512);

KeyPair kp = kpg.generateKeyPair();
KeyInfoFactory kif = fac.getKeyInfoFactory();
KeyValue kv = kif.newKeyValue(kp.getPublic());

KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));

XMLSignature signature = fac.newXMLSignature(si, ki);

DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc);

signature.sign(dsc);

OutputStream os;
if (args.length > 0){
os = new FileOutputStream(args[1]);
}else{
os = System.out;
}

TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));
}

The full error message is as below:

-------------------
[java] Exception in thread "main" javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: java.lang.NullPointerException
[java] at org.jcp.xml.dsig.internal.dom.DOMReference.dereference(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMReference.digest(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.digestReference(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.sign(Unknown Source)
[java] at dsig.detached.GenSig.main(GenSig.java:90)
[java] Caused by: javax.xml.crypto.URIReferenceException: java.lang.NullPointerException
[java] at org.jcp.xml.dsig.internal.dom.DOMURIDereferencer.dereference(Unknown Source)
[java] ... 5 more
[java] Caused by: java.lang.NullPointerException
[java] at com.sun.org.apache.xml.internal.security.utils.resolver.implementations.ResolverDirectHTTP.engineCanResolve(Unknown Source)
[java] at com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver.canResolve(Unknown Source)
[java] at com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver.getInstance(Unknown Source)
[java] ... 6 more
[java] javax.xml.crypto.URIReferenceException: java.lang.NullPointerException
[java] at org.jcp.xml.dsig.internal.dom.DOMURIDereferencer.dereference(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMReference.dereference(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMReference.digest(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.digestReference(Unknown Source)
[java] at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.sign(Unknown Source)
[java] at dsig.detached.GenSig.main(GenSig.java:90)
[java] Caused by: java.lang.NullPointerException
[java] at com.sun.org.apache.xml.internal.security.utils.resolver.implementations.ResolverDirectHTTP.engineCanResolve(Unknown Source)
[java] at com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver.canResolve(Unknown Source)
[java] at com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver.getInstance(Unknown Source)
[java] ... 6 more
[java] Java Result: 1
-------------------

Can anyone point me to an answer ?
Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mullan
Offline
Joined: 2003-06-16