Skip to content

Retain certificate handles while creating Schannel and MsQuic credentials - #136645

Merged
rzikm merged 4 commits into
dotnet:mainfrom
rzikm:fix-certificate-handle-lifetime
Nov 3, 2026
Merged

rzikm merged 4 commits into
dotnet:mainfrom
rzikm:fix-certificate-handle-lifetime

Conversation

@rzikm

@rzikm rzikm commented Oct 29, 2026

Copy link
Copy Markdown
Member

Hold a reference to the certificate's underlying SafeHandle while constructing Schannel credentials and loading MsQuic credentials, rather than passing an unprotected raw certificate context to native code.

Use a shared, allocation-free accessor to the cryptography-owned handle, pair DangerousAddRef with DangerousRelease in finally, and keep the reference through native error handling. This covers both SCHANNEL_CRED and SCH_CREDENTIALS, as well as the MsQuic Schannel backend; portable PKCS#12 credential loading remains unchanged.

Validation

  • .\.dotnet\dotnet.exe build /t:test src\libraries\System.Net.Security\tests\FunctionalTests\System.Net.Security.Tests.csproj — passed; 5,386 total, 36 skipped, zero failures.
  • .\.dotnet\dotnet.exe build /t:test src\libraries\System.Net.Security\tests\UnitTests\System.Net.Security.Unit.Tests.csproj — passed; 121 total, 4 skipped, zero failures.
  • .\.dotnet\dotnet.exe build /t:test src\libraries\System.Net.Quic\tests\FunctionalTests\System.Net.Quic.Functional.Tests.csproj — passed; 449 total, 1 skipped, zero failures.
  • .\.dotnet\dotnet.exe build src\libraries\System.Net.Quic\src\System.Net.Quic.csproj -f net11.0-windows — passed, zero warnings/errors. The same command with -f net11.0-linux and -f net11.0-osx also passed with zero warnings/errors (cross-target compilation on Windows).
  • .\.dotnet\dotnet.exe build src\libraries\System.Net.Http.WinHttpHandler\src\System.Net.Http.WinHttpHandler.csproj — passed across its target frameworks, verifying the shared helper's downlevel compatibility.

An allocation-focused BenchmarkDotNet comparison on the existing Debug-library testhost measured 0 B/op for the accessor plus SafeHandle add-ref/release, versus 232 B/op for an X509Certificate2 copy/dispose pair. No throughput claim is made from that configuration.

Note

This pull request description and code changes were generated with GitHub Copilot.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@rzikm rzikm added this to the 12.0.0 milestone Oct 29, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@GrabYourPitchforks

Copy link
Copy Markdown
Member

Disclosure: This case came in through our security reporting channels and was assessed by our team as severity None. The reason is that any underlying app crash would be the result of the hosting application intentionally disposing a certificate object which it knows is actively in use. That puts an app-level defect in the causal chain of any such crash, driving this down to sev none.

@GrabYourPitchforks GrabYourPitchforks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The folks who own the certificates namespace aren't comfortable with the use of reflection here. Talk to them first then clear my block once everybody is happy. :)

@rzikm
rzikm requested a review from bartonjs October 29, 2026 18:22
@rzikm

rzikm commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

The folks who own the certificates namespace aren't comfortable with the use of reflection here. Talk to them first then clear my block once everybody is happy. :)

The alternative is to do using var certPin = new X509Certificate2(certificate) which ends up allocating another SafeHandle, but I wanted to avoid the allocation.

@bartonjs What do you think? Do you have any other ideas?

@bartonjs bartonjs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reflection use here is unacceptable.

Either run cert.Handle through CertDuplicateCertificateContext or ask for something like cert.GetSafeHandle().

@bartonjs

Copy link
Copy Markdown
Member

but I wanted to avoid the allocation.

Given the objects involved, the extra allocation here is basically ignorable.

If you really want to avoid it, and you're only on Windows, you could P/Invoke to CertDuplicateCertificateContext as nint in/nint out.

@GrabYourPitchforks
GrabYourPitchforks dismissed their stale review October 29, 2026 18:47

The cert folks have chimed in. Lifting my block since it's unnecessary now.

@vcsjones

Copy link
Copy Markdown
Member

CertDuplicateCertificateContext

I don't think that is sufficiently because its a race we are trying to solve here - if the handle goes invalid right before the CertDuplicateCertificateContext happens then it contains junk.

Replace private SafeHandle accessors with certificate copies and document their lifetime purpose.

Co-authored-by: Copilot App <[email protected]>
Construct the retained certificate as X509Certificate2 and inspect its HasPrivateKey property so ephemeral-key failures retain the expected authentication diagnostics.

Co-authored-by: Copilot App <[email protected]>
@rzikm
rzikm merged commit 6e83aff into dotnet:main Nov 3, 2026
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants