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 Java.
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 byte[]. Write those bytes as they are.
Sign at a chosen level
signPkcs12 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 |
String options = """
{"reason":"Approved",
"level":"B-LTA",
"tsa_url":"http://timestamp.example.com",
"timestamp":{"hash":"SHA-256","field":"Timestamp1"}}
"""; // "timestamp" is optional
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("contract.pdf"))) {
byte[] signed = doc.signPkcs12(Files.readAllBytes(Path.of("signer.p12")), "p12-password", options);
Files.write(Path.of("contract-lta.pdf"), signed);
}timestamp 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.
byte[] data = Files.readAllBytes(Path.of("contract-signed.pdf")); // a B-T signature
byte[] lta = NexPdf.addDocumentTimestampBytes(data,
"{\"tsa_url\":\"http://timestamp.example.com\",\"ltv\":true}");
Files.write(Path.of("contract-lta.pdf"), lta);| Option | Default | Meaning |
|---|---|---|
tsa_url | the TSA callback | The RFC 3161 timestamp authority. Required when no TSA callback is set |
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(tsa) registers, process-wide, a timestamp authority your application
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 -> companyTsa.timestamp(request)); // byte[] -> byte[]
byte[] signed = doc.signPkcs12(p12, "p12-password", "{\"level\":\"B-LTA\"}"); // no tsa_url needed
NexPdf.clearTsaCallback();The 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. The callback may run on any thread that calls
NexPDF and must not call setTsaCallback. An exception, or a null or empty answer, fails the
call with "TSA callback failed (code N)". A new callback replaces the earlier one.
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:
byte[] prepared;
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("contract-signed.pdf"))) {
prepared = doc.prepareDocumentTimestamp("{\"field\":\"Archive2\"}"); // options without tsa_url
}
byte[] request = NexPdf.documentTimestampRequest(prepared, "SHA-256"); // DER TimeStampReq
byte[] token = myTsa.post(request, "application/timestamp-query"); // your call
byte[] stamped = NexPdf.completeDocumentTimestamp(prepared, token);token is the DER TimeStampResp or TimeStampToken. It must verify and match the document.
null as the hash of documentTimestampRequest means SHA-256.
Online signing in steps
For hosts that make the HTTP calls themselves, the signature timestamp (B-T) and the validation data (B-LT) can also be added in steps:
// B-T: a DER TimeStampReq over the last signature value (null hash = SHA-256)
byte[] request = NexPdf.signatureTimestampRequest(signed, null);
byte[] token = myTsa.post(request, "application/timestamp-query");
byte[] bt = NexPdf.addSignatureTimestamp(signed, token);
// B-LT: the HTTP requests still needed, [{"method","url","content_type","body":<base64>}]
ObjectMapper json = new ObjectMapper();
String responses = null;
String requests;
while (!json.readTree(requests = NexPdf.ltvRequestsJson(bt, responses)).isEmpty()) {
// your HTTP code: the same objects plus "response":<base64>, or "error":"HTTP 404"
responses = fetchAll(requests);
}
byte[] blt = NexPdf.addLtvWithResponses(bt, responses);addSignatureTimestamp stores the token in the last signature without changing the signed bytes.
The token must verify and be issued for that signature. Missing answers count as failed in
addLtvWithResponses.
Verify
NexPdf.verifySignaturesJson 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 |
ObjectMapper json = new ObjectMapper(); // Jackson, one JSON library among many
byte[] data = Files.readAllBytes(Path.of("contract-lta.pdf"));
for (JsonNode s : json.readTree(NexPdf.verifySignaturesJson(data))) {
String kind = s.get("document_timestamp").asBoolean() ? "timestamp" : "signature";
boolean ok = s.get("digest_ok").asBoolean() && s.get("signature_ok").asBoolean();
System.out.println(kind + " " + s.get("signer").asText() + " " + s.get("pades_level").asText() + " " + 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.
JsonNode lta = json.readTree(NexPdf.validateLtaJson(data, Files.readString(Path.of("roots.pem"))));
System.out.println("complete: " + lta.get("complete"));
for (JsonNode ts : lta.get("timestamps")) {
for (JsonNode issue : ts.get("issues")) {
System.out.println(ts.get("field").asText() + " " + issue.get("field").asText() + " "
+ issue.get("role").asText() + " " + issue.get("kind").asText());
}
}Each timestamp has field, time, valid, has_dss, complete, covers (the fields it
protects) and issues. An issue kind is 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.