1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "net/ntlm/ntlm_client.h"
#include <string>
#include "base/containers/span.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "net/ntlm/ntlm.h"
#include "net/ntlm/ntlm_buffer_reader.h"
#include "net/ntlm/ntlm_buffer_writer.h"
#include "net/ntlm/ntlm_test_data.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net::ntlm {
namespace {
std::vector<uint8_t> GenerateAuthMsg(const NtlmClient& client,
base::span<const uint8_t> challenge_msg) {
return client.GenerateAuthenticateMessage(
test::kNtlmDomain, test::kUser, test::kPassword, test::kHostnameAscii,
reinterpret_cast<const char*>(test::kChannelBindings), test::kNtlmSpn,
test::kClientTimestamp, test::kClientChallenge, challenge_msg);
}
std::vector<uint8_t> GenerateAuthMsg(const NtlmClient& client,
const NtlmBufferWriter& challenge_writer) {
return GenerateAuthMsg(client, challenge_writer.GetBuffer());
}
bool GetAuthMsgResult(const NtlmClient& client,
const NtlmBufferWriter& challenge_writer) {
return !GenerateAuthMsg(client, challenge_writer).empty();
}
bool ReadBytesPayload(NtlmBufferReader* reader, base::span<uint8_t> buffer) {
SecurityBuffer sec_buf;
return reader->ReadSecurityBuffer(&sec_buf) &&
(sec_buf.length == buffer.size()) &&
reader->ReadBytesFrom(sec_buf, buffer);
}
// Reads bytes from a payload and assigns them to a string. This makes
// no assumptions about the underlying encoding.
bool ReadStringPayload(NtlmBufferReader* reader, std::string* str) {
SecurityBuffer sec_buf;
if (!reader->ReadSecurityBuffer(&sec_buf))
return false;
str->resize(sec_buf.length);
if (!reader->ReadBytesFrom(sec_buf, base::as_writable_byte_span(*str))) {
return false;
}
return true;
}
// Reads bytes from a payload and assigns them to a string16. This makes
// no assumptions about the underlying encoding. This will fail if there
// are an odd number of bytes in the payload.
bool ReadString16Payload(NtlmBufferReader* reader, std::u16string* str) {
SecurityBuffer sec_buf;
if (!reader->ReadSecurityBuffer(&sec_buf) || (sec_buf.length % 2 != 0))
return false;
std::vector<uint8_t> raw(sec_buf.length);
if (!reader->ReadBytesFrom(sec_buf, raw))
return false;
#if defined(ARCH_CPU_BIG_ENDIAN)
for (size_t i = 0; i < raw.size(); i += 2) {
std::swap(raw[i], raw[i + 1]);
}
#endif
str->assign(reinterpret_cast<const char16_t*>(raw.data()), raw.size() / 2);
return true;
}
void MakeV2ChallengeMessage(size_t target_info_len, std::vector<uint8_t>* out) {
static const size_t kChallengeV2HeaderLen = 56;
// Leave room for the AV_PAIR header and the EOL pair.
size_t server_name_len = target_info_len - kAvPairHeaderLen * 2;
// See [MS-NLP] Section 2.2.1.2.
NtlmBufferWriter challenge(kChallengeV2HeaderLen + target_info_len);
ASSERT_TRUE(challenge.WriteMessageHeader(MessageType::kChallenge));
ASSERT_TRUE(
challenge.WriteSecurityBuffer(SecurityBuffer(0, 0))); // target name
ASSERT_TRUE(challenge.WriteFlags(NegotiateFlags::kTargetInfo));
ASSERT_TRUE(challenge.WriteZeros(kChallengeLen)); // server challenge
ASSERT_TRUE(challenge.WriteZeros(8)); // reserved
ASSERT_TRUE(challenge.WriteSecurityBuffer(
SecurityBuffer(kChallengeV2HeaderLen, target_info_len))); // target info
ASSERT_TRUE(challenge.WriteZeros(8)); // version
ASSERT_EQ(kChallengeV2HeaderLen, challenge.GetCursor());
ASSERT_TRUE(challenge.WriteAvPair(
AvPair(TargetInfoAvId::kServerName,
std::vector<uint8_t>(server_name_len, 'a'))));
ASSERT_TRUE(challenge.WriteAvPairTerminator());
ASSERT_TRUE(challenge.IsEndOfBuffer());
*out = challenge.Pass();
}
} // namespace
TEST(NtlmClientTest, SimpleConstructionV1) {
NtlmClient client(NtlmFeatures(false));
ASSERT_FALSE(client.IsNtlmV2());
ASSERT_FALSE(client.IsEpaEnabled());
ASSERT_FALSE(client.IsMicEnabled());
}
TEST(NtlmClientTest, VerifyNegotiateMessageV1) {
NtlmClient client(NtlmFeatures(false));
std::vector<uint8_t> result = client.GetNegotiateMessage();
ASSERT_EQ(kNegotiateMessageLen, result.size());
ASSERT_EQ(0, memcmp(test::kExpectedNegotiateMsg, result.data(),
kNegotiateMessageLen));
}
TEST(NtlmClientTest, MinimalStructurallyValidChallenge) {
NtlmClient client(NtlmFeatures(false));
NtlmBufferWriter writer(kMinChallengeHeaderLen);
ASSERT_TRUE(writer.WriteBytes(
base::span(test::kMinChallengeMessage).first<kMinChallengeHeaderLen>()));
ASSERT_TRUE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, MinimalStructurallyValidChallengeZeroOffset) {
NtlmClient client(NtlmFeatures(false));
// The spec (2.2.1.2) states that the length SHOULD be 0 and the offset
// SHOULD be where the payload would be if it was present. This is the
// expected response from a compliant server when no target name is sent.
// In reality the offset should always be ignored if the length is zero.
// Also implementations often just write zeros.
uint8_t raw[kMinChallengeHeaderLen];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Modify the default valid message to overwrite the offset to zero.
ASSERT_NE(0x00, raw[16]);
raw[16] = 0x00;
NtlmBufferWriter writer(kMinChallengeHeaderLen);
ASSERT_TRUE(writer.WriteBytes(raw));
ASSERT_TRUE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, ChallengeMsgTooShort) {
NtlmClient client(NtlmFeatures(false));
// Fail because the minimum size valid message is 32 bytes.
NtlmBufferWriter writer(kMinChallengeHeaderLen - 1);
ASSERT_TRUE(writer.WriteBytes(base::span(test::kMinChallengeMessage)
.first<kMinChallengeHeaderLen - 1>()));
ASSERT_FALSE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, ChallengeMsgNoSig) {
NtlmClient client(NtlmFeatures(false));
// Fail because the first 8 bytes don't match "NTLMSSP\0"
uint8_t raw[kMinChallengeHeaderLen];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Modify the default valid message to overwrite the last byte of the
// signature.
ASSERT_NE(0xff, raw[7]);
raw[7] = 0xff;
NtlmBufferWriter writer(kMinChallengeHeaderLen);
ASSERT_TRUE(writer.WriteBytes(raw));
ASSERT_FALSE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, ChallengeMsgWrongMessageType) {
NtlmClient client(NtlmFeatures(false));
// Fail because the message type should be MessageType::kChallenge
// (0x00000002)
uint8_t raw[kMinChallengeHeaderLen];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Modify the message type.
ASSERT_NE(0x03, raw[8]);
raw[8] = 0x03;
NtlmBufferWriter writer(kMinChallengeHeaderLen);
ASSERT_TRUE(writer.WriteBytes(raw));
ASSERT_FALSE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, ChallengeWithNoTargetName) {
NtlmClient client(NtlmFeatures(false));
// The spec (2.2.1.2) states that the length SHOULD be 0 and the offset
// SHOULD be where the payload would be if it was present. This is the
// expected response from a compliant server when no target name is sent.
// In reality the offset should always be ignored if the length is zero.
// Also implementations often just write zeros.
uint8_t raw[kMinChallengeHeaderLen];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Modify the default valid message to overwrite the offset to zero.
ASSERT_NE(0x00, raw[16]);
raw[16] = 0x00;
NtlmBufferWriter writer(kMinChallengeHeaderLen);
ASSERT_TRUE(writer.WriteBytes(raw));
ASSERT_TRUE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, Type2MessageWithTargetName) {
NtlmClient client(NtlmFeatures(false));
// One extra byte is provided for target name.
uint8_t raw[kMinChallengeHeaderLen + 1];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Put something in the target name.
raw[kMinChallengeHeaderLen] = 'Z';
// Modify the default valid message to indicate 1 byte is present in the
// target name payload.
ASSERT_NE(0x01, raw[12]);
ASSERT_EQ(0x00, raw[13]);
ASSERT_NE(0x01, raw[14]);
ASSERT_EQ(0x00, raw[15]);
raw[12] = 0x01;
raw[14] = 0x01;
NtlmBufferWriter writer(kChallengeHeaderLen + 1);
ASSERT_TRUE(writer.WriteBytes(raw));
ASSERT_TRUE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, NoTargetNameOverflowFromOffset) {
NtlmClient client(NtlmFeatures(false));
uint8_t raw[kMinChallengeHeaderLen];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Modify the default valid message to claim that the target name field is 1
// byte long overrunning the end of the message message.
ASSERT_NE(0x01, raw[12]);
ASSERT_EQ(0x00, raw[13]);
ASSERT_NE(0x01, raw[14]);
ASSERT_EQ(0x00, raw[15]);
raw[12] = 0x01;
raw[14] = 0x01;
NtlmBufferWriter writer(kMinChallengeHeaderLen);
ASSERT_TRUE(writer.WriteBytes(raw));
// The above malformed message could cause an implementation to read outside
// the message buffer because the offset is past the end of the message.
// Verify it gets rejected.
ASSERT_FALSE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, NoTargetNameOverflowFromLength) {
NtlmClient client(NtlmFeatures(false));
// Message has 1 extra byte of space after the header for the target name.
// One extra byte is provided for target name.
uint8_t raw[kMinChallengeHeaderLen + 1];
memcpy(raw, test::kMinChallengeMessage, kMinChallengeHeaderLen);
// Put something in the target name.
raw[kMinChallengeHeaderLen] = 'Z';
// Modify the default valid message to indicate 2 bytes are present in the
// target name payload (however there is only space for 1).
ASSERT_NE(0x02, raw[12]);
ASSERT_EQ(0x00, raw[13]);
ASSERT_NE(0x02, raw[14]);
ASSERT_EQ(0x00, raw[15]);
raw[12] = 0x02;
raw[14] = 0x02;
NtlmBufferWriter writer(kMinChallengeHeaderLen + 1);
ASSERT_TRUE(writer.WriteBytes(raw));
// The above malformed message could cause an implementation
// to read outside the message buffer because the length is
// longer than available space. Verify it gets rejected.
ASSERT_FALSE(GetAuthMsgResult(client, writer));
}
TEST(NtlmClientTest, Type3UnicodeWithSessionSecuritySpecTest) {
NtlmClient client(NtlmFeatures(false));
std::vector<uint8_t> result = GenerateAuthMsg(client, test::kChallengeMsgV1);
ASSERT_FALSE(result.empty());
ASSERT_EQ(std::size(test::kExpectedAuthenticateMsgSpecResponseV1),
result.size());
ASSERT_EQ(0, memcmp(test::kExpectedAuthenticateMsgSpecResponseV1,
result.data(), result.size()));
}
TEST(NtlmClientTest, Type3WithoutUnicode) {
NtlmClient client(NtlmFeatures(false));
std::vector<uint8_t> result =
GenerateAuthMsg(client, base::span(test::kMinChallengeMessageNoUnicode)
.first<kMinChallengeHeaderLen>());
ASSERT_FALSE(result.empty());
NtlmBufferReader reader(result);
ASSERT_TRUE(reader.MatchMessageHeader(MessageType::kAuthenticate));
// Read the LM and NTLM Response Payloads.
uint8_t actual_lm_response[kResponseLenV1];
uint8_t actual_ntlm_response[kResponseLenV1];
ASSERT_TRUE(ReadBytesPayload(&reader, actual_lm_response));
ASSERT_TRUE(ReadBytesPayload(&reader, actual_ntlm_response));
ASSERT_EQ(0, memcmp(test::kExpectedLmResponseWithV1SS, actual_lm_response,
kResponseLenV1));
ASSERT_EQ(0, memcmp(test::kExpectedNtlmResponseWithV1SS, actual_ntlm_response,
kResponseLenV1));
std::string domain;
std::string username;
std::string hostname;
ASSERT_TRUE(ReadStringPayload(&reader, &domain));
ASSERT_EQ(test::kNtlmDomainAscii, domain);
ASSERT_TRUE(ReadStringPayload(&reader, &username));
ASSERT_EQ(test::kUserAscii, username);
ASSERT_TRUE(ReadStringPayload(&reader, &hostname));
ASSERT_EQ(test::kHostnameAscii, hostname);
// The session key is not used in HTTP. Since NTLMSSP_NEGOTIATE_KEY_EXCH
// was not sent this is empty.
ASSERT_TRUE(reader.MatchEmptySecurityBuffer());
// Verify the unicode flag is not set and OEM flag is.
NegotiateFlags flags;
ASSERT_TRUE(reader.ReadFlags(&flags));
ASSERT_EQ(NegotiateFlags::kNone, flags & NegotiateFlags::kUnicode);
ASSERT_EQ(NegotiateFlags::kOem, flags & NegotiateFlags::kOem);
}
TEST(NtlmClientTest, ClientDoesNotDowngradeSessionSecurity) {
NtlmClient client(NtlmFeatures(false));
std::vector<uint8_t> result =
GenerateAuthMsg(client, base::span(test::kMinChallengeMessageNoSS)
.first<kMinChallengeHeaderLen>());
ASSERT_FALSE(result.empty());
NtlmBufferReader reader(result);
ASSERT_TRUE(reader.MatchMessageHeader(MessageType::kAuthenticate));
// Read the LM and NTLM Response Payloads.
uint8_t actual_lm_response[kResponseLenV1];
uint8_t actual_ntlm_response[kResponseLenV1];
ASSERT_TRUE(ReadBytesPayload(&reader, actual_lm_response));
ASSERT_TRUE(ReadBytesPayload(&reader, actual_ntlm_response));
// The important part of this test is that even though the
// server told the client to drop session security. The client
// DID NOT drop it.
ASSERT_EQ(0, memcmp(test::kExpectedLmResponseWithV1SS, actual_lm_response,
kResponseLenV1));
ASSERT_EQ(0, memcmp(test::kExpectedNtlmResponseWithV1SS, actual_ntlm_response,
kResponseLenV1));
std::u16string domain;
std::u16string username;
std::u16string hostname;
ASSERT_TRUE(ReadString16Payload(&reader, &domain));
ASSERT_EQ(test::kNtlmDomain, domain);
ASSERT_TRUE(ReadString16Payload(&reader, &username));
ASSERT_EQ(test::kUser, username);
ASSERT_TRUE(ReadString16Payload(&reader, &hostname));
ASSERT_EQ(test::kHostname, hostname);
// The session key is not used in HTTP. Since NTLMSSP_NEGOTIATE_KEY_EXCH
// was not sent this is empty.
ASSERT_TRUE(reader.MatchEmptySecurityBuffer());
// Verify the unicode and session security flag is set.
NegotiateFlags flags;
ASSERT_TRUE(reader.ReadFlags(&flags));
ASSERT_EQ(NegotiateFlags::kUnicode, flags & NegotiateFlags::kUnicode);
ASSERT_EQ(NegotiateFlags::kExtendedSessionSecurity,
flags & NegotiateFlags::kExtendedSessionSecurity);
}
// ------------------------------------------------
// NTLM V2 specific tests.
// ------------------------------------------------
TEST(NtlmClientTest, SimpleConstructionV2) {
NtlmClient client(NtlmFeatures(true));
ASSERT_TRUE(client.IsNtlmV2());
ASSERT_TRUE(client.IsEpaEnabled());
ASSERT_TRUE(client.IsMicEnabled());
}
TEST(NtlmClientTest, VerifyNegotiateMessageV2) {
NtlmClient client(NtlmFeatures(true));
std::vector<uint8_t> result = client.GetNegotiateMessage();
ASSERT_FALSE(result.empty());
ASSERT_EQ(std::size(test::kExpectedNegotiateMsg), result.size());
ASSERT_EQ(0,
memcmp(test::kExpectedNegotiateMsg, result.data(), result.size()));
}
TEST(NtlmClientTest, VerifyAuthenticateMessageV2) {
// Generate the auth message from the client based on the test challenge
// message.
NtlmClient client(NtlmFeatures(true));
std::vector<uint8_t> result =
GenerateAuthMsg(client, test::kChallengeMsgFromSpecV2);
ASSERT_FALSE(result.empty());
ASSERT_EQ(std::size(test::kExpectedAuthenticateMsgSpecResponseV2),
result.size());
ASSERT_EQ(0, memcmp(test::kExpectedAuthenticateMsgSpecResponseV2,
result.data(), result.size()));
}
TEST(NtlmClientTest,
VerifyAuthenticateMessageInResponseToChallengeWithoutTargetInfoV2) {
// Test how the V2 client responds when the server sends a challenge that
// does not contain target info. eg. Windows 2003 and earlier do not send
// this. See [MS-NLMP] Appendix B Item 8. These older Windows servers
// support NTLMv2 but don't send target info. Other implementations may
// also be affected.
NtlmClient client(NtlmFeatures(true));
std::vector<uint8_t> result = GenerateAuthMsg(client, test::kChallengeMsgV1);
ASSERT_FALSE(result.empty());
ASSERT_EQ(std::size(test::kExpectedAuthenticateMsgToOldV1ChallegeV2),
result.size());
ASSERT_EQ(0, memcmp(test::kExpectedAuthenticateMsgToOldV1ChallegeV2,
result.data(), result.size()));
}
// When the challenge message's target info is maximum size, adding new AV_PAIRs
// to the response will overflow SecurityBuffer. Test that we handle this.
TEST(NtlmClientTest, AvPairsOverflow) {
{
NtlmClient client(NtlmFeatures(/*enable_NTLMv2=*/true));
std::vector<uint8_t> short_challenge;
ASSERT_NO_FATAL_FAILURE(MakeV2ChallengeMessage(0xfff, &short_challenge));
EXPECT_FALSE(GenerateAuthMsg(client, short_challenge).empty());
}
{
NtlmClient client(NtlmFeatures(/*enable_NTLMv2=*/true));
std::vector<uint8_t> long_challenge;
ASSERT_NO_FATAL_FAILURE(MakeV2ChallengeMessage(0xffff, &long_challenge));
EXPECT_TRUE(GenerateAuthMsg(client, long_challenge).empty());
}
}
} // namespace net::ntlm
|