Skip to main content

PdfSignatureValidator Class

A validator that checks PKCS#7 signatures and Long Term Validation (LTV) information in a PDF document.

Declaration

public class PdfSignatureValidator extends JavaObject

Remarks

Pass a stream that contains a signed document to the PdfSignatureValidator constructor. Call the following methods to validate signatures:

Method Description
getPkcs7Signature(String formFieldName) Returns the PKCS#7 signature of the specified signature field.
verifyLtv(boolean verifyEntireCertificateChain, boolean tryFetchCrlOnline, boolean tryFetchOcspOnline) Returns Long Term Validation (LTV) information about signature certificates in the document.

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.

Refer to the following help topic for additional information: Validate PDF Signatures — PDF Document API for Java.

Example

Validate a PDF Signature

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());
        }
    }
}

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
PdfSignatureValidator
See Also

Constructors

PdfSignatureValidator(InputStream documentStream, String documentPassword) Constructor

Initializes a new instance of the PdfSignatureValidator class with the specified document stream and password.

Declaration

public PdfSignatureValidator(InputStream documentStream, String documentPassword)

Parameters

Name Type Description
documentStream InputStream

A stream that contains the signed PDF document.

documentPassword String

The password of an encrypted document.

Exceptions

Type Description
IllegalArgumentException

Thrown if the documentStream parameter is null.

PdfIncorrectPasswordException

Thrown if the document is encrypted and the documentPassword value is empty or incorrect. The exception message is “The Document Open password is empty or incorrect.”

PdfSignatureValidator(InputStream documentStream) Constructor

Initializes a new instance of the PdfSignatureValidator class with the specified document stream.

Declaration

public PdfSignatureValidator(InputStream documentStream)

Parameters

Name Type Description
documentStream InputStream

A stream that contains the signed PDF document.

Exceptions

Type Description
IllegalArgumentException

Thrown if the documentStream parameter is null.

PdfIncorrectPasswordException

Thrown if the document requires a Document Open (user) password. The exception message is “The Document Open password is empty or incorrect.”

PdfSignatureValidator(ReadableByteChannel documentChannel, String documentPassword) Constructor

Initializes a new instance of the PdfSignatureValidator class with the specified document channel and password.

Declaration

public PdfSignatureValidator(ReadableByteChannel documentChannel, String documentPassword)

Parameters

Name Type Description
documentChannel ReadableByteChannel

A channel that contains the signed PDF document.

documentPassword String

The password of an encrypted document.

Exceptions

Type Description
IllegalArgumentException

Thrown if the documentChannel parameter is null.

PdfIncorrectPasswordException

Thrown if the document is encrypted and the documentPassword value is empty or incorrect. The exception message is “The Document Open password is empty or incorrect.”

PdfSignatureValidator(ReadableByteChannel documentChannel) Constructor

Initializes a new instance of the PdfSignatureValidator class with the specified document channel.

Declaration

public PdfSignatureValidator(ReadableByteChannel documentChannel)

Parameters

Name Type Description
documentChannel ReadableByteChannel

A channel that contains the signed PDF document.

Exceptions

Type Description
IllegalArgumentException

Thrown if the documentChannel parameter is null.

PdfIncorrectPasswordException

Thrown if the document requires a Document Open (user) password. The exception message is “The Document Open password is empty or incorrect.”

Methods

getPkcs7Signature(String formFieldName) Method

Returns the PKCS#7 signature of the specified signature field.

Declaration

public Pkcs7Signature getPkcs7Signature(String formFieldName)

Parameters

Name Type Description
formFieldName String

The full name of the signature field.

Returns

Type Description
Pkcs7Signature

An object that contains the signature and allows you to verify it.

Exceptions

Type Description
ArgumentNullException

Thrown if the formFieldName parameter is null.

ArgumentException

Thrown if the document does not contain a form field with the specified name, or the form field is not a signed signature field.

Remarks

For a nested field, pass the full field name (for example, Approvals.Signature1).

verifyLtv(boolean verifyEntireCertificateChain, boolean tryFetchCrlOnline, boolean tryFetchOcspOnline) Method

Returns Long Term Validation (LTV) information about signature certificates in the document.

Declaration

public Iterable<LtvVerificationStatus> verifyLtv(boolean verifyEntireCertificateChain, boolean tryFetchCrlOnline, boolean tryFetchOcspOnline)

Parameters

Name Type Description
verifyEntireCertificateChain boolean

true to check the revocation status of all certificates in the certificate chain; false to check the signing certificate only.

tryFetchCrlOnline boolean

true to download Certificate Revocation Lists (CRLs) if the document does not contain CRLs for a certificate; otherwise, false.

tryFetchOcspOnline boolean

true to request the certificate status from an Online Certificate Status Protocol (OCSP) responder if the document does not contain this status; otherwise, false.

Returns

Type Description
java.lang.Iterable<LtvVerificationStatus>

LTV verification results (one item for each signature).

Exceptions

Type Description
PdfLtvException

Thrown if the document is not signed, the document changed after a signature was applied, or a certificate in the chain is not valid at the current date and time. The exception message contains the signature field name (for example, “Signature Signature1: Document has been altered or corrupted since it was signed.”). Also thrown if the API cannot build a certificate chain.

Remarks

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.

Example: Check Certificate Revocation and Verify LTV Information

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());
            }
        }
    }
}