sign method

X509Request sign(
  1. EvpPkey privateKey, {
  2. String digestName = 'SHA256',
})

Signs the request and returns the wrapper. privateKey Key used to sign (must allow signing).

Implementation

X509Request sign(EvpPkey privateKey, {String digestName = 'SHA256'}) {
  _ensureUsable();
  try {
      final digestLabel = digestName.toNativeUtf8(allocator: calloc);
      final md = _context.bindings.EVP_get_digestbyname(digestLabel.cast());
      calloc.free(digestLabel);

      if (md == nullptr) throw OpenSslException('Unknown digest: $digestName');

      // X509_REQ_sign returns size > 0 on success
      if (_context.bindings.X509_REQ_sign(_req, privateKey.handle, md) <= 0) {
           throw OpenSslException('Failed to sign X509_REQ');
      }

      // We transfer ownership of `_req` to the returned object?
      // Wait, if X509Request takes ownership (finalizer), we are good.
      // But if we call sign twice? The builder holds the pointer.
      // Ideally builder should duplicate or we "consume" the builder.
      // Our X509Request constructor attaches a finalizer. If we keep using it in builder, we have aliasing.
      // Let's assume sign is terminal or we dup.
      // To be safe, let's just pass ownership and make the builder "invalid" or create a copy (X509_REQ_dup?).
      // Simple approach: Builder manages it until sign, then gives it to wrapper. Builder shouldn't be used after.

      _finalizer.detach(this);
      _isConsumed = true;
      return X509Request(_req, _context);
  } catch (e) {
     // If fails, we still own _req, it will be leaked if user loses builder?
     // We should implement dispose or Finalizer on Builder too if valid.
     // But for now, let's trust flow.
     rethrow;
  }
}