18
Oct
2011
lelissam

Validación desde Java de PDF firmado digitalmente

Para validar desde Java un PDF firmado digitalmente con las librerias itext y bouncycastle hacer:

File in = new File("/temp/sign.pdf");

KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("/temp/lelissam.pfx"), "lelissam".toCharArray());

PdfReader reader = new PdfReader(new FileInputStream(in));
AcroFields af = reader.getAcroFields();
ArrayList names = af.getSignatureNames();
for (int i = 0; i  0)
		out.write(bb, 0, n);
	out.close();
	revision.close();

	// validate signature
	PdfPKCS7 pk = af.verifySignature(name);
	System.out.println("Subject: " + PdfPKCS7.getSubjectFields(pk.getSigningCertificate()));
	System.out.println("Signature valid: " + pk.verify());

	// validate certificates
	Certificate pkc[] = pk.getCertificates();
	Calendar cal = pk.getSignDate();
	Object fails[] = PdfPKCS7.verifyCertificates(pkc, ks, null, cal);
	if (fails == null)
		System.out.println("Certificates verified against the KeyStore");
	else
		System.out.println("Certificate failed: " + fails[1]);
}