Skip to content

Reset server MAC after TransformFinalBlock on .NET FW - #1830

Merged
WojciechNagorski merged 2 commits into
sshnet:developfrom
scott-xu:fix/initialize-mac-netframework
Sep 1, 2026
Merged

Reset server MAC after TransformFinalBlock on .NET FW#1830
WojciechNagorski merged 2 commits into
sshnet:developfrom
scott-xu:fix/initialize-mac-netframework

Conversation

@scott-xu

Copy link
Copy Markdown
Collaborator

Call _serverMac.Initialize() after TransformFinalBlock when running on .NET Framework. This ensures the MAC is properly reset, addressing differences in cryptographic API behavior across different versions of mscorlib.dll.

Close #1829

Comment thread src/Renci.SshNet/Session.cs Outdated
@scott-xu
scott-xu force-pushed the fix/initialize-mac-netframework branch from b407a2d to f8d1e3b Compare August 24, 2026 13:14
Add `#if NETFRAMEWORK` block to call `_serverMac.Initialize()` after `TransformFinalBlock` in `ReceiveMessage`. Ensures MAC is reset for reuse, addressing issues on older .NET Framework builds (e.g., mscorlib.dll 4.8.4110.0).
@scott-xu
scott-xu force-pushed the fix/initialize-mac-netframework branch from f8d1e3b to b0a4569 Compare August 24, 2026 13:18

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

MAC reinitialization should be unconditional per the API contract, with regression coverage for repeated verification.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes receive-MAC reuse after TransformFinalBlock, addressing second-packet failures on older .NET Framework builds.

Changes:

  • Resets server MAC state in ETM and non-ETM receive paths.
  • Applies resets conditionally to .NET Framework.
File summaries
File Description
src/Renci.SshNet/Session.cs Resets receive-MAC state after packet verification.
Review details

Suppressed comments (1)

src/Renci.SshNet/Session.cs:1386

  • The same API contract applies in the non-ETM path: after TransformFinalBlock, reusing the instance without Initialize() is not guaranteed on any target framework. Remove the conditional compilation here as well.
#if NETFRAMEWORK
                // Ensure MAC is reset so that it can be reused for the next packet
                // It is only required for certain older .NET Framework builds, e.g., mscorlib.dll version 4.8.4110.0
                _serverMac.Initialize();
#endif
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1330 to +1334
#if NETFRAMEWORK
// Ensure MAC is reset so that it can be reused for the next packet
// It is only required for certain older .NET Framework builds, e.g., mscorlib.dll version 4.8.4110.0
_serverMac.Initialize();
#endif
#if NETFRAMEWORK
// Ensure MAC is reset so that it can be reused for the next packet
// It is only required for certain older .NET Framework builds, e.g., mscorlib.dll version 4.8.4110.0
_serverMac.Initialize();

@WojciechNagorski WojciechNagorski left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. I verified this PR using Copilot and it produced the same solution. #1834

I think it's good to leave #if NETFRAMEWORK, so that this change only applies to the old framework on which this problem occurs.

@WojciechNagorski
WojciechNagorski merged commit f099365 into sshnet:develop Sep 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MAC error / CryptographicException on .NET Framework in 2026.0.0: _serverMac is reused across packets without Initialize()

4 participants