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)); | 
Partager