verifyCmsDetachedContentInfo method

bool verifyCmsDetachedContentInfo({
  1. required CmsContent cms,
  2. required Uint8List content,
  3. X509Store? store,
  4. Uint8List? trustedCertDer,
  5. bool skipSignerCertVerify = false,
})
inherited

Verifies a detached CMS signature using a provided CmsContent.

Implementation

bool verifyCmsDetachedContentInfo({
  required CmsContent cms,
  required Uint8List content,
  X509Store? store,
  Uint8List? trustedCertDer,
  bool skipSignerCertVerify = false,
}) {
  if (!skipSignerCertVerify && store == null && trustedCertDer == null) {
    throw ArgumentError('Either store or trustedCertDer must be provided.');
  }

  final bindings = this.bindings;

  Pointer<X509_STORE> tempStore = nullptr;
  Pointer<X509> cert = nullptr;
  Pointer<BIO> dataBio = nullptr;

  try {
    Pointer<X509_STORE> storePtr;

    if (store != null) {
      storePtr = store.handle;
    } else if (trustedCertDer != null) {
      cert = _d2iX509(bindings, trustedCertDer);
      tempStore = bindings.X509_STORE_new();
      if (tempStore == nullptr) {
        throw OpenSslException('X509_STORE_new failed');
      }
      if (bindings.X509_STORE_add_cert(tempStore, cert) != 1) {
        throw OpenSslException('X509_STORE_add_cert failed');
      }
      storePtr = tempStore;
    } else {
      // Signature-only verification without chain validation.
      tempStore = bindings.X509_STORE_new();
      if (tempStore == nullptr) {
        throw OpenSslException('X509_STORE_new failed');
      }
      storePtr = tempStore;
    }

    dataBio = _bioFromBytes(bindings, content);

    var flags = CMS_BINARY | CMS_DETACHED;
    if (skipSignerCertVerify) {
      flags |= CMS_NO_SIGNER_CERT_VERIFY;
    }

    final result = bindings.CMS_verify(
      cms.handle,
      nullptr,
      storePtr,
      dataBio,
      nullptr,
      flags,
    );

    return result == 1;
  } finally {
    if (dataBio != nullptr) bindings.BIO_free(dataBio);
    if (tempStore != nullptr) bindings.X509_STORE_free(tempStore);
    if (cert != nullptr) bindings.X509_free(cert);
  }
}