Fix ManagedNtlm on big-endian architectures#124598
Fix ManagedNtlm on big-endian architectures#124598ShreyaLaxminarayan wants to merge 2 commits intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones |
| // Derive session base key | ||
| Span<byte> sessionBaseKey = stackalloc byte[HMACMD5.HashSizeInBytes]; | ||
| int sessionKeyWritten = HMACMD5.HashData(ntlm2hash, responseAsSpan.Slice(response.NtChallengeResponse.PayloadOffset, 16), sessionBaseKey); | ||
| var payloadSlice = (BitConverter.IsLittleEndian) ? responseAsSpan.Slice(response.NtChallengeResponse.PayloadOffset, 16) : responseAsSpan.Slice(BinaryPrimitives.ReverseEndianness(response.NtChallengeResponse.PayloadOffset), 16); |
There was a problem hiding this comment.
I would probably get correct PayloadOffset value first instead of duplicating the whole Slice block. But that is mostly style and readability preference.
| NtlmHeader.CopyTo(asBytes); | ||
| message.Header.MessageType = MessageType.Negotiate; | ||
| message.Flags = requiredFlags; | ||
| message.Flags = BitConverter.IsLittleEndian ? requiredFlags : (Flags)BinaryPrimitives.ReverseEndianness((uint)requiredFlags); |
There was a problem hiding this comment.
I am not a big fan of those manual branches. For one, when you look at message.Flags using a debugger, the value will be misrepresented on BE systems, and if you miss one place, you end up with hard to debug bugs. Other questions arise from looking at the code like why do we reverse some properties and not others....
Can we modify the definition of NegotiateMessage to have setter/getter of those properties handle the endianness difference transparently to the caller?
There was a problem hiding this comment.
Out of the fields that are set for NegotiateMessage, message.Header.MessageType is a byte and is represented the same in little-endian and big-endian. Flags is an uint and needs to be endian specific. Version is a struct with ProductBuild as ushort and the rest are bytes, and hence only ProductBuild needs to be reversed. I will implement it using getters and setters.
Fixes multiple endianness bugs in ManagedNtlm that caused incorrect behavior on big-endian platforms.
Validated via existing Ntlm test cases.