verifyChain method

VerificationResult verifyChain({
  1. required X509Certificate certificate,
  2. required List<X509Certificate> anchors,
  3. List<X509Certificate> intermediates = const [],
  4. List<X509Crl> crls = const [],
  5. bool checkRevocation = false,
  6. bool checkRevocationChain = false,
  7. DateTime? verificationTime,
  8. bool ignoreValidity = false,
  9. bool partialChain = false,
  10. int? maxDepth,
})

Verifies certificate against the trusted anchors, optionally using intermediates to build the chain.

  • crls: revocation lists to consult. Passing CRLs without checkRevocation has no effect.
  • checkRevocation: turns on X509_V_FLAG_CRL_CHECK (leaf certificate only). Together with checkRevocationChain it also turns on X509_V_FLAG_CRL_CHECK_ALL, which demands a CRL for every level — with no CRL for one of them the result is UNABLE_TO_GET_CRL.
  • verificationTime: validates as if it were that instant instead of now. Essential to check old signatures, whose certificate has expired since but was valid at signing time.
  • ignoreValidity: disables the time check (NO_CHECK_TIME).
  • partialChain: accepts any certificate in anchors as an anchor, even when it is not a self-signed root (PARTIAL_CHAIN).
  • maxDepth: limit on the number of chain levels.

The certificates in VerificationResult.chain belong to the caller: call VerificationResult.dispose when done.

Implementation

VerificationResult verifyChain({
  required X509Certificate certificate,
  required List<X509Certificate> anchors,
  List<X509Certificate> intermediates = const [],
  List<X509Crl> crls = const [],
  bool checkRevocation = false,
  bool checkRevocationChain = false,
  DateTime? verificationTime,
  bool ignoreValidity = false,
  bool partialChain = false,
  int? maxDepth,
}) {
  if (anchors.isEmpty) {
    throw ArgumentError.value(anchors, 'anchors', 'provide at least one');
  }

  final store = bindings.X509_STORE_new();
  if (store == nullptr) {
    throw OpenSslException('X509_STORE_new failed');
  }

  Pointer<X509_STORE_CTX> ctx = nullptr;
  Pointer<OPENSSL_STACK> intermediateStack = nullptr;

  try {
    for (final anchor in anchors) {
      if (bindings.X509_STORE_add_cert(store, anchor.handle) != 1) {
        throw OpenSslException('X509_STORE_add_cert failed');
      }
    }
    for (final crl in crls) {
      if (bindings.X509_STORE_add_crl(store, crl.handle) != 1) {
        throw OpenSslException('X509_STORE_add_crl failed');
      }
    }

    var flags = 0;
    if (checkRevocation || checkRevocationChain) {
      flags |= X509_V_FLAG_CRL_CHECK;
    }
    if (checkRevocationChain) {
      flags |= X509_V_FLAG_CRL_CHECK_ALL;
    }
    if (partialChain) {
      flags |= X509_V_FLAG_PARTIAL_CHAIN;
    }
    if (ignoreValidity) {
      flags |= X509_V_FLAG_NO_CHECK_TIME;
    } else if (verificationTime != null) {
      flags |= X509_V_FLAG_USE_CHECK_TIME;
    }
    if (flags != 0) {
      bindings.X509_STORE_set_flags(store, flags);
    }

    ctx = bindings.X509_STORE_CTX_new();
    if (ctx == nullptr) {
      throw OpenSslException('X509_STORE_CTX_new failed');
    }

    if (intermediates.isNotEmpty) {
      intermediateStack = _newCertStack(intermediates);
    }

    if (bindings.X509_STORE_CTX_init(
          ctx,
          store,
          certificate.handle,
          intermediateStack.cast<stack_st_X509>(),
        ) !=
        1) {
      throw OpenSslException('X509_STORE_CTX_init failed');
    }

    if (verificationTime != null && !ignoreValidity) {
      final param = bindings.X509_STORE_CTX_get0_param(ctx);
      if (param == nullptr) {
        throw OpenSslException('X509_STORE_CTX_get0_param failed');
      }
      bindings.X509_VERIFY_PARAM_set_time(
        param,
        verificationTime.toUtc().millisecondsSinceEpoch ~/ 1000,
      );
    }

    if (maxDepth != null) {
      final param = bindings.X509_STORE_CTX_get0_param(ctx);
      if (param != nullptr) {
        bindings.X509_VERIFY_PARAM_set_depth(param, maxDepth);
      }
    }

    OpenSslException.clearError(bindings);
    final result = bindings.X509_verify_cert(ctx);
    final code = bindings.X509_STORE_CTX_get_error(ctx);

    return VerificationResult(
      valid: result == 1 && code == X509_V_OK,
      errorCode: code,
      message: _errorMessage(code),
      depth: code == X509_V_OK
          ? -1
          : bindings.X509_STORE_CTX_get_error_depth(ctx),
      failingCertificate: code == X509_V_OK ? null : _currentSubject(ctx),
      chain: result == 1 ? _chainFromContext(ctx) : const [],
    );
  } finally {
    if (ctx != nullptr) {
      bindings.X509_STORE_CTX_free(ctx);
    }
    if (intermediateStack != nullptr) {
      bindings.OPENSSL_sk_free(intermediateStack.cast());
    }
    bindings.X509_STORE_free(store);
    OpenSslException.clearError(bindings);
  }
}