// Open input file for reading.
PdfReader reader = new PdfReader(inputFileName);
// Get the list of signatures in the document.
AcroFields fields = reader.getAcroFields();
ArrayList<String> signatures = fields.getSignatureNames();
if (signatures.size() == 0) {
throw new NoSignatureException(messages.getString(
"Document_has_no_signature."));
}
// Check if the last signature is a timestamp.
String lastSignature = signatures.get(signatures.size() - 1);
PdfPKCS7 pkcs7 = fields.verifySignature(lastSignature);
if (pkcs7.isTsp()) {
// Prepare to store certificates, OCSP responses and CRLs only for the last timestamp.
signatures.clear();
signatures.add(lastSignature);
}
// Prepare structures to store all certificates and CRLs.
ArrayList<X509Certificate> allCertificates = new ArrayList<X509Certificate>();
ArrayList<String> allCRLDistributionPoints = new ArrayList<String>();
HashMap<String, ArrayList<X509Certificate>> certMap = new HashMap<String, ArrayList<X509Certificate>>();
HashMap<String, ArrayList<X509CRL>> crlMap = new HashMap<String, ArrayList<X509CRL>>();
HashMap<String, ArrayList<BasicOCSPResp>> ocspMap = new HashMap<String, ArrayList<BasicOCSPResp>>();
// Store certificates, OCSP responses and CRLs for each signature.
for (String signature : signatures) {
// Get signature data.
pkcs7 = fields.verifySignature(signature);
// Get certificate path.
X509Certificate signingCertificate = (X509Certificate) factory.generateCertificate(
new ByteArrayInputStream(pkcs7.getSigningCertificate().getEncoded()));
Certificate[] chain = pkcs7.getSignCertificateChain();