summaryrefslogtreecommitdiff
path: root/chromium/net/ntlm/ntlm_unittest.cc
blob: 6db29dae271a08b361bea0d34cbd0924dba10358 (plain)
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
// 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.

// Tests on exact results from cryptographic operations are based on test data
// provided in [MS-NLMP] Version 28.0 [1] Section 4.2.
//
// Additional sanity checks on the low level hashing operations test for
// properties of the outputs, such as whether the hashes change, whether they
// should be zeroed out, or whether they should be the same or different.
//
// [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx

#include "net/ntlm/ntlm.h"

#include <algorithm>
#include <iterator>
#include <string>

#include "base/strings/utf_string_conversions.h"
#include "net/ntlm/ntlm_test_data.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net::ntlm {

namespace {

AvPair MakeDomainAvPair() {
  return AvPair(TargetInfoAvId::kDomainName,
                std::vector<uint8_t>{std::begin(test::kNtlmDomainRaw),
                                     std::end(test::kNtlmDomainRaw)});
}

AvPair MakeServerAvPair() {
  return AvPair(TargetInfoAvId::kServerName,
                std::vector<uint8_t>{std::begin(test::kServerRaw),
                                     std::end(test::kServerRaw)});
}

// Clear the least significant bit in each byte.
void ClearLsb(base::span<uint8_t> data) {
  for (uint8_t& byte : data) {
    byte &= ~1;
  }
}

}  // namespace

