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

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 Uint8List. Write those bytes as they are.

Sign at a chosen level

nexpdf_sign_pkcs12 and nexpdf_complete_external_signature take a level option. Signing has no Dart method; call it through dart:ffi as in Digital signatures:

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
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';

// NexPdfBytes nexpdf_sign_pkcs12(const NexPdfDocument *doc, const uint8_t *p12, size_t p12_len,
//     const char *password, const char *options_json);
final signPkcs12 = nexpdfLib.lookupFunction<
    NexPdfBytes Function(Pointer<Void>, Pointer<Uint8>, Size, Pointer<Utf8>, Pointer<Utf8>),
    NexPdfBytes Function(Pointer<Void>, Pointer<Uint8>, int, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_sign_pkcs12');

final options = jsonEncode({
  'reason': 'Approved',
  'level': 'B-LTA',
  'tsa_url': 'http://timestamp.example.com',
  'timestamp': {'hash': 'SHA-256', 'field': 'Timestamp1'},   // optional
});

final signed = using((a) => nexpdfCall(() => takeBytes(signPkcs12(doc.handle, toNative(p12, a), p12.length,
    'p12-password'.toNativeUtf8(allocator: a), options.toNativeUtf8(allocator: a)))));
await File(ltaPath).writeAsBytes(signed);

timestamp configures the document timestamp: hash, field and reserved (bytes kept for the token).

Add a document timestamp

doc.addDocumentTimestamp(options) timestamps an open document, and sdk.addDocumentTimestampBytes(bytes, options) timestamps the bytes of a file, for example straight from a signing call.

final data = await File(signedPath).readAsBytes();   // a B-T signature
final lta = sdk.addDocumentTimestampBytes(data, {
  'tsa_url': 'http://timestamp.example.com',
  'ltv': true,
});
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. The engine contacts the TSA itself, so the device needs network access.

Your own TSA or HSM

The Flutter SDK has no TSA callback binding (nexpdf_set_tsa_callback is not wrapped). Use a tsa_url, or do the network part in your own Dart code: the three steps below for a document timestamp, and online signing in steps for the signature timestamp and the validation data.

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

final doc = sdk.open(await File(signedPath).readAsBytes());
final prepared = doc.prepareDocumentTimestamp(options: {'field': 'Archive2'});   // options without tsa_url
doc.dispose();

final request = sdk.documentTimestampRequest(prepared, hash: 'SHA-256');   // DER TimeStampReq
final token = await myTsa.post(request, contentType: 'application/timestamp-query');   // your call

final stamped = sdk.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 calls itself, add the signature timestamp (B-T) and the validation data (B-LT) in steps. Sign at B-B first ("reserved" leaves room for the timestamp).

B-T. signature_timestamp_request returns the DER TimeStampReq over the value of the last signature (a null hash 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, before any other change to the file. The signed bytes are not changed; the token must verify and be issued for that signature.

B-LT. ltv_requests_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 adds the DSS; missing answers count as failed. For B-LTA, add a document timestamp after that.

These four functions have no typed Dart method yet; call them through sdk.invoke. Bytes go in as Uint8List and come back as base64 strings.

final signed = await File(signedPath).readAsBytes();   // a B-B signature
final request = base64Decode(sdk.invoke('signature_timestamp_request', [signed, null]) as String);
final token = await myTsa.post(request, contentType: 'application/timestamp-query');   // your call
final bt = base64Decode(sdk.invoke('add_signature_timestamp', [signed, token]) as String);

String? answers;
for (;;) {
  final needed = jsonDecode(sdk.invoke('ltv_requests_json', [bt, answers]) as String) as List;
  if (needed.isEmpty) break;
  answers = jsonEncode(await fetchAll(needed));   // your HTTP code: each object plus response or error
}
final blt = base64Decode(sdk.invoke('add_ltv_with_responses', [bt, answers]) as String);

Verify

nexpdf_verify_signatures_json 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
// char *nexpdf_verify_signatures_json(const uint8_t *data, size_t len);
final verify = nexpdfLib.lookupFunction<Pointer<Utf8> Function(Pointer<Uint8>, Size),
    Pointer<Utf8> Function(Pointer<Uint8>, int)>('nexpdf_verify_signatures_json');

final json = using((a) => nexpdfCall(() => takeString(verify(toNative(lta, a), lta.length))));
for (final s in (jsonDecode(json ?? '[]') as List).cast<Map<String, Object?>>()) {
  final kind = s['document_timestamp'] == true ? 'timestamp' : 'signature';
  print('$kind ${s['signer']} ${s['pades_level']} ${s['digest_ok'] == true && s['signature_ok'] == true}');
}

Validate B-LTA

validate_lta_json (through sdk.invoke, with the file and optional PEM roots) 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. The PEM roots are optional (roots that need not be in the DSS).

final lta = jsonDecode(sdk.invoke('validate_lta_json', [await File(ltaPath).readAsBytes(),
    await File(rootsPath).readAsString()]) as String) as Map<String, Object?>;
print('complete: ${lta['complete']}');
for (final ts in lta['timestamps'] as List) {
  for (final issue in ts['issues'] as List) {
    print('${ts['field']} ${issue['field']} ${issue['role']} ${issue['kind']}');
  }
}

Each timestamp has field, time, valid, has_dss, complete, covers (the fields it protects) and issues. An issue has field, role (signer, signature-timestamp or document-timestamp), subject and kind: missing-certificate, missing-revocation, revoked, ocsp-bad-signature, ocsp-unauthorized-responder, ocsp-untrusted-responder, ocsp-not-current or ocsp-unknown-status.

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

On this page