I have looked at this for too long and cannot understand what I am doing wrong.
So, I'm trying to create a Xades signature for some content. Unfortunately, I always encounter 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 am trying to sign a Request element (obviously ...) with the following code:
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);
KeyingDataProvider kp = new CustomKeyingDataProvider(certificate, privateKey);
XadesSigningProfile p = new XadesTSigningProfile(kp);
XadesSigner signer = p.newSigner();
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());
signer.sign(dataObjs, doc);
I get this error in return (abbreviated as 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 on the Internet, but the only similar error I found was the following: https://code.google.com/p/xades4j/wiki/QeA (almost on top). I cannot find the answer to his question, but as far as I can see, my XML document has a root element (like its second example). So I don’t know what I am doing wrong ...
- ? .