1

我收到带有 Xades EPES 签名的 XML 发票,我必须控制它。所以我尝试用 Xades4j 做到这一点。我已经修复了很多错误但我坚持错误:

errxades4j.verification.SignaturePolicyNotAvailableException: Verification failed for property 'SignaturePolicyIdentifier': signature policy document is not available
    at xades4j.verification.SignaturePolicyVerifier.verify(SignaturePolicyVerifier.java:67)
    at xades4j.verification.SignaturePolicyVerifier.verify(SignaturePolicyVerifier.java:38)
    at xades4j.verification.QualifyingPropertiesVerifierImpl.verifyProperties(QualifyingPropertiesVerifierImpl.java:58)
    at xades4j.verification.XadesVerifierImpl.verify(XadesVerifierImpl.java:202)

Java 代码用于验证签名:

FileInputStream fis = new FileInputStream("keystore.jks");
KeyStore trustAnchors = KeyStore.getInstance("jks");
trustAnchors.load(fis,"password".toCharArray());
fis.close();

CertificateValidationProvider certValidator = new PKIXCertificateValidationProvider(trustAnchors, false);
XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
XadesVerifier v = p.newVerifier();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db =  dbf.newDocumentBuilder();
FileInputStream is = new FileInputStream(filename);
Document doc = db.parse(is);

Element sigElem = (Element)doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE).item(0);
XAdESVerificationResult r = null;
r = v.verify(sigElem,null);

如有必要,我可以发布我在匿名化后尝试验证的 XML 文件的摘录。

谢谢。我已经查看并尝试了我找到的所有内容(在网上,xades4j 示例,xades4j junit 类......)但似乎没有任何东西可以解决我的错误。

4

1 回答 1

2

您需要像这样指定自己的政策文件提供者:p.withPolicyDocumentProvider(). 并且您必须实现自己的策略提供程序,该提供程序实现接口SignaturePolicyInfoProviderSignaturePolicyDocumentProvider. (至少我是这样做的)

编辑您的评论:

您需要实现SignaturePolicyInfoProvider.getSignaturePolicyDocumentStream(),它返回一个InputStream. 这可以是一个FileInputStream

示例

public class FilebasedSignaturePolicyProvider implements  SignaturePolicyDocumentProvider {

    @Override
    public InputStream getSignaturePolicyDocumentStream(ObjectIdentifier sigPolicyId) {
        String oid = sigPolicyId.getIdentifier();

        try {
            return new FileInputStream("directory-to-my-policy-files/" + oid);
        } catch (FileNotFoundException e) {
            // handle error
        }
    }
}
于 2014-07-30T08:40:37.270 回答