Error signing a sample XML using Xades4J: ReferenceNotInitializedException: Cannot resolve element with ID

huangapple go评论63阅读模式
英文:

Error signing a sample XML using Xades4J: ReferenceNotInitializedException: Cannot resolve element with ID

问题

以下是您提供的内容的翻译部分:

我正在尝试使用Xades4J对一个简单的XML进行签名,使用这个示例,但我遇到了以下错误:

> 主线程中的异常"xades4j.XAdES4jXMLSigException":
> 无法解析带有ID的元素
> 位于 xades4j.production.SignerBES.sign(SignerBES.java:277)
> 在 xades4j.production.SignerBES.sign(SignerBES.java:130)
> 在 xmlSigner.Main.main(Main.java:63)
> 原因是:
> org.apache.xml.security.signature.ReferenceNotInitializedException: 无法解析带有ID的元素
> 原始异常是 org.apache.xml.security.utils.resolver.ResourceResolverException: 无法解析带有ID的元素
> 在 org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:437)
> 在 org.apache.xml.security.signature.Reference.calculateDigest(Reference.java:722)
> 在 org.apache.xml.security.signature.Reference.generateDigestValue(Reference.java:414)
> 在 org.apache.xml.security.signature.Manifest.generateDigestValues(Manifest.java:205)
> 在 org.apache.xml.security.signature.XMLSignature.sign(XMLSignature.java:628)
> 在 xades4j.production.SignerBES.sign(SignerBES.java:273)
> ... 还有 2 行
> 原因是:
> org.apache.xml.security.utils.resolver.ResourceResolverException:
> 无法解析带有ID的元素 at
> org.apache.xml.security.utils.resolver.implementations.ResolverFragment.engineResolveURI(ResolverFragment.java:81)
> at
> org.apache.xml.security.utils.resolver.ResourceResolver.resolve(ResourceResolver.java:288)
> at
> org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:435)
> ... 还有 7 行

我的代码如下:

// ... (这里省略了代码内容)

XML 是一个关于海关流程的示例文档,可以在这里找到。

任何建议将不胜感激。

英文:

I'm trying to sign a simple XML using Xades4J using this example, but I got this error:

> Exception in thread "main" xades4j.XAdES4jXMLSigException:
> Cannot resolve element with ID
> at xades4j.production.SignerBES.sign(SignerBES.java:277)
> at xades4j.production.SignerBES.sign(SignerBES.java:130)
> at xmlSigner.Main.main(Main.java:63)
> Caused by:
> org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID
> Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID
> at org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:437)
> at org.apache.xml.security.signature.Reference.calculateDigest(Reference.java:722)
> at org.apache.xml.security.signature.Reference.generateDigestValue(Reference.java:414)
> at org.apache.xml.security.signature.Manifest.generateDigestValues(Manifest.java:205)
> at org.apache.xml.security.signature.XMLSignature.sign(XMLSignature.java:628)
> at xades4j.production.SignerBES.sign(SignerBES.java:273)
> ... 2 more
> Caused by:
> org.apache.xml.security.utils.resolver.ResourceResolverException:
> Cannot resolve element with ID at
> org.apache.xml.security.utils.resolver.implementations.ResolverFragment.engineResolveURI(ResolverFragment.java:81)
> at
> org.apache.xml.security.utils.resolver.ResourceResolver.resolve(ResourceResolver.java:288)
> at
> org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:435)
> ... 7 more

My code is this

package xmlSigner;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import xades4j.algorithms.EnvelopedSignatureTransform;
import xades4j.production.DataObjectReference;
import xades4j.production.SignedDataObjects;
import xades4j.production.XadesBesSigningProfile;
import xades4j.production.XadesSigner;
import xades4j.properties.DataObjectDesc;
import xades4j.providers.KeyingDataProvider;
import xades4j.providers.impl.FileSystemKeyStoreKeyingDataProvider;
import xades4j.providers.impl.DirectPasswordProvider;
import xades4j.providers.impl.FirstCertificateSelector;
import xades4j.utils.DOMHelper;

public class Main {

		private static final String CERT_FOLDER = "C:\\Users\\...";
	    private static final String CERT        = "keystore.p12";
	    //private static final String KEY_STORE   = "KeyStorage";
	    private static final String PASS        = "mypass"; //the same in cert and keystorage

	    //private static final String TSA_URL     = "http://XXX.XXX.XXX/ts.inx";
	    //private static final String TSA_USER    = "XXXXXXXX";
	    //private static final String TSA_PASS    = "XXXXXXXX";

	    //private static final String UNSIGNED    = "C:/Test/sign-verify/unsigned.xml";
	    private static final String SIGNED      = "C:\\Users\\...\\FLUSSO_A_signed.xml";
	    //private static final String SIGNEDT     = "C:/Test/sign-verify/signed-t-bes.xml";    
	    //private static final String VERIFY      = "C:/Test/sign-verify/verify-bes.txt";
	    //private static final String VERIFYT     = "C:/Test/sign-verify/verify-t-bes.txt";
	    private static final String DOCUMENT    = "C:\\Users\\..\\FLUSSO_A.xml";
	    //private static final String DOCSIGNED   = "C:/Test/sign-verify/signed.bes.xml";

	    public static void main(String[] args) throws Exception {

	        Document doc = DocumentBuilderFactory
	                .newInstance()
	                .newDocumentBuilder()
	                .parse(new File(DOCUMENT));
	        Element elem = doc.getDocumentElement();
	        DOMHelper.useIdAsXmlId(elem);
	        

	        KeyingDataProvider kdp = new FileSystemKeyStoreKeyingDataProvider(
	                "pkcs12",
	                CERT_FOLDER + CERT,
	                new FirstCertificateSelector(),
	                new DirectPasswordProvider(PASS),
	                new DirectPasswordProvider(PASS),
	                true);
            // here I tried also EManifest (the XML root tag) and name (since elem has a "name" attribute)
	        DataObjectDesc obj = new DataObjectReference("#" + elem.getAttribute("Id")) 
	                .withTransform(new EnvelopedSignatureTransform());
	        SignedDataObjects dataObjs = new SignedDataObjects().withSignedDataObject(obj);

	        XadesSigner signer = new XadesBesSigningProfile(kdp).newSigner();
	        signer.sign(dataObjs, elem); // fails here

	        /*TransformerFactory tFactory = TransformerFactory.newInstance();
	        Transformer transformer = tFactory.newTransformer();
	        DOMSource source = new DOMSource(doc);        
	        StreamResult result = new StreamResult(new File(SIGNED));
	        transformer.transform(source, result);*/
	    }
	}

The XML is an example document for customs processes and can be found here.
Any advice would be greatly appreciated.

答案1

得分: 1

不需要创建该ID。您可以这样做:

DataObjectDesc dataObjRef = new DataObjectReference("").withTransform(new EnvelopedSignatureTransform());

将“”传递到DataObjectReference以进行总结。

英文:

Is not needed create the ID. You can do this:

DataObjectDesc dataObjRef = new DataObjectReference("").withTransform(new EnvelopedSignatureTransform());

summarizing pass "" to DataObjectReference

答案2

得分: 0

我找到了解决方案。
我的 XML 的根标签如下所示:

    <EManifest xmlns="http://www.iso.org/28005-ITA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.iso.org/28005-ITA">

将属性添加到标签中(特别是 ` Id="test" ` )解决了这个问题。

现在我的根标签看起来是这样的:

    <EManifest xmlns="http://www.iso.org/28005-ITA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.iso.org/28005-ITA" Id="test">
英文:

I found the solution.
The root tag of my XML looks like this

<EManifest xmlns="http://www.iso.org/28005-ITA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.iso.org/28005-ITA">

adding the attribute to the tag (specifically Id="test" ) solved the issue.

Now my root tag looks like this

<EManifest xmlns="http://www.iso.org/28005-ITA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.iso.org/28005-ITA" Id="test">

huangapple
  • 本文由 发表于 2020年4月7日 17:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61076851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定