PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
A digital signature without a timestamp takes its signing time from the signing device's local clock. That time can be changed, and it cannot prove to a verifier that the signature was made while the certificate was valid. A timestamped signature requests an RFC 3161 timestamp token from a Time Stamp Authority (TSA) at signing time and embeds it in the signature, so a trusted third party attests that the signature existed at that time. Even after the signing certificate expires, a verifier can still confirm that the signature was made within the certificate's validity period, as long as the timestamp is trusted.
Note: The timestamp, signature workflow, PAdES, and revocation-fetching methods in this section and in Signature Workflow and Validation Reports, PAdES and Long-Term Validation (LTV), and External and Two-Step Signing are synchronous. They read and write the whole file and may perform network requests. Never call them on the main thread: run them on a background queue and return to the main thread to update the UI.
Choose a Timestamp Client
ComPDF SDK talks to a TSA through the CPDFTSAClient protocol and provides two implementations:
| Class | Description |
|---|---|
CPDFURLSessionTSAClient | Recommended. Sends requests through NSURLSession, so App Transport Security (ATS), the system proxy, and VPN settings apply. Replace its session property to add certificate pinning or client certificates. |
CPDFHttpTSAClient | Not recommended for shipping apps. The core library performs a blocking HTTP request itself, bypassing NSURLSession, so ATS, the system proxy, and any certificate pinning in your app do not apply. |
To obtain timestamps through your own networking layer or an offline service, implement the CPDFTSAClient protocol yourself.
Note: requestTimestamp: of CPDFTSAClient is called synchronously on the SDK's internal task queue. A custom implementation should block until the result is available and must never dispatch_sync to the main queue, or the signing call will deadlock.
Add a Timestamp When Signing
In the signing flow from Create Digital Signatures, replace writeSignatureToURL:withWidget:PKCS12Cert:password:location:reason:isSetSignAp:permissions: with the variant that takes a tsaClient: parameter to embed a timestamp while writing the signature. Passing nil for tsaClient behaves like the method without a timestamp. In the example, widgetAnnotation is a signature field created and given an appearance as described in Create Digital Signatures.
This example shows how to create a timestamped digital signature:
// Create a timestamp client (recommended). Requests go through NSURLSession and respect ATS, the system proxy, and VPN settings.
let tsaClient = CPDFURLSessionTSAClient(url: URL(string: "https://tsa.example.com/tsr")!)
tsaClient.timeoutInterval = 30
tsaClient.retryCount = 1
// Signing and the timestamp request are synchronous. Run them on a background queue.
DispatchQueue.global(qos: .userInitiated).async {
let isSuccess = document.writeSignature(to: URL(fileURLWithPath: writeFilePath),
withWidget: widgetAnnotation,
pkcs12Cert: certificatePath,
password: "123",
location: "<your signing location here>",
reason: "I am the owner of the document.",
isSetSignAp: false,
permissions: .none,
tsaClient: tsaClient)
DispatchQueue.main.async {
// Update the UI on the main thread.
print("Signed with timestamp: \(isSuccess)")
}
}// Create a timestamp client (recommended). Requests go through NSURLSession and respect ATS, the system proxy, and VPN settings.
CPDFURLSessionTSAClient *tsaClient = [[CPDFURLSessionTSAClient alloc] initWithURL:[NSURL URLWithString:@"https://tsa.example.com/tsr"]];
tsaClient.timeoutInterval = 30;
tsaClient.retryCount = 1;
// Signing and the timestamp request are synchronous. Run them on a background queue.
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
BOOL isSuccess = [document writeSignatureToURL:[NSURL fileURLWithPath:writeFilePath]
withWidget:widgetAnnotation
PKCS12Cert:certificatePath
password:@"123"
location:@"<your signing location here>"
reason:@"I am the owner of the document."
isSetSignAp:NO
permissions:CPDFSignaturePermissionsNone
tsaClient:tsaClient];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI on the main thread.
NSLog(@"Signed with timestamp: %d", isSuccess);
});
});Read the Timestamp Source of a Signature
The timestampSource property of CPDFSignature tells where the signing time comes from, and hasEmbeddedTimestamp tells whether the signature embeds an RFC 3161 timestamp token.
| Value | Description |
|---|---|
CPDFSignatureTimestampSourceNone | The signature carries no time information. |
CPDFSignatureTimestampSourceLocalClock | The signing time comes from the signing device's clock. |
CPDFSignatureTimestampSourceEmbedded | The signature embeds an RFC 3161 timestamp token. |
CPDFSignatureTimestampSourceDocument | The signature itself is a document timestamp. |
This example shows how to read the timestamp source of signatures:
let document = CPDFDocument(url: URL(fileURLWithPath: signedFilePath))
for signature in document?.signatures() ?? [] {
switch signature.timestampSource {
case .embedded:
// The signature embeds an RFC 3161 timestamp; the TSA attests the signing time.
print("\(signature.name ?? ""): embedded timestamp, token = \(signature.hasEmbeddedTimestamp)")
case .document:
// The signature itself is a document timestamp.
print("\(signature.name ?? ""): document timestamp")
case .localClock:
// The signing time comes from the signing device's local clock, with no third-party proof.
print("\(signature.name ?? ""): local clock")
default:
// The signature carries no time information.
print("\(signature.name ?? ""): no timestamp")
}
}CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:signedFilePath]];
for (CPDFSignature *signature in [document signatures]) {
switch (signature.timestampSource) {
case CPDFSignatureTimestampSourceEmbedded:
// The signature embeds an RFC 3161 timestamp; the TSA attests the signing time.
NSLog(@"%@: embedded timestamp, token = %d", signature.name, signature.hasEmbeddedTimestamp);
break;
case CPDFSignatureTimestampSourceDocument:
// The signature itself is a document timestamp.
NSLog(@"%@: document timestamp", signature.name);
break;
case CPDFSignatureTimestampSourceLocalClock:
// The signing time comes from the signing device's local clock, with no third-party proof.
NSLog(@"%@: local clock", signature.name);
break;
default:
// The signature carries no time information.
NSLog(@"%@: no timestamp", signature.name);
break;
}
}To verify the timestamp itself (whether the digest matches, whether the token signature is valid, and whether the TSA certificate chain is trusted), see the CPDFValidationCategoryTimestamp items described in Signature Workflow and Validation Reports.
Obtain a Standalone Timestamp Token
To timestamp arbitrary data, such as a detached hash, or to test a TSA endpoint before signing, use CPDFTimestampService. It computes the digest, builds the RFC 3161 request, sends it through the TSA client, and returns the DER-encoded timestamp token. You do not need this class to timestamp a signature.
For the digest options that apply to the timestamp request, see Algorithms and Certificate Formats.
This example shows how to obtain a standalone timestamp token:
let tsaClient = CPDFURLSessionTSAClient(url: URL(string: "https://tsa.example.com/tsr")!)
let service = CPDFTimestampService(tsaClient: tsaClient)
let options = CPDFTimestampOptions()
options.digestAlgorithm = .SHA256
options.includeNonce = true
DispatchQueue.global(qos: .userInitiated).async {
// Digest the data and request a timestamp token from the TSA (a synchronous network call).
let response = service.timestampData(fileData, options: options)
if response.isSuccess, let token = response.tokenDER {
// token is the DER-encoded RFC 3161 TimeStampToken. Store or archive it as needed.
print("Token size: \(token.count), genTime: \(response.genTime ?? "")")
} else {
// errorCode is a stable identifier, such as timestamp.transport.*
print("Timestamp failed: \(response.errorCode ?? "") \(response.errorMessage ?? "")")
}
}CPDFURLSessionTSAClient *tsaClient = [[CPDFURLSessionTSAClient alloc] initWithURL:[NSURL URLWithString:@"https://tsa.example.com/tsr"]];
CPDFTimestampService *service = [[CPDFTimestampService alloc] initWithTSAClient:tsaClient];
CPDFTimestampOptions *options = [[CPDFTimestampOptions alloc] init];
options.digestAlgorithm = CPDFSignatureDigestAlgorithmSHA256;
options.includeNonce = YES;
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
// Digest the data and request a timestamp token from the TSA (a synchronous network call).
CPDFTimestampResponse *response = [service timestampData:fileData options:options];
if (response.isSuccess) {
// tokenDER is the DER-encoded RFC 3161 TimeStampToken. Store or archive it as needed.
NSLog(@"Token size: %lu, genTime: %@", (unsigned long)response.tokenDER.length, response.genTime);
} else {
// errorCode is a stable identifier, such as timestamp.transport.*
NSLog(@"Timestamp failed: %@ %@", response.errorCode, response.errorMessage);
}
});Load the AATL and EUTL Trust Lists
ComPDF SDK can load the Adobe Approved Trust List (AATL) and the EU Trusted Lists (EUTL). The first time, call loadAATLAndEUTLWithAATLPath:EUTLPath:cachePath: to parse the trust list files and write the SHA-1 lists of their trusted certificates to a cache. On later launches, call loadAATLAndEUTLFromCachePath: to load from the cache without parsing again. When the trust list files are updated, call the former again to refresh the cache. The SDK does not ship the trust list files; obtain them from the official Adobe and EU sources.
This example shows how to load the AATL and EUTL trust lists:
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] + "/TrustList"
DispatchQueue.global(qos: .utility).async {
// Load from the cache first. If no cache exists yet, parse the trust lists and write the cache.
if !CPDFSignature.loadAATLAndEUTL(fromCachePath: cachePath) {
let isLoaded = CPDFSignature.loadAATLAndEUTL(withAATLPath: aatlPath, eutlPath: eutlPath, cachePath: cachePath)
print("Trust lists loaded: \(isLoaded)")
}
}NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"TrustList"];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
// Load from the cache first. If no cache exists yet, parse the trust lists and write the cache.
if (![CPDFSignature loadAATLAndEUTLFromCachePath:cachePath]) {
BOOL isLoaded = [CPDFSignature loadAATLAndEUTLWithAATLPath:aatlPath EUTLPath:eutlPath cachePath:cachePath];
NSLog(@"Trust lists loaded: %d", isLoaded);
}
});