Repository navigation
[8.0.0 Cherry-pick] Connection encryption info – Slice C: Managed SNI certificate capture - #5126
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
…#4835) * Connection encryption info: capture Managed SNI server certificate details (Slice C) Capture SHA-256 of the presented leaf certificate, the effective SqlClient validation verdict, and the original callback chain status flags in Managed SNI, and flow them into the per-attempt encryption info capture so both SqlConnection.EncryptionInfo and SqlException.EncryptionInfo report them. No trust decisions, messages, or tracing are changed. Part of #2353 Co-authored-by: Copilot App <[email protected]> * Keep Managed SNI certificate verdict when the host name check throws Named pipes validate against the target '.', which X509Certificate2.MatchesHostname rejects with ArgumentException before the final verdict is recorded. Record the errors already established (e.g. ChainError and callback chain status) before the host name check so the capture survives. The exception is not caught or changed. Co-authored-by: Copilot App <[email protected]> * Managed SNI: capture negotiated cipher suite Record (int)SslStream.NegotiatedCipherSuite after the handshake (authoritative) and inside the certificate validation callback (adopt-if-none), so failed certificate validation keeps the observed suite. TCP and named-pipe handles; MARS passes through. Reads are exception-safe; 0 is treated as unavailable. Co-authored-by: Copilot App <[email protected]> * Mark Managed SNI capture simulator failures as expected TLS handshake failures Main's #4891 makes the TDS simulator rethrow connection faults on Stop. The untrusted, host-name-mismatch and pin-mismatch capture tests now set ExpectTlsHandshakeFailure so the server treats the client's rejection as expected. Test-only change. Co-authored-by: Copilot App <[email protected]> * Adapt Slice C tests and design note to NegotiatedCipherSuiteId Assert the public CLS-compliant NegotiatedCipherSuiteId property in the Managed SNI simulator tests and describe the two-property shape in the Managed capture section. Co-authored-by: Copilot App <[email protected]> * Capture the presented server certificate instead of its hash (Managed SNI) Managed SNI now captures a copy of the presented leaf certificate's DER encoding (GetRawCertData) instead of its SHA-256 hash, and passes it to the snapshot so SqlConnectionEncryptionInfo.GetServerCertificate() can return it. Certificates over 64 KiB are not captured, and the first certificate presented in an attempt is kept. Capture still happens in the validation callback's finally block, so it is present on TrustServerCertificate bypass and on validation failure. Co-authored-by: Copilot App <[email protected]> * Address review feedback on Managed SNI certificate capture - Reset the attempt's encryption info capture when Connect retries after an instance-resolution failure, so only the retried attempt's values publish. - Document that an observed protocol is kept after a failure. - Compare capture outcomes against an independent restatement of the validation rules instead of the legacy overload. - Add routed single-login tests proving only the terminal attempt's certificate is published, including a terminal attempt that fails before TLS. - Add XML summaries and Arrange/Act/Assert structure to the capture tests. Co-authored-by: Copilot App <[email protected]> * Split Managed SNI capture simulator tests into Open and OpenAsync tests Replace the bool-async theories with separate Open_ and OpenAsync_ tests that call each API directly, sharing setup and scenario assertion helpers. Co-authored-by: Copilot App <[email protected]> * Align native SNI failure note with Managed SNI capture Co-authored-by: Copilot App <[email protected]> * Adapt reconnect tests to Managed SNI certificate capture Slice B's reconnect tests asserted that certificate fields are null. Managed SNI now records the presented certificate under TrustServerCertificate, so use the bypass assertion. Co-authored-by: Copilot App <[email protected]> * Adapt Slice C tests to the Slice A review follow-up helpers Use the shared EncryptionInfoTestData.AssertEmpty helper and value-returning Assert.NotNull instead of ad-hoc IsEmpty checks and null-forgiving operators. Test-only; no product change. Co-authored-by: Copilot App <[email protected]> * Adapt Slice C to the Slice B accumulator design Move C's certificate-field tests into EncryptionInfoAccumulatorTests using the accumulator's settable properties, drop the RecordServerCertificate-based tests, remove the inherited AssertCertificateFieldsNull use from the enlistment test (Managed SNI now records the presented certificate), and update the remaining EncryptionInfoCapture references in docs and comments. Co-authored-by: Copilot App <[email protected]> * Address Slice C review feedback - Rename ServerCertificateCapture to ServerCertificate, with matching handle, state-object, factory, file and test names. - Document MaxRawCertificateSize as 64 KiB (65,536 bytes) and sort properties. - Document SNI handle overrides with inheritdoc; make SniMarsConnection pass-throughs internal and document them. - Use a volatile field for the captured certificate on TCP and NP handles. - Use a using declaration in SniNpHandle.ValidateServerCertificate. - Replace the accumulator property-subset theory with the existing per-property tests. Co-authored-by: Copilot App <[email protected]> * Address Paul's review: TlsHandshakeInfo record as single source of truth - Rename ServerCertificate to TlsHandshakeInfo (DerBytes, MaxDerBytesSize, TlsCipherSuite? CipherSuite on .NET); GetTlsHandshakeInfo() on state objects. - Remove NegotiatedCipherSuite from SNI handles and GetNegotiatedCipherSuite; EnableSsl assigns all record values directly, without precedence logic. - Fold helpers into CreateTlsHandshakeInfo(SslStream, ...) and GetChainStatus; only CryptographicException is caught; latest callback record wins. - Rename accumulator ServerCertificateRawData to ServerCertificateDerBytes. - Enable nullable for new code, simplify conditionals, update design note. - Replace synthetic tests with in-callback loopback handshake tests. Co-authored-by: Copilot App <[email protected]> * Explain the 64 KiB certificate capture limit in the design note Co-authored-by: Copilot App <[email protected]> * Address Slice C TLS diagnostics review feedback Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5b0df491-21cd-4da0-b776-7cd9a8af085e * Scope Managed SNI switch to each certificate capture test Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5b0df491-21cd-4da0-b776-7cd9a8af085e * Record Managed SNI handshake directly into the attempt accumulator Pass the per-attempt EncryptionInfoAccumulator into EnableSsl and register a per-handshake certificate validation callback through SslClientAuthenticationOptions. The callback captures a local reference that is cleared in finally, so the handle keeps no handshake state and a late callback cannot write to the attempt. SniCommon.RecordTlsHandshake writes the certificate, chain status, cipher suite, protocol and validation errors straight to the accumulator, which owns normalization and the 64 KiB cap. This removes TlsHandshakeInfo and the copy step in TdsParser.EnableSsl. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5b0df491-21cd-4da0-b776-7cd9a8af085e * Avoid copying an oversized Managed SNI server certificate Read the presented certificate through X509Certificate2.RawDataMemory and add EncryptionInfoAccumulator.SetServerCertificateDerBytes, which copies the span only when it is within the 64 KiB cap. An oversized certificate is now never copied, rather than copied and then dropped. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5b0df491-21cd-4da0-b776-7cd9a8af085e * Share the accumulator's server certificate copy with the snapshot ServerCertificateDerBytes is now ReadOnlyMemory<byte>? and its setter applies the size cap and stores the accumulator's own copy, replacing SetServerCertificateDerBytes. SqlConnectionEncryptionInfo retains that never-mutated copy instead of cloning it, so ToSnapshot copies nothing. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5b0df491-21cd-4da0-b776-7cd9a8af085e --------- Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5b0df491-21cd-4da0-b776-7cd9a8af085e (cherry picked from commit ce73774)
930c7e8 to
1ca26c2
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The cross-platform TLS authentication changes need human review because trusted immutable review guidance was not supplied and current-head platform CI is still running.
No open issues 🎉
What changed in this PR
Ports Slice C of connection-encryption diagnostics (#2353) to the stacked 8.0 branch. Building on Slices A and B, it populates connection and exception snapshots with Managed SNI certificate details. Native SNI capture remains in Slice D; this PR remains marked DO NOT MERGE until the complete feature is ready.
Changes:
- Captures the presented certificate, validation results, chain status, TLS protocol, and cipher suite during managed TLS handshakes.
- Passes diagnostics through the per-attempt accumulator, limits retained certificates to 64 KiB, and shares immutable certificate bytes with snapshots.
- Adds and updates tests and documentation for successful and failed handshakes, validation bypass, pinning, routing, and Strict encryption.
| File | Description |
|---|---|
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ManagedSniCertificateCaptureTests.cs |
Adds synchronous and asynchronous certificate-capture scenarios. |
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionEncryptionInfoTests.cs |
Updates snapshot expectations for Managed SNI capture. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionEncryptionInfoTests.cs |
Removes the constructor-copy test after ownership moves to the accumulator. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleTests.cs |
Adapts the TCP timeout test to the new handshake signature. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniReceiveTests.cs |
Updates the receive-test handle stub. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniPacketTests.cs |
Updates the packet-test handle stub. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniNpHandleTests.cs |
Adapts the named-pipe timeout test to the new signature. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniCommonTlsHandshakeRecordingTest.cs |
Adds verdict, callback-recording, size-limit, and privacy tests. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/Connection/EncryptionInfoAccumulatorTests.cs |
Tests certificate ownership, limits, flags, reset, and cipher normalization. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptionInfo.cs |
Retains accumulator-owned certificate memory without another copy. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectNative.cs |
Accepts the accumulator parameter without adding native capture. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectManaged.netcore.cs |
Passes the accumulator to managed handles. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObject.cs |
Extends the handshake contract to accept diagnostic storage. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParser.cs |
Supplies per-attempt storage and preserves callback-recorded protocol values. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs |
Records TCP handshake diagnostics, including validation failures. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniSslStream.netcore.cs |
Moves callback configuration out of stream construction. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniNpHandle.netcore.cs |
Records named-pipe handshake diagnostics. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniMarsHandle.netcore.cs |
Forwards the accumulator through the MARS handle. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniMarsConnection.netcore.cs |
Forwards the accumulator to the physical transport. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniHandle.netcore.cs |
Configures per-handshake certificate-validation callbacks. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniCommon.netcore.cs |
Records effective validation results and observed TLS details. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/EncryptionInfoAccumulator.cs |
Adds certificate storage, validation fields, limits, and snapshot propagation. |
doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptionInfo.xml |
Clarifies certificate availability, capture limits, and diagnostic-only use. |
doc/design-notes/2353-connection-encryption-info.md |
Documents managed capture, ownership, failure reporting, and native limitations. |
Review effort: Balanced
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## dev/automation/pr-4831-to-8.0.0 #5126 +/- ##
==================================================================
Coverage ? 70.12%
==================================================================
Files ? 315
Lines ? 69996
Branches ? 0
==================================================================
Hits ? 49085
Misses ? 20911
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
a050273
into
dev/automation/pr-4831-to-8.0.0
Description
Important
DO NOT MERGE. The connection-encryption info feature (#2353) will land in release/8.0 as a whole, only after all slices have merged to
main(remaining: Slice D #4836). This PR is up for review now so the 8.0 port is ready when the feature is complete.8.0.0 cherry-pick of Slice C of the connection-encryption info feature: #4835 (main
ce73774d1). Captures the server certificate and certificate-validation details during the managed SNI TLS handshake and records them into the connection's encryption-info accumulator.Stack order (each PR is based on the previous one; merge bottom-up):
release/8.0dev/automation/pr-4830-to-8.0.0-v2dev/automation/pr-4831-to-8.0.0(this PR)Slice C cannot apply directly to
release/8.0because it builds on the types and accumulator that Slices A and B add. It is therefore stacked on the Slice B port, and the bot's original base was changed to that port.Conflicts
None.
git cherry-pick -x ce73774d1on top of the Slice B port (8fcb746d7) applied cleanly (TdsParser.csauto-merged). The bot's placeholder commit was replaced.Issues
Ports #4835 to release/8.0. Feature: #2353.
Testing
Builds (0 warnings, 0 errors each):
dotnet build build.proj -t:BuildSqlClient: succeededdotnet build src\Microsoft.Data.SqlClient\tests\UnitTests\Microsoft.Data.SqlClient.UnitTests.csproj: succeededFocused unit tests (
$f = 'FullyQualifiedName~EncryptionInfo|FullyQualifiedName~CertificateCapture|FullyQualifiedName~TlsHandshakeRecording|FullyQualifiedName~SqlExceptionTest'):dotnet test ...UnitTests.csproj --no-build -f net10.0 --filter $f: passed, 230/230dotnet test ...UnitTests.csproj --no-build -f net462 --filter $f(with a clean Windows PowerShellPSModulePath): passed, 139/139. Without the clean path, the simulated-server fixture fails locally with "Cannot find drive 'Cert'". This is a local environment issue, not a code issue.Guidelines
Please review the contribution guidelines before submitting a pull request: