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 C++.
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 std::vector<uint8_t>. Write those bytes
as they are.
Sign at a chosen level
sign_pkcs12 and complete_external_signature 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 |
#include <nexpdf.hpp>
#include <nlohmann/json.hpp> // an example; use your JSON library
namespace nexpdf = nexdevit::nexpdf;
auto doc = nexpdf::Document::open("contract.pdf");
nlohmann::json options = {
{"reason", "Approved"},
{"level", "B-LTA"},
{"tsa_url", "http://timestamp.example.com"},
{"timestamp", {{"hash", "SHA-256"}, {"field", "Timestamp1"}}}, // optional
};
auto signed_pdf = doc.sign_pkcs12(read_file("signer.p12"), "p12-password", options.dump());
write_file("contract-lta.pdf", signed_pdf);timestamp configures the document timestamp: hash, field and reserved (bytes kept for the
token).
Add a document timestamp
doc.add_document_timestamp(options_json) timestamps an open document, and
nexpdf::add_document_timestamp_bytes(data, options_json) timestamps the bytes of a file, for
example straight from a signing call.
auto data = read_file("contract-signed.pdf"); // a B-T signature
auto lta = nexpdf::add_document_timestamp_bytes(
data, R"({"tsa_url":"http://timestamp.example.com","ltv":true})");
write_file("contract-lta.pdf", lta);| Option | Default | Meaning |
|---|---|---|
tsa_url | the TSA callback | The RFC 3161 timestamp authority. Required unless a 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
nexpdf::set_tsa_callback(callback) makes a function of yours the time-stamp authority of the
process (an HSM, or an HTTP client with authentication). It receives the DER RFC 3161
TimeStampReq and returns the DER TimeStampResp (or TimeStampToken). NexPDF uses it when no
tsa_url is given, in add_document_timestamp, add_document_timestamp_bytes, and in
sign_pkcs12 and complete_external_signature with level B-T, B-LT or B-LTA. tsa_url always
wins.
nexpdf::set_tsa_callback([](const std::vector<uint8_t>& request) {
// POST the DER TimeStampReq (Content-Type: application/timestamp-query) to your TSA
// and return the DER answer
return my_http_post("https://tsa.example.com", "application/timestamp-query", request);
});
auto lta = nexpdf::add_document_timestamp_bytes(data, R"({"ltv":true})"); // no tsa_url: the callback answers
nexpdf::clear_tsa_callback();The callback is a nexpdf::TsaCallback
(std::function<std::vector<uint8_t>(const std::vector<uint8_t>&)>). It may run on any thread
that calls NexPDF and must not call set_tsa_callback. If it throws, the NexPDF call fails with
"TSA callback failed (code 1)"; an empty answer gives code 2. clear_tsa_callback() (or an empty
function) unregisters it. nexpdf::net::post(url, content_type, body) in nexpdf/sync.hpp is a
small HTTP client you can use for plain http:// authorities.
To do the HTTP yourself without a callback, timestamp in three steps with no state in between:
auto prepared = nexpdf::Document::open("contract-signed.pdf")
.prepare_document_timestamp(R"({"field":"Archive2"})"); // options without tsa_url
auto request = nexpdf::document_timestamp_request(prepared, "SHA-256"); // DER TimeStampReq
std::vector<uint8_t> token = my_tsa.post(request, "application/timestamp-query"); // your call
auto stamped = nexpdf::complete_document_timestamp(prepared, token);token is the DER TimeStampResp or TimeStampToken. It must verify and match the document. The
hash of document_timestamp_request defaults to SHA-256 (std::nullopt).
Online signing in steps
When your application makes the HTTP requests itself, sign at B-B (with reserved bytes for the
timestamp) and finish B-T and B-LT in steps. Add the signature timestamp before any other change
to the file. For B-LTA, add a document timestamp afterwards.
| Call | Result |
|---|---|
nexpdf::signature_timestamp_request(data, hash) | The DER TimeStampReq over the last signature value. POST it as application/timestamp-query |
nexpdf::add_signature_timestamp(data, token) | The file with the token added as the signature timestamp (B-T) |
nexpdf::ltv_requests_json(data, responses_json) | The HTTP requests still needed for B-LT: [{"method","url","content_type","body"}] (body in base64). Pass std::nullopt at first, then the same objects with "response" (base64) or "error" (such as "HTTP 404") added, until it returns [] |
nexpdf::add_ltv_with_responses(data, responses_json) | The file with the DSS built from those answers (B-LT) |
auto signed_pdf = doc.sign_pkcs12(read_file("signer.p12"), "p12-password",
R"({"level":"B-B","reserved":32768})"); // room for the timestamp
auto request = nexpdf::signature_timestamp_request(signed_pdf, "SHA-256");
std::vector<uint8_t> token = my_tsa.post(request, "application/timestamp-query"); // your call
auto bt = nexpdf::add_signature_timestamp(signed_pdf, token);
std::optional<std::string> responses;
for (;;) {
auto pending = nexpdf::ltv_requests_json(bt, responses);
if (pending == "[]") break;
responses = fetch_all(pending); // your calls: each object plus "response" or "error"
}
auto blt = nexpdf::add_ltv_with_responses(bt, responses);Check B-LTA completeness
nexpdf::validate_lta_json(data, trust_roots_pem) checks, for every document timestamp, that the
DSS of the revision it time-stamps holds every certificate and revocation entry for the earlier
signatures, their TSAs and 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.
auto lta = nlohmann::json::parse(nexpdf::validate_lta_json(read_file("contract-lta.pdf"),
read_text("roots.pem")));
std::cout << "complete: " << lta["complete"] << "\n";
for (const auto& ts : lta["timestamps"])
for (const auto& issue : ts["issues"])
std::cout << ts["field"] << " " << issue["role"] << " " << issue["subject"] << " "
<< issue["kind"] << "\n";Each timestamp has field, time, valid, has_dss, complete, covers (the fields it
protects) and issues. Issue kind is one of missing-certificate, missing-revocation,
revoked, ocsp-bad-signature, ocsp-unauthorized-responder, ocsp-untrusted-responder,
ocsp-not-current or ocsp-unknown-status.
Verify
verify_signatures_json 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 |
for (const auto& s : nlohmann::json::parse(nexpdf::verify_signatures_json(read_file("contract-lta.pdf")))) {
const char* kind = s["document_timestamp"].get<bool>() ? "timestamp" : "signature";
bool ok = s["digest_ok"].get<bool>() && s["signature_ok"].get<bool>();
std::cout << kind << " " << s["signer"] << " " << s["pades_level"] << " " << ok << "\n";
}See Digital signatures for signing with PKCS#12 files and external devices.