Long-term signatures (B-LTA)
Sign at PAdES B-LTA, add RFC 3161 document timestamps, renew archive timestamps and timestamp with your own TSA or HSM, with NexPDF for iOS (Swift).
License module
Signature levels and document timestamps need the signatures module (Business tier and above).
Verification needs the same module.
PAdES B-LTA keeps a signature verifiable for decades. On top of a timestamped signature with its validation data (B-LT), it adds a document timestamp: an RFC 3161 timestamp over the whole file, in an invisible signature field. Before the timestamp authority's certificate expires, you add another one (an archive timestamp), and so on.
Every call on this page returns the complete new file as Data. Write that data as it is. The calls
that take a tsa_url contact the timestamp authority over the network and block until it answers:
run them off the main thread.
Sign at a chosen level
signPkcs12(p12:password:optionsJson:) and NexPdf.completeExternalSignature take a level
option:
level | What you get | Needs |
|---|---|---|
B-B | Signature only | |
B-T | Plus a signature timestamp | tsa_url or a TSA callback |
B-LT | Plus certificates, OCSP responses and CRLs (DSS) | tsa_url or a TSA callback |
B-LTA | Plus a document timestamp | tsa_url or a TSA callback |
import NexPdf
let doc = try NexPdfDocument.open(url: contractURL)
let options = """
{
"reason": "Approved",
"level": "B-LTA",
"tsa_url": "http://timestamp.example.com",
"timestamp": { "hash": "SHA-256", "field": "Timestamp1" }
}
"""
let signed = try doc.signPkcs12(p12: try Data(contentsOf: p12URL), password: "p12-password", optionsJson: options)
try signed.write(to: ltaURL)timestamp is optional and configures the document timestamp: hash, field and reserved
(bytes kept for the token).
Add a document timestamp
doc.addDocumentTimestamp(optionsJson:) timestamps an open document, and
NexPdf.addDocumentTimestampBytes(data:optionsJson:) timestamps the bytes of a file, for example
straight from a signing call.
let signedBT = try Data(contentsOf: signedURL) // a B-T signature
let lta = try NexPdf.addDocumentTimestampBytes(data: signedBT,
optionsJson: #"{"tsa_url":"http://timestamp.example.com","ltv":true}"#)
try lta.write(to: ltaURL)| Option | Default | Meaning |
|---|---|---|
tsa_url | required without a TSA callback | The RFC 3161 timestamp authority |
hash | SHA-256 | SHA-256, SHA-384 or SHA-512 |
field | Timestamp1 | Name of the timestamp's signature field |
reserved | 16384 | Bytes reserved for the token |
ltv | First add the certificates, OCSP responses and CRLs of every earlier signature and timestamp to the DSS |
With ltv, the same call turns a B-T signature into B-LTA, and renews the archive timestamp of a
B-LTA file. Run it again before the last timestamp's certificate expires.
Your own TSA or HSM
As a callback
NexPdf.setTsaCallback(_:) registers, for the whole process, a timestamp authority your app
provides: an HSM, or an HTTP client that adds authentication. The function gets the DER RFC 3161
TimeStampReq and returns the DER TimeStampResp or TimeStampToken.
NexPdf.setTsaCallback { request in
try companyTsa.timestamp(request) // Data -> Data, your HSM or HTTP client
}
let signed = try doc.signPkcs12(p12: p12, password: "p12-password", optionsJson: #"{"level":"B-LTA"}"#) // no tsa_url
NexPdf.clearTsaCallback() // tsa_url is needed againThe callback is used when no tsa_url is given: by addDocumentTimestamp,
NexPdf.addDocumentTimestampBytes, and by signPkcs12 and NexPdf.completeExternalSignature at
level B-T, B-LT or B-LTA. A tsa_url always wins. A new callback replaces the earlier one.
- The function is synchronous and runs on the thread that called NexPDF, possibly on several threads at once; call NexPDF off the main thread.
- A thrown error or an empty answer fails the call with "TSA callback failed (code N)".
- It must not call
setTsaCallbackorclearTsaCallback.
In steps
When the timestamp comes from your own service and you prefer no callback, do it in three steps with no state in between:
let doc = try NexPdfDocument.open(url: signedURL)
let prepared = try doc.prepareDocumentTimestamp(optionsJson: #"{"field":"Archive2"}"#) // options without tsa_url
let request = try NexPdf.documentTimestampRequest(prepared: prepared, hash: "SHA-256") // DER TimeStampReq
var post = URLRequest(url: myTsaURL) // your service
post.httpMethod = "POST"
post.setValue("application/timestamp-query", forHTTPHeaderField: "Content-Type")
let (token, _) = try await URLSession.shared.upload(for: post, from: request)
let stamped = try NexPdf.completeDocumentTimestamp(prepared: prepared, token: token)token is the DER TimeStampResp or TimeStampToken. It must verify and match the document.
Online signing in steps
When your application makes the HTTP calls itself, add the signature timestamp (B-T) and the
validation data (B-LT) in steps. Sign at B-B first ("reserved" leaves room for the timestamp).
B-T. NexPdf.signatureTimestampRequest(data:hash:) returns the DER TimeStampReq over the value of the last signature
(a nil hash is SHA-256; the signature must cover the whole file). POST it to the TSA as
application/timestamp-query and store the answer with NexPdf.addSignatureTimestamp(data:token:), before any other change to
the file. The signed bytes are not changed; the token must verify and be issued for that signature.
B-LT. NexPdf.ltvRequestsJson(data:responsesJson:) lists the HTTP requests still needed for the DSS,
[{"method","url","content_type","body"}] with body in base64. Fetch them, give the answers back
(each object plus "response":"<base64>", or "error":"HTTP 404" for one that failed) and call
again until it returns []. Then NexPdf.addLtvWithResponses(data:responsesJson:) adds the DSS; missing answers count as failed.
For B-LTA, add a document timestamp after that.
let signed = try Data(contentsOf: signedURL) // a B-B signature
let request = try NexPdf.signatureTimestampRequest(data: signed) // SHA-256
let token = try await myTsa.post(request, contentType: "application/timestamp-query")
let bt = try NexPdf.addSignatureTimestamp(data: signed, token: token)
var answers: String? = nil
while true {
let needed = NexPdf.json(try NexPdf.ltvRequestsJson(data: bt, responsesJson: answers)) as? [[String: Any]] ?? []
if needed.isEmpty { break }
answers = try await fetchAll(needed) // your HTTP code: each object plus "response" or "error"
}
let blt = try NexPdf.addLtvWithResponses(data: bt, responsesJson: answers)Verify
NexPdf.verifySignaturesJson(data:) reports every signature and every document timestamp. Two
fields are new in 1.1:
| Field | Meaning |
|---|---|
document_timestamp | true for a document timestamp. Then signer is the TSA, digest_ok checks the message imprint and trusted checks the TSA chain against your roots |
pades_level | B-B, B-T, B-LT or B-LTA |
let entries = NexPdf.json(try NexPdf.verifySignaturesJson(data: try Data(contentsOf: ltaURL))) as? [[String: Any]] ?? []
for s in entries {
let kind = s["document_timestamp"] as? Bool == true ? "timestamp" : "signature"
let ok = (s["digest_ok"] as? Bool ?? false) && (s["signature_ok"] as? Bool ?? false)
print(kind, s["signer"] ?? "", s["pades_level"] ?? "", ok)
}Validate B-LTA
NexPdf.validateLtaJson(data:trustRootsPem:) checks, for every document timestamp, whether the DSS of the revision it
timestamps holds every certificate and revocation entry for the earlier signatures, their TSAs and
earlier document timestamps. OCSP responses are verified: signature, responder (the issuing CA or a
certificate it delegated with id-kp-OCSPSigning), responder chain to your roots,
thisUpdate/nextUpdate around the timestamp that protects the DSS, and status. The PEM roots are
optional (roots that need not be in the DSS).
let lta = NexPdf.json(try NexPdf.validateLtaJson(data: try Data(contentsOf: ltaURL),
trustRootsPem: try String(contentsOf: rootsURL, encoding: .utf8))) as? [String: Any] ?? [:]
print("complete:", lta["complete"] ?? false)
for ts in lta["timestamps"] as? [[String: Any]] ?? [] {
for issue in ts["issues"] as? [[String: Any]] ?? [] {
print(ts["field"] ?? "", issue["field"] ?? "", issue["role"] ?? "", issue["kind"] ?? "")
}
}Each timestamp has field, time, valid, has_dss, complete, covers (the fields it
protects) and issues. An issue has field, role (signer, signature-timestamp or
document-timestamp), subject and kind: missing-certificate, missing-revocation, revoked,
ocsp-bad-signature, ocsp-unauthorized-responder, ocsp-untrusted-responder,
ocsp-not-current or ocsp-unknown-status.
See Digital signatures for signing with PKCS#12 files and external devices.