Question

I've been looking way too long at this and can't figure out what I'm doing wrong.

So, I'm trying to generate a Xades signature for some content. Unfortunately I always run into the same error: "HIERARCHY_REQUEST_ERR". This is my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<object>
    <request id="f9e1294a-64b7-488b-b475-7511e317e399">(some arbitrary base64 encoded content)</request>
</object>

I'm trying to sign the "Request" element (obviously...), with the following code:

/*create a document*/
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element objectElement = doc.createElement("object");
doc.appendChild(objectElement);
Element requestElement = doc.createElement("request");
requestElement.appendChild(doc.createTextNode(decodedContent));
requestElement.setAttribute("id", UUID.randomUUID().toString());
objectElement.appendChild(requestElement);

/*Key provider, signing profile & signer itself*/
KeyingDataProvider kp = new CustomKeyingDataProvider(certificate, privateKey);
XadesSigningProfile p = new XadesTSigningProfile(kp);
XadesSigner signer = p.newSigner();

/*Signed data*/
DataObjectDesc flatFile = new DataObjectReference("#" + requestElement.getAttribute("id"))
    .withTransform(new GenericAlgorithm("http://www.w3.org/2000/09/xmldsig#base64"))
    .withDataObjectTimeStamp();
SignedDataObjects dataObjs = new SignedDataObjects(flatFile).withCommitmentType(AllDataObjsCommitmentTypeProperty.proofOfOrigin());

/*Actual signing*/
signer.sign(dataObjs, doc);

I get this error in return (abbreviated to what was necessary):

class org.w3c.dom.DOMException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An    
attempt was made to insert a node where it is not permitted. 
    at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
    at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
    at xades4j.production.AppendAsLastChildStrategy.append(SignatureAppendingStrategies.java:55)
    at xades4j.production.SignerBES.sign(SignerBES.java:210)
    at xades4j.production.SignerBES.sign(SignerBES.java:122)
    ...

I searched the web, but the only similar error I found was this one: https://code.google.com/p/xades4j/wiki/QeA (almost on top). I can't really find an answer to his question, but as far as I can see, my XML document is with a root element (just like his second example). So I don't really know what I'm doing wrong...

Is there anyone able to help me? Thanks in advance.

Was it helpful?

Solution

You're trying to append the signature as the root element of the document, since you're supplying doc as the parent on the sign method. However, there's already a root element on the document (object), and only one is alowed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top