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

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 a binary string. Write those bytes as they are.

Sign at a chosen level

signPkcs12 and NexPdf::completeExternalSignature take a level option:

levelWhat you getNeeds
B-BSignature only
B-TPlus a signature timestamptsa_url
B-LTPlus certificates, OCSP responses and CRLs (DSS)tsa_url
B-LTAPlus a document timestamptsa_url
use Nexdevit\NexPdf\Document;

$doc = Document::open('contract.pdf');
$signed = $doc->signPkcs12(file_get_contents('signer.p12'), 'p12-password', json_encode([
    'reason' => 'Approved',
    'level' => 'B-LTA',
    'tsa_url' => 'http://timestamp.example.com',
    'timestamp' => ['hash' => 'SHA-256', 'field' => 'Timestamp1'],   // optional
]));
file_put_contents('contract-lta.pdf', $signed);

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.

use Nexdevit\NexPdf\NexPdf;

$data = file_get_contents('contract-signed.pdf');        // a B-T signature
$lta = NexPdf::addDocumentTimestampBytes($data, json_encode([
    'tsa_url' => 'http://timestamp.example.com',
    'ltv' => true,
]));
file_put_contents('contract-lta.pdf', $lta);
OptionDefaultMeaning
tsa_urlrequiredThe RFC 3161 timestamp authority
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

Register a callable with NexPdf::setTsaCallback. It receives the DER TimeStampReq and returns the DER TimeStampResp or TimeStampToken. Every call that needs a timestamp and has no tsa_url then uses it: addDocumentTimestamp, and signPkcs12 or completeExternalSignature at B-T, B-LT or B-LTA. A tsa_url always wins. null unregisters it, and when the callable throws, the call fails and NexPdf::tsaCallbackError() holds the exception. You can also do the HTTP yourself with the step functions on this page: the document timestamp below, and online signing in steps for B-T and B-LT.

Document timestamp in steps

When the timestamp comes from your own service, do it in three steps with no state in between:

$prepared = Document::open('contract-signed.pdf')
    ->prepareDocumentTimestamp('{"field":"Archive2"}');        // options without tsa_url

$request = NexPdf::documentTimestampRequest($prepared, 'SHA-256');   // DER TimeStampReq
$token = $myTsa->post($request, 'application/timestamp-query');      // your call

$stamped = NexPdf::completeDocumentTimestamp($prepared, $token);

$token is the DER TimeStampResp or TimeStampToken. It must verify and match the document. The hash of documentTimestampRequest defaults to SHA-256 (null).

Online signing in steps

When your application does the HTTP itself (an authenticated TSA, a proxy, an HSM service), sign at B-B and add the rest in steps.

B-T. NexPdf::signatureTimestampRequest($data, $hash) returns the DER TimeStampReq over the value of the last signature ($hash null is SHA-256). POST it as application/timestamp-query and give the answer to NexPdf::addSignatureTimestamp($data, $token). The signed bytes are not changed.

$signed = $doc->signPkcs12(file_get_contents('signer.p12'), 'p12-password', '{"level":"B-B"}');
$request = NexPdf::signatureTimestampRequest($signed, 'SHA-256');
$token = $myTsa->post($request, 'application/timestamp-query');     // your call
$bt = NexPdf::addSignatureTimestamp($signed, $token);

B-LT. NexPdf::ltvRequestsJson($data, $responsesJson) 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 NexPdf::addLtvWithResponses($data, $responsesJson) adds the DSS.

$answers = [];
while ($needed = json_decode(NexPdf::ltvRequestsJson($bt, $answers ? json_encode($answers) : null), true)) {
    foreach ($needed as $r) {
        $post = $r['method'] === 'POST';
        $body = @file_get_contents($r['url'], false, stream_context_create(['http' => [
            'method' => $r['method'],
            'header' => $post ? "Content-Type: {$r['content_type']}\r\n" : '',
            'content' => $post ? base64_decode($r['body']) : '',
        ]]));
        if ($body === false) {
            $r['error'] = 'request failed';
        } else {
            $r['response'] = base64_encode($body);
        }
        $answers[] = $r;
    }
}
$blt = NexPdf::addLtvWithResponses($bt, json_encode($answers));

Then add a document timestamp (above) for B-LTA.

Verify

NexPdf::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
foreach (json_decode(NexPdf::verifySignaturesJson(file_get_contents('contract-lta.pdf')), true) as $s) {
    $kind = $s['document_timestamp'] ? 'timestamp' : 'signature';
    echo $kind, ' ', $s['signer'], ' ', $s['pades_level'], ' ',
        $s['digest_ok'] && $s['signature_ok'] ? 'valid' : 'INVALID', "\n";
}

Validate B-LTA

NexPdf::validateLtaJson($data, $trustRootsPem) 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. $trustRootsPem (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 = json_decode(NexPdf::validateLtaJson(file_get_contents('contract-lta.pdf'),
    file_get_contents('roots.pem')), true);
echo $report['complete'] ? "complete\n" : "incomplete\n";
foreach ($report['timestamps'] as $ts) {
    echo $ts['field'], ' ', $ts['time'], ' ', implode(',', $ts['covers']), "\n";   // also valid, has_dss, complete
    foreach ($ts['issues'] as $i) {
        echo '  ', $i['field'], ' ', $i['role'], ' ', $i['subject'], ' ', $i['kind'], "\n";
    }
}
Issue kindMeaning
missing-certificateA certificate of a chain is not in the DSS
missing-revocationNo usable OCSP response or CRL for a certificate
revokedA certificate was revoked
ocsp-bad-signatureThe OCSP response's signature does not verify
ocsp-unauthorized-responderThe responder is neither the issuing CA nor delegated by it
ocsp-untrusted-responderThe responder's chain does not reach the roots
ocsp-not-currentthisUpdate/nextUpdate do not cover the protecting timestamp
ocsp-unknown-statusThe 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.

On this page