Search code examples
xpathdigital-signaturexades4jxml-dsig

Error using XAdES4j to produce a detached signature with a xpath transform


I'm using the xades4j for produce xades signatures. I want to include a xpath transform in a reference. My problem is the fact that it is a detached signature and the xpath has namespaces...

I tried with the following xml (an excerpt):

<collection xmlns:t="http://test.xades4j/tracks" Id="root">
    <album>
        <title>Questions, unanswered</title>
        <artist>Steve and the flubberblubs</artist>
        <year>1989</year>
        <t:tracks xmlns:t="http://test.xades4j/tracks">
            <t:song length="4:05" tracknumber="1">
                <t:title>What do you know?</t:title>
                <t:artist>Steve and the flubberblubs</t:artist>
                <t:lastplayed>2006-10-17-08:31</t:lastplayed>
            </t:song>
            <t:song length="3:45" tracknumber="2">
                <t:title>Who do you know?</t:title>
                <t:artist>Steve and the flubberblubs</t:artist>
                <t:lastplayed>2006-10-17-08:35</t:lastplayed>
            </t:song>

If I try to do an enveloped signature of this document, with the code:

String xpathString = "/collection/album/t:tracks/t:song[ @tracknumber = 1 ]";

DataObjectDesc obj1 = new DataObjectReference("");
obj1.withTransform(new EnvelopedSignatureTransform());
obj1.withTransform(XPath2Filter.intersect( xpathString ));

SignedDataObjects objs = new SignedDataObjects( obj1 );         
Document doc = getDocument(path+fileName);

signer.sign( objs, doc.getDocumentElement() );

It works fine and reference looks like this:

<ds:Reference Id="xmldsig-44c42d30-9a42-4290-afba-b89dc807a668-ref0" URI="">
                <ds:Transforms>
                    <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                    <ds:Transform Algorithm="http://www.w3.org/2002/06/xmldsig-filter2">
                        <dsig-xpath:XPath xmlns:dsig-xpath="http://www.w3.org/2002/06/xmldsig-filter2" Filter="intersect">/collection/album/t:tracks/t:song[ @tracknumber = 1 ]</dsig-xpath:XPath>
                    </ds:Transform>
                </ds:Transforms>

BUT, if I try the detached version, with the code:

String xpathString = "/collection/album/t:tracks/t:song[ @tracknumber = 1 ]";

DataObjectDesc obj1 = new DataObjectReference( fileName );
obj1.withTransform(XPath2Filter.intersect( xpathString ));

SignedDataObjects objs = new SignedDataObjects( obj1 );
objs.withBaseUri( "file:///"+path );

signer.sign( objs, db.newDocument());

I get the error: Prefix must resolve to a namespace: t

My problem is similar to the one described here: namespace and xpath But there are no answers and what I have is a little different. I guest that an answer to that problem may help me...

So, how can I set the namespace to a transform in xades4j?


Solution

  • Nice catch! Currently there's no way to specify this. However, I think the underlying Apache Santuario API supports this using the setXPathNamespaceContext method. Looks like the output XPath element will contain the namespace declarations. I'll test this and add support if it's possible.