我必须签名然后会签一些带有签名的xml。这是我的代码:
private String singXadesEnveloped(String mode, Document document, Certificate[] certificateChain, PrivateKey signingKey, String mimeType, String encoding)
throws XAdES4jException, ClassCastException, UnsupportedEncodingException, ClassNotFoundException,
InstantiationException, IllegalAccessException {
try {
DataObjectDesc desc = null;
KeyingDataProvider kp = new StaticKeyingDataProvider(certificateChain, signingKey);
BasicSignatureOptionsProvider bop=new BasicSignatureOptionsProvider() {
public boolean signSigningCertificate() {
// TODO Auto-generated method stub
return false;
}
public boolean includeSigningCertificate() {
// TODO Auto-generated method stub
return true;
}
public boolean includePublicKey() {
// TODO Auto-generated method stub
return true;
}
};
//System.out.println("bop.includePublic="+bop.includePublicKey());
XadesSigningProfile sp = new XadesBesSigningProfile(kp).withTimeStampTokenProvider(CertumFreeTimeStampProvider.class).withBasicSignatureOptionsProvider(bop);
XadesSigner signer = sp.newSigner();
desc = new DataObjectReference("")
.withDataObjectFormat(new DataObjectFormatProperty(mimeType, encoding))
.withTransform(new EnvelopedSignatureTransform());
SignedDataObjects dataObjects = new SignedDataObjects(desc)
.withCommitmentType(AllDataObjsCommitmentTypeProperty.proofOfOrigin());
Element el = document.getDocumentElement();
//System.out.println("element="+el.getNodeName());
XadesSignatureResult sign = signer.sign(dataObjects, el);
String signed_xml = serializeDocument(document);
//System.out.println("\n\nPodpisany xml:\n"+signed_xml+"\n\n");
XadesSignatureFormatExtender extender = new XadesFormatExtenderProfile().getFormatExtender();
Element sigElem = sign.getSignature().getElement();
//System.out.println("\n\nTag do podpisu:"+sigElem.getNodeName()+"\n\n");
XMLSignature sig = new XMLSignature(sigElem, sigElem.getOwnerDocument().getBaseURI());
XadesSigningProfile profile = new XadesBesSigningProfile(kp).withTimeStampTokenProvider(CertumFreeTimeStampProvider.class).withBasicSignatureOptionsProvider(bop);
final XadesSigner counterSigner = profile.newSigner();
// .withTransform(new ExclusiveCanonicalXMLWithoutComments());
//System.out.println("\n\nNode sygnatury: "+sig.getElement().getNodeName()+"\n\n");
Collection<UnsignedSignatureProperty> usp = new ArrayList(1);
usp.add(new CounterSignatureProperty(counterSigner));
extender.enrichSignature(sig, new UnsignedProperties(usp));
} catch (XMLSignatureException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (XMLSecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*-----------------------------------------------------*/
//alternatywny sposób realizowania podpisu
//new Enveloped(signer).sign(document.getDocumentElement());
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tf.newTransformer();
transformer.transform(domSource, result);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("\n\nsignXades signed before serializedocument: \n\n" + writer.toString());
//return serializeDocument(signed_document);
return writer.toString();
}
它签署我的 xml 并添加反签名。不幸的是,当我验证我的 xml 时,签名非常好,但反签名不是(签名的摘要与文件数据的摘要不相等)。
我的代码有什么问题?这是带有签名和反签名的 XML: