Extract timestamp information from signed PDF

1.5k Views Asked by At

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?

1

There are 1 best solutions below

0
Lonzak On

and getSigningCertificate() returns the info related to the certificate used to sign the document,

Correct - it does what the name says ;-)

which has nothing to do with the one used by the TSA.

Again correct.

In fact getTimeStampToken() returns something like org.bouncycastle.tsp.TimeStampToken@59f55efc

This just means that the toString() method has not been overwritten. As mkl indicated did you try to access some of the fields/attributes? Instead of

System.out.println("Timestamp token : " + pdfPkcs7.getTimeStampToken().toString());

try something like:

org.bouncycastle.tsp.TimeStampToken ts = pdfPkcs7.getTimeStampToken();
ASN1ObjectIdentifier algOID = ts.getTimeStampInfo().getMessageImprintAlgOID();
...
//there are so many infos, just have a look at the TimeStampToken object!