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 Android.
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 a ByteArray. Write those bytes as they
are. An empty array means the call failed; NexPdfJni.lastError() says why. Calls with tsa_url
or ltv go to the network: add the INTERNET permission to your manifest and run them off the main
thread.
Sign at a chosen level
NexPdfJni.signPkcs12 and 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 |
import com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
val options = """
{
"reason": "Approved",
"level": "B-LTA",
"tsa_url": "http://timestamp.example.com",
"timestamp": { "hash": "SHA-256", "field": "Timestamp1" }
}
"""
val signed = NexPdfDocument.openBytes(contract).use { doc ->
NexPdfJni.signPkcs12(doc.handle(), p12Bytes, "p12-password", options)
}
check(signed.isNotEmpty()) { NexPdfJni.lastError() }
File(filesDir, "contract-lta.pdf").writeBytes(signed)timestamp is optional and 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
NexPdfJni.addDocumentTimestampBytes(pdfBytes, optionsJson) timestamps the bytes of a file, for
example straight from a signing call.
val data = File(filesDir, "contract-signed.pdf").readBytes() // a B-T signature
val lta = NexPdfJni.addDocumentTimestampBytes(data, """{"tsa_url":"http://timestamp.example.com","ltv":true}""")
check(lta.isNotEmpty()) { NexPdfJni.lastError() }| 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
NexPdfTsa.setCallback(callback) registers, process-wide, a timestamp authority your app provides:
an HSM, or an HTTP client that adds authentication. The callback gets the DER RFC 3161
TimeStampReq and returns the DER TimeStampResp or TimeStampToken.
import com.nexdevit.nexpdf.NexPdfTsa
NexPdfTsa.setCallback { request -> companyTsa.timestamp(request) } // ByteArray -> ByteArray
val signed = NexPdfJni.signPkcs12(doc.handle(), p12Bytes, "p12-password", """{"level":"B-LTA"}""") // no tsa_url needed
NexPdfTsa.clearCallback()The callback is used when no tsa_url is given, by addDocumentTimestamp,
NexPdfJni.addDocumentTimestampBytes, and by NexPdfJni.signPkcs12 and
completeExternalSignature at level B-T, B-LT or B-LTA. A tsa_url always wins. It runs on the
thread that called NexPDF, possibly several at once, and must not call NexPdfTsa. Throwing, or
returning null or an empty array, 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:
val prepared = NexPdfDocument.openBytes(signedBytes).use { doc ->
doc.prepareDocumentTimestamp("""{"field":"Archive2"}""") // options without tsa_url
}
val request = NexPdfJni.documentTimestampRequest(prepared, "SHA-256") // DER TimeStampReq
val token = myTsa.post(request, "application/timestamp-query") // your call
val stamped = NexPdfJni.completeDocumentTimestamp(prepared, token)
check(stamped.isNotEmpty()) { NexPdfJni.lastError() }token is the DER TimeStampResp or TimeStampToken. It must verify and match the document.
documentTimestampRequest takes null for SHA-256.
Calls without a typed method
NexPdfJni has no typed method for online signing in steps or for B-LTA validation. Reach these
engine calls through NexPdfJni.invoke, the whole C API as one JSON call (bytes as base64):
| C function | Does |
|---|---|
signature_timestamp_request(data, hash) | B-T: a DER TimeStampReq over the last signature value (null hash = SHA-256), to POST as application/timestamp-query |
add_signature_timestamp(data, token) | Stores the TSA's answer in the last signature without changing the signed bytes |
ltv_requests_json(data, responses_json) | B-LT: the HTTP requests still needed, [{"method","url","content_type","body"}]. Call again with the same objects plus "response" (base64) or "error" until it returns [] |
add_ltv_with_responses(data, responses_json) | Adds the DSS from those answers |
validate_lta_json(data, trust_roots_pem) | Full B-LTA validation, with OCSP checks |
import android.util.Base64
import org.json.JSONArray
import org.json.JSONObject
fun call(fn: String, vararg args: Any?): Any? {
val request = JSONObject().put("fn", fn).put("args", JSONArray(args.toList()))
val response = JSONObject(NexPdfJni.invoke(request.toString()))
if (!response.getBoolean("ok")) error(response.getString("error"))
return response.opt("result")
}
fun b64(bytes: ByteArray) = Base64.encodeToString(bytes, Base64.NO_WRAP)
fun unb64(text: Any?) = Base64.decode(text as String, Base64.DEFAULT)
val tsReq = unb64(call("signature_timestamp_request", b64(signed), null))
val token = myTsa.post(tsReq, "application/timestamp-query") // your call
val bt = unb64(call("add_signature_timestamp", b64(signed), b64(token)))
val lta = JSONObject(call("validate_lta_json", b64(ltaBytes), rootsPem) as String)
Log.i("NexPDF", "complete: ${lta.getBoolean("complete")}")validate_lta_json 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. It returns
complete and timestamps, each with field, time, valid, has_dss, complete, covers and
issues (field, role, subject, kind). 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.
Verify
NexPdfJni.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 |
val sigs = JSONArray(NexPdfJni.verifySignaturesJson(File(filesDir, "contract-lta.pdf").readBytes()))
for (i in 0 until sigs.length()) {
val s = sigs.getJSONObject(i)
val kind = if (s.optBoolean("document_timestamp")) "timestamp" else "signature"
Log.i("NexPDF", "$kind ${s.optString("signer")} ${s.optString("pades_level")} ${s.optBoolean("digest_ok") && s.optBoolean("signature_ok")}")
}See Digital signatures for signing with PKCS#12 files and external devices.