verifyChain method
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,
Verifies certificate against the trusted anchors, optionally using
intermediates to build the chain.
crls: revocation lists to consult. Passing CRLs withoutcheckRevocationhas no effect.checkRevocation: turns onX509_V_FLAG_CRL_CHECK(leaf certificate only). Together withcheckRevocationChainit also turns onX509_V_FLAG_CRL_CHECK_ALL, which demands a CRL for every level — with no CRL for one of them the result isUNABLE_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 inanchorsas 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);
}
}