Signatures XMLDsig toujours invalides
Bonjour tout le monde !
J'ai une Applet (un ? Je sais jamais) qui récupère des fichiers XML dans un répertoire passé en paramètre et je donne la possibilité à l'utilisateur de les signer au format XMLDSig, comme cela :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| String text = "Signature du fichier : " + toSignFile.getName();
ConsoleJFrame.printMessageToConsole(text);
AppletAWS.print(text);
fireText(text);
if (this.cert == null) {
this.fireErrors(AppletAWS.getI18n("pkcs7.noCertCrypto"));
Exception execpt = new Exception("Certificat inaccessible.");
this.fireErreur(new Erreur(execpt, AppletAWS.getI18n("pkcs7.noCertCrypto")));
}
if (this.privatekey == null) {
this.fireErrors(AppletAWS.getI18n("pkcs7.accessDeniedPrivateKey"));
Exception execpt = new Exception("Clef privée inaccessible.");
this.fireErreur(new Erreur(execpt, AppletAWS.getI18n("pkcs7.accessDeniedPrivateKey")));
}
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Create a Reference to the enveloped document (in this case,
// you are signing the whole document, so a URI of "" signifies
// that, and also specify the SHA1 digest algorithm and
// the ENVELOPED Transform.
Reference ref = fac.newReference(
"",
fac.newDigestMethod(DigestMethod.SHA1, null),
Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
null,
null
);
// Create the SignedInfo.
SignedInfo si = fac.newSignedInfo(
fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null),
fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
Collections.singletonList(ref)
);
// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
List x509Content = new ArrayList();
x509Content.add(this.cert.getSubjectX500Principal().getName());
x509Content.add(this.cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
System.out.println("Traitement du XML...");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(this.toSignFile);
DOMBuilder builder = new DOMBuilder();
Document docSigned = builder.build(doc);
Format form = Format.getPrettyFormat();
form.setIndent("\t");
form.setLineSeparator("\n");
// XMLOutputter outp = new XMLOutputter();
// FileOutputStream fOut = new FileOutputStream(this.toSignFile);
// outp.setFormat(form);
// outp.output(docSigned, fOut);
// fOut.close
doc = db.parse(this.toSignFile);
// Create a DOMSignContext and specify the RSA PrivateKey and
// location of the resulting XMLSignature's parent element.
DOMSignContext dsc = new DOMSignContext(this.privatekey, doc.getDocumentElement());
XMLSignature signature = fac.newXMLSignature(si, ki); // Create the XMLSignature, but don't sign it yet.
signature.sign(dsc); // Marshal, generate, and sign the enveloped signature.
TransformerFactory tf = TransformerFactory.newInstance();
tf.newTransformer().transform(new DOMSource(doc), new StreamResult(this.toSignFile)); |
La signature s'effectue sans souci (quand je vais voir dans les fichiers XML, je retrouve bien l'élément signature) mais la validation de la signature me retourne false à chaque fois. Du coup, j'ai trouvé sur le net un moyen de voir pourquoi cette validation est à false :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| /**
* Je récupère la signature depuis mon fichier XML, tout ça, tout ça... [...]
*/
boolean coreValidity = signature.validate(valContext); // Validate the XMLSignature.
// Check core validation status.
if (!coreValidity) {
System.err.println("\nSignature failed core validation");
boolean sv = signature.getSignatureValue().validate(valContext);
System.out.println("Signature validation status: " + sv);
if (!sv) {
// Check the validation status of each Reference.
Iterator iter = signature.getSignedInfo().getReferences().iterator();
for (int j = 0; iter.hasNext(); j++) {
Reference refer = (Reference) iter.next();
boolean refValid = refer.validate(valContext);
retour += "\tRéférence : " + refer.getId() + " - " + refer.getType() + " - " + refer.getURI() + " ; Status : " + refValid + "\n";
}
this.checkList.add(false);
rapport.setEtat(Constantes.SIGN_INVALID);
sign.setStatut(Constantes.SIGN_INVALID);
}
}
else {
System.err.println("Signature succeed core validation");
retour = AppletAWS.getI18n("pkcs7.verif.success");
this.checkList.add(true);
rapport.setEtat(Constantes.SIGN_VALID);
sign.setStatut(Constantes.SIGN_VALID);
} |
Et là, à chaque fois, ma variable sv me renvoie true. Est-ce que ça signifie que ma signature est en fait validée ? Je me demande vu que je fais le test sur la valeur de la signature (signature.getSignatureValue().validate(valContext))...
Si ce n'est pas le cas, est-ce que quelqu'un pourrait me dire pourquoi ma signature n'est pas validée ? Ou un moyen d'en déterminer la cause ?
Merci d'avance. :)
DarkSeiryu