gpt4 book ai didi

java - 使用 XADES4j 验证 XADES 签名

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:26 26 4
gpt4 key购买 nike

我们想创建一个验证“XADES”签名的 java web 应用程序,这个应用程序应该有两个文件:原始文件和他的分离签名。

我正在使用 XADES4j 库,这是一个很棒的项目。使用 XADES4j 有没有办法在不验证 URI 文件引用的情况下验证签名?因为无法访问 xml 签名中的给定引用文件。

对于引用验证:我希望比较从给定原始文件计算的摘要值和从签名文件中提取的摘要值。

这里是异常(exception)

Exception in thread "main" xades4j.XAdES4jXMLSigException: Error verifying the signature
at xades4j.verification.XadesVerifierImpl.doCoreVerification(XadesVerifierImpl.java:285)
at xades4j.verification.XadesVerifierImpl.verify(XadesVerifierImpl.java:188)
at com.wct.VerifyXades.main(VerifyXades.java:33)
Caused by: org.apache.xml.security.signature.MissingResourceFailureException: The Reference for URI file:/D:/workspace/xades4j-487d7a9bb9e5/data_to_sign/test.txt has no XMLSignatureInput
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: D:\workspace\xades4j-487d7a9bb9e5\data_to_sign\test.txt (Le fichier spécifié est introuvable)
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: D:\workspace\xades4j-487d7a9bb9e5\data_to_sign\test.txt (Le fichier spécifié est introuvable)
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: D:\workspace\xades4j-487d7a9bb9e5\data_to_sign\test.txt (Le fichier spécifié est introuvable)
Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: D:\workspace\xades4j-487d7a9bb9e5\data_to_sign\test.txt (Le fichier spécifié est introuvable)
Original Exception was java.io.FileNotFoundException: D:\workspace\xades4j-487d7a9bb9e5\data_to_sign\test.txt (Le fichier spécifié est introuvable)
at org.apache.xml.security.signature.Manifest.verifyReferences(Manifest.java:412)
at org.apache.xml.security.signature.SignedInfo.verify(SignedInfo.java:256)
at org.apache.xml.security.signature.XMLSignature.checkSignatureValue(XMLSignature.java:764)
at org.apache.xml.security.signature.XMLSignature.checkSignatureValue(XMLSignature.java:696)
at xades4j.verification.XadesVerifierImpl.doCoreVerification(XadesVerifierImpl.java:278)
... 2 more

这是我用来验证 XADES 签名的源代码:

package com.wct;

import java.io.FileInputStream;
import java.security.cert.X509CertSelector;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import xades4j.providers.CertificateValidationException;
import xades4j.providers.CertificateValidationProvider;
import xades4j.providers.ValidationData;
import xades4j.verification.UnexpectedJCAException;
import xades4j.verification.XAdESVerificationResult;
import xades4j.verification.XadesVerificationProfile;
import xades4j.verification.XadesVerifier;

public class VerifyXades {

public static void main(String[] args) throws Exception {
CertificateValidationProvider certValidator = new CertificateValidationProviderImpl();
XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
p.acceptUnknownProperties(true);
XadesVerifier v = p.newVerifier();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream("data_signed/detachedTestSignature.xml"));
XAdESVerificationResult vr = v.verify(doc.getDocumentElement(), null);
}
}

class CertificateValidationProviderImpl implements CertificateValidationProvider {
@Override
public ValidationData validate(X509CertSelector certSelector,
Date validationDate, Collection<X509Certificate> otherCerts)
throws CertificateValidationException, UnexpectedJCAException {
return new ValidationData((List<X509Certificate>) otherCerts);
}
}

我是签名/验证开发的新手,我没有很好的经验。请帮忙

在此先感谢您的帮助

最佳答案

您不应该进行任何摘要比较。该文件必须能够以某种方式访问​​,以便可以在签名验证过程中检查引用。

您能控制签名生成吗?如果是这样,您应该将 URI 更改为相对 URI,或者使用匿名引用(更多信息见下文)。无论如何,您所有的选择都将基于 SignatureSpecificVerificationOptions .

如果您可以更改签名验证:

如果您无法更改签名制作:

关于java - 使用 XADES4j 验证 XADES 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com