verifyCmsDetachedWithResult method
CmsValidationResult
verifyCmsDetachedWithResult({})
inherited
Verifies a detached CMS/PKCS#7 signature and returns detailed result.
Implementation
CmsValidationResult verifyCmsDetachedWithResult({
required Uint8List cmsDer,
required Uint8List content,
X509Store? store,
Uint8List? trustedCertDer,
}) {
final bindings = this.bindings;
// Clear error queue before starting
while (bindings.ERR_get_error() != 0) {}
if (store == null && trustedCertDer == null) {
return CmsValidationResult(
isValid: false,
errorMessage: 'Either store or trustedCertDer must be provided.'
);
}
Pointer<CMS_ContentInfo> cms = nullptr;
Pointer<X509_STORE> tempStore = nullptr;
Pointer<X509> cert = nullptr;
Pointer<BIO> dataBio = nullptr;
// We need to capture errors if any step fails
int lastError = 0;
String? lastErrorMsg;
try {
// 1. Parse CMS
try {
cms = _d2iCms(bindings, cmsDer);
} catch (e) {
return CmsValidationResult(isValid: false, errorMessage: 'Invalid CMS structure: $e');
}
Pointer<X509_STORE> storePtr;
if (store != null) {
storePtr = store.handle;
} else {
// Create temporary store for single cert
cert = _d2iX509(bindings, trustedCertDer!);
tempStore = bindings.X509_STORE_new();
if (tempStore == nullptr) {
return CmsValidationResult(isValid: false, errorMessage: 'Internal Error: X509_STORE_new failed');
}
bindings.X509_STORE_add_cert(tempStore, cert);
storePtr = tempStore;
}
dataBio = _bioFromBytes(bindings, content);
// 2. Verify
final result = bindings.CMS_verify(
cms,
nullptr,
storePtr,
dataBio,
nullptr,
CMS_BINARY | CMS_DETACHED,
);
if (result == 1) {
return CmsValidationResult(isValid: true);
} else {
// Capture Error
lastError = bindings.ERR_get_error();
final strPtr = bindings.ERR_error_string(lastError, nullptr);
lastErrorMsg = strPtr == nullptr
? 'Unknown OpenSSL error'
: strPtr.cast<Utf8>().toDartString();
return CmsValidationResult(
isValid: false,
errorCode: lastError,
errorMessage: lastErrorMsg
);
}
} catch (e) {
return CmsValidationResult(isValid: false, errorMessage: 'Exception: $e');
} finally {
if (dataBio != nullptr) bindings.BIO_free(dataBio);
if (tempStore != nullptr) bindings.X509_STORE_free(tempStore);
if (cert != nullptr) bindings.X509_free(cert);
if (cms != nullptr) bindings.CMS_ContentInfo_free(cms);
}
}