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
Inheritance
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 |
|
| tryFetchOcspOnline | boolean |
|
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; |
getIsDocumentTimeStamp() Method
Returns whether the signature is a document-level timestamp.
Declaration
public boolean getIsDocumentTimeStamp()
Returns
| Type | Description |
|---|---|
| boolean |
|
getIsSignatureVerified() Method
Returns whether the signature is valid and the signed content has not changed.
Declaration
public boolean getIsSignatureVerified()
Returns
| Type | Description |
|---|---|
| boolean |
|
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 |
|
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; |
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; |
Remarks
A Time Stamp Authority (TSA) issues the timestamp.