buildTimestampRequest method

TimestampRequest buildTimestampRequest({
  1. required Uint8List hash,
  2. String hashAlgorithm = 'SHA256',
  3. String? policyOid,
  4. bool requestCertificate = true,
  5. bool useNonce = true,
  6. BigInt? nonce,
})

Builds a TimeStampReq for hash.

hash must be the digest of the data being time-stamped, computed with hashAlgorithm — its length is checked against the algorithm.

nonce defaults to 64 fresh random bits. Sending one and checking it in the answer is the defence against a replayed token; pass false to useNonce only when talking to a TSA that rejects nonces.

requestCertificate asks the TSA to embed its certificate in the token, which is what makes the token verifiable on its own later.

Implementation

TimestampRequest buildTimestampRequest({
  required Uint8List hash,
  String hashAlgorithm = 'SHA256',
  String? policyOid,
  bool requestCertificate = true,
  bool useNonce = true,
  BigInt? nonce,
}) {
  final md = _digestByName(hashAlgorithm);
  final expected = bindings.EVP_MD_get_size(md);
  if (expected > 0 && hash.length != expected) {
    throw ArgumentError.value(
      hash.length,
      'hash.length',
      'a $hashAlgorithm imprint must be $expected bytes',
    );
  }

  final effectiveNonce =
      nonce ?? (useNonce ? _randomNonce() : null);

  final req = bindings.TS_REQ_new();
  if (req == nullptr) {
    throw OpenSslException('TS_REQ_new failed');
  }

  Pointer<TS_MSG_IMPRINT> imprint = nullptr;
  Pointer<X509_ALGOR> algor = nullptr;
  Pointer<ASN1_INTEGER> nonceInt = nullptr;
  Pointer<ASN1_OBJECT> policy = nullptr;

  try {
    if (bindings.TS_REQ_set_version(req, 1) != 1) {
      throw OpenSslException('TS_REQ_set_version failed');
    }

    imprint = bindings.TS_MSG_IMPRINT_new();
    if (imprint == nullptr) {
      throw OpenSslException('TS_MSG_IMPRINT_new failed');
    }

    algor = bindings.X509_ALGOR_new();
    if (algor == nullptr) {
      throw OpenSslException('X509_ALGOR_new failed');
    }
    // X509_ALGOR_set_md returns void: it cannot fail for a fetched digest.
    bindings.X509_ALGOR_set_md(algor, md);
    if (bindings.TS_MSG_IMPRINT_set_algo(imprint, algor) != 1) {
      throw OpenSslException('TS_MSG_IMPRINT_set_algo failed');
    }

    final hashPtr = calloc<UnsignedChar>(hash.length);
    try {
      hashPtr.cast<Uint8>().asTypedList(hash.length).setAll(0, hash);
      if (bindings.TS_MSG_IMPRINT_set_msg(imprint, hashPtr, hash.length) !=
          1) {
        throw OpenSslException('TS_MSG_IMPRINT_set_msg failed');
      }
    } finally {
      calloc.free(hashPtr);
    }

    if (bindings.TS_REQ_set_msg_imprint(req, imprint) != 1) {
      throw OpenSslException('TS_REQ_set_msg_imprint failed');
    }

    if (effectiveNonce != null) {
      nonceInt = _asn1IntegerFromBigInt(effectiveNonce);
      if (bindings.TS_REQ_set_nonce(req, nonceInt) != 1) {
        throw OpenSslException('TS_REQ_set_nonce failed');
      }
    }

    if (policyOid != null) {
      policy = _objFromOid(policyOid);
      if (bindings.TS_REQ_set_policy_id(req, policy) != 1) {
        throw OpenSslException('TS_REQ_set_policy_id failed');
      }
    }

    if (bindings.TS_REQ_set_cert_req(req, requestCertificate ? 1 : 0) != 1) {
      throw OpenSslException('TS_REQ_set_cert_req failed');
    }

    return TimestampRequest(
      der: _encode(
        (out) => bindings.i2d_TS_REQ(req, out),
        'i2d_TS_REQ',
      ),
      hash: Uint8List.fromList(hash),
      hashAlgorithm: hashAlgorithm,
      nonce: effectiveNonce,
      policyOid: policyOid,
      requestCertificate: requestCertificate,
    );
  } finally {
    if (nonceInt != nullptr) bindings.ASN1_INTEGER_free(nonceInt);
    if (policy != nullptr) bindings.ASN1_OBJECT_free(policy);
    // The imprint (and with it the algorithm) belongs to the request once
    // set; when set failed, free it here.
    if (imprint != nullptr &&
        bindings.TS_REQ_get_msg_imprint(req) != imprint) {
      bindings.TS_MSG_IMPRINT_free(imprint);
    } else if (algor != nullptr && imprint == nullptr) {
      bindings.X509_ALGOR_free(algor);
    }
    bindings.TS_REQ_free(req);
  }
}