M
Marcin 'golish' Goliszewski
Hello.
I'm writing an application which creates and verifies XML DSig
signatures. I'm using the Java API described in JSR105 and implemented
in JDK6. I have encountered a weird problem, which I wasn't able to
solve myself. I have the following fragment of source code in my
application:
#v+
public static boolean verifySignature(Element signatureElement) {
DOMValidateContext validateContext = new DOMValidateContext(
new KeyValueKeySelector(), signatureElement);
XMLSignatureFactory signatureFactory = XMLSignatureFactory
.getInstance("DOM");
XMLSignature signature = null;
try {
signature = signatureFactory.unmarshalXMLSignature(validateContext);
} catch (MarshalException e) {
// TODO: add content
}
boolean coreValidity = true;
try {
coreValidity = signature.validate(validateContext);
} catch (XMLSignatureException e) {
// TODO: add content
}
return coreValidity;
}
#v-
Unfortunately, this code reaches the line with the
unmarshalXMLSignature() invocation - a NullPointerException is thrown
inside this method. Here's the stack trace:
#v+
Exception in thread "main" java.lang.NullPointerException
at org.jcp.xml.dsig.internal.dom.DOMReference.<init>(DOMReference.java:156)
at org.jcp.xml.dsig.internal.dom.DOMSignedInfo.<init>(DOMSignedInfo.java:128)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.<init>(DOMXMLSignature.java:118)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:152)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:116)
at net.xmxp.lib.SignatureVerifier.verifySignature(SignatureVerifier.java:100)
at net.xmxp.receiver.TestReceiver.gotEnvelope(TestReceiver.java:61)
at net.xmxp.receiver.ReceiverConnectionHandler.endElement(ReceiverConnectionHandler.java:513)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1772)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2923)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at net.xmxp.receiver.ReceiverConnectionHandler.serve(ReceiverConnectionHandler.java:978)
at net.xmxp.receiver.TestReceiver.main(TestReceiver.java:55)
#v-
According to the javadocs, NullPointerException may be thrown if
validateContext would be null. But it's not. So I did some debugging.
The interesting fragment from the DOMReference class is:
#v+
151 public DOMReference(Element refElem, XMLCryptoContext context)
152 throws MarshalException {
153 // unmarshal Transforms, if specified
154 Element nextSibling = DOMUtils.getFirstChildElement(refElem);
156 List transforms = new ArrayList(5);
157 if (nextSibling.getLocalName().equals("Transforms")) {
158 Element transformElem = DOMUtils.getFirstChildElement(nextSibling);
159 while (transformElem != null) {
160 transforms.add(new DOMTransform(transformElem, context));
161 transformElem = DOMUtils.getNextSiblingElement(transformElem);
162 }
163 nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
164 }
#v-
I've spotted that the NullPointerException is thrown because the
expression nextSibling.getLocalName() in line 156 is null. The weird
thing (at least for me) is the cause for which it's null: according to
the Eclipse debugger the method called is NodeImpl.getLocalName() (which
returns null) instead of ElementImpl.getLocalName(), which AFAIK should
be called. Did anyone encounter this (or a similar) problem? Or maybe
someone has an idea how to solve/avoid this problem?
This problem is repeatable in various environments:
* 64-bit Linux + 64-bit JDK,
* 64-bit Linux + 32-bit JDK,
* 32-bit Linux,
* 32-bit Windows.
All tests were performed using JDK6 because AFAIK previous versions
didn't include a JSR105 implementation.
Thanks in advance for all the help and tips.
Regards,
I'm writing an application which creates and verifies XML DSig
signatures. I'm using the Java API described in JSR105 and implemented
in JDK6. I have encountered a weird problem, which I wasn't able to
solve myself. I have the following fragment of source code in my
application:
#v+
public static boolean verifySignature(Element signatureElement) {
DOMValidateContext validateContext = new DOMValidateContext(
new KeyValueKeySelector(), signatureElement);
XMLSignatureFactory signatureFactory = XMLSignatureFactory
.getInstance("DOM");
XMLSignature signature = null;
try {
signature = signatureFactory.unmarshalXMLSignature(validateContext);
} catch (MarshalException e) {
// TODO: add content
}
boolean coreValidity = true;
try {
coreValidity = signature.validate(validateContext);
} catch (XMLSignatureException e) {
// TODO: add content
}
return coreValidity;
}
#v-
Unfortunately, this code reaches the line with the
unmarshalXMLSignature() invocation - a NullPointerException is thrown
inside this method. Here's the stack trace:
#v+
Exception in thread "main" java.lang.NullPointerException
at org.jcp.xml.dsig.internal.dom.DOMReference.<init>(DOMReference.java:156)
at org.jcp.xml.dsig.internal.dom.DOMSignedInfo.<init>(DOMSignedInfo.java:128)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.<init>(DOMXMLSignature.java:118)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:152)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:116)
at net.xmxp.lib.SignatureVerifier.verifySignature(SignatureVerifier.java:100)
at net.xmxp.receiver.TestReceiver.gotEnvelope(TestReceiver.java:61)
at net.xmxp.receiver.ReceiverConnectionHandler.endElement(ReceiverConnectionHandler.java:513)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1772)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2923)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at net.xmxp.receiver.ReceiverConnectionHandler.serve(ReceiverConnectionHandler.java:978)
at net.xmxp.receiver.TestReceiver.main(TestReceiver.java:55)
#v-
According to the javadocs, NullPointerException may be thrown if
validateContext would be null. But it's not. So I did some debugging.
The interesting fragment from the DOMReference class is:
#v+
151 public DOMReference(Element refElem, XMLCryptoContext context)
152 throws MarshalException {
153 // unmarshal Transforms, if specified
154 Element nextSibling = DOMUtils.getFirstChildElement(refElem);
156 List transforms = new ArrayList(5);
157 if (nextSibling.getLocalName().equals("Transforms")) {
158 Element transformElem = DOMUtils.getFirstChildElement(nextSibling);
159 while (transformElem != null) {
160 transforms.add(new DOMTransform(transformElem, context));
161 transformElem = DOMUtils.getNextSiblingElement(transformElem);
162 }
163 nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
164 }
#v-
I've spotted that the NullPointerException is thrown because the
expression nextSibling.getLocalName() in line 156 is null. The weird
thing (at least for me) is the cause for which it's null: according to
the Eclipse debugger the method called is NodeImpl.getLocalName() (which
returns null) instead of ElementImpl.getLocalName(), which AFAIK should
be called. Did anyone encounter this (or a similar) problem? Or maybe
someone has an idea how to solve/avoid this problem?
This problem is repeatable in various environments:
* 64-bit Linux + 64-bit JDK,
* 64-bit Linux + 32-bit JDK,
* 32-bit Linux,
* 32-bit Windows.
All tests were performed using JDK6 because AFAIK previous versions
didn't include a JSR105 implementation.
Thanks in advance for all the help and tips.
Regards,