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 Rust.
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 Vec<u8>. 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 |
use nexpdf_sdk::Document;
use serde_json::json;
fn main() -> nexpdf_sdk::Result<()> {
let doc = Document::open("contract.pdf")?;
let options = json!({
"reason": "Approved",
"level": "B-LTA",
"tsa_url": "http://timestamp.example.com",
"timestamp": {"hash": "SHA-256", "field": "Timestamp1"} // optional
});
let signed = doc.sign_pkcs12(&std::fs::read("signer.p12")?, "p12-password", &options.to_string())?;
std::fs::write("contract-lta.pdf", &signed)?;
Ok(())
}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_sdk::add_document_timestamp_bytes(data, options_json) timestamps the bytes of a file, for
example straight from a signing call.
let data = std::fs::read("contract-signed.pdf")?; // a B-T signature
let lta = nexpdf_sdk::add_document_timestamp_bytes(
&data,
r#"{"tsa_url":"http://timestamp.example.com","ltv":true}"#,
)?;
std::fs::write("contract-lta.pdf", <a)?;| 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
When the timestamp comes from your own service, do it in three steps with no state in between:
let doc = Document::open("contract-signed.pdf")?;
let prepared = doc.prepare_document_timestamp(r#"{"field":"Archive2"}"#)?; // options without tsa_url
let request = nexpdf_sdk::document_timestamp_request(&prepared, "SHA-256")?; // DER TimeStampReq
let token: Vec<u8> = my_tsa.post(&request, "application/timestamp-query"); // your call
let stamped = nexpdf_sdk::complete_document_timestamp(&prepared, &token)?;token is the DER TimeStampResp or TimeStampToken. It must verify and match the document.
A TSA callback
nexpdf_sdk::set_tsa_callback(f) registers, for the whole process, a timestamp authority of your
own: an HSM, or a TSA behind your own HTTP client with authentication. f receives the DER RFC
3161 TimeStampReq and returns Ok with the DER TimeStampResp (or TimeStampToken).
nexpdf_sdk::set_tsa_callback(|request: &[u8]| -> Result<Vec<u8>, std::io::Error> {
my_tsa.post(request, "application/timestamp-query") // your HTTP client or HSM
});
let doc = Document::open("contract.pdf")?;
let signed = doc.sign_pkcs12(&p12, "p12-password", r#"{"level":"B-LTA"}"#)?; // no tsa_url
nexpdf_sdk::clear_tsa_callback(); // tsa_url is needed againThe callback is used when no tsa_url is given: by add_document_timestamp,
add_document_timestamp_bytes, and by sign_pkcs12 and complete_external_signature with level
B-T, B-LT or B-LTA. tsa_url always wins. A later set_tsa_callback replaces the callback.
- It may run on any thread that calls NexPDF, several at once, so it must be
Send + Sync. - An
Err, or a panic (which is caught), fails the NexPDF call with "TSA callback failed (code N)", N beingTSA_CALLBACK_ERROR(1) orTSA_CALLBACK_PANIC(2). - It must not call
set_tsa_callbackorclear_tsa_callback.
Online signing in steps
When your application makes the HTTP calls itself, add the signature timestamp and the validation data in steps.
B-T. signature_timestamp_request(data, hash) returns the DER TimeStampReq over the value of
the last signature (None is SHA-256; the signature must cover the whole file). POST it to the TSA
as application/timestamp-query and store the answer with add_signature_timestamp(data, token):
let signed = std::fs::read("contract-signed.pdf")?; // a B-B signature
let request = nexpdf_sdk::signature_timestamp_request(&signed, None)?;
let token: Vec<u8> = my_tsa.post(&request, "application/timestamp-query");
let bt = nexpdf_sdk::add_signature_timestamp(&signed, &token)?; // signed bytes unchangedB-LT. ltv_requests_json(data, responses_json) 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 add_ltv_with_responses(data, responses_json) adds the DSS:
let mut answers = serde_json::json!([]);
loop {
let needed = nexpdf_sdk::ltv_requests_json_value(&bt, &answers.to_string())?; // `serde`
let needed = needed.as_array().cloned().unwrap_or_default();
if needed.is_empty() { break; }
for mut r in needed {
r["response"] = my_http.fetch(&r).into(); // base64 of the answer body
answers.as_array_mut().unwrap().push(r);
}
}
let lt = nexpdf_sdk::add_ltv_with_responses(&bt, &answers.to_string())?;Pass None as responses_json on the first call. Missing answers count as failed.
Verify
verify_signatures_json reports every signature and every document timestamp. Two fields
describe the level:
| 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 data = std::fs::read("contract-lta.pdf")?;
for s in nexpdf_sdk::verify_signatures_json_value(&data)?.as_array().into_iter().flatten() {
let kind = if s["document_timestamp"].as_bool() == Some(true) { "timestamp" } else { "signature" };
let ok = s["digest_ok"].as_bool() == Some(true) && s["signature_ok"].as_bool() == Some(true);
println!("{kind} {} {} {ok}", s["signer"], s["pades_level"]);
}Validate B-LTA
validate_lta_json(data, trust_roots_pem) checks, for every document timestamp, whether the DSS of
the revision it timestamps holds every certificate and revocation entry (OCSP or CRL) for the
earlier signatures, their timestamp authorities and earlier document timestamps. trust_roots_pem
(PEM roots that need not be in the DSS) may be None.
let data = std::fs::read("contract-lta.pdf")?;
let roots = std::fs::read_to_string("roots.pem")?;
let lta = nexpdf_sdk::validate_lta_json_value(&data, &roots)?; // `serde`; or validate_lta_json
println!("complete: {}", lta["complete"]);
for t in lta["timestamps"].as_array().into_iter().flatten() {
for i in t["issues"].as_array().into_iter().flatten() {
println!("{} {} {} {}", t["field"], i["role"], i["subject"], i["kind"]);
}
}Each timestamp has field, time, valid, has_dss, complete, covers (the fields it
protects) and issues. Each issue has field, role (signer, signature-timestamp or
document-timestamp), subject and kind.
OCSP responses are verified: the signature, the responder (the issuing CA, or a certificate it
delegated with id-kp-OCSPSigning), the responder's chain to the roots, thisUpdate/nextUpdate
around the timestamp that protects the DSS, and the status. A rejected response is reported with
its reason, plus missing-revocation unless other data covers the certificate.
kind | Meaning |
|---|---|
missing-certificate | A certificate of a chain is not in the DSS |
missing-revocation | No usable OCSP response or CRL for a certificate |
revoked | The certificate is revoked |
ocsp-bad-signature | The OCSP response's signature does not verify |
ocsp-unauthorized-responder | The responder is neither the issuing CA nor delegated by it |
ocsp-untrusted-responder | The responder's chain does not reach the roots |
ocsp-not-current | thisUpdate/nextUpdate do not cover the protecting timestamp |
ocsp-unknown-status | The responder does not know the certificate |
See Digital signatures for signing with PKCS#12 files and external devices.