verifyCmsDetached method

bool verifyCmsDetached({
  1. required Uint8List cmsDer,
  2. required Uint8List content,
  3. X509Store? store,
  4. Uint8List? trustedCertDer,
})
inherited

Verifies a detached CMS/PKCS#7 signature using the provided content and a trusted store or a specific trustedCertDer.

Implementation

bool verifyCmsDetached({
  required Uint8List cmsDer,
  required Uint8List content,
  X509Store? store,
  Uint8List? trustedCertDer,
}) {
  if (store == null && trustedCertDer == null) {
    throw ArgumentError('Either store or trustedCertDer must be provided.');
  }

  final bindings = this.bindings;

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

  try {
    cms = _d2iCms(bindings, cmsDer);

    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) {
        throw OpenSslException('X509_STORE_new failed');
      }
      if (bindings.X509_STORE_add_cert(tempStore, cert) != 1) {
        throw OpenSslException('X509_STORE_add_cert failed');
      }
      storePtr = tempStore;
    }

    dataBio = _bioFromBytes(bindings, content);

    final result = bindings.CMS_verify(
      cms,
      nullptr, // certs (if we want to pass extra certs explicitly, but usually they are in cms or store)
      storePtr,
      dataBio,
      nullptr, // out (we don't need output content)
      CMS_BINARY | CMS_DETACHED,
    );

    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);
    }
    if (cms != nullptr) {
      bindings.CMS_ContentInfo_free(cms);
    }
  }
}