I have thousands of PDF documents signed and timestamped with iText.
For legal reasons, I now need to be able to extract from any PDF all data related to the timestamp (including date/time of course but also the TSA name, his public certificate, the signed hash and the algorithm used).
I can detect if the PDF is actually timestamped, and extract date/time with the following piece of code (inspired by the one found here) :
AcroFields acroFields = reader.getAcroFields();
List<String> names = acroFields.getSignatureNames();
String sigName = names.get(names.size() - 1);
PdfPKCS7 pdfPkcs7 = acroFields.verifySignature(sigName);
for (String name: names) {
System.out.printf("Signature : %s\n", name);
if (pdfPkcs7.getTimeStampDate() != null) {
SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
System.out.println("Signed on: " + date_format.format(pdfPkcs7.getSignDate().getTime()));
System.out.println("TimeStamp: " + date_format.format(pdfPkcs7.getTimeStampDate().getTime()));
System.out.println("Timestamp token : " + pdfPkcs7.getTimeStampToken().toString());
System.out.println("Timestamp verified : " + pdfPkcs7.verifyTimestampImprint());
System.out.println("Subject: " + pdfPkcs7.getSigningCertificate());
} else {
System.out.println("No timestamp found");
}
I thought my goal could easily be fulfilled by using the getTimeStampToken() and getSigningCertificate() functions... In fact getTimeStampToken()
returns something like org.bouncycastle.tsp.TimeStampToken@59f55efc
, and getSigningCertificate()
returns the info related to the certificate used to sign the document, which has nothing to do with the one used by the TSA.
Could some help me to get the expected result?