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 Python.
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 bytes. 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 |
import json
import nexpdf
with nexpdf.Document.open("contract.pdf") as doc, open("signer.p12", "rb") as f:
signed = doc.sign_pkcs12(f.read(), "p12-password", json.dumps({
"reason": "Approved",
"level": "B-LTA",
"tsa_url": "http://timestamp.example.com",
"timestamp": {"hash": "SHA-256", "field": "Timestamp1"}, # optional
}))
with open("contract-lta.pdf", "wb") as f:
f.write(signed)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.
data = open("contract-signed.pdf", "rb").read() # a B-T signature
lta = nexpdf.add_document_timestamp_bytes(data, json.dumps({
"tsa_url": "http://timestamp.example.com",
"ltv": True,
}))| Option | Default | Meaning |
|---|---|---|
tsa_url | required unless a TSA callback is set | 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
nexpdf.set_tsa_callback(fn) makes your function the timestamp authority. NexPDF calls it when no
tsa_url is given: by add_document_timestamp and add_document_timestamp_bytes, and by
sign_pkcs12 and complete_external_signature with level B-T, B-LT or B-LTA. fn(request)
receives the DER RFC 3161 TimeStampReq and returns the DER TimeStampResp (the body a TSA answers
to a POST with Content-Type: application/timestamp-query) or a TimeStampToken.
import urllib.request
def my_tsa(request: bytes) -> bytes:
req = urllib.request.Request("https://tsa.example.com", data=request, headers={
"Content-Type": "application/timestamp-query",
"Authorization": "Bearer " + TSA_TOKEN,
})
with urllib.request.urlopen(req) as r:
return r.read()
nexpdf.set_tsa_callback(my_tsa)
signed = doc.sign_pkcs12(p12, "p12-password", '{"level":"B-T"}') # no tsa_url: my_tsa is asked
nexpdf.clear_tsa_callback() # tsa_url is needed againThe callback is process-wide and may run on any thread that calls NexPDF. A tsa_url in the
options always wins. An exception in the function, or an empty answer, fails the call with
"TSA callback failed (code N)". Do not call set_tsa_callback from inside the callback.
Document timestamp in steps
When the timestamp comes from your own service, do it in three steps with no state in between:
with nexpdf.Document.open("contract-signed.pdf") as doc:
prepared = doc.prepare_document_timestamp('{"field":"Archive2"}') # options without tsa_url
request = nexpdf.document_timestamp_request(prepared, "SHA-256") # DER TimeStampReq
token = my_tsa.post(request, content_type="application/timestamp-query") # your call
stamped = nexpdf.complete_document_timestamp(prepared, token)token is the DER TimeStampResp or TimeStampToken. It must verify and match the document.
Online signing in steps
When your application does the HTTP itself (a proxy, an authenticated TSA, a sandbox without
network access for NexPDF), sign at B-B and add the rest in steps.
B-T. signature_timestamp_request(data, hash) returns the DER TimeStampReq over the value of
the last signature (hash None is SHA-256). POST it as application/timestamp-query and give the
answer to add_signature_timestamp(data, token). The signed bytes are not changed.
signed = doc.sign_pkcs12(p12, "p12-password", '{"level":"B-B"}')
request = nexpdf.signature_timestamp_request(signed, "SHA-256")
token = my_tsa(request)
bt = nexpdf.add_signature_timestamp(signed, token)B-LT. ltv_requests_json(data, responses_json) lists the HTTP requests still needed for the
validation data: [{"method", "url", "content_type", "body"}], with body in base64. Send them,
add "response" (base64) or "error" (such as "HTTP 404") to each, and call again with the
answers until it returns []. Then add_ltv_with_responses(data, responses_json) adds the DSS.
import base64, urllib.request
answers = []
while True:
needed = nexpdf.loads(nexpdf.ltv_requests_json(bt, json.dumps(answers) if answers else None))
if not needed:
break
for r in needed:
post = r["method"] == "POST"
req = urllib.request.Request(r["url"], method=r["method"],
data=base64.b64decode(r["body"]) if post else None,
headers={"Content-Type": r["content_type"]} if post else {})
try:
with urllib.request.urlopen(req) as resp:
r["response"] = base64.b64encode(resp.read()).decode()
except Exception as e:
r["error"] = str(e)
answers.append(r)
blt = nexpdf.add_ltv_with_responses(bt, json.dumps(answers))Then add a document timestamp (above) for B-LTA.
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 s in nexpdf.loads(nexpdf.verify_signatures_json(open("contract-lta.pdf", "rb").read())):
kind = "timestamp" if s["document_timestamp"] else "signature"
print(kind, s["signer"], s["pades_level"], s["digest_ok"] and s["signature_ok"])Validate B-LTA
validate_lta_json(data, trust_roots_pem) checks a B-LTA file in full: for every document
timestamp, whether the DSS of the revision it timestamps holds every certificate and revocation
entry needed by the earlier signatures, their timestamp authorities and earlier document
timestamps. trust_roots_pem (optional) lists PEM roots that need not be in the DSS.
OCSP responses are verified: their signature, the responder (the issuing CA or a certificate it
delegated with id-kp-OCSPSigning), the responder's chain to the roots, thisUpdate and
nextUpdate around the timestamp that protects the DSS, and the status.
report = nexpdf.loads(nexpdf.validate_lta_json(open("contract-lta.pdf", "rb").read(),
open("roots.pem").read()))
print("complete" if report["complete"] else "incomplete")
for ts in report["timestamps"]:
print(ts["field"], ts["time"], ts["valid"], ts["has_dss"], ts["complete"], ts["covers"])
for i in ts["issues"]:
print(" ", i["field"], i["role"], i["subject"], i["kind"])Issue 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 | 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 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 answered "unknown" |
role is signer, signature-timestamp or document-timestamp. A rejected OCSP response is
reported with its reason, plus missing-revocation unless other data covers the certificate.
See Digital signatures for signing with PKCS#12 files and external devices.