Skip to content

Conversation

@filipnavara
Copy link
Member

@filipnavara filipnavara commented Jun 25, 2022

This reduces allocations during NTLM/Negotiate authentication by reusing an existing buffer. It saves about 3Kb of allocated memory for a typical NTLM authentication exchange on Windows.

It also converts a couple of the internals to use Span/Memory. This would be a prerequisite for offering public API for encryption/decryption on NegotiateAuthentication class. As a side effect, it removes a big chunk of cumbersome interop marshaling on Windows.

@ghost ghost added area-System.Net.Security community-contribution Indicates that the PR has been added by a community member labels Jun 25, 2022
@ghost
Copy link

ghost commented Jun 25, 2022

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

Issue Details

This reduces allocations during NTLM/Negotiate authentication by reusing an existing buffer. It saves about 3Kb of allocated memory for a typical NTLM authentication exchange.

It also converts a couple of the internals to use Span/Memory. This would be a prerequisite for offering public API for encryption/decryption on NegotiateAuthentication class. As a side effect, it removes a big chunk of cumbersome interop marshaling on Windows.

Author: filipnavara
Assignees: -
Labels:

area-System.Net.Security

Milestone: -

@filipnavara filipnavara requested review from stephentoub and wfurt June 25, 2022 13:02
@filipnavara
Copy link
Member Author

filipnavara commented Jun 25, 2022

While refactoring the code I found a couple of bugs that I plan to fix in subsequent PRs:

  • Negotiate authentication on SMTP only works by happy coincidence, the last token in authentication is not processed correctly
  • Incorrect error code is used on the wire for NegotiateStream to indicate denied login. This happened during conversion to support multiple platforms
  • Some of the code in NegotiateStream is reading numbers using BitConverter which would produce incorrect results on big-endian machines

@filipnavara filipnavara force-pushed the memorify-security-buffer branch from 52b3db7 to 1a2892e Compare June 25, 2022 15:33
@filipnavara filipnavara force-pushed the memorify-security-buffer branch from 1a2892e to 3b4aff6 Compare June 25, 2022 16:44
out Status minorStatus,
SafeGssContextHandle? contextHandle,
byte[] inputBytes,
byte* inputBytes,
Copy link
Member

Choose a reason for hiding this comment

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

Do we need offset any more? It feels like if we pass pointer we can just do count.

Copy link
Member Author

@filipnavara filipnavara Jun 27, 2022

Choose a reason for hiding this comment

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

We don't. Unfortunately, dotnet/sqlclient uses the native APIs, so I didn't feel confident in changing it. I am quite sure they don't use this particular API though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't the pointer change by itself be breaking for them?

Copy link
Member Author

@filipnavara filipnavara Jun 27, 2022

Choose a reason for hiding this comment

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

It doesn't change the native code side, the prototype remains unchanged there. They basically copied the managed side of the interop. (We cannot change the signature, add parameters, remove parameters, or change their types. We can change how they are marshalled on the C# side though.)

Copy link
Member

Choose a reason for hiding this comment

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

cc: @JRahnama just in case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I cross-checked with the SqlClient source and this particular native method is not referenced so we can remove the offset parameter. I will do that in a follow-up PR (#71373) since I need to update the native interop there anyway.

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

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

nice. Thanks @filipnavara

@wfurt wfurt merged commit 1e8eaef into dotnet:main Jun 29, 2022
@karelz karelz added this to the 7.0.0 milestone Jul 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 18, 2022
@filipnavara filipnavara deleted the memorify-security-buffer branch June 5, 2025 07:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Net.Security community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants