我正在尝试使用 xades4j 库使用分离的 XAdES 签名“test.zip.xades”验证文件“test.zip”的完整性。我知道更简单的方法是使用 md5,但我需要使用 XAdES。我不想验证 CA 链等,只是验证文件的完整性。可能吗?我有以下代码:
public static void main(String[] args) throws Exception {
System.out.println("verifyDetachedC");
FileInputStream fis = new FileInputStream("cacerts");
KeyStore trustAnchors = KeyStore.getInstance("jks");
trustAnchors.load(fis,"changeit".toCharArray());
fis.close();
CertificateValidationProvider certValidator = new CertificateValidationProviderImpl();
XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
p.acceptUnknownProperties(true);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
FileInputStream isXades = new FileInputStream("test.zip.xades");
Document doc = db.parse(isXades);
Element signatureNode = getSigElement(doc);
FileInputStream is = new FileInputStream("test.zip");
SignatureSpecificVerificationOptions options = new SignatureSpecificVerificationOptions().useDataForAnonymousReference(is);
XAdESVerificationResult res = verifier.verify(signatureNode, options);
is.close();
}
class CertificateValidationProviderImpl implements CertificateValidationProvider {
@Override
public ValidationData validate(X509CertSelector certSelector,
Date validationDate,
Collection<X509Certificate> otherCerts)
throws CertificateValidationException, UnexpectedJCAException {
return new ValidationData((List<X509Certificate>) otherCerts);
}
}
我收到“签名 ID-39XXXX 的签名值无效”。我不确定代码是否正常工作,但签名错误或代码不工作。签名有 Signature Id="ID-39XXXX, Reference URI=test.zip (relative), 和 ds:X509Certificate。