TEST(NtlmTest, MapHashToDesKeysAllOnes) {
  // Test mapping an NTLM hash with all 1 bits.
  const uint8_t hash[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  const uint8_t expected[24] = {0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
                                0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
                                0xfe, 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00};

  uint8_t result[24];
  Create3DesKeysFromNtlmHash(hash, result);
  // The least significant bit in result from |Create3DesKeysFromNtlmHash|
  // is undefined, so clear it to do memcmp.
  ClearLsb(result);

  EXPECT_TRUE(std::equal(std::begin(expected), std::end(expected),
                         std::begin(result), std::end(result)));
}

TEST(NtlmTest, MapHashToDesKeysAllZeros) {
  // Test mapping an NTLM hash with all 0 bits.
  const uint8_t hash[16] = {0x00};
  const uint8_t expected[24] = {0x00};

  uint8_t result[24];
  Create3DesKeysFromNtlmHash(hash, result);
  // The least significant bit in result from |Create3DesKeysFromNtlmHash|
  // is undefined, so clear it to do memcmp.
  ClearLsb(result);

  EXPECT_TRUE(std::equal(std::begin(expected), std::end(expected),
                         std::begin(result), std::end(result)));
}

TEST(NtlmTest, MapHashToDesKeysAlternatingBits) {
  // Test mapping an NTLM hash with alternating 0 and 1 bits.
  const uint8_t hash[16] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
                            0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
  const uint8_t expected[24] = {0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x54,
                                0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x54,
                                0xaa, 0x54, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00};

  uint8_t result[24];
  Create3DesKeysFromNtlmHash(hash, result);
  // The least significant bit in result from |Create3DesKeysFromNtlmHash|
  // is undefined, so clear it to do memcmp.
  ClearLsb(result);

  EXPECT_TRUE(std::equal(std::begin(expected), std::end(expected),
                         std::begin(result), std::end(result)));
}

TEST(NtlmTest, GenerateNtlmHashV1PasswordSpecTests) {
  uint8_t hash[kNtlmHashLen];
  GenerateNtlmHashV1(test::kPassword, hash);
  ASSERT_EQ(0, memcmp(hash, test::kExpectedNtlmHashV1, kNtlmHashLen));
}

TEST(NtlmTest, GenerateNtlmHashV1PasswordChangesHash) {
  std::u16string password1 = u"pwd01";
  std::u16string password2 = u"pwd02";
  uint8_t hash1[kNtlmHashLen];
  uint8_t hash2[kNtlmHashLen];

  GenerateNtlmHashV1(password1, hash1);
  GenerateNtlmHashV1(password2, hash2);

  // Verify that the hash is different with a different password.
  ASSERT_NE(0, memcmp(hash1, hash2, kNtlmHashLen));
}

TEST(NtlmTest, GenerateResponsesV1SpecTests) {
  uint8_t lm_response[kResponseLenV1];
  uint8_t ntlm_response[kResponseLenV1];
  GenerateResponsesV1(test::kPassword, test::kServerChallenge, lm_response,
                      ntlm_response);

  ASSERT_EQ(
      0, memcmp(test::kExpectedNtlmResponseV1, ntlm_response, kResponseLenV1));

  // This implementation never sends an LMv1 response (spec equivalent of the
  // client variable NoLMResponseNTLMv1 being false) so the LM response is
  // equal to the NTLM response when
  // NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY is not negotiated. See
  // [MS-NLMP] Section 3.3.1.
  ASSERT_EQ(0,
            memcmp(test::kExpectedNtlmResponseV1, lm_response, kResponseLenV1));
}

TEST(NtlmTest, GenerateResponsesV1WithSessionSecuritySpecTests) {
  uint8_t lm_response[kResponseLenV1];
  uint8_t ntlm_response[kResponseLenV1];
  GenerateResponsesV1WithSessionSecurity(
      test::kPassword, test::kServerChallenge, test::kClientChallenge,
      lm_response, ntlm_response);

  ASSERT_EQ(0, memcmp(test::kExpectedLmResponseWithV1SS, lm_response,
                      kResponseLenV1));
  ASSERT_EQ(0, memcmp(test::kExpectedNtlmResponseWithV1SS, ntlm_response,
                      kResponseLenV1));
}

TEST(NtlmTest, GenerateResponsesV1WithSessionSecurityClientChallengeUsed) {
  uint8_t lm_response1[kResponseLenV1];
  uint8_t lm_response2[kResponseLenV1];
  uint8_t ntlm_response1[kResponseLenV1];
  uint8_t ntlm_response2[kResponseLenV1];
  uint8_t client_challenge1[kChallengeLen];
  uint8_t client_challenge2[kChallengeLen];

  memset(client_challenge1, 0x01, kChallengeLen);
  memset(client_challenge2, 0x02, kChallengeLen);

  GenerateResponsesV1WithSessionSecurity(
      test::kPassword, test::kServerChallenge, client_challenge1, lm_response1,
      ntlm_response1);
  GenerateResponsesV1WithSessionSecurity(
      test::kPassword, test::kServerChallenge, client_challenge2, lm_response2,
      ntlm_response2);

  // The point of session security is that the client can introduce some
  // randomness, so verify different client_challenge gives a different result.
  ASSERT_NE(0, memcmp(lm_response1, lm_response2, kResponseLenV1));
  ASSERT_NE(0, memcmp(ntlm_response1, ntlm_response2, kResponseLenV1));

  // With session security the lm and ntlm hash should be different.
  ASSERT_NE(0, memcmp(lm_response1, ntlm_response1, kResponseLenV1));
  ASSERT_NE(0, memcmp(lm_response2, ntlm_response2, kResponseLenV1));
}

TEST(NtlmTest, GenerateResponsesV1WithSessionSecurityVerifySSUsed) {
  uint8_t lm_response1[kResponseLenV1];
  uint8_t lm_response2[kResponseLenV1];
  uint8_t ntlm_response1[kResponseLenV1];
  uint8_t ntlm_response2[kResponseLenV1];

  GenerateResponsesV1WithSessionSecurity(
      test::kPassword, test::kServerChallenge, test::kClientChallenge,
      lm_response1, ntlm_response1);
  GenerateResponsesV1(test::kPassword, test::kServerChallenge, lm_response2,
                      ntlm_response2);

  // Verify that the responses with session security are not the
  // same as without it.
  ASSERT_NE(0, memcmp(lm_response1, lm_response2, kResponseLenV1));
  ASSERT_NE(0, memcmp(ntlm_response1, ntlm_response2, kResponseLenV1));
}

// ------------------------------------------------
// NTLM V2 specific tests.
// ------------------------------------------------

TEST(NtlmTest, GenerateNtlmHashV2SpecTests) {
  uint8_t hash[kNtlmHashLen];
  GenerateNtlmHashV2(test::kNtlmDomain, test::kUser, test::kPassword, hash);
  ASSERT_EQ(0, memcmp(hash, test::kExpectedNtlmHashV2, kNtlmHashLen));
}

TEST(NtlmTest, GenerateProofInputV2SpecTests) {
  std::vector<uint8_t> proof_input;
  proof_input =
      GenerateProofInputV2(test::kServerTimestamp, test::kClientChallenge);
  ASSERT_EQ(kProofInputLenV2, proof_input.size());

  // |GenerateProofInputV2| generates the first |kProofInputLenV2| bytes of
  // what [MS-NLMP] calls "temp".
  ASSERT_EQ(0, memcmp(test::kExpectedTempFromSpecV2, proof_input.data(),
                      proof_input.size()));
}

TEST(NtlmTest, GenerateNtlmProofV2SpecTests) {
  // Only the first |kProofInputLenV2| bytes of |test::kExpectedTempFromSpecV2|
  // are read and this is equivalent to the output of |GenerateProofInputV2|.
  // See |GenerateProofInputV2SpecTests| for validation.
  uint8_t v2_proof[kNtlmProofLenV2];
  GenerateNtlmProofV2(test::kExpectedNtlmHashV2, test::kServerChallenge,
                      base::make_span(test::kExpectedTempFromSpecV2)
                          .subspan<0, kProofInputLenV2>(),
                      test::kExpectedTargetInfoFromSpecV2, v2_proof);

  ASSERT_EQ(0,
            memcmp(test::kExpectedProofFromSpecV2, v2_proof, kNtlmProofLenV2));
}

TEST(NtlmTest, GenerateSessionBaseKeyV2SpecTests) {
  // Generate the session base key.
  uint8_t session_base_key[kSessionKeyLenV2];
  GenerateSessionBaseKeyV2(test::kExpectedNtlmHashV2,
                           test::kExpectedProofFromSpecV2, session_base_key);

  // Verify the session base key.
  ASSERT_EQ(0, memcmp(test::kExpectedSessionBaseKeyFromSpecV2, session_base_key,
                      kSessionKeyLenV2));
}

TEST(NtlmTest, GenerateSessionBaseKeyWithClientTimestampV2SpecTests) {
  // Generate the session base key.
  uint8_t session_base_key[kSessionKeyLenV2];
  GenerateSessionBaseKeyV2(
      test::kExpectedNtlmHashV2,
      test::kExpectedProofSpecResponseWithClientTimestampV2, session_base_key);

  // Verify the session base key.
  ASSERT_EQ(0, memcmp(test::kExpectedSessionBaseKeyWithClientTimestampV2,
                      session_base_key, kSessionKeyLenV2));
}

TEST(NtlmTest, GenerateChannelBindingHashV2SpecTests) {
  uint8_t v2_channel_binding_hash[kChannelBindingsHashLen];
  GenerateChannelBindingHashV2(
      reinterpret_cast<const char*>(test::kChannelBindings),
      v2_channel_binding_hash);

  ASSERT_EQ(0, memcmp(test::kExpectedChannelBindingHashV2,
                      v2_channel_binding_hash, kChannelBindingsHashLen));
}

TEST(NtlmTest, GenerateMicV2Simple) {
  // The MIC is defined as HMAC_MD5(session_base_key, CONCAT(a, b, c)) where
  // a, b, c are the negotiate, challenge and authenticate messages
  // respectively.
  //
  // This compares a simple set of inputs to a precalculated result.
  const std::vector<uint8_t> a{0x44, 0x44, 0x44, 0x44};
  const std::vector<uint8_t> b{0x66, 0x66, 0x66, 0x66, 0x66, 0x66};
  const std::vector<uint8_t> c{0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88};

  // expected_mic = HMAC_MD5(
  //          key=8de40ccadbc14a82f15cb0ad0de95ca3,
  //          input=444444446666666666668888888888888888)
  uint8_t expected_mic[kMicLenV2] = {0x71, 0xfe, 0xef, 0xd7, 0x76, 0xd4,
                                     0x42, 0xa8, 0x5f, 0x6e, 0x18, 0x0a,
                                     0x6b, 0x02, 0x47, 0x20};

  uint8_t mic[kMicLenV2];
  GenerateMicV2(test::kExpectedSessionBaseKeyFromSpecV2, a, b, c, mic);
  ASSERT_EQ(0, memcmp(expected_mic, mic, kMicLenV2));
}

TEST(NtlmTest, GenerateMicSpecResponseV2) {
  std::vector<uint8_t> authenticate_msg(
      std::begin(test::kExpectedAuthenticateMsgSpecResponseV2),
      std::end(test::kExpectedAuthenticateMsgSpecResponseV2));
  memset(&authenticate_msg[kMicOffsetV2], 0x00, kMicLenV2);

  uint8_t mic[kMicLenV2];
  GenerateMicV2(test::kExpectedSessionBaseKeyWithClientTimestampV2,
                test::kExpectedNegotiateMsg, test::kChallengeMsgFromSpecV2,
                authenticate_msg, mic);
  ASSERT_EQ(0, memcmp(test::kExpectedMicV2, mic, kMicLenV2));
}

TEST(NtlmTest, GenerateUpdatedTargetInfo) {
  // This constructs a std::vector<AvPair> that corresponds to the test input
  // values in [MS-NLMP] Section 4.2.4.
  std::vector<AvPair> server_av_pairs;
  server_av_pairs.push_back(MakeDomainAvPair());
  server_av_pairs.push_back(MakeServerAvPair());

  uint64_t server_timestamp = UINT64_MAX;
  std::vector<uint8_t> updated_target_info = GenerateUpdatedTargetInfo(
      true, true, reinterpret_cast<const char*>(test::kChannelBindings),
      test::kNtlmSpn, server_av_pairs, &server_timestamp);

  // With MIC and EPA enabled 3 additional AvPairs will be added.
  // 1) A flags AVPair with the MIC_PRESENT bit set.
  // 2) A channel bindings AVPair containing the channel bindings hash.
  // 3) A target name AVPair containing the SPN of the server.
  ASSERT_EQ(std::size(test::kExpectedTargetInfoSpecResponseV2),
            updated_target_info.size());
  ASSERT_EQ(0, memcmp(test::kExpectedTargetInfoSpecResponseV2,
                      updated_target_info.data(), updated_target_info.size()));
}

TEST(NtlmTest, GenerateUpdatedTargetInfoNoEpaOrMic) {
  // This constructs a std::vector<AvPair> that corresponds to the test input
  // values in [MS-NLMP] Section 4.2.4.
  std::vector<AvPair> server_av_pairs;
  server_av_pairs.push_back(MakeDomainAvPair());
  server_av_pairs.push_back(MakeServerAvPair());

  uint64_t server_timestamp = UINT64_MAX;

  // When both EPA and MIC are false the target info does not get modified by
  // the client.
  std::vector<uint8_t> updated_target_info = GenerateUpdatedTargetInfo(
      false, false, reinterpret_cast<const char*>(test::kChannelBindings),
      test::kNtlmSpn, server_av_pairs, &server_timestamp);
  ASSERT_EQ(std::size(test::kExpectedTargetInfoFromSpecV2),
            updated_target_info.size());
  ASSERT_EQ(0, memcmp(test::kExpectedTargetInfoFromSpecV2,
                      updated_target_info.data(), updated_target_info.size()));
}

TEST(NtlmTest, GenerateUpdatedTargetInfoWithServerTimestamp) {
  // This constructs a std::vector<AvPair> that corresponds to the test input
  // values in [MS-NLMP] Section 4.2.4 with an additional server timestamp.
  std::vector<AvPair> server_av_pairs;
  server_av_pairs.push_back(MakeDomainAvPair());
  server_av_pairs.push_back(MakeServerAvPair());

  // Set the timestamp to |test::kServerTimestamp| and the buffer to all zeros.
  AvPair pair(TargetInfoAvId::kTimestamp,
              std::vector<uint8_t>(sizeof(uint64_t), 0));
  pair.timestamp = test::kServerTimestamp;
  server_av_pairs.push_back(std::move(pair));

  uint64_t server_timestamp = UINT64_MAX;
  // When both EPA and MIC are false the target info does not get modified by
  // the client.
  std::vector<uint8_t> updated_target_info = GenerateUpdatedTargetInfo(
      false, false, reinterpret_cast<const char*>(test::kChannelBindings),
      test::kNtlmSpn, server_av_pairs, &server_timestamp);
  // Verify that the server timestamp was read from the target info.
  ASSERT_EQ(test::kServerTimestamp, server_timestamp);
  ASSERT_EQ(std::size(test::kExpectedTargetInfoFromSpecPlusServerTimestampV2),
            updated_target_info.size());
  ASSERT_EQ(0, memcmp(test::kExpectedTargetInfoFromSpecPlusServerTimestampV2,
                      updated_target_info.data(), updated_target_info.size()));
}

TEST(NtlmTest, GenerateUpdatedTargetInfoWhenServerSendsNoTargetInfo) {
  // In some older implementations the server supports NTLMv2 but does not
  // send target info. This manifests as an empty list of AvPairs.
  std::vector<AvPair> server_av_pairs;

  uint64_t server_timestamp = UINT64_MAX;
  std::vector<uint8_t> updated_target_info = GenerateUpdatedTargetInfo(
      true, true, reinterpret_cast<const char*>(test::kChannelBindings),
      test::kNtlmSpn, server_av_pairs, &server_timestamp);

  // With MIC and EPA enabled 3 additional AvPairs will be added.
  // 1) A flags AVPair with the MIC_PRESENT bit set.
  // 2) A channel bindings AVPair containing the channel bindings hash.
  // 3) A target name AVPair containing the SPN of the server.
  //
  // Compared to the spec example in |GenerateUpdatedTargetInfo| the result
  // is the same but with the first 32 bytes (which were the Domain and
  // Server pairs) not present.
  const size_t kMissingServerPairsLength = 32;

  ASSERT_EQ(std::size(test::kExpectedTargetInfoSpecResponseV2) -
                kMissingServerPairsLength,
            updated_target_info.size());
  ASSERT_EQ(0, memcmp(test::kExpectedTargetInfoSpecResponseV2 +
                          kMissingServerPairsLength,
                      updated_target_info.data(), updated_target_info.size()));
}

TEST(NtlmTest, GenerateNtlmProofV2) {
  uint8_t proof[kNtlmProofLenV2];

  GenerateNtlmProofV2(test::kExpectedNtlmHashV2, test::kServerChallenge,
                      base::make_span(test::kExpectedTempFromSpecV2)
                          .subspan<0, kProofInputLenV2>(),
                      test::kExpectedTargetInfoSpecResponseV2, proof);
  ASSERT_EQ(0,
            memcmp(test::kExpectedProofSpecResponseV2, proof, kNtlmProofLenV2));
}

TEST(NtlmTest, GenerateNtlmProofWithClientTimestampV2) {
  uint8_t proof[kNtlmProofLenV2];

  // Since the test data for "temp" in the spec does not include the client
  // timestamp, a separate proof test value must be validated for use in full
  // message validation.
  GenerateNtlmProofV2(test::kExpectedNtlmHashV2, test::kServerChallenge,
                      base::make_span(test::kExpectedTempWithClientTimestampV2)
                          .subspan<0, kProofInputLenV2>(),
                      test::kExpectedTargetInfoSpecResponseV2, proof);
  ASSERT_EQ(0, memcmp(test::kExpectedProofSpecResponseWithClientTimestampV2,
                      proof, kNtlmProofLenV2));
}

}  // namespace net::ntlm