verifyTimestamp method

TimestampVerification verifyTimestamp({
  1. Uint8List? responseDer,
  2. Uint8List? tokenDer,
  3. required Uint8List hash,
  4. required List<X509Certificate> anchors,
  5. String hashAlgorithm = 'SHA256',
  6. BigInt? nonce,
  7. String? policyOid,
  8. List<X509Certificate> intermediates = const [],
  9. bool checkSignerName = false,
})

Verifies a time-stamp token against hash and a set of trust anchors.

Pass either responseDer (the whole TimeStampResp) or tokenDer (the bare token). hash, hashAlgorithm, nonce and policyOid describe the request the token is supposed to answer: OpenSSL checks the signature, the message imprint, the nonce and the policy against them, and builds a chain from the TSA certificate up to anchors.

When verifying an archived token whose request is long gone, leave nonce as null — the nonce check is then skipped, and everything else still holds.

Implementation

TimestampVerification verifyTimestamp({
  Uint8List? responseDer,
  Uint8List? tokenDer,
  required Uint8List hash,
  required List<X509Certificate> anchors,
  String hashAlgorithm = 'SHA256',
  BigInt? nonce,
  String? policyOid,
  List<X509Certificate> intermediates = const [],
  bool checkSignerName = false,
}) {
  if ((responseDer == null) == (tokenDer == null)) {
    throw ArgumentError('provide exactly one of responseDer or tokenDer');
  }
  if (anchors.isEmpty) {
    throw ArgumentError.value(anchors, 'anchors', 'provide at least one');
  }

  final arena = Arena();
  Pointer<TS_REQ> req = nullptr;
  Pointer<TS_VERIFY_CTX> ctx = nullptr;
  Pointer<TS_RESP> resp = nullptr;
  Pointer<PKCS7> token = nullptr;

  try {
    // TS_REQ_to_TS_VERIFY_CTX is the only way to set the expected imprint
    // together with its digest algorithm, so the request is rebuilt here
    // from what the caller knows.
    final template = buildTimestampRequest(
      hash: hash,
      hashAlgorithm: hashAlgorithm,
      policyOid: policyOid,
      useNonce: false,
      nonce: nonce,
    );
    req = _d2i(
      template.der,
      arena,
      (inOut, len) => bindings.d2i_TS_REQ(nullptr, inOut, len),
      'd2i_TS_REQ',
    );

    ctx = bindings.TS_REQ_to_TS_VERIFY_CTX(req, nullptr);
    if (ctx == nullptr) {
      throw OpenSslException('TS_REQ_to_TS_VERIFY_CTX failed');
    }

    var flags = TS_VFY_SIGNATURE | TS_VFY_VERSION | TS_VFY_IMPRINT;
    if (nonce != null) flags |= TS_VFY_NONCE;
    if (policyOid != null) flags |= TS_VFY_POLICY;
    if (checkSignerName) flags |= TS_VFY_SIGNER;
    bindings.TS_VERIFY_CTX_set_flags(ctx, flags);

    // The context takes ownership of the store and of the certificate stack,
    // so both are built fresh here.
    final store = bindings.X509_STORE_new();
    if (store == nullptr) {
      throw OpenSslException('X509_STORE_new failed');
    }
    for (final anchor in anchors) {
      if (bindings.X509_STORE_add_cert(store, anchor.handle) != 1) {
        bindings.X509_STORE_free(store);
        throw OpenSslException('X509_STORE_add_cert failed');
      }
    }
    if (!_setVerifyStore(ctx, store)) {
      bindings.X509_STORE_free(store);
      throw OpenSslException('TS_VERIFY_CTX_set0_store failed');
    }

    if (intermediates.isNotEmpty) {
      final stack = bindings.OPENSSL_sk_new_null();
      if (stack == nullptr) {
        throw OpenSslException('OPENSSL_sk_new_null failed');
      }
      for (final cert in intermediates) {
        final copy = bindings.X509_dup(cert.handle);
        if (copy == nullptr ||
            bindings.OPENSSL_sk_push(stack.cast(), copy.cast()) == 0) {
          throw OpenSslException('failed to stack an intermediate');
        }
      }
      if (!_setVerifyCerts(ctx, stack.cast<stack_st_X509>())) {
        throw OpenSslException('TS_VERIFY_CTX_set0_certs failed');
      }
    }

    OpenSslException.clearError(bindings);

    final int result;
    if (responseDer != null) {
      resp = _d2i(
        responseDer,
        arena,
        (inOut, len) => bindings.d2i_TS_RESP(nullptr, inOut, len),
        'd2i_TS_RESP',
      );
      result = bindings.TS_RESP_verify_response(ctx, resp);
    } else {
      token = _d2i(
        tokenDer!,
        arena,
        (inOut, len) => bindings.d2i_PKCS7(nullptr, inOut, len),
        'd2i_PKCS7',
      );
      result = bindings.TS_RESP_verify_token(ctx, token);
    }

    final parsed = responseDer != null
        ? parseTimestampResponse(responseDer).token
        : parseTimestampToken(tokenDer!);

    return TimestampVerification(
      valid: result == 1,
      error: result == 1 ? null : _drainErrors(),
      token: parsed,
    );
  } finally {
    if (resp != nullptr) bindings.TS_RESP_free(resp);
    if (token != nullptr) bindings.PKCS7_free(token);
    if (ctx != nullptr) bindings.TS_VERIFY_CTX_free(ctx);
    if (req != nullptr) bindings.TS_REQ_free(req);
    arena.releaseAll();
    OpenSslException.clearError(bindings);
  }
}