Validate PDF Signatures — PDF Document API for Java
- 5 minutes to read
The PDF Document API allows you to validate PKCS#7 signatures in PDF documents. You can check whether the signed content has changed, inspect the signing certificate and the signature timestamp, check whether the signing certificate is revoked, and verify Long Term Validation (LTV) information.
Create a Signature Validator
The PdfSignatureValidator class validates signatures in a PDF document. Pass a stream that contains the signed document to the PdfSignatureValidator(documentStream) constructor. If the document is encrypted, call the PdfSignatureValidator(documentStream, documentPassword) constructor.
Note
The validator can read document data from the stream when you call validator methods. Keep the stream open while you use the validator and Pkcs7Signature objects that the validator returns. The PdfSignatureValidator class does not close the stream.
Verify a PKCS#7 Signature
Call the PdfSignatureValidator.getPkcs7Signature(formFieldName) method to get a Pkcs7Signature object for a signature field. Specify the full name of a nested field (for example, Approvals.Signature1).
Call the following Pkcs7Signature methods to check the signature:
| Method | Description |
|---|---|
| getIsSignatureVerified() | Returns whether the signature is valid and the signed content has not changed. |
| getIsTimeStampVerified() | Returns whether the signature timestamp is valid. |
| getTimeStampDate() | Returns the date and time from the signature timestamp. |
| getIsDocumentTimeStamp() | Returns whether the signature is a document-level timestamp. |
| getSignatureCertificate() | Returns the signing certificate. |
| getSignatureCertificateChain() | Returns the certificate chain of the signing certificate. |
| getCrlList() | Returns Certificate Revocation Lists (CRLs) embedded in the signature. |
| getOcspList() | Returns Online Certificate Status Protocol (OCSP) responses embedded in the signature. |
Note
The Pkcs7Signature class does not check whether the signature certificate chain is trusted.
The getIsSignatureVerified() method does not check whether the signing certificate is revoked. Refer to the Check Certificate Revocation section for additional information.
The following code snippet verifies the Signature1 field signature in Document.pdf and displays the signing certificate, certificate chain, and signature timestamp:
import java.io.*;
import java.nio.file.*;
import java.security.cert.X509Certificate;
import com.devexpress.docs.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream documentStream = Files.newInputStream(Path.of("Document.pdf"))) {
PdfSignatureValidator validator = new PdfSignatureValidator(documentStream);
// Verify the signature of the "Signature1" field.
Pkcs7Signature signature = validator.getPkcs7Signature("Signature1");
System.out.println("Signature verified: " + signature.getIsSignatureVerified());
System.out.println("Signed by: "
+ signature.getSignatureCertificate().getSubjectX500Principal().getName());
// Display the certificate chain.
for (X509Certificate certificate : signature.getSignatureCertificateChain())
System.out.println("Chain: " + certificate.getSubjectX500Principal().getName());
// Check the signature timestamp.
if (signature.getTimeStampDate() != null)
System.out.println("Timestamp: " + signature.getTimeStampDate()
+ ", verified: " + signature.getIsTimeStampVerified());
}
}
}
Check Certificate Revocation
Call the Pkcs7Signature.checkCertificateRevocation method to check whether the signing certificate was revoked at the specified date and time. The checkCertificateRevocation method checks Certificate Revocation Lists (CRLs) and Online Certificate Status Protocol (OCSP) responses embedded in the signature. Pass true as the tryFetchCrlOnline and tryFetchOcspOnline parameters to download CRLs and request OCSP responses if the signature does not contain them.
If the signature contains a timestamp, pass the timestamp date as the atDateTime parameter to check the certificate status at the signing time.
The checkCertificateRevocation method returns a SignatureCertificateRevocationStatus object. Call the following methods of this object to get the revocation status:
| Method | Description |
|---|---|
| getIsCrlRevoked() | Returns whether a Certificate Revocation List (CRL) lists the certificate as revoked. |
| getIsCrlFoundOnline() | Returns whether a Certificate Revocation List (CRL) downloaded from a server lists the certificate as revoked. |
| getOcspRevocationStatus() | Returns the certificate status from an Online Certificate Status Protocol (OCSP) response. |
| getIsOcspFoundOnline() | Returns whether the API received the certificate status from an Online Certificate Status Protocol (OCSP) responder. |
The getOcspRevocationStatus() method returns one of the following values:
| Value | Description |
|---|---|
| NONE | No valid OCSP response contains the certificate status. |
| GOOD | The certificate is not revoked. |
| REVOKED | The certificate is revoked. |
| UNKNOWN | The OCSP responder does not know the certificate status. |
Verify Long Term Validation (LTV) Information
Call the PdfSignatureValidator.verifyLtv method to check whether signatures contain information required for long-term validation.
The verifyLtv method checks all signatures in the document, from the most recent signature to the first signature. For each signature, the verifyLtv method checks that the signed content has not changed and that certificates are valid at the current date and time. The verifyLtv method also gets the revocation status of signature certificates.
The verifyLtv method returns one LtvVerificationStatus object for each signature. Call the LtvVerificationStatus.getCertificateRevocationStatuses() method to get the revocation status of each verified certificate.
The verifyLtv method accepts the following parameters:
verifyEntireCertificateChain- Pass
trueto check all certificates in the certificate chain. Passfalseto check the signing certificate only. tryFetchCrlOnline- Pass
trueto download CRLs if the document does not contain CRLs for a certificate. tryFetchOcspOnline- Pass
trueto request the certificate status from an OCSP responder if the document does not contain this status.
If the document is not signed, the signed content changed, or a certificate is not valid, the verifyLtv method throws a PdfLtvException. The exception message contains the signature field name.
The following code snippet checks the revocation status of the Signature1 field signing certificate in Document.pdf and verifies LTV information for all signatures:
import java.io.*;
import java.nio.file.*;
import java.security.cert.X509Certificate;
import java.time.OffsetDateTime;
import java.util.Map;
import com.devexpress.docs.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream documentStream = Files.newInputStream(Path.of("Document.pdf"))) {
PdfSignatureValidator validator = new PdfSignatureValidator(documentStream);
Pkcs7Signature signature = validator.getPkcs7Signature("Signature1");
// Check whether the certificate was revoked at the timestamp time (or now if there is no timestamp).
OffsetDateTime checkDate = signature.getTimeStampDate() != null
? signature.getTimeStampDate() : OffsetDateTime.now();
SignatureCertificateRevocationStatus revocationStatus =
signature.checkCertificateRevocation(checkDate, true, true);
System.out.println("CRL revoked: " + revocationStatus.getIsCrlRevoked());
System.out.println("OCSP status: " + revocationStatus.getOcspRevocationStatus());
// Verify LTV information for all signatures.
for (LtvVerificationStatus ltvStatus : validator.verifyLtv(true, false, false)) {
System.out.println("LTV for " + ltvStatus.getSignatureFieldName() + ":");
for (Map.Entry<X509Certificate, SignatureCertificateRevocationStatus> item :
ltvStatus.getCertificateRevocationStatuses().entrySet())
System.out.println(" " + item.getKey().getSubjectX500Principal().getName()
+ ": " + item.getValue().getOcspRevocationStatus());
}
}
}
}