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 .NET.
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 NexPdfSdk.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 |
using System.Text.Json;
using Nexdevit.NexPdf;
var options = JsonSerializer.Serialize(new
{
reason = "Approved",
level = "B-LTA",
tsa_url = "http://timestamp.example.com",
timestamp = new { hash = "SHA-256", field = "Timestamp1" }, // optional
});
using var doc = NexPdfDocument.Open("contract.pdf");
var signed = doc.SignPkcs12(File.ReadAllBytes("signer.p12"), "p12-password", options);
File.WriteAllBytes("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
NexPdfSdk.AddDocumentTimestampBytes(data, optionsJson) timestamps the bytes of a file, for example
straight from a signing call.
var data = File.ReadAllBytes("contract-signed.pdf"); // a B-T signature
var lta = NexPdfSdk.AddDocumentTimestampBytes(data,
"""{"tsa_url":"http://timestamp.example.com","ltv":true}""");
File.WriteAllBytes("contract-lta.pdf", lta);| 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
A TSA callback
NexPdfSdk.SetTsaCallback(callback) registers a timestamp authority of your own for the whole
process: an HSM, a signing service, or a TSA behind your own HTTP client (with authentication or a
proxy). The callback receives the DER RFC 3161 TimeStampReq and returns the DER TimeStampResp
or TimeStampToken.
using var http = new HttpClient();
NexPdfSdk.SetTsaCallback(request =>
{
var content = new ByteArrayContent(request);
content.Headers.ContentType = new("application/timestamp-query");
using var response = http.PostAsync("https://tsa.example.com", content).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
return response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
});
var signed = doc.SignPkcs12(File.ReadAllBytes("signer.p12"), "p12-password", """{"level":"B-LTA"}""");
NexPdfSdk.ClearTsaCallback(); // B-T and later levels need tsa_url againThe callback is used when no tsa_url is given: by AddDocumentTimestamp,
NexPdfSdk.AddDocumentTimestampBytes, and by SignPkcs12 and
NexPdfSdk.CompleteExternalSignature at B-T, B-LT or B-LTA. A tsa_url always wins. The
callback is synchronous and may run on any thread that calls NexPDF; do not call SetTsaCallback
from inside it. An exception, or an empty answer, fails the call with "TSA callback failed
(code 1)".
In steps, with your own HTTP
When the timestamp comes from your own service, do it in three steps with no state in between:
byte[] prepared;
using (var doc = NexPdfDocument.Open("contract-signed.pdf"))
prepared = doc.PrepareDocumentTimestamp("""{"field":"Archive2"}"""); // options without tsa_url
var request = NexPdfSdk.DocumentTimestampRequest(prepared, "SHA-256"); // DER TimeStampReq
using var http = new HttpClient();
var content = new ByteArrayContent(request);
content.Headers.ContentType = new("application/timestamp-query");
var response = await http.PostAsync("https://tsa.example.com", content); // your TSA
var token = await response.Content.ReadAsByteArrayAsync();
var stamped = NexPdfSdk.CompleteDocumentTimestamp(prepared, token);token is the DER TimeStampResp or TimeStampToken. It must verify and match the document. The
hash of DocumentTimestampRequest defaults to SHA-256.
Online signing in steps
The signature timestamp (B-T) and the validation data (B-LT) can also be added in steps, when your
application does the HTTP itself. Sign at B-B first, with reserved bytes to leave room for the
timestamp, then:
using System.Text.Json.Nodes;
var data = doc.SignPkcs12(p12, "p12-password", """{"level":"B-B","reserved":32768}""");
// B-T: time-stamp the last signature (before any other change to the file)
var tsReq = NexPdfSdk.SignatureTimestampRequest(data, "SHA-256"); // POST as application/timestamp-query
var tsToken = await PostAsync("https://tsa.example.com", "application/timestamp-query", tsReq);
data = NexPdfSdk.AddSignatureTimestamp(data, tsToken);
// B-LT: fetch what the DSS still needs, until nothing is missing
var answered = new JsonArray();
while (true)
{
var pending = JsonNode.Parse(NexPdfSdk.LtvRequestsJson(data, answered.ToJsonString()))!.AsArray();
if (pending.Count == 0) break;
foreach (var r in pending)
{
var body = Convert.FromBase64String((string)r!["body"]!);
var answer = await PostAsync((string)r["url"]!, (string)r["content_type"]!, body);
var done = r.DeepClone();
done["response"] = Convert.ToBase64String(answer); // or "error": "HTTP 404"
answered.Add(done);
}
}
data = NexPdfSdk.AddLtvWithResponses(data, answered.ToJsonString());PostAsync stands for your own HTTP call. LtvRequestsJson returns the requests still needed,
[{"method","url","content_type","body"}] with a base64 body; give back the same objects with
response (base64) or error. For B-LTA, add a document timestamp afterwards.
Check a B-LTA file
NexPdfSdk.ValidateLtaJson(data, trustRootsPem) checks, for each document timestamp, that the DSS
of the revision it time-stamps holds every certificate and revocation entry needed for the earlier
signatures, their TSAs and the earlier document timestamps. OCSP responses are verified: their
signature, the responder (the issuing CA or a delegated id-kp-OCSPSigning certificate), the
responder's chain to your roots, thisUpdate and nextUpdate around the protecting timestamp, and
the status.
var lta = JsonNode.Parse(NexPdfSdk.ValidateLtaJson(File.ReadAllBytes("contract-lta.pdf"),
File.ReadAllText("roots.pem")))!;
Console.WriteLine($"complete: {lta["complete"]}");
foreach (var t in lta["timestamps"]!.AsArray())
foreach (var i in t!["issues"]!.AsArray())
Console.WriteLine($"{t["field"]} {t["time"]}: {i!["kind"]} {i["role"]} {i["subject"]}");Each timestamp has field, time, valid, has_dss, complete, covers (the fields it
protects) and issues (field, role, subject, kind).
kind | Meaning |
|---|---|
missing-certificate | A certificate is not in the DSS |
missing-revocation | No OCSP response or CRL for a certificate |
revoked | A certificate was revoked |
ocsp-bad-signature | The OCSP response's signature does not verify |
ocsp-unauthorized-responder | The responder is neither the issuing CA nor a delegated OCSP signer |
ocsp-untrusted-responder | The responder's chain does not reach your roots |
ocsp-not-current | The response is not current at the protecting timestamp |
ocsp-unknown-status | The response gives the status as unknown |
Verify
NexPdfSdk.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 |
using System.Text.Json.Nodes;
foreach (var s in JsonNode.Parse(NexPdfSdk.VerifySignaturesJson(File.ReadAllBytes("contract-lta.pdf")))!.AsArray())
{
var kind = s!["document_timestamp"]!.GetValue<bool>() ? "timestamp" : "signature";
var ok = s["digest_ok"]!.GetValue<bool>() && s["signature_ok"]!.GetValue<bool>();
Console.WriteLine($"{kind} {s["signer"]} {s["pades_level"]} {ok}");
}See Digital signatures for signing with PKCS#12 files and external devices.