Repository navigation
Retain certificate handles while creating Schannel and MsQuic credentials - #136645
Conversation
|
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. |
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
|
Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones |
|
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
left a comment
There was a problem hiding this comment.
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 @bartonjs What do you think? Do you have any other ideas? |
bartonjs
left a comment
There was a problem hiding this comment.
Reflection use here is unacceptable.
Either run cert.Handle through CertDuplicateCertificateContext or ask for something like cert.GetSafeHandle().
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. |
The cert folks have chimed in. Lifting my block since it's unnecessary now.
|
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 |
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]>
Hold a reference to the certificate's underlying
SafeHandlewhile 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
DangerousAddRefwithDangerousReleaseinfinally, and keep the reference through native error handling. This covers bothSCHANNEL_CREDandSCH_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-linuxand-f net11.0-osxalso 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
X509Certificate2copy/dispose pair. No throughput claim is made from that configuration.Note
This pull request description and code changes were generated with GitHub Copilot.