Jure1873
Jure1873

Reputation: 899

xades4j: Sign only one element

I'm using this code to sign a xml document:

Document doc = getDocument(xml_to_sign);
Element elemToSign = doc.getDocumentElement();

String file_uri_path = elemToSign.getBaseURI();

DataObjectDesc obj1 = new DataObjectReference(file_uri_path).withType("http://www.gzs.si/shemas/eslog/racun/1.5#Racun");
SignedDataObjects dataObjs = new SignedDataObjects(obj1);

signer.sign(dataObjs, elemToSign);

xml_to_sign is the full path to the xml file.

The problem is, that I would like to sign only the node with the id "data" (#data), but append the signature to the node elemToSign.

Is it possible to do this with xades4j?

Upvotes: 1

Views: 1399

Answers (2)

tsaixingwei
tsaixingwei

Reputation: 656

You should specify that the attribute named "Id" in your xml document is the XML ID attribute that Apache Santuario (used internally by Xades4j) will use in the getElementById() (as lgoncalves has pointed out in his commments to his own answer).

Element parent = doc.getDocumentElement();
parent.setIdAttribute("Id", true);
//or parent.setIdAttributeNS("http://your.name.space", "Id", true);

I had the same problem, and this additional line of code solved it.

Upvotes: 0

lgoncalves
lgoncalves

Reputation: 2090

Yes, it is. The sign method's argument is the parent node, not the element to sign (it could be the same node, depending on the configured references). In your example you should add a reference for "#data":

Document doc = getDocument(xml_to_sign);
Element parent = doc.getDocumentElement();

DataObjectDesc obj1 = new DataObjectReference("#data").withType("http://www.gzs.si/shemas/eslog/racun/1.5#Racun");
SignedDataObjects dataObjs = new SignedDataObjects(obj1);

signer.sign(dataObjs, parent);

Another option is to add a reference for the whole XML document (empty URI) and use a XPath transform.

Upvotes: 1

Related Questions