Skip to main content

Pkcs7Signature Class

A PKCS#7 signature in a PDF document.

Declaration

public class Pkcs7Signature extends JavaObject

Remarks

Call the PdfSignatureValidator.getPkcs7Signature(formFieldName) method to get the PKCS#7 signature of a signature field.

Call the getIsSignatureVerified() method to check whether the signature is valid. Call the checkCertificateRevocation(OffsetDateTime atDateTime, boolean tryFetchCrlOnline, boolean tryFetchOcspOnline) method to check whether the signing certificate is revoked.

Note

The Pkcs7Signature class does not check whether the signature certificate chain is trusted.

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
Pkcs7Signature
See Also

Methods

checkCertificateRevocation(OffsetDateTime atDateTime, boolean tryFetchCrlOnline, boolean tryFetchOcspOnline) Method

Returns the revocation status of the signing certificate at the specified date and time.

Declaration

public SignatureCertificateRevocationStatus checkCertificateRevocation(OffsetDateTime atDateTime, boolean tryFetchCrlOnline, boolean tryFetchOcspOnline)

Parameters

Name Type Description
atDateTime OffsetDateTime

The date and time at which to check the certificate status.

tryFetchCrlOnline boolean

true to download Certificate Revocation Lists (CRLs) if the signature does not contain CRLs; otherwise, false.

tryFetchOcspOnline boolean

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

Returns

Type Description
SignatureCertificateRevocationStatus

The revocation status of the signing certificate.

Remarks

The checkCertificateRevocation method checks CRLs and OCSP responses embedded in the signature (getCrlList() and getOcspList() methods).

To check the certificate status at the signing time, pass the getTimeStampDate() result as the atDateTime parameter.

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

getCrlList() Method

Returns Certificate Revocation Lists (CRLs) embedded in the signature.

Declaration

public Iterable<byte[]> getCrlList()

Returns

Type Description
java.lang.Iterable<byte[]>

DER-encoded CRLs; null if the signature does not contain CRLs.

getIsDocumentTimeStamp() Method

Returns whether the signature is a document-level timestamp.

Declaration

public boolean getIsDocumentTimeStamp()

Returns

Type Description
boolean

true if the signature is a document-level timestamp; otherwise, false.

getIsSignatureVerified() Method

Returns whether the signature is valid and the signed content has not changed.

Declaration

public boolean getIsSignatureVerified()

Returns

Type Description
boolean

true if the signature is valid; otherwise, false.

Remarks

The getIsSignatureVerified() method checks the signature value and the hash of the signed content.

Note

The getIsSignatureVerified() method does not check whether the signature certificate is trusted or revoked.

getIsTimeStampVerified() Method

Returns whether the signature timestamp is valid.

Declaration

public boolean getIsTimeStampVerified()

Returns

Type Description
boolean

true if the signature contains a timestamp that matches the signature value; otherwise, false.

Remarks

The getIsTimeStampVerified() method does not check whether the certificate of the Time Stamp Authority (TSA) is trusted or revoked.

getOcspList() Method

Returns Online Certificate Status Protocol (OCSP) responses embedded in the signature.

Declaration

public Iterable<byte[]> getOcspList()

Returns

Type Description
java.lang.Iterable<byte[]>

DER-encoded OCSP responses; null if the signature does not contain OCSP responses.

getSignatureCertificate() Method

Returns the signing certificate.

Declaration

public X509Certificate getSignatureCertificate()

Returns

Type Description
X509Certificate

The signing certificate.

getSignatureCertificateChain() Method

Returns the certificate chain of the signing certificate.

Declaration

public List<X509Certificate> getSignatureCertificateChain()

Returns

Type Description
java.util.List<java.security.cert.X509Certificate>

Certificates in the chain.

Remarks

The API builds the chain from certificates embedded in the signature and does not check whether the chain is trusted. If the API cannot build the chain, the list contains only the signing certificate.

getTimeStampDate() Method

Returns the date and time from the signature timestamp.

Declaration

public OffsetDateTime getTimeStampDate()

Returns

Type Description
OffsetDateTime

The timestamp date and time in UTC; null if the signature does not contain a timestamp.

Remarks

A Time Stamp Authority (TSA) issues the timestamp.