Skip to content

Connection encryption info – Slice C: Managed SNI certificate capture - #4835

Open
priyankatiwari08 wants to merge 12 commits into
dev/automation/encryption-info-slice-b-mode-protocolfrom
dev/automation/encryption-info-slice-c-managed-sni
Open

priyankatiwari08 wants to merge 12 commits into
dev/automation/encryption-info-slice-b-mode-protocolfrom
dev/automation/encryption-info-slice-c-managed-sni

Conversation

@priyankatiwari08

@priyankatiwari08 priyankatiwari08 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #4831 — review only the commits on top of the Slice B branch (dev/automation/encryption-info-slice-b-mode-protocol).

Description

Slice C of the connection encryption info feature. Managed SNI now captures the server certificate details from RemoteCertificateValidationCallback during the TLS handshake. It flows them through TdsParserStateObject into Slice B's per-attempt EncryptionInfoAccumulator, so both snapshots include them:

  • SqlConnection.EncryptionInfo on success
  • SqlException.EncryptionInfo on failure

Native SNI does not return certificate data yet; that is Slice D.

Scope and guarantees

  • Observation only. Capture reuses the callback's inputs and SqlClient's existing verdict. It performs no extra validation or network work, and it does not rebuild the chain.
  • No behavior change. Trust decisions, exception messages and tracing text are unchanged. Certificate data and flags never appear in Message, ToString() or EventSource output.
  • No public API change. Slice C fills the snapshot constructor added in Slice A (Connection encryption info – Slice A: public API surface #4830).
  • Per attempt. Values reset at the start of every TLS handshake or login attempt. The callback writes them to a volatile field in its finally block, and they are read after AuthenticateAsClient returns inside EnableSsl.

Capture points

Field Source null when
GetServerCertificate() / internal PresentedServerCertificateRawData Copy of the presented leaf certificate's DER (GetRawCertData()), taken in the callback's finally. The first certificate in the attempt is kept. Captured even under TrustServerCertificate bypass and on validation failure. No certificate was presented, or its DER exceeds 64 KiB.
ServerCertificateValidationErrors SqlClient's effective verdict: ServerCertificateMissing (null certificate or RemoteCertificateNotAvailable), ChainError, HostNameMismatch (after HostNameInCertificate), PinnedCertificateMismatch. None when validation passed. Validation bypassed (TrustServerCertificate), or the ServerCertificate file couldn't be read.
ServerCertificateChainStatus OR of the original callback X509Chain.ChainStatus flags (no rebuild). NoError when the chain was clean. Bypass; ServerCertificate pin mode (chain not consulted, even on match); no certificate; null chain; or RemoteCertificateChainErrors with no flags.
NegotiatedEncryptionProtocol (Managed contribution) SslStream.SslProtocol, read inside the callback (guarded). Used only if the attempt has no protocol yet. Sender isn't an SslStream, the read throws, or the value is None.
NegotiatedCipherSuiteId (Managed contribution) (int)SslStream.NegotiatedCipherSuite. Recorded after the handshake (authoritative) and inside the callback (adopted only if none was recorded). TCP and named pipes; MARS passes through. No TLS, sender isn't an SslStream, the read throws, or the value is 0.
NegotiatedEncryptionMode Slice B. On a failed handshake, recorded only when a protocol was observed. No protocol observed.

The protocol and cipher suite contributions are .NET only, because Managed SNI isn't compiled for .NET Framework.

Notable behavior

  • Validation failure on Managed SNI. The exception snapshot reports mode Full, the observed protocol (for example Tls12) and cipher suite, together with the certificate, ServerCertificateValidationErrors and ServerCertificateChainStatus. Slice B keeps these values because a protocol was observed. Native SNI still reports none of them.
  • Named pipes. Managed SNI validates named pipes against the target ., and X509Certificate2.MatchesHostname(".") throws ArgumentException, as it already does on main. ValidateSslServerCertificate records the errors it already knows (for example ChainError and the chain status) before the host name check, so the capture survives the throw. The exception is not caught or changed. If no error is known before the check, the fields stay null rather than reporting None.
  • Certificate instead of hash. Following the Threat Model Review, the earlier SHA-256 hash was replaced by the certificate itself. The internal names are ServerCertificateCapture.RawCertificate and EncryptionInfoAccumulator.ServerCertificateRawData.

Documentation

doc/design-notes/2353-connection-encryption-info.md gains a "Managed SNI certificate capture" section. Its failure-path notes now distinguish Managed SNI (mode, protocol and certificate reported) from native SNI (none reported).

Issues

Part of #2353. Stacked on #4831 (Slice B), which is stacked on #4830 (Slice A).

Testing

Automated tests

  • SniCommonServerCertificateCaptureTest (unit, .NET):
    • Table-driven verdicts: null certificate, each SslPolicyErrors combination, HostNameInCertificate override and mismatch, pin match, mismatch, missing and unreadable.
    • Outcome and message match the legacy overload, using an independent message baseline.
    • Every X509ChainStatusFlags value, and the full set, is preserved. An unbuilt chain is not rebuilt.
    • Captured bytes equal the DER of a CertificateRequest self-signed certificate and are a copy. A certificate over 64 KiB is not captured. The first certificate wins. GetServerCertificate().RawData round-trips.
    • null (unavailable) and zero values stay distinct. Nothing leaks into Message or ToString().
    • Protocol and cipher suite: a sender that isn't an SslStream, a null or unauthenticated SslStream (null, no throw), and adopt-if-none.
    • ValidateSslServerCertificate_NamedPipeDotTarget_CapturesKnownErrorsAndKeepsException. Without the fix it fails with Expected ChainError, Actual null.
  • ManagedSniCertificateCaptureTests (simulated TDS server over TLS, Managed SNI forced on Windows). Each scenario has separate Open and OpenAsync tests:
    • TrustServerCertificate bypass (certificate only), and a matching pin.
    • Untrusted self-signed (ChainError, chain flags, certificate), host name mismatch, HostNameInCertificate override and mismatch, and pin mismatch. Failure cases also assert mode Full and protocol Tls12.
    • Per-attempt isolation, including failure then success. This was mutation-checked against removing the per-attempt Reset().
    • The cipher suite equals SslStream.NegotiatedCipherSuite on success. On failure it equals the suite a trusted handshake with the same server negotiates.
  • ConnectionEncryptionInfoTests (Slice B tests, adapted): TrustServerCertificate bypass and reconnect on Managed SNI report the certificate. An untrusted failure on Managed SNI reports mode, protocol and cipher suite.

Results on head f771fb427

Base: #4831 @ dca9d2681 (Slice B's accumulator design from the review rounds). Filter: ManagedSni|SniCommon|SimulatedServerTests|SqlException|SqlConnection|EncryptionInfo.

Target Result
net10.0 1102/1102 passed
net462 (clean PSModulePath, --blame-hang-timeout 10m) 826/826 passed. The run reports only the known post-run testhost teardown hang.
Driver, ref, notsupported, UnitTests and ManualTests builds 0 warnings, 0 errors

Changes from the previous head (1aed09457), on top of the rebase:

  • C's certificate fields are now settable properties on EncryptionInfoAccumulator. TdsParser.EnableSsl assigns them directly from the transport's ServerCertificateCapture, along with the callback's protocol and cipher suite when none was already determined.
  • A certificate-validation failure reports the PRELOGIN-selected mode (Full), plus the protocol and cipher suite that Managed SNI observed.
  • C's accumulator tests moved to EncryptionInfoAccumulatorTests.

Manual tests still needed

  • Real SQL Server with trusted, untrusted and host-name-mismatch certificates, on Managed SNI (Windows and Linux).
  • Named pipes over TLS.
  • TDS 8 / Encrypt=Strict. The simulated server only supports PRELOGIN TLS.
  • MARS over TLS.

Guidelines

Please review the contribution guidelines before submitting a pull request:

Copilot AI balanced review requested due to automatic review settings October 13, 2026 03:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

TLS transport changes still lack effective same-attempt isolation coverage and require the listed real-server validation.

4 open findings
What changed in this PR

Adds Slice C of connection encryption diagnostics, capturing Managed SNI server certificate details for successful and failed connections.

Changes:

  • Captures certificate hashes, validation verdicts, and chain status.
  • Propagates captured data through TDS and MARS layers.
  • Adds unit coverage and design documentation.
File Description
ManagedSniCertificateCaptureTests.cs Adds simulated TLS integration tests.
ConnectionEncryptionInfoTests.cs Updates managed-SNI hash expectations.
SniCommonServerCertificateCaptureTest.cs Tests validation and capture logic.
TdsParserStateObjectManaged.netcore.cs Exposes managed transport capture.
TdsParserStateObject.cs Defines the transport capture hook.
TdsParser.cs Records capture after TLS negotiation.
SniTcpHandle.netcore.cs Captures TCP TLS certificate details.
SniNpHandle.netcore.cs Captures Named Pipes TLS certificate details.
SniMarsHandle.netcore.cs Forwards capture through MARS handles.
SniMarsConnection.netcore.cs Exposes the physical MARS capture.
SniHandle.netcore.cs Adds the capture property contract.
SniCommon.netcore.cs Produces validation verdicts, hashes, and chain flags.
ServerCertificateCapture.cs Defines the immutable transport capture.
EncryptionInfoCapture.cs Publishes certificate fields in snapshots.
2353-connection-encryption-info.md Documents Managed SNI capture semantics.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.34091% with 17 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (dev/automation/encryption-info-slice-b-mode-protocol@dca9d26). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...oft/Data/SqlClient/ManagedSni/SniCommon.netcore.cs 93.33% 6 Missing ⚠️
...t/Data/SqlClient/ManagedSni/SniNpHandle.netcore.cs 69.23% 4 Missing ⚠️
.../SqlClient/ManagedSni/SniMarsConnection.netcore.cs 0.00% 2 Missing ⚠️
...Data/SqlClient/ManagedSni/SniMarsHandle.netcore.cs 0.00% 2 Missing ⚠️
...t/src/Microsoft/Data/SqlClient/Parser/TdsParser.cs 90.00% 2 Missing ⚠️
...oft/Data/SqlClient/ManagedSni/SniHandle.netcore.cs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@                                   Coverage Diff                                   @@
##             dev/automation/encryption-info-slice-b-mode-protocol    #4835   +/-   ##
=======================================================================================
  Coverage                                                        ?   72.45%           
=======================================================================================
  Files                                                           ?      316           
  Lines                                                           ?    71329           
  Branches                                                        ?        0           
=======================================================================================
  Hits                                                            ?    51678           
  Misses                                                          ?    19651           
  Partials                                                        ?        0           
Flag Coverage Δ
sqlclient-pr 72.45% <90.34%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI balanced review requested due to automatic review settings October 13, 2026 09:20
@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from 7d60501 to 549fd10 Compare October 13, 2026 09:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Four unresolved review findings leave regression and attempt-isolation coverage incomplete.

4 open findings

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 15, 2026 09:28
@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from 549fd10 to 502f3f3 Compare October 15, 2026 09:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Security-sensitive TLS changes still need the documented cross-platform real-server, Strict encryption, Named Pipes, and MARS validation.

4 open findings

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 19, 2026 09:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Retry isolation needs correction, and real-server, Strict, Named Pipes, and MARS TLS validation remains pending.

5 open issues

Review effort: Balanced

@priyankatiwari08 priyankatiwari08 added this to the 8.0.0-preview1 milestone Oct 19, 2026
@priyankatiwari08 priyankatiwari08 changed the title [Draft] Connection encryption info – Slice C: Managed SNI certificate capture [DO NOT MERGE] [Draft] Connection encryption info – Slice C: Managed SNI certificate capture Oct 19, 2026
@mdaigle mdaigle linked an issue Oct 20, 2026 that may be closed by this pull request
Copilot AI balanced review requested due to automatic review settings October 22, 2026 08:31
@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from e4e6869 to e9fbf4b Compare October 22, 2026 08:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human validation remains necessary for real-server certificate scenarios on Windows/Linux, named-pipe TLS, Strict/TDS 8, and MARS.

5 open issues

Review effort: Balanced

@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from e9fbf4b to 78f101e Compare October 26, 2026 17:45
@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from 88d2d83 to 73c72c2 Compare November 10, 2026 16:21
Copilot AI balanced review requested due to automatic review settings November 10, 2026 16:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Simulator coverage does not establish TLS diagnostic correctness across real SQL Server transport paths, leaving Windows/Linux validation of certificate handling, Strict/TDS 8, named-pipe TLS, and MARS outstanding.

No open issues 🎉

Review effort: Balanced

@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from 73c72c2 to 928f40a Compare November 10, 2026 18:09
@priyankatiwari08 priyankatiwari08 added the Hotfix 8.0.0 PRs that must be backported to release/8.0 label Nov 11, 2026
@priyankatiwari08
priyankatiwari08 force-pushed the dev/automation/encryption-info-slice-c-managed-sni branch from 928f40a to 8a0b3a3 Compare November 11, 2026 12:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

TLS-state capture still needs real SQL Server validation across Windows/Linux, Strict/TDS 8, named pipes, and MARS, which the limited simulator coverage and pending build/test pipeline do not establish.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Platform-sensitive TLS capture still requires real SQL Server validation on Windows/Linux, including Strict/TDS 8, named-pipe TLS, and MARS, which simulated-server tests cannot establish.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Real SQL Server validation is still outstanding for platform-sensitive TLS diagnostics on Windows/Linux, Strict/TDS 8, named pipes, and actual MARS; simulator tests do not establish certificate/cipher capture or failure behavior on these paths.

No open issues 🎉

Review effort: Balanced

benrr101
benrr101 previously approved these changes Nov 12, 2026

@benrr101 benrr101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall it's probably fine. A few stylistic changes requested, but they're not going to be blockers.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Platform-dependent TLS observation still requires real SQL Server validation on Windows/Linux for Strict/TDS 8, named pipes, and MARS, which PRELOGIN/TCP simulations cannot establish.

No open issues 🎉

Review effort: Balanced

priyankatiwari08 and others added 12 commits November 17, 2026 12:39
…tails (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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
… 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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
… 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 <223556219+Copilot@users.noreply.github.com>
- 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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Platform-sensitive TLS callback capture lacks real-server validation on Windows/Linux, Strict/TDS 8, named pipes, and MARS, leaving key behaviors unverified by the supplied TLS 1.2 TCP simulator coverage.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hotfix 8.0.0 PRs that must be backported to release/8.0

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Feature | Expose connection encryption information to clients

4 participants