Vérifier un document pdf signé en Java

02.04.2008, 17:31 A TESTER

http://itextpdf.sourceforge.net/howtosign.html#howtoverify
article sur un blog
doc adobe

CodeKeyStore kall = PdfPKCS7.loadCacertsKeyStore();
PdfReader reader = new PdfReader("my_signed_doc.pdf");
AcroFields af = reader.getAcroFields();
ArrayList names = af.getSignatureNames();
for (int k = 0; k < names.size(); ++k) {
String name = (String)names.get(k);
System.out.println("Signature name: " + name);
System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
System.out.println("Document revision: " + af.getRevision(name) + " of " + af.getTotalRevisions());
// Start revision extraction
FileOutputStream out = new FileOutputStream("revision_" + af.getRevision(name) + ".pdf");
byte bb[] = new byte[8192];
InputStream ip = af.extractRevision(name);
int n = 0;
while ((n = ip.read(bb)) > 0)
out.write(bb, 0, n);
out.close();
ip.close();
// End revision extraction
PdfPKCS7 pk = af.verifySignature(name);
Calendar cal = pk.getSignDate();
Certificate pkc[] = pk.getCertificates();
System.out.println("Subject: " + PdfPKCS7.getSubjectFields(pk.getSigningCertificate()));
System.out.println("Document modified: " + !pk.verify());
Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal);
if (fails == null)
System.out.println("Certificates verified against the KeyStore");
else
System.out.println("Certificate failed: " + fails[1]);
}


Note that if you were using the self signed mode you would have to load kall with the certificate provided by whoever signed the document. The certificate could be loaded this way:

CitationCertificateFactory cf = CertificateFactory.getInstance("X509");
Collection col = cf.generateCertificates(new FileInputStream("self.p7b"));
KeyStore kall = KeyStore.getInstance(KeyStore.getDefaultType());
kall.load(null, null);
for (Iterator it = col.iterator(); it.hasNext();) {
X509Certificate cert = (X509Certificate)it.next();
kall.setCertificateEntry(cert.getSerialNumber().toString(Character.MAX_RADIX), cert);
}



mumuri forum.ashots.org
mumuri
mumuri
Messages : 13 727

  • forum
  • astuces de jeux vidéos
  • allo ciné
  • dessin animé
  • smileys
  • actualité de jeux vidéos
  • séries télé
  • avis livres
  • bourse
  • anime
  • avis films
Tous droits réservés © 2008 . Top flood Crédits des logiciels tiers.   - en partenariat avec flood.fr .