Skip to content

fix(ffi): return Box<PickyError> from Pkcs12CryptoContext constructor - #516

Merged
Richard Markiewicz (thenextman) merged 1 commit into
masterfrom
fix/pkcs12-ffi-constructor-abi
Jul 21, 2026
Merged

fix(ffi): return Box<PickyError> from Pkcs12CryptoContext constructor#516
Richard Markiewicz (thenextman) merged 1 commit into
masterfrom
fix/pkcs12-ffi-constructor-abi

Conversation

@thenextman

Copy link
Copy Markdown
Member

Pfx.FromDer (and anything that uses a Pkcs12CryptoContext) crashes or hangs from the .NET bindings. This was shipped in NuGet 2026.6.29; but I've since yanked the package from nuget.org because of this problem.

An FFI ABI mismatch was introduced in #448. That PR made the RNG-seeding fallible, so Pkcs12CryptoContext::with_password / no_password changed from infallible (Box<Pkcs12CryptoContext>) to Result<Box<Pkcs12CryptoContext>, Pkcs12Error>, but the generated C# bindings were not regenerated.

As a result the C# side still called these constructors with the old infallible ABI, using the returned pointer directly as the Pkcs12CryptoContext* handle. On the Rust side the function now returns a Diplomat Result wrapper pointer, so C# handed the wrapper to Pfx.FromDer; crypto_context.0.lock() then dereferenced the wrong pointer and wrote the mutex state out of bounds and corrupted the heap.

Two details made this easy to miss:

  • Only the constructors were stale. Pfx.FromDer's own binding was ResultBox-aware, so the two were inconsistent.
  • The constructors used a bare Pkcs12Error rather than the Box<PickyError> opaque error that every other Result-returning FFI function uses.

Fix

  • Return Result<Box<Pkcs12CryptoContext>, Box<PickyError>> from both constructors, matching the rest of the FFI surface.
  • Regenerate the Diplomat C# bindings. The wrappers now unwrap the ResultBox, throw PickyException on error, and pass the correct handle.

@thenextman
Richard Markiewicz (thenextman) requested a review from a team July 21, 2026 01:43
@thenextman
Richard Markiewicz (thenextman) merged commit 1046e39 into master Jul 21, 2026
11 checks passed
@thenextman
Richard Markiewicz (thenextman) deleted the fix/pkcs12-ffi-constructor-abi branch July 21, 2026 01:47
Benoît Cortier (CBenoit) added a commit that referenced this pull request Jul 21, 2026
Follow-up to #516. Adds a CI job that builds the native library and runs
the .NET binding tests (`Devolutions.Picky.Tests`) on Windows; the
suite that would have caught the ABI mismatch fixed in #516, but which
no workflow was running.

Co-authored-by: Benoît CORTIER <git.divisible626@passmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants