| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/http/http_auth_handler_ntlm.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 // For gethostname |
| 9 #if defined(OS_POSIX) |
| 10 #include <unistd.h> |
| 11 #elif defined(OS_WIN) |
| 12 #include <winsock2.h> |
| 13 #endif |
| 14 |
| 15 #include "base/md5.h" |
| 16 #include "base/rand_util.h" |
| 17 #include "base/string_util.h" |
| 18 #include "base/sys_string_conversions.h" |
| 19 #include "net/base/base64.h" |
| 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/net_util.h" |
| 22 #include "net/http/des.h" |
| 23 #include "net/http/md4.h" |
| 24 |
| 25 namespace net { |
| 26 |
| 27 // Based on mozilla/security/manager/ssl/src/nsNTLMAuthModule.cpp, |
| 28 // CVS rev. 1.14. |
| 29 // |
| 30 // TODO(wtc): |
| 31 // - The IS_BIG_ENDIAN code is not tested. |
| 32 // - Enable the logging code or just delete it. |
| 33 // - Delete or comment out the LM code, which hasn't been tested and isn't |
| 34 // being used. |
| 35 |
| 36 /* ***** BEGIN LICENSE BLOCK ***** |
| 37 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 38 * |
| 39 * The contents of this file are subject to the Mozilla Public License Version |
| 40 * 1.1 (the "License"); you may not use this file except in compliance with |
| 41 * the License. You may obtain a copy of the License at |
| 42 * http://www.mozilla.org/MPL/ |
| 43 * |
| 44 * Software distributed under the License is distributed on an "AS IS" basis, |
| 45 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 46 * for the specific language governing rights and limitations under the |
| 47 * License. |
| 48 * |
| 49 * The Original Code is Mozilla. |
| 50 * |
| 51 * The Initial Developer of the Original Code is IBM Corporation. |
| 52 * Portions created by IBM Corporation are Copyright (C) 2003 |
| 53 * IBM Corporation. All Rights Reserved. |
| 54 * |
| 55 * Contributor(s): |
| 56 * Darin Fisher <darin@meer.net> |
| 57 * |
| 58 * Alternatively, the contents of this file may be used under the terms of |
| 59 * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 60 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 61 * in which case the provisions of the GPL or the LGPL are applicable instead |
| 62 * of those above. If you wish to allow use of your version of this file only |
| 63 * under the terms of either the GPL or the LGPL, and not to allow others to |
| 64 * use your version of this file under the terms of the MPL, indicate your |
| 65 * decision by deleting the provisions above and replace them with the notice |
| 66 * and other provisions required by the GPL or the LGPL. If you do not delete |
| 67 * the provisions above, a recipient may use your version of this file under |
| 68 * the terms of any one of the MPL, the GPL or the LGPL. |
| 69 * |
| 70 * ***** END LICENSE BLOCK ***** */ |
| 71 |
| 72 // Discover the endianness by testing processor architecture. |
| 73 #if defined(ARCH_CPU_X86) || defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARMEL) |
| 74 #define IS_LITTLE_ENDIAN 1 |
| 75 #undef IS_BIG_ENDIAN |
| 76 #else |
| 77 #error "Unknown endianness" |
| 78 #endif |
| 79 |
| 80 #define NTLM_LOG(x) ((void) 0) |
| 81 |
| 82 //----------------------------------------------------------------------------- |
| 83 // This file contains a cross-platform NTLM authentication implementation. It |
| 84 // is based on documentation from: http://davenport.sourceforge.net/ntlm.html |
| 85 //----------------------------------------------------------------------------- |
| 86 |
| 87 enum { |
| 88 NTLM_NegotiateUnicode = 0x00000001, |
| 89 NTLM_NegotiateOEM = 0x00000002, |
| 90 NTLM_RequestTarget = 0x00000004, |
| 91 NTLM_Unknown1 = 0x00000008, |
| 92 NTLM_NegotiateSign = 0x00000010, |
| 93 NTLM_NegotiateSeal = 0x00000020, |
| 94 NTLM_NegotiateDatagramStyle = 0x00000040, |
| 95 NTLM_NegotiateLanManagerKey = 0x00000080, |
| 96 NTLM_NegotiateNetware = 0x00000100, |
| 97 NTLM_NegotiateNTLMKey = 0x00000200, |
| 98 NTLM_Unknown2 = 0x00000400, |
| 99 NTLM_Unknown3 = 0x00000800, |
| 100 NTLM_NegotiateDomainSupplied = 0x00001000, |
| 101 NTLM_NegotiateWorkstationSupplied = 0x00002000, |
| 102 NTLM_NegotiateLocalCall = 0x00004000, |
| 103 NTLM_NegotiateAlwaysSign = 0x00008000, |
| 104 NTLM_TargetTypeDomain = 0x00010000, |
| 105 NTLM_TargetTypeServer = 0x00020000, |
| 106 NTLM_TargetTypeShare = 0x00040000, |
| 107 NTLM_NegotiateNTLM2Key = 0x00080000, |
| 108 NTLM_RequestInitResponse = 0x00100000, |
| 109 NTLM_RequestAcceptResponse = 0x00200000, |
| 110 NTLM_RequestNonNTSessionKey = 0x00400000, |
| 111 NTLM_NegotiateTargetInfo = 0x00800000, |
| 112 NTLM_Unknown4 = 0x01000000, |
| 113 NTLM_Unknown5 = 0x02000000, |
| 114 NTLM_Unknown6 = 0x04000000, |
| 115 NTLM_Unknown7 = 0x08000000, |
| 116 NTLM_Unknown8 = 0x10000000, |
| 117 NTLM_Negotiate128 = 0x20000000, |
| 118 NTLM_NegotiateKeyExchange = 0x40000000, |
| 119 NTLM_Negotiate56 = 0x80000000 |
| 120 }; |
| 121 |
| 122 // We send these flags with our type 1 message. |
| 123 enum { |
| 124 NTLM_TYPE1_FLAGS = |
| 125 NTLM_NegotiateUnicode | |
| 126 NTLM_NegotiateOEM | |
| 127 NTLM_RequestTarget | |
| 128 NTLM_NegotiateNTLMKey | |
| 129 NTLM_NegotiateAlwaysSign | |
| 130 NTLM_NegotiateNTLM2Key |
| 131 }; |
| 132 |
| 133 static const char NTLM_SIGNATURE[] = "NTLMSSP"; |
| 134 static const char NTLM_TYPE1_MARKER[] = { 0x01, 0x00, 0x00, 0x00 }; |
| 135 static const char NTLM_TYPE2_MARKER[] = { 0x02, 0x00, 0x00, 0x00 }; |
| 136 static const char NTLM_TYPE3_MARKER[] = { 0x03, 0x00, 0x00, 0x00 }; |
| 137 |
| 138 enum { |
| 139 NTLM_TYPE1_HEADER_LEN = 32, |
| 140 NTLM_TYPE2_HEADER_LEN = 32, |
| 141 NTLM_TYPE3_HEADER_LEN = 64, |
| 142 |
| 143 LM_HASH_LEN = 16, |
| 144 LM_RESP_LEN = 24, |
| 145 |
| 146 NTLM_HASH_LEN = 16, |
| 147 NTLM_RESP_LEN = 24 |
| 148 }; |
| 149 |
| 150 //----------------------------------------------------------------------------- |
| 151 |
| 152 // The return value of this function controls whether or not the LM hash will |
| 153 // be included in response to a NTLM challenge. |
| 154 // |
| 155 // In Mozilla, this function returns the value of the boolean preference |
| 156 // "network.ntlm.send-lm-response". By default, the preference is disabled |
| 157 // since servers should almost never need the LM hash, and the LM hash is what |
| 158 // makes NTLM authentication less secure. See |
| 159 // https://bugzilla.mozilla.org/show_bug.cgi?id=250691 for further details. |
| 160 // |
| 161 // We just return a hardcoded false. |
| 162 static bool SendLM() { |
| 163 return false; |
| 164 } |
| 165 |
| 166 //----------------------------------------------------------------------------- |
| 167 |
| 168 #define LogFlags(x) ((void) 0) |
| 169 #define LogBuf(a, b, c) ((void) 0) |
| 170 #define LogToken(a, b, c) ((void) 0) |
| 171 |
| 172 //----------------------------------------------------------------------------- |
| 173 |
| 174 // Byte order swapping. |
| 175 #define SWAP16(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff)) |
| 176 #define SWAP32(x) ((SWAP16((x) & 0xffff) << 16) | (SWAP16((x) >> 16))) |
| 177 |
| 178 static void* WriteBytes(void* buf, const void* data, uint32 data_len) { |
| 179 memcpy(buf, data, data_len); |
| 180 return static_cast<char*>(buf) + data_len; |
| 181 } |
| 182 |
| 183 static void* WriteDWORD(void* buf, uint32 dword) { |
| 184 #ifdef IS_BIG_ENDIAN |
| 185 // NTLM uses little endian on the wire. |
| 186 dword = SWAP32(dword); |
| 187 #endif |
| 188 return WriteBytes(buf, &dword, sizeof(dword)); |
| 189 } |
| 190 |
| 191 static void* WriteSecBuf(void* buf, uint16 length, uint32 offset) { |
| 192 #ifdef IS_BIG_ENDIAN |
| 193 length = SWAP16(length); |
| 194 offset = SWAP32(offset); |
| 195 #endif |
| 196 buf = WriteBytes(buf, &length, sizeof(length)); |
| 197 buf = WriteBytes(buf, &length, sizeof(length)); |
| 198 buf = WriteBytes(buf, &offset, sizeof(offset)); |
| 199 return buf; |
| 200 } |
| 201 |
| 202 #ifdef IS_BIG_ENDIAN |
| 203 /** |
| 204 * WriteUnicodeLE copies a unicode string from one buffer to another. The |
| 205 * resulting unicode string is in little-endian format. The input string is |
| 206 * assumed to be in the native endianness of the local machine. It is safe |
| 207 * to pass the same buffer as both input and output, which is a handy way to |
| 208 * convert the unicode buffer to little-endian on big-endian platforms. |
| 209 */ |
| 210 static void* WriteUnicodeLE(void* buf, const char16* str, uint32 str_len) { |
| 211 // Convert input string from BE to LE. |
| 212 uint8* cursor = static_cast<uint8*>(buf); |
| 213 const uint8* input = reinterpret_cast<const uint8*>(str); |
| 214 for (uint32 i = 0; i < str_len; ++i, input += 2, cursor += 2) { |
| 215 // Allow for the case where |buf == str|. |
| 216 uint8 temp = input[0]; |
| 217 cursor[0] = input[1]; |
| 218 cursor[1] = temp; |
| 219 } |
| 220 return buf; |
| 221 } |
| 222 #endif |
| 223 |
| 224 static uint16 ReadUint16(const uint8*& buf) { |
| 225 uint16 x = (static_cast<uint16>(buf[0])) | |
| 226 (static_cast<uint16>(buf[1]) << 8); |
| 227 buf += sizeof(x); |
| 228 return x; |
| 229 } |
| 230 |
| 231 static uint32 ReadUint32(const uint8*& buf) { |
| 232 uint32 x = (static_cast<uint32>(buf[0])) | |
| 233 (static_cast<uint32>(buf[1]) << 8) | |
| 234 (static_cast<uint32>(buf[2]) << 16) | |
| 235 (static_cast<uint32>(buf[3]) << 24); |
| 236 buf += sizeof(x); |
| 237 return x; |
| 238 } |
| 239 |
| 240 //----------------------------------------------------------------------------- |
| 241 |
| 242 // LM_Hash computes the LM hash of the given password. |
| 243 // |
| 244 // param password |
| 245 // unicode password. |
| 246 // param hash |
| 247 // 16-byte result buffer |
| 248 // |
| 249 // Note: This function is not being used because our SendLM() function always |
| 250 // returns false. |
| 251 static void LM_Hash(const string16& password, uint8* hash) { |
| 252 static const uint8 LM_MAGIC[] = "KGS!@#$%"; |
| 253 |
| 254 // Convert password to OEM character set. We'll just use the native |
| 255 // filesystem charset. |
| 256 std::string passbuf = base::SysWideToNativeMB(UTF16ToWide(password)); |
| 257 StringToUpperASCII(&passbuf); |
| 258 passbuf.resize(14, '\0'); |
| 259 |
| 260 uint8 k1[8], k2[8]; |
| 261 DESMakeKey(reinterpret_cast<const uint8*>(passbuf.data()) , k1); |
| 262 DESMakeKey(reinterpret_cast<const uint8*>(passbuf.data()) + 7, k2); |
| 263 ZapString(&passbuf); |
| 264 |
| 265 // Use password keys to hash LM magic string twice. |
| 266 DESEncrypt(k1, LM_MAGIC, hash); |
| 267 DESEncrypt(k2, LM_MAGIC, hash + 8); |
| 268 } |
| 269 |
| 270 // NTLM_Hash computes the NTLM hash of the given password. |
| 271 // |
| 272 // param password |
| 273 // null-terminated unicode password. |
| 274 // param hash |
| 275 // 16-byte result buffer |
| 276 static void NTLM_Hash(const string16& password, uint8* hash) { |
| 277 #ifdef IS_BIG_ENDIAN |
| 278 uint32 len = password.length(); |
| 279 uint8* passbuf; |
| 280 |
| 281 passbuf = static_cast<uint8*>(malloc(len * 2)); |
| 282 WriteUnicodeLE(passbuf, password.data(), len); |
| 283 weak_crypto::MD4Sum(passbuf, len * 2, hash); |
| 284 |
| 285 ZapBuf(passbuf, len * 2); |
| 286 free(passbuf); |
| 287 #else |
| 288 weak_crypto::MD4Sum(reinterpret_cast<const uint8*>(password.data()), |
| 289 password.length() * 2, hash); |
| 290 #endif |
| 291 } |
| 292 |
| 293 //----------------------------------------------------------------------------- |
| 294 |
| 295 // LM_Response generates the LM response given a 16-byte password hash and the |
| 296 // challenge from the Type-2 message. |
| 297 // |
| 298 // param hash |
| 299 // 16-byte password hash |
| 300 // param challenge |
| 301 // 8-byte challenge from Type-2 message |
| 302 // param response |
| 303 // 24-byte buffer to contain the LM response upon return |
| 304 static void LM_Response(const uint8* hash, |
| 305 const uint8* challenge, |
| 306 uint8* response) { |
| 307 uint8 keybytes[21], k1[8], k2[8], k3[8]; |
| 308 |
| 309 memcpy(keybytes, hash, 16); |
| 310 ZapBuf(keybytes + 16, 5); |
| 311 |
| 312 DESMakeKey(keybytes , k1); |
| 313 DESMakeKey(keybytes + 7, k2); |
| 314 DESMakeKey(keybytes + 14, k3); |
| 315 |
| 316 DESEncrypt(k1, challenge, response); |
| 317 DESEncrypt(k2, challenge, response + 8); |
| 318 DESEncrypt(k3, challenge, response + 16); |
| 319 } |
| 320 |
| 321 //----------------------------------------------------------------------------- |
| 322 |
| 323 // Returns OK or a network error code. |
| 324 static int GenerateType1Msg(void** out_buf, uint32* out_len) { |
| 325 // |
| 326 // Verify that buf_len is sufficient. |
| 327 // |
| 328 *out_len = NTLM_TYPE1_HEADER_LEN; |
| 329 *out_buf = malloc(*out_len); |
| 330 if (!*out_buf) |
| 331 return ERR_OUT_OF_MEMORY; |
| 332 |
| 333 // |
| 334 // Write out type 1 message. |
| 335 // |
| 336 void* cursor = *out_buf; |
| 337 |
| 338 // 0 : signature |
| 339 cursor = WriteBytes(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)); |
| 340 |
| 341 // 8 : marker |
| 342 cursor = WriteBytes(cursor, NTLM_TYPE1_MARKER, sizeof(NTLM_TYPE1_MARKER)); |
| 343 |
| 344 // 12 : flags |
| 345 cursor = WriteDWORD(cursor, NTLM_TYPE1_FLAGS); |
| 346 |
| 347 // |
| 348 // NOTE: It is common for the domain and workstation fields to be empty. |
| 349 // This is true of Win2k clients, and my guess is that there is |
| 350 // little utility to sending these strings before the charset has |
| 351 // been negotiated. We follow suite -- anyways, it doesn't hurt |
| 352 // to save some bytes on the wire ;-) |
| 353 // |
| 354 |
| 355 // 16 : supplied domain security buffer (empty) |
| 356 cursor = WriteSecBuf(cursor, 0, 0); |
| 357 |
| 358 // 24 : supplied workstation security buffer (empty) |
| 359 cursor = WriteSecBuf(cursor, 0, 0); |
| 360 |
| 361 return OK; |
| 362 } |
| 363 |
| 364 struct Type2Msg { |
| 365 uint32 flags; // NTLM_Xxx bitwise combination |
| 366 uint8 challenge[8]; // 8 byte challenge |
| 367 const void* target; // target string (type depends on flags) |
| 368 uint32 target_len; // target length in bytes |
| 369 }; |
| 370 |
| 371 // Returns OK or a network error code. |
| 372 // TODO(wtc): This function returns ERR_UNEXPECTED when the input message is |
| 373 // invalid. We should return a better error code. |
| 374 static int ParseType2Msg(const void* in_buf, uint32 in_len, Type2Msg* msg) { |
| 375 // Make sure in_buf is long enough to contain a meaningful type2 msg. |
| 376 // |
| 377 // 0 NTLMSSP Signature |
| 378 // 8 NTLM Message Type |
| 379 // 12 Target Name |
| 380 // 20 Flags |
| 381 // 24 Challenge |
| 382 // 32 end of header, start of optional data blocks |
| 383 // |
| 384 if (in_len < NTLM_TYPE2_HEADER_LEN) |
| 385 return ERR_UNEXPECTED; |
| 386 |
| 387 const uint8* cursor = (const uint8*) in_buf; |
| 388 |
| 389 // verify NTLMSSP signature |
| 390 if (memcmp(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)) != 0) |
| 391 return ERR_UNEXPECTED; |
| 392 cursor += sizeof(NTLM_SIGNATURE); |
| 393 |
| 394 // verify Type-2 marker |
| 395 if (memcmp(cursor, NTLM_TYPE2_MARKER, sizeof(NTLM_TYPE2_MARKER)) != 0) |
| 396 return ERR_UNEXPECTED; |
| 397 cursor += sizeof(NTLM_TYPE2_MARKER); |
| 398 |
| 399 // read target name security buffer |
| 400 uint32 target_len = ReadUint16(cursor); |
| 401 ReadUint16(cursor); // discard next 16-bit value |
| 402 uint32 offset = ReadUint32(cursor); // get offset from in_buf |
| 403 msg->target_len = 0; |
| 404 msg->target = NULL; |
| 405 // Check the offset / length combo is in range of the input buffer, including |
| 406 // integer overflow checking. |
| 407 if (offset + target_len > offset && offset + target_len <= in_len) { |
| 408 msg->target_len = target_len; |
| 409 msg->target = ((const uint8*) in_buf) + offset; |
| 410 } |
| 411 |
| 412 // read flags |
| 413 msg->flags = ReadUint32(cursor); |
| 414 |
| 415 // read challenge |
| 416 memcpy(msg->challenge, cursor, sizeof(msg->challenge)); |
| 417 cursor += sizeof(msg->challenge); |
| 418 |
| 419 NTLM_LOG(("NTLM type 2 message:\n")); |
| 420 LogBuf("target", (const uint8*) msg->target, msg->target_len); |
| 421 LogBuf("flags", (const uint8*) &msg->flags, 4); |
| 422 LogFlags(msg->flags); |
| 423 LogBuf("challenge", msg->challenge, sizeof(msg->challenge)); |
| 424 |
| 425 // We currently do not implement LMv2/NTLMv2 or NTLM2 responses, |
| 426 // so we can ignore target information. We may want to enable |
| 427 // support for these alternate mechanisms in the future. |
| 428 return OK; |
| 429 } |
| 430 |
| 431 static void GenerateRandom(uint8* output, size_t n) { |
| 432 for (size_t i = 0; i < n; ++i) |
| 433 output[i] = base::RandInt(0, 255); |
| 434 } |
| 435 |
| 436 // Returns OK or a network error code. |
| 437 static int GenerateType3Msg(const string16& domain, |
| 438 const string16& username, |
| 439 const string16& password, |
| 440 const std::string& hostname, |
| 441 const void* rand_8_bytes, |
| 442 const void* in_buf, |
| 443 uint32 in_len, |
| 444 void** out_buf, |
| 445 uint32* out_len) { |
| 446 // in_buf contains Type-2 msg (the challenge) from server. |
| 447 |
| 448 int rv; |
| 449 Type2Msg msg; |
| 450 |
| 451 rv = ParseType2Msg(in_buf, in_len, &msg); |
| 452 if (rv != OK) |
| 453 return rv; |
| 454 |
| 455 bool unicode = (msg.flags & NTLM_NegotiateUnicode) != 0; |
| 456 |
| 457 // Temporary buffers for unicode strings |
| 458 #ifdef IS_BIG_ENDIAN |
| 459 string16 ucs_domain_buf, ucs_user_buf; |
| 460 #endif |
| 461 string16 ucs_host_buf; |
| 462 // Temporary buffers for oem strings |
| 463 std::string oem_domain_buf, oem_user_buf; |
| 464 // Pointers and lengths for the string buffers; encoding is unicode if |
| 465 // the "negotiate unicode" flag was set in the Type-2 message. |
| 466 const void* domain_ptr; |
| 467 const void* user_ptr; |
| 468 const void* host_ptr; |
| 469 uint32 domain_len, user_len, host_len; |
| 470 |
| 471 // |
| 472 // Get domain name. |
| 473 // |
| 474 if (unicode) { |
| 475 #ifdef IS_BIG_ENDIAN |
| 476 ucs_domain_buf = domain; |
| 477 domain_ptr = ucs_domain_buf.data(); |
| 478 domain_len = ucs_domain_buf.length() * 2; |
| 479 WriteUnicodeLE(const_cast<void*>(domain_ptr), (const char16*) domain_ptr, |
| 480 ucs_domain_buf.length()); |
| 481 #else |
| 482 domain_ptr = domain.data(); |
| 483 domain_len = domain.length() * 2; |
| 484 #endif |
| 485 } else { |
| 486 oem_domain_buf = base::SysWideToNativeMB(UTF16ToWide(domain)); |
| 487 domain_ptr = oem_domain_buf.data(); |
| 488 domain_len = oem_domain_buf.length(); |
| 489 } |
| 490 |
| 491 // |
| 492 // Get user name. |
| 493 // |
| 494 if (unicode) { |
| 495 #ifdef IS_BIG_ENDIAN |
| 496 ucs_user_buf = username; |
| 497 user_ptr = ucs_user_buf.data(); |
| 498 user_len = ucs_user_buf.length() * 2; |
| 499 WriteUnicodeLE(const_cast<void*>(user_ptr), (const char16*) user_ptr, |
| 500 ucs_user_buf.length()); |
| 501 #else |
| 502 user_ptr = username.data(); |
| 503 user_len = username.length() * 2; |
| 504 #endif |
| 505 } else { |
| 506 oem_user_buf = base::SysWideToNativeMB(UTF16ToWide(username)); |
| 507 user_ptr = oem_user_buf.data(); |
| 508 user_len = oem_user_buf.length(); |
| 509 } |
| 510 |
| 511 // |
| 512 // Get workstation name (use local machine's hostname). |
| 513 // |
| 514 if (unicode) { |
| 515 // hostname is ASCII, so we can do a simple zero-pad expansion: |
| 516 ucs_host_buf.assign(hostname.begin(), hostname.end()); |
| 517 host_ptr = ucs_host_buf.data(); |
| 518 host_len = ucs_host_buf.length() * 2; |
| 519 #ifdef IS_BIG_ENDIAN |
| 520 WriteUnicodeLE(const_cast<void*>(host_ptr), (const char16*) host_ptr, |
| 521 ucs_host_buf.length()); |
| 522 #endif |
| 523 } else { |
| 524 host_ptr = hostname.data(); |
| 525 host_len = hostname.length(); |
| 526 } |
| 527 |
| 528 // |
| 529 // Now that we have generated all of the strings, we can allocate out_buf. |
| 530 // |
| 531 *out_len = NTLM_TYPE3_HEADER_LEN + host_len + domain_len + user_len + |
| 532 LM_RESP_LEN + NTLM_RESP_LEN; |
| 533 *out_buf = malloc(*out_len); |
| 534 if (!*out_buf) |
| 535 return ERR_OUT_OF_MEMORY; |
| 536 |
| 537 // |
| 538 // Next, we compute the LM and NTLM responses. |
| 539 // |
| 540 uint8 lm_resp[LM_RESP_LEN]; |
| 541 uint8 ntlm_resp[NTLM_RESP_LEN]; |
| 542 uint8 ntlm_hash[NTLM_HASH_LEN]; |
| 543 if (msg.flags & NTLM_NegotiateNTLM2Key) { |
| 544 // compute NTLM2 session response |
| 545 MD5Digest session_hash; |
| 546 uint8 temp[16]; |
| 547 |
| 548 memcpy(lm_resp, rand_8_bytes, 8); |
| 549 memset(lm_resp + 8, 0, LM_RESP_LEN - 8); |
| 550 |
| 551 memcpy(temp, msg.challenge, 8); |
| 552 memcpy(temp + 8, lm_resp, 8); |
| 553 MD5Sum(temp, 16, &session_hash); |
| 554 |
| 555 NTLM_Hash(password, ntlm_hash); |
| 556 LM_Response(ntlm_hash, session_hash.a, ntlm_resp); |
| 557 } else { |
| 558 NTLM_Hash(password, ntlm_hash); |
| 559 LM_Response(ntlm_hash, msg.challenge, ntlm_resp); |
| 560 |
| 561 if (SendLM()) { |
| 562 uint8 lm_hash[LM_HASH_LEN]; |
| 563 LM_Hash(password, lm_hash); |
| 564 LM_Response(lm_hash, msg.challenge, lm_resp); |
| 565 } else { |
| 566 // According to http://davenport.sourceforge.net/ntlm.html#ntlmVersion2, |
| 567 // the correct way to not send the LM hash is to send the NTLM hash twice |
| 568 // in both the LM and NTLM response fields. |
| 569 LM_Response(ntlm_hash, msg.challenge, lm_resp); |
| 570 } |
| 571 } |
| 572 |
| 573 // |
| 574 // Finally, we assemble the Type-3 msg :-) |
| 575 // |
| 576 void* cursor = *out_buf; |
| 577 uint32 offset; |
| 578 |
| 579 // 0 : signature |
| 580 cursor = WriteBytes(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)); |
| 581 |
| 582 // 8 : marker |
| 583 cursor = WriteBytes(cursor, NTLM_TYPE3_MARKER, sizeof(NTLM_TYPE3_MARKER)); |
| 584 |
| 585 // 12 : LM response sec buf |
| 586 offset = NTLM_TYPE3_HEADER_LEN + domain_len + user_len + host_len; |
| 587 cursor = WriteSecBuf(cursor, LM_RESP_LEN, offset); |
| 588 memcpy(static_cast<uint8*>(*out_buf) + offset, lm_resp, LM_RESP_LEN); |
| 589 |
| 590 // 20 : NTLM response sec buf |
| 591 offset += LM_RESP_LEN; |
| 592 cursor = WriteSecBuf(cursor, NTLM_RESP_LEN, offset); |
| 593 memcpy(static_cast<uint8*>(*out_buf) + offset, ntlm_resp, NTLM_RESP_LEN); |
| 594 |
| 595 // 28 : domain name sec buf |
| 596 offset = NTLM_TYPE3_HEADER_LEN; |
| 597 cursor = WriteSecBuf(cursor, domain_len, offset); |
| 598 memcpy(static_cast<uint8*>(*out_buf) + offset, domain_ptr, domain_len); |
| 599 |
| 600 // 36 : user name sec buf |
| 601 offset += domain_len; |
| 602 cursor = WriteSecBuf(cursor, user_len, offset); |
| 603 memcpy(static_cast<uint8*>(*out_buf) + offset, user_ptr, user_len); |
| 604 |
| 605 // 44 : workstation (host) name sec buf |
| 606 offset += user_len; |
| 607 cursor = WriteSecBuf(cursor, host_len, offset); |
| 608 memcpy(static_cast<uint8*>(*out_buf) + offset, host_ptr, host_len); |
| 609 |
| 610 // 52 : session key sec buf (not used) |
| 611 cursor = WriteSecBuf(cursor, 0, 0); |
| 612 |
| 613 // 60 : negotiated flags |
| 614 cursor = WriteDWORD(cursor, msg.flags & NTLM_TYPE1_FLAGS); |
| 615 |
| 616 return OK; |
| 617 } |
| 618 |
| 619 // NTLM authentication is specified in "NTLM Over HTTP Protocol Specification" |
| 620 // [MS-NTHT]. |
| 621 |
| 622 // static |
| 623 HttpAuthHandlerNTLM::GenerateRandomProc |
| 624 HttpAuthHandlerNTLM::generate_random_proc_ = GenerateRandom; |
| 625 |
| 626 // static |
| 627 HttpAuthHandlerNTLM::HostNameProc |
| 628 HttpAuthHandlerNTLM::get_host_name_proc_ = GetHostName; |
| 629 |
| 630 HttpAuthHandlerNTLM::HttpAuthHandlerNTLM() { |
| 631 } |
| 632 |
| 633 HttpAuthHandlerNTLM::~HttpAuthHandlerNTLM() { |
| 634 // Wipe our copy of the password from memory, to reduce the chance of being |
| 635 // written to the paging file on disk. |
| 636 ZapString(&password_); |
| 637 } |
| 638 |
| 639 // static |
| 640 HttpAuthHandlerNTLM::GenerateRandomProc |
| 641 HttpAuthHandlerNTLM::SetGenerateRandomProc( |
| 642 GenerateRandomProc proc) { |
| 643 GenerateRandomProc old_proc = generate_random_proc_; |
| 644 generate_random_proc_ = proc; |
| 645 return old_proc; |
| 646 } |
| 647 |
| 648 // static |
| 649 HttpAuthHandlerNTLM::HostNameProc HttpAuthHandlerNTLM::SetHostNameProc( |
| 650 HostNameProc proc) { |
| 651 HostNameProc old_proc = get_host_name_proc_; |
| 652 get_host_name_proc_ = proc; |
| 653 return old_proc; |
| 654 } |
| 655 |
| 656 bool HttpAuthHandlerNTLM::NeedsIdentity() { |
| 657 return !auth_data_.empty(); |
| 658 } |
| 659 |
| 660 int HttpAuthHandlerNTLM::GetNextToken(const void* in_token, |
| 661 uint32 in_token_len, |
| 662 void** out_token, |
| 663 uint32* out_token_len) { |
| 664 int rv; |
| 665 |
| 666 // If in_token is non-null, then assume it contains a type 2 message... |
| 667 if (in_token) { |
| 668 LogToken("in-token", in_token, in_token_len); |
| 669 std::string hostname = get_host_name_proc_(); |
| 670 if (hostname.empty()) |
| 671 return ERR_UNEXPECTED; |
| 672 uint8 rand_buf[8]; |
| 673 generate_random_proc_(rand_buf, 8); |
| 674 rv = GenerateType3Msg(domain_, username_, password_, hostname, rand_buf, |
| 675 in_token, in_token_len, out_token, out_token_len); |
| 676 } else { |
| 677 rv = GenerateType1Msg(out_token, out_token_len); |
| 678 } |
| 679 |
| 680 if (rv == OK) |
| 681 LogToken("out-token", *out_token, *out_token_len); |
| 682 |
| 683 return rv; |
| 684 } |
| 685 |
| 686 int HttpAuthHandlerNTLM::InitializeBeforeFirstChallenge() { |
| 687 return OK; |
| 688 } |
| 689 |
| 690 } // namespace net |
| OLD | NEW |