如何验证Xades4j
会签 xml 文档的使用库。验证时出现以下错误Xades4j
:
xades4j.verification.CounterSignatureSigValueRefException:属性“CounterSignature”的验证失败:计数器签名未引用 xades4j.verification.CounterSignatureVerifier.verify(CounterSignatureVerifier.java:75) 处 xades4j.verification.CounterSignatureVerifier 处的会签签名的 SignatureValue 元素。 verify(CounterSignatureVerifier.java:37) at xades4j.verification.GenericDOMDataVerifier.verify(GenericDOMDataVerifier.java:65) at xades4j.verification.GenericDOMDataVerifier.verify(GenericDOMDataVerifier.java:30) at xades4j.verification.QualifyingPropertiesVerifierImpl.verifyProperties(QualifyingPropertiesVerifierImpl.java :59) 在 xades4j.verification.XadesVerifierImpl.verify(XadesVerifierImpl.java:187) 在 com.fit.einvoice.ingcountersigner.service。xades.XadesVerifyOperation.verifySignature(XadesVerifyOperation.java:92) 在 com.fit.einvoice.ingcountersigner.service.xades.XadesVerifyOperation.verifySignature(XadesVerifyOperation.java:87) 在 com.fit.einvoice.ingcountersigner.service.xades.XadesVerifyOperation。验证签名(XadesVerifyOperation.java:64)
我的验证功能:
static void checkSigned(File file) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
XadesVerifyOperation verifyOperation = new XadesVerifyOperation();
ArrayList<XadesVerificationResults> results = verifyOperation.verifySignature(inputStream);
System.out.println("results size: " + results.size());
for (XadesVerificationResults result : results) {
System.out.println(result.SigningCertificate.getIssuerDN());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException ex) {
}
}
}
编辑:
我的会签功能:
public void CounterSign() throws TransformerFactoryConfigurationError, Exception {
Document doc = SignatureServicesBase.getDocument(_inputStream);
Element sigElem = (Element) doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE).item(0);
System.out.println(sigElem.getNodeName());
org.apache.xml.security.Init.init();
XMLSignature xmlSig = new XMLSignature(sigElem, doc.getBaseURI());
//Create counter signer
XadesBesSigningProfile signingProfile = new XadesBesSigningProfile(new Pkcs11KeyingDataProvider(_certInfo));
signingProfile.withAlgorithmsProvider(Sha1AlgProvider.class);
signingProfile.withBasicSignatureOptionsProvider(new MyBasicSignatureOptionsProvider(true, true, false));
final XadesSigner counterSigner = signingProfile.newSigner();
//Extend with counter signature
XadesFormatExtenderProfile extenderProfile = new XadesFormatExtenderProfile();
XadesSignatureFormatExtender extender = extenderProfile.getFormatExtender();
List unsignedProps = Arrays.asList(new CounterSignatureProperty(counterSigner));
extender.enrichSignature(xmlSig, new UnsignedProperties(unsignedProps));
SignatureServicesBase.outputDocument(doc, _outStream);
if (!_isStream) {
_inputStream.close();
_outStream.close();
}
}