| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_auth_handler_ntlm.h" | 5 #include "net/http/http_auth_handler_ntlm.h" |
| 6 | 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" | 7 #include "base/string_util.h" |
| 18 #include "base/sys_string_conversions.h" | |
| 19 #include "net/base/base64.h" | 8 #include "net/base/base64.h" |
| 20 #include "net/base/net_errors.h" | 9 #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 | 10 |
| 25 namespace net { | 11 namespace net { |
| 26 | 12 |
| 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 static void ZapBuf(void* buf, size_t buf_len) { | |
| 243 memset(buf, 0, buf_len); | |
| 244 } | |
| 245 | |
| 246 // TODO(wtc): Can we implement ZapString as | |
| 247 // s.replace(0, s.size(), s.size(), '\0)? | |
| 248 static void ZapString(std::string* s) { | |
| 249 ZapBuf(&(*s)[0], s->length()); | |
| 250 } | |
| 251 | |
| 252 static void ZapString(string16* s) { | |
| 253 ZapBuf(&(*s)[0], s->length() * 2); | |
| 254 } | |
| 255 | |
| 256 // LM_Hash computes the LM hash of the given password. | |
| 257 // | |
| 258 // param password | |
| 259 // unicode password. | |
| 260 // param hash | |
| 261 // 16-byte result buffer | |
| 262 // | |
| 263 // Note: This function is not being used because our SendLM() function always | |
| 264 // returns false. | |
| 265 static void LM_Hash(const string16& password, uint8* hash) { | |
| 266 static const uint8 LM_MAGIC[] = "KGS!@#$%"; | |
| 267 | |
| 268 // Convert password to OEM character set. We'll just use the native | |
| 269 // filesystem charset. | |
| 270 std::string passbuf = base::SysWideToNativeMB(UTF16ToWide(password)); | |
| 271 StringToUpperASCII(&passbuf); | |
| 272 passbuf.resize(14, '\0'); | |
| 273 | |
| 274 uint8 k1[8], k2[8]; | |
| 275 DESMakeKey(reinterpret_cast<const uint8*>(passbuf.data()) , k1); | |
| 276 DESMakeKey(reinterpret_cast<const uint8*>(passbuf.data()) + 7, k2); | |
| 277 ZapString(&passbuf); | |
| 278 | |
| 279 // Use password keys to hash LM magic string twice. | |
| 280 DESEncrypt(k1, LM_MAGIC, hash); | |
| 281 DESEncrypt(k2, LM_MAGIC, hash + 8); | |
| 282 } | |
| 283 | |
| 284 // NTLM_Hash computes the NTLM hash of the given password. | |
| 285 // | |
| 286 // param password | |
| 287 // null-terminated unicode password. | |
| 288 // param hash | |
| 289 // 16-byte result buffer | |
| 290 static void NTLM_Hash(const string16& password, uint8* hash) { | |
| 291 #ifdef IS_BIG_ENDIAN | |
| 292 uint32 len = password.length(); | |
| 293 uint8* passbuf; | |
| 294 | |
| 295 passbuf = static_cast<uint8*>(malloc(len * 2)); | |
| 296 WriteUnicodeLE(passbuf, password.data(), len); | |
| 297 weak_crypto::MD4Sum(passbuf, len * 2, hash); | |
| 298 | |
| 299 ZapBuf(passbuf, len * 2); | |
| 300 free(passbuf); | |
| 301 #else | |
| 302 weak_crypto::MD4Sum(reinterpret_cast<const uint8*>(password.data()), | |
| 303 password.length() * 2, hash); | |
| 304 #endif | |
| 305 } | |
| 306 | |
| 307 //----------------------------------------------------------------------------- | |
| 308 | |
| 309 // LM_Response generates the LM response given a 16-byte password hash and the | |
| 310 // challenge from the Type-2 message. | |
| 311 // | |
| 312 // param hash | |
| 313 // 16-byte password hash | |
| 314 // param challenge | |
| 315 // 8-byte challenge from Type-2 message | |
| 316 // param response | |
| 317 // 24-byte buffer to contain the LM response upon return | |
| 318 static void LM_Response(const uint8* hash, | |
| 319 const uint8* challenge, | |
| 320 uint8* response) { | |
| 321 uint8 keybytes[21], k1[8], k2[8], k3[8]; | |
| 322 | |
| 323 memcpy(keybytes, hash, 16); | |
| 324 ZapBuf(keybytes + 16, 5); | |
| 325 | |
| 326 DESMakeKey(keybytes , k1); | |
| 327 DESMakeKey(keybytes + 7, k2); | |
| 328 DESMakeKey(keybytes + 14, k3); | |
| 329 | |
| 330 DESEncrypt(k1, challenge, response); | |
| 331 DESEncrypt(k2, challenge, response + 8); | |
| 332 DESEncrypt(k3, challenge, response + 16); | |
| 333 } | |
| 334 | |
| 335 //----------------------------------------------------------------------------- | |
| 336 | |
| 337 // Returns OK or a network error code. | |
| 338 static int GenerateType1Msg(void** out_buf, uint32* out_len) { | |
| 339 // | |
| 340 // Verify that buf_len is sufficient. | |
| 341 // | |
| 342 *out_len = NTLM_TYPE1_HEADER_LEN; | |
| 343 *out_buf = malloc(*out_len); | |
| 344 if (!*out_buf) | |
| 345 return ERR_OUT_OF_MEMORY; | |
| 346 | |
| 347 // | |
| 348 // Write out type 1 message. | |
| 349 // | |
| 350 void* cursor = *out_buf; | |
| 351 | |
| 352 // 0 : signature | |
| 353 cursor = WriteBytes(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)); | |
| 354 | |
| 355 // 8 : marker | |
| 356 cursor = WriteBytes(cursor, NTLM_TYPE1_MARKER, sizeof(NTLM_TYPE1_MARKER)); | |
| 357 | |
| 358 // 12 : flags | |
| 359 cursor = WriteDWORD(cursor, NTLM_TYPE1_FLAGS); | |
| 360 | |
| 361 // | |
| 362 // NOTE: It is common for the domain and workstation fields to be empty. | |
| 363 // This is true of Win2k clients, and my guess is that there is | |
| 364 // little utility to sending these strings before the charset has | |
| 365 // been negotiated. We follow suite -- anyways, it doesn't hurt | |
| 366 // to save some bytes on the wire ;-) | |
| 367 // | |
| 368 | |
| 369 // 16 : supplied domain security buffer (empty) | |
| 370 cursor = WriteSecBuf(cursor, 0, 0); | |
| 371 | |
| 372 // 24 : supplied workstation security buffer (empty) | |
| 373 cursor = WriteSecBuf(cursor, 0, 0); | |
| 374 | |
| 375 return OK; | |
| 376 } | |
| 377 | |
| 378 struct Type2Msg { | |
| 379 uint32 flags; // NTLM_Xxx bitwise combination | |
| 380 uint8 challenge[8]; // 8 byte challenge | |
| 381 const void* target; // target string (type depends on flags) | |
| 382 uint32 target_len; // target length in bytes | |
| 383 }; | |
| 384 | |
| 385 // Returns OK or a network error code. | |
| 386 // TODO(wtc): This function returns ERR_UNEXPECTED when the input message is | |
| 387 // invalid. We should return a better error code. | |
| 388 static int ParseType2Msg(const void* in_buf, uint32 in_len, Type2Msg* msg) { | |
| 389 // Make sure in_buf is long enough to contain a meaningful type2 msg. | |
| 390 // | |
| 391 // 0 NTLMSSP Signature | |
| 392 // 8 NTLM Message Type | |
| 393 // 12 Target Name | |
| 394 // 20 Flags | |
| 395 // 24 Challenge | |
| 396 // 32 end of header, start of optional data blocks | |
| 397 // | |
| 398 if (in_len < NTLM_TYPE2_HEADER_LEN) | |
| 399 return ERR_UNEXPECTED; | |
| 400 | |
| 401 const uint8* cursor = (const uint8*) in_buf; | |
| 402 | |
| 403 // verify NTLMSSP signature | |
| 404 if (memcmp(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)) != 0) | |
| 405 return ERR_UNEXPECTED; | |
| 406 cursor += sizeof(NTLM_SIGNATURE); | |
| 407 | |
| 408 // verify Type-2 marker | |
| 409 if (memcmp(cursor, NTLM_TYPE2_MARKER, sizeof(NTLM_TYPE2_MARKER)) != 0) | |
| 410 return ERR_UNEXPECTED; | |
| 411 cursor += sizeof(NTLM_TYPE2_MARKER); | |
| 412 | |
| 413 // read target name security buffer | |
| 414 uint32 target_len = ReadUint16(cursor); | |
| 415 ReadUint16(cursor); // discard next 16-bit value | |
| 416 uint32 offset = ReadUint32(cursor); // get offset from in_buf | |
| 417 msg->target_len = 0; | |
| 418 msg->target = NULL; | |
| 419 // Check the offset / length combo is in range of the input buffer, including | |
| 420 // integer overflow checking. | |
| 421 if (offset + target_len > offset && offset + target_len <= in_len) { | |
| 422 msg->target_len = target_len; | |
| 423 msg->target = ((const uint8*) in_buf) + offset; | |
| 424 } | |
| 425 | |
| 426 // read flags | |
| 427 msg->flags = ReadUint32(cursor); | |
| 428 | |
| 429 // read challenge | |
| 430 memcpy(msg->challenge, cursor, sizeof(msg->challenge)); | |
| 431 cursor += sizeof(msg->challenge); | |
| 432 | |
| 433 NTLM_LOG(("NTLM type 2 message:\n")); | |
| 434 LogBuf("target", (const uint8*) msg->target, msg->target_len); | |
| 435 LogBuf("flags", (const uint8*) &msg->flags, 4); | |
| 436 LogFlags(msg->flags); | |
| 437 LogBuf("challenge", msg->challenge, sizeof(msg->challenge)); | |
| 438 | |
| 439 // We currently do not implement LMv2/NTLMv2 or NTLM2 responses, | |
| 440 // so we can ignore target information. We may want to enable | |
| 441 // support for these alternate mechanisms in the future. | |
| 442 return OK; | |
| 443 } | |
| 444 | |
| 445 static void GenerateRandom(uint8* output, size_t n) { | |
| 446 for (size_t i = 0; i < n; ++i) | |
| 447 output[i] = base::RandInt(0, 255); | |
| 448 } | |
| 449 | |
| 450 // Returns OK or a network error code. | |
| 451 static int GenerateType3Msg(const string16& domain, | |
| 452 const string16& username, | |
| 453 const string16& password, | |
| 454 const std::string& hostname, | |
| 455 const void* rand_8_bytes, | |
| 456 const void* in_buf, | |
| 457 uint32 in_len, | |
| 458 void** out_buf, | |
| 459 uint32* out_len) { | |
| 460 // in_buf contains Type-2 msg (the challenge) from server. | |
| 461 | |
| 462 int rv; | |
| 463 Type2Msg msg; | |
| 464 | |
| 465 rv = ParseType2Msg(in_buf, in_len, &msg); | |
| 466 if (rv != OK) | |
| 467 return rv; | |
| 468 | |
| 469 bool unicode = (msg.flags & NTLM_NegotiateUnicode) != 0; | |
| 470 | |
| 471 // Temporary buffers for unicode strings | |
| 472 #ifdef IS_BIG_ENDIAN | |
| 473 string16 ucs_domain_buf, ucs_user_buf; | |
| 474 #endif | |
| 475 string16 ucs_host_buf; | |
| 476 // Temporary buffers for oem strings | |
| 477 std::string oem_domain_buf, oem_user_buf; | |
| 478 // Pointers and lengths for the string buffers; encoding is unicode if | |
| 479 // the "negotiate unicode" flag was set in the Type-2 message. | |
| 480 const void* domain_ptr; | |
| 481 const void* user_ptr; | |
| 482 const void* host_ptr; | |
| 483 uint32 domain_len, user_len, host_len; | |
| 484 | |
| 485 // | |
| 486 // Get domain name. | |
| 487 // | |
| 488 if (unicode) { | |
| 489 #ifdef IS_BIG_ENDIAN | |
| 490 ucs_domain_buf = domain; | |
| 491 domain_ptr = ucs_domain_buf.data(); | |
| 492 domain_len = ucs_domain_buf.length() * 2; | |
| 493 WriteUnicodeLE(const_cast<void*>(domain_ptr), (const char16*) domain_ptr, | |
| 494 ucs_domain_buf.length()); | |
| 495 #else | |
| 496 domain_ptr = domain.data(); | |
| 497 domain_len = domain.length() * 2; | |
| 498 #endif | |
| 499 } else { | |
| 500 oem_domain_buf = base::SysWideToNativeMB(UTF16ToWide(domain)); | |
| 501 domain_ptr = oem_domain_buf.data(); | |
| 502 domain_len = oem_domain_buf.length(); | |
| 503 } | |
| 504 | |
| 505 // | |
| 506 // Get user name. | |
| 507 // | |
| 508 if (unicode) { | |
| 509 #ifdef IS_BIG_ENDIAN | |
| 510 ucs_user_buf = username; | |
| 511 user_ptr = ucs_user_buf.data(); | |
| 512 user_len = ucs_user_buf.length() * 2; | |
| 513 WriteUnicodeLE(const_cast<void*>(user_ptr), (const char16*) user_ptr, | |
| 514 ucs_user_buf.length()); | |
| 515 #else | |
| 516 user_ptr = username.data(); | |
| 517 user_len = username.length() * 2; | |
| 518 #endif | |
| 519 } else { | |
| 520 oem_user_buf = base::SysWideToNativeMB(UTF16ToWide(username)); | |
| 521 user_ptr = oem_user_buf.data(); | |
| 522 user_len = oem_user_buf.length(); | |
| 523 } | |
| 524 | |
| 525 // | |
| 526 // Get workstation name (use local machine's hostname). | |
| 527 // | |
| 528 if (unicode) { | |
| 529 // hostname is ASCII, so we can do a simple zero-pad expansion: | |
| 530 ucs_host_buf.assign(hostname.begin(), hostname.end()); | |
| 531 host_ptr = ucs_host_buf.data(); | |
| 532 host_len = ucs_host_buf.length() * 2; | |
| 533 #ifdef IS_BIG_ENDIAN | |
| 534 WriteUnicodeLE(const_cast<void*>(host_ptr), (const char16*) host_ptr, | |
| 535 ucs_host_buf.length()); | |
| 536 #endif | |
| 537 } else { | |
| 538 host_ptr = hostname.data(); | |
| 539 host_len = hostname.length(); | |
| 540 } | |
| 541 | |
| 542 // | |
| 543 // Now that we have generated all of the strings, we can allocate out_buf. | |
| 544 // | |
| 545 *out_len = NTLM_TYPE3_HEADER_LEN + host_len + domain_len + user_len + | |
| 546 LM_RESP_LEN + NTLM_RESP_LEN; | |
| 547 *out_buf = malloc(*out_len); | |
| 548 if (!*out_buf) | |
| 549 return ERR_OUT_OF_MEMORY; | |
| 550 | |
| 551 // | |
| 552 // Next, we compute the LM and NTLM responses. | |
| 553 // | |
| 554 uint8 lm_resp[LM_RESP_LEN]; | |
| 555 uint8 ntlm_resp[NTLM_RESP_LEN]; | |
| 556 uint8 ntlm_hash[NTLM_HASH_LEN]; | |
| 557 if (msg.flags & NTLM_NegotiateNTLM2Key) { | |
| 558 // compute NTLM2 session response | |
| 559 MD5Digest session_hash; | |
| 560 uint8 temp[16]; | |
| 561 | |
| 562 memcpy(lm_resp, rand_8_bytes, 8); | |
| 563 memset(lm_resp + 8, 0, LM_RESP_LEN - 8); | |
| 564 | |
| 565 memcpy(temp, msg.challenge, 8); | |
| 566 memcpy(temp + 8, lm_resp, 8); | |
| 567 MD5Sum(temp, 16, &session_hash); | |
| 568 | |
| 569 NTLM_Hash(password, ntlm_hash); | |
| 570 LM_Response(ntlm_hash, session_hash.a, ntlm_resp); | |
| 571 } else { | |
| 572 NTLM_Hash(password, ntlm_hash); | |
| 573 LM_Response(ntlm_hash, msg.challenge, ntlm_resp); | |
| 574 | |
| 575 if (SendLM()) { | |
| 576 uint8 lm_hash[LM_HASH_LEN]; | |
| 577 LM_Hash(password, lm_hash); | |
| 578 LM_Response(lm_hash, msg.challenge, lm_resp); | |
| 579 } else { | |
| 580 // According to http://davenport.sourceforge.net/ntlm.html#ntlmVersion2, | |
| 581 // the correct way to not send the LM hash is to send the NTLM hash twice | |
| 582 // in both the LM and NTLM response fields. | |
| 583 LM_Response(ntlm_hash, msg.challenge, lm_resp); | |
| 584 } | |
| 585 } | |
| 586 | |
| 587 // | |
| 588 // Finally, we assemble the Type-3 msg :-) | |
| 589 // | |
| 590 void* cursor = *out_buf; | |
| 591 uint32 offset; | |
| 592 | |
| 593 // 0 : signature | |
| 594 cursor = WriteBytes(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)); | |
| 595 | |
| 596 // 8 : marker | |
| 597 cursor = WriteBytes(cursor, NTLM_TYPE3_MARKER, sizeof(NTLM_TYPE3_MARKER)); | |
| 598 | |
| 599 // 12 : LM response sec buf | |
| 600 offset = NTLM_TYPE3_HEADER_LEN + domain_len + user_len + host_len; | |
| 601 cursor = WriteSecBuf(cursor, LM_RESP_LEN, offset); | |
| 602 memcpy(static_cast<uint8*>(*out_buf) + offset, lm_resp, LM_RESP_LEN); | |
| 603 | |
| 604 // 20 : NTLM response sec buf | |
| 605 offset += LM_RESP_LEN; | |
| 606 cursor = WriteSecBuf(cursor, NTLM_RESP_LEN, offset); | |
| 607 memcpy(static_cast<uint8*>(*out_buf) + offset, ntlm_resp, NTLM_RESP_LEN); | |
| 608 | |
| 609 // 28 : domain name sec buf | |
| 610 offset = NTLM_TYPE3_HEADER_LEN; | |
| 611 cursor = WriteSecBuf(cursor, domain_len, offset); | |
| 612 memcpy(static_cast<uint8*>(*out_buf) + offset, domain_ptr, domain_len); | |
| 613 | |
| 614 // 36 : user name sec buf | |
| 615 offset += domain_len; | |
| 616 cursor = WriteSecBuf(cursor, user_len, offset); | |
| 617 memcpy(static_cast<uint8*>(*out_buf) + offset, user_ptr, user_len); | |
| 618 | |
| 619 // 44 : workstation (host) name sec buf | |
| 620 offset += user_len; | |
| 621 cursor = WriteSecBuf(cursor, host_len, offset); | |
| 622 memcpy(static_cast<uint8*>(*out_buf) + offset, host_ptr, host_len); | |
| 623 | |
| 624 // 52 : session key sec buf (not used) | |
| 625 cursor = WriteSecBuf(cursor, 0, 0); | |
| 626 | |
| 627 // 60 : negotiated flags | |
| 628 cursor = WriteDWORD(cursor, msg.flags & NTLM_TYPE1_FLAGS); | |
| 629 | |
| 630 return OK; | |
| 631 } | |
| 632 | |
| 633 // NTLM authentication is specified in "NTLM Over HTTP Protocol Specification" | |
| 634 // [MS-NTHT]. | |
| 635 | |
| 636 // static | |
| 637 HttpAuthHandlerNTLM::GenerateRandomProc | |
| 638 HttpAuthHandlerNTLM::generate_random_proc_ = GenerateRandom; | |
| 639 | |
| 640 // static | |
| 641 HttpAuthHandlerNTLM::HostNameProc | |
| 642 HttpAuthHandlerNTLM::get_host_name_proc_ = GetHostName; | |
| 643 | |
| 644 HttpAuthHandlerNTLM::HttpAuthHandlerNTLM() { | |
| 645 } | |
| 646 | |
| 647 HttpAuthHandlerNTLM::~HttpAuthHandlerNTLM() { | |
| 648 // Wipe our copy of the password from memory, to reduce the chance of being | |
| 649 // written to the paging file on disk. | |
| 650 ZapString(&password_); | |
| 651 } | |
| 652 | |
| 653 bool HttpAuthHandlerNTLM::NeedsIdentity() { | |
| 654 return !auth_data_.empty(); | |
| 655 } | |
| 656 | |
| 657 std::string HttpAuthHandlerNTLM::GenerateCredentials( | 13 std::string HttpAuthHandlerNTLM::GenerateCredentials( |
| 658 const std::wstring& username, | 14 const std::wstring& username, |
| 659 const std::wstring& password, | 15 const std::wstring& password, |
| 660 const HttpRequestInfo* request, | 16 const HttpRequestInfo* request, |
| 661 const ProxyInfo* proxy) { | 17 const ProxyInfo* proxy) { |
| 662 // TODO(wtc): See if we can use char* instead of void* for in_buf and | 18 // TODO(wtc): See if we can use char* instead of void* for in_buf and |
| 663 // out_buf. This change will need to propagate to GetNextToken, | 19 // out_buf. This change will need to propagate to GetNextToken, |
| 664 // GenerateType1Msg, and GenerateType3Msg, and perhaps further. | 20 // GenerateType1Msg, and GenerateType3Msg, and perhaps further. |
| 665 const void* in_buf; | 21 const void* in_buf; |
| 666 void* out_buf; | 22 void* out_buf; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 679 user = username.substr(backslash_idx + 1); | 35 user = username.substr(backslash_idx + 1); |
| 680 } | 36 } |
| 681 domain_ = WideToUTF16(domain); | 37 domain_ = WideToUTF16(domain); |
| 682 username_ = WideToUTF16(user); | 38 username_ = WideToUTF16(user); |
| 683 password_ = WideToUTF16(password); | 39 password_ = WideToUTF16(password); |
| 684 | 40 |
| 685 // Initial challenge. | 41 // Initial challenge. |
| 686 if (auth_data_.empty()) { | 42 if (auth_data_.empty()) { |
| 687 in_buf_len = 0; | 43 in_buf_len = 0; |
| 688 in_buf = NULL; | 44 in_buf = NULL; |
| 45 int rv = InitializeBeforeFirstChallenge(); |
| 46 if (rv != OK) |
| 47 return std::string(); |
| 689 } else { | 48 } else { |
| 690 // Decode |auth_data_| into the input buffer. | 49 // Decode |auth_data_| into the input buffer. |
| 691 int len = auth_data_.length(); | 50 int len = auth_data_.length(); |
| 692 | 51 |
| 693 // Strip off any padding. | 52 // Strip off any padding. |
| 694 // (See https://bugzilla.mozilla.org/show_bug.cgi?id=230351.) | 53 // (See https://bugzilla.mozilla.org/show_bug.cgi?id=230351.) |
| 695 // | 54 // |
| 696 // Our base64 decoder requires that the length be a multiple of 4. | 55 // Our base64 decoder requires that the length be a multiple of 4. |
| 697 while (len > 0 && len % 4 != 0 && auth_data_[len - 1] == '=') | 56 while (len > 0 && len % 4 != 0 && auth_data_[len - 1] == '=') |
| 698 len--; | 57 len--; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 712 std::string encode_input(static_cast<char*>(out_buf), out_buf_len); | 71 std::string encode_input(static_cast<char*>(out_buf), out_buf_len); |
| 713 std::string encode_output; | 72 std::string encode_output; |
| 714 bool ok = Base64Encode(encode_input, &encode_output); | 73 bool ok = Base64Encode(encode_input, &encode_output); |
| 715 // OK, we are done with |out_buf| | 74 // OK, we are done with |out_buf| |
| 716 free(out_buf); | 75 free(out_buf); |
| 717 if (!ok) | 76 if (!ok) |
| 718 return std::string(); | 77 return std::string(); |
| 719 return std::string("NTLM ") + encode_output; | 78 return std::string("NTLM ") + encode_output; |
| 720 } | 79 } |
| 721 | 80 |
| 722 // static | |
| 723 HttpAuthHandlerNTLM::GenerateRandomProc | |
| 724 HttpAuthHandlerNTLM::SetGenerateRandomProc( | |
| 725 GenerateRandomProc proc) { | |
| 726 GenerateRandomProc old_proc = generate_random_proc_; | |
| 727 generate_random_proc_ = proc; | |
| 728 return old_proc; | |
| 729 } | |
| 730 | |
| 731 // static | |
| 732 HttpAuthHandlerNTLM::HostNameProc HttpAuthHandlerNTLM::SetHostNameProc( | |
| 733 HostNameProc proc) { | |
| 734 HostNameProc old_proc = get_host_name_proc_; | |
| 735 get_host_name_proc_ = proc; | |
| 736 return old_proc; | |
| 737 } | |
| 738 | |
| 739 // The NTLM challenge header looks like: | 81 // The NTLM challenge header looks like: |
| 740 // WWW-Authenticate: NTLM auth-data | 82 // WWW-Authenticate: NTLM auth-data |
| 741 bool HttpAuthHandlerNTLM::ParseChallenge( | 83 bool HttpAuthHandlerNTLM::ParseChallenge( |
| 742 std::string::const_iterator challenge_begin, | 84 std::string::const_iterator challenge_begin, |
| 743 std::string::const_iterator challenge_end) { | 85 std::string::const_iterator challenge_end) { |
| 744 scheme_ = "ntlm"; | 86 scheme_ = "ntlm"; |
| 745 score_ = 3; | 87 score_ = 3; |
| 746 properties_ = ENCRYPTS_IDENTITY | IS_CONNECTION_BASED; | 88 properties_ = ENCRYPTS_IDENTITY | IS_CONNECTION_BASED; |
| 747 auth_data_.clear(); | 89 auth_data_.clear(); |
| 748 | 90 |
| 749 // Verify the challenge's auth-scheme. | 91 // Verify the challenge's auth-scheme. |
| 750 HttpAuth::ChallengeTokenizer challenge_tok(challenge_begin, challenge_end); | 92 HttpAuth::ChallengeTokenizer challenge_tok(challenge_begin, challenge_end); |
| 751 if (!challenge_tok.valid() || | 93 if (!challenge_tok.valid() || |
| 752 !LowerCaseEqualsASCII(challenge_tok.scheme(), "ntlm")) | 94 !LowerCaseEqualsASCII(challenge_tok.scheme(), "ntlm")) |
| 753 return false; | 95 return false; |
| 754 | 96 |
| 755 // Extract the auth-data. We can't use challenge_tok.GetNext() because | 97 // Extract the auth-data. We can't use challenge_tok.GetNext() because |
| 756 // auth-data is base64-encoded and may contain '=' padding at the end, | 98 // auth-data is base64-encoded and may contain '=' padding at the end, |
| 757 // which would be mistaken for a name=value pair. | 99 // which would be mistaken for a name=value pair. |
| 758 challenge_begin += 4; // Skip over "NTLM". | 100 challenge_begin += 4; // Skip over "NTLM". |
| 759 HttpUtil::TrimLWS(&challenge_begin, &challenge_end); | 101 HttpUtil::TrimLWS(&challenge_begin, &challenge_end); |
| 760 | 102 |
| 761 auth_data_.assign(challenge_begin, challenge_end); | 103 auth_data_.assign(challenge_begin, challenge_end); |
| 762 | 104 |
| 763 return true; | 105 return true; |
| 764 } | 106 } |
| 765 | 107 |
| 766 int HttpAuthHandlerNTLM::GetNextToken(const void* in_token, | |
| 767 uint32 in_token_len, | |
| 768 void** out_token, | |
| 769 uint32* out_token_len) { | |
| 770 int rv; | |
| 771 | |
| 772 // If in_token is non-null, then assume it contains a type 2 message... | |
| 773 if (in_token) { | |
| 774 LogToken("in-token", in_token, in_token_len); | |
| 775 std::string hostname = get_host_name_proc_(); | |
| 776 if (hostname.empty()) | |
| 777 return ERR_UNEXPECTED; | |
| 778 uint8 rand_buf[8]; | |
| 779 generate_random_proc_(rand_buf, 8); | |
| 780 rv = GenerateType3Msg(domain_, username_, password_, hostname, rand_buf, | |
| 781 in_token, in_token_len, out_token, out_token_len); | |
| 782 } else { | |
| 783 rv = GenerateType1Msg(out_token, out_token_len); | |
| 784 } | |
| 785 | |
| 786 if (rv == OK) | |
| 787 LogToken("out-token", *out_token, *out_token_len); | |
| 788 | |
| 789 return rv; | |
| 790 } | |
| 791 | |
| 792 } // namespace net | 108 } // namespace net |
| OLD | NEW |