Skip to Content
NexPDF

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 Go.

English

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 CompleteExternalSignature take a level option:

levelWhat you getNeeds
B-BSignature only
B-TPlus a signature timestamptsa_url or a TSA callback
B-LTPlus certificates, OCSP responses and CRLs (DSS)tsa_url or a TSA callback
B-LTAPlus a document timestamptsa_url or a TSA callback
doc, err := nexpdf.Open("contract.pdf")
if err != nil {
	log.Fatal(err)
}
defer doc.Close()

p12, _ := os.ReadFile("signer.p12")
options, _ := json.Marshal(map[string]any{
	"reason":    "Approved",
	"level":     "B-LTA",
	"tsa_url":   "http://timestamp.example.com",
	"timestamp": map[string]any{"hash": "SHA-256", "field": "Timestamp1"}, // optional
})
signed, err := doc.SignPKCS12(p12, "p12-password", string(options))
if err != nil {
	log.Fatal(err)
}
os.WriteFile("contract-lta.pdf", signed, 0o644)

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 nexpdf.AddDocumentTimestampBytes(data, optionsJson) timestamps the bytes of a file, for example straight from a signing call.

data, _ := os.ReadFile("contract-signed.pdf") // a B-T signature
lta, err := nexpdf.AddDocumentTimestampBytes(data,
	`{"tsa_url":"http://timestamp.example.com","ltv":true}`)
if err != nil {
	log.Fatal(err)
}
os.WriteFile("contract-lta.pdf", lta, 0o644)
OptionDefaultMeaning
tsa_urlthe TSA callbackThe RFC 3161 timestamp authority. Required unless a TSA callback is set
hashSHA-256SHA-256, SHA-384 or SHA-512
fieldTimestamp1Name of the timestamp's signature field
reserved16384Bytes reserved for the token
ltvFirst 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.SetTsaCallback(fn) 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 AddDocumentTimestamp, AddDocumentTimestampBytes, and in SignPKCS12 and CompleteExternalSignature with level B-T, B-LT or B-LTA. tsa_url always wins.

nexpdf.SetTsaCallback(func(request []byte) ([]byte, error) {
	req, _ := http.NewRequest("POST", "https://tsa.example.com", bytes.NewReader(request))
	req.Header.Set("Content-Type", "application/timestamp-query")
	req.Header.Set("Authorization", "Bearer "+tsaToken)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	return io.ReadAll(res.Body)
})
defer nexpdf.ClearTsaCallback()

lta, err := nexpdf.AddDocumentTimestampBytes(data, `{"ltv":true}`) // no tsa_url: the callback answers

The function may run on any goroutine that calls NexPDF, at the same time as other calls, and must not call SetTsaCallback or ClearTsaCallback. An error, a panic or an empty answer fails that NexPDF call with "TSA callback failed (code N)". ClearTsaCallback() (or SetTsaCallback(nil)) unregisters it.

To do the HTTP yourself without a callback, timestamp in three steps with no state in between:

doc, err := nexpdf.Open("contract-signed.pdf")
if err != nil {
	log.Fatal(err)
}
prepared, err := doc.PrepareDocumentTimestamp(`{"field":"Archive2"}`) // options without tsa_url
doc.Close()
if err != nil {
	log.Fatal(err)
}

request, err := nexpdf.DocumentTimestampRequest(prepared, "SHA-256") // DER TimeStampReq; "" is SHA-256
if err != nil {
	log.Fatal(err)
}
token := myTSA.Post(request, "application/timestamp-query") // your call

stamped, err := nexpdf.CompleteDocumentTimestamp(prepared, token)

token is the DER TimeStampResp or TimeStampToken. It must verify and match the document.

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.

CallResult
SignatureTimestampRequest(data, hash)The DER TimeStampReq over the last signature value. POST it as application/timestamp-query
AddSignatureTimestamp(data, token)The file with the token added as the signature timestamp (B-T)
LTVRequestsJSON(data, responsesJSON)The HTTP requests still needed for B-LT: [{"method","url","content_type","body"}] (body in base64). Pass "" at first, then the same objects with "response" (base64) or "error" (such as "HTTP 404") added, until it returns []
AddLTVWithResponses(data, responsesJSON)The file with the DSS built from those answers (B-LT)
signed, _ := doc.SignPKCS12(p12, "p12-password", `{"level":"B-B","reserved":32768}`) // room for the timestamp

request, err := nexpdf.SignatureTimestampRequest(signed, "SHA-256")
if err != nil {
	log.Fatal(err)
}
token := myTSA.Post(request, "application/timestamp-query") // your call
bt, err := nexpdf.AddSignatureTimestamp(signed, token)
if err != nil {
	log.Fatal(err)
}

responses := ""
for {
	pending, err := nexpdf.LTVRequestsJSON(bt, responses)
	if err != nil {
		log.Fatal(err)
	}
	if pending == "[]" {
		break
	}
	responses = fetchAll(pending) // your calls: each object plus "response" or "error"
}
blt, err := nexpdf.AddLTVWithResponses(bt, responses)

Check B-LTA completeness

nexpdf.ValidateLtaJSON(data, trustRootsPEM) 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.

s, err := nexpdf.ValidateLtaJSON(data, string(rootsPEM))
if err != nil {
	log.Fatal(err)
}
var lta struct {
	Complete   bool `json:"complete"`
	Timestamps []struct {
		Field  string   `json:"field"`
		Time   string   `json:"time"`
		Valid  bool     `json:"valid"`
		HasDSS bool     `json:"has_dss"`
		Covers []string `json:"covers"`
		Issues []struct {
			Field, Role, Subject, Kind string
		} `json:"issues"`
	} `json:"timestamps"`
}
json.Unmarshal([]byte(s), &lta)

Each timestamp also has complete. 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

VerifySignaturesJSON reports every signature and every document timestamp. Two fields are new in 1.1:

FieldMeaning
document_timestamptrue 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_levelB-B, B-T, B-LT or B-LTA
data, _ := os.ReadFile("contract-lta.pdf")
s, err := nexpdf.VerifySignaturesJSON(data)
if err != nil {
	log.Fatal(err)
}
var entries []struct {
	Signer            string `json:"signer"`
	PadesLevel        string `json:"pades_level"`
	DocumentTimestamp bool   `json:"document_timestamp"`
	DigestOK          bool   `json:"digest_ok"`
	SignatureOK       bool   `json:"signature_ok"`
}
json.Unmarshal([]byte(s), &entries)
for _, e := range entries {
	kind := "signature"
	if e.DocumentTimestamp {
		kind = "timestamp"
	}
	fmt.Println(kind, e.Signer, e.PadesLevel, e.DigestOK && e.SignatureOK)
}

See Digital signatures for signing with PKCS#12 files and external devices.

On this page