This source file includes following definitions.
- SendLM
- WriteBytes
- WriteDWORD
- WriteSecBuf
- WriteUnicodeLE
- ReadUint16
- ReadUint32
- LM_Hash
- NTLM_Hash
- LM_Response
- GenerateType1Msg
- ParseType2Msg
- GenerateRandom
- GenerateType3Msg
- NeedsIdentity
- AllowsDefaultCredentials
- InitializeBeforeFirstChallenge
- SetGenerateRandomProc
- SetHostNameProc
- GetNextToken
- CreateAuthHandler
#include "net/http/http_auth_handler_ntlm.h"
#include <stdlib.h>
#if defined(OS_POSIX)
#include <unistd.h>
#elif defined(OS_WIN)
#include <winsock2.h>
#endif
#include "base/md5.h"
#include "base/rand_util.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/zap.h"
#include "net/http/des.h"
#include "net/http/md4.h"
namespace net {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
#define IS_LITTLE_ENDIAN 1
#undef IS_BIG_ENDIAN
#elif defined(ARCH_CPU_BIG_ENDIAN)
#define IS_BIG_ENDIAN 1
#undef IS_LITTLE_ENDIAN
#else
#error "Unknown endianness"
#endif
#define NTLM_LOG(x) ((void) 0)
enum {
NTLM_NegotiateUnicode = 0x00000001,
NTLM_NegotiateOEM = 0x00000002,
NTLM_RequestTarget = 0x00000004,
NTLM_Unknown1 = 0x00000008,
NTLM_NegotiateSign = 0x00000010,
NTLM_NegotiateSeal = 0x00000020,
NTLM_NegotiateDatagramStyle = 0x00000040,
NTLM_NegotiateLanManagerKey = 0x00000080,
NTLM_NegotiateNetware = 0x00000100,
NTLM_NegotiateNTLMKey = 0x00000200,
NTLM_Unknown2 = 0x00000400,
NTLM_Unknown3 = 0x00000800,
NTLM_NegotiateDomainSupplied = 0x00001000,
NTLM_NegotiateWorkstationSupplied = 0x00002000,
NTLM_NegotiateLocalCall = 0x00004000,
NTLM_NegotiateAlwaysSign = 0x00008000,
NTLM_TargetTypeDomain = 0x00010000,
NTLM_TargetTypeServer = 0x00020000,
NTLM_TargetTypeShare = 0x00040000,
NTLM_NegotiateNTLM2Key = 0x00080000,
NTLM_RequestInitResponse = 0x00100000,
NTLM_RequestAcceptResponse = 0x00200000,
NTLM_RequestNonNTSessionKey = 0x00400000,
NTLM_NegotiateTargetInfo = 0x00800000,
NTLM_Unknown4 = 0x01000000,
NTLM_Unknown5 = 0x02000000,
NTLM_Unknown6 = 0x04000000,
NTLM_Unknown7 = 0x08000000,
NTLM_Unknown8 = 0x10000000,
NTLM_Negotiate128 = 0x20000000,
NTLM_NegotiateKeyExchange = 0x40000000,
NTLM_Negotiate56 = 0x80000000
};
enum {
NTLM_TYPE1_FLAGS = (NTLM_NegotiateUnicode |
NTLM_NegotiateOEM |
NTLM_RequestTarget |
NTLM_NegotiateNTLMKey |
NTLM_NegotiateAlwaysSign |
NTLM_NegotiateNTLM2Key)
};
static const char NTLM_SIGNATURE[] = "NTLMSSP";
static const char NTLM_TYPE1_MARKER[] = { 0x01, 0x00, 0x00, 0x00 };
static const char NTLM_TYPE2_MARKER[] = { 0x02, 0x00, 0x00, 0x00 };
static const char NTLM_TYPE3_MARKER[] = { 0x03, 0x00, 0x00, 0x00 };
enum {
NTLM_TYPE1_HEADER_LEN = 32,
NTLM_TYPE2_HEADER_LEN = 32,
NTLM_TYPE3_HEADER_LEN = 64,
LM_HASH_LEN = 16,
LM_RESP_LEN = 24,
NTLM_HASH_LEN = 16,
NTLM_RESP_LEN = 24
};
static bool SendLM() {
return false;
}
#define LogFlags(x) ((void) 0)
#define LogBuf(a, b, c) ((void) 0)
#define LogToken(a, b, c) ((void) 0)
#define SWAP16(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
#define SWAP32(x) ((SWAP16((x) & 0xffff) << 16) | (SWAP16((x) >> 16)))
static void* WriteBytes(void* buf, const void* data, uint32 data_len) {
memcpy(buf, data, data_len);
return static_cast<char*>(buf) + data_len;
}
static void* WriteDWORD(void* buf, uint32 dword) {
#ifdef IS_BIG_ENDIAN
dword = SWAP32(dword);
#endif
return WriteBytes(buf, &dword, sizeof(dword));
}
static void* WriteSecBuf(void* buf, uint16 length, uint32 offset) {
#ifdef IS_BIG_ENDIAN
length = SWAP16(length);
offset = SWAP32(offset);
#endif
buf = WriteBytes(buf, &length, sizeof(length));
buf = WriteBytes(buf, &length, sizeof(length));
buf = WriteBytes(buf, &offset, sizeof(offset));
return buf;
}
#ifdef IS_BIG_ENDIAN
static void* WriteUnicodeLE(
void* buf, const base::char16* str, uint32 str_len) {
uint8* cursor = static_cast<uint8*>(buf);
const uint8* input = reinterpret_cast<const uint8*>(str);
for (uint32 i = 0; i < str_len; ++i, input += 2, cursor += 2) {
uint8 temp = input[0];
cursor[0] = input[1];
cursor[1] = temp;
}
return buf;
}
#endif
static uint16 ReadUint16(const uint8*& buf) {
uint16 x = (static_cast<uint16>(buf[0])) |
(static_cast<uint16>(buf[1]) << 8);
buf += sizeof(x);
return x;
}
static uint32 ReadUint32(const uint8*& buf) {
uint32 x = (static_cast<uint32>(buf[0])) |
(static_cast<uint32>(buf[1]) << 8) |
(static_cast<uint32>(buf[2]) << 16) |
(static_cast<uint32>(buf[3]) << 24);
buf += sizeof(x);
return x;
}
static void LM_Hash(const base::string16& password, uint8* hash) {
static const uint8 LM_MAGIC[] = "KGS!@#$%";
std::string passbuf = base::SysWideToNativeMB(base::UTF16ToWide(password));
StringToUpperASCII(&passbuf);
passbuf.resize(14, '\0');
uint8 k1[8], k2[8];
DESMakeKey(reinterpret_cast<const uint8*>(passbuf.data()) , k1);
DESMakeKey(reinterpret_cast<const uint8*>(passbuf.data()) + 7, k2);
ZapString(&passbuf);
DESEncrypt(k1, LM_MAGIC, hash);
DESEncrypt(k2, LM_MAGIC, hash + 8);
}
static void NTLM_Hash(const base::string16& password, uint8* hash) {
#ifdef IS_BIG_ENDIAN
uint32 len = password.length();
uint8* passbuf;
passbuf = static_cast<uint8*>(malloc(len * 2));
WriteUnicodeLE(passbuf, password.data(), len);
weak_crypto::MD4Sum(passbuf, len * 2, hash);
ZapBuf(passbuf, len * 2);
free(passbuf);
#else
weak_crypto::MD4Sum(reinterpret_cast<const uint8*>(password.data()),
password.length() * 2, hash);
#endif
}
static void LM_Response(const uint8* hash,
const uint8* challenge,
uint8* response) {
uint8 keybytes[21], k1[8], k2[8], k3[8];
memcpy(keybytes, hash, 16);
ZapBuf(keybytes + 16, 5);
DESMakeKey(keybytes , k1);
DESMakeKey(keybytes + 7, k2);
DESMakeKey(keybytes + 14, k3);
DESEncrypt(k1, challenge, response);
DESEncrypt(k2, challenge, response + 8);
DESEncrypt(k3, challenge, response + 16);
}
static int GenerateType1Msg(void** out_buf, uint32* out_len) {
*out_len = NTLM_TYPE1_HEADER_LEN;
*out_buf = malloc(*out_len);
if (!*out_buf)
return ERR_OUT_OF_MEMORY;
void* cursor = *out_buf;
cursor = WriteBytes(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE));
cursor = WriteBytes(cursor, NTLM_TYPE1_MARKER, sizeof(NTLM_TYPE1_MARKER));
cursor = WriteDWORD(cursor, NTLM_TYPE1_FLAGS);
cursor = WriteSecBuf(cursor, 0, 0);
cursor = WriteSecBuf(cursor, 0, 0);
return OK;
}
struct Type2Msg {
uint32 flags;
uint8 challenge[8];
const void* target;
uint32 target_len;
};
static int ParseType2Msg(const void* in_buf, uint32 in_len, Type2Msg* msg) {
if (in_len < NTLM_TYPE2_HEADER_LEN)
return ERR_UNEXPECTED;
const uint8* cursor = (const uint8*) in_buf;
if (memcmp(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)) != 0)
return ERR_UNEXPECTED;
cursor += sizeof(NTLM_SIGNATURE);
if (memcmp(cursor, NTLM_TYPE2_MARKER, sizeof(NTLM_TYPE2_MARKER)) != 0)
return ERR_UNEXPECTED;
cursor += sizeof(NTLM_TYPE2_MARKER);
uint32 target_len = ReadUint16(cursor);
ReadUint16(cursor);
uint32 offset = ReadUint32(cursor);
msg->target_len = 0;
msg->target = NULL;
if (offset + target_len > offset && offset + target_len <= in_len) {
msg->target_len = target_len;
msg->target = ((const uint8*) in_buf) + offset;
}
msg->flags = ReadUint32(cursor);
memcpy(msg->challenge, cursor, sizeof(msg->challenge));
cursor += sizeof(msg->challenge);
NTLM_LOG(("NTLM type 2 message:\n"));
LogBuf("target", (const uint8*) msg->target, msg->target_len);
LogBuf("flags", (const uint8*) &msg->flags, 4);
LogFlags(msg->flags);
LogBuf("challenge", msg->challenge, sizeof(msg->challenge));
return OK;
}
static void GenerateRandom(uint8* output, size_t n) {
for (size_t i = 0; i < n; ++i)
output[i] = base::RandInt(0, 255);
}
static int GenerateType3Msg(const base::string16& domain,
const base::string16& username,
const base::string16& password,
const std::string& hostname,
const void* rand_8_bytes,
const void* in_buf,
uint32 in_len,
void** out_buf,
uint32* out_len) {
int rv;
Type2Msg msg;
rv = ParseType2Msg(in_buf, in_len, &msg);
if (rv != OK)
return rv;
bool unicode = (msg.flags & NTLM_NegotiateUnicode) != 0;
#ifdef IS_BIG_ENDIAN
base::string16 ucs_domain_buf, ucs_user_buf;
#endif
base::string16 ucs_host_buf;
std::string oem_domain_buf, oem_user_buf;
const void* domain_ptr;
const void* user_ptr;
const void* host_ptr;
uint32 domain_len, user_len, host_len;
if (unicode) {
#ifdef IS_BIG_ENDIAN
ucs_domain_buf = domain;
domain_ptr = ucs_domain_buf.data();
domain_len = ucs_domain_buf.length() * 2;
WriteUnicodeLE(const_cast<void*>(domain_ptr),
(const base::char16*) domain_ptr,
ucs_domain_buf.length());
#else
domain_ptr = domain.data();
domain_len = domain.length() * 2;
#endif
} else {
oem_domain_buf = base::SysWideToNativeMB(base::UTF16ToWide(domain));
domain_ptr = oem_domain_buf.data();
domain_len = oem_domain_buf.length();
}
if (unicode) {
#ifdef IS_BIG_ENDIAN
ucs_user_buf = username;
user_ptr = ucs_user_buf.data();
user_len = ucs_user_buf.length() * 2;
WriteUnicodeLE(const_cast<void*>(user_ptr), (const base::char16*) user_ptr,
ucs_user_buf.length());
#else
user_ptr = username.data();
user_len = username.length() * 2;
#endif
} else {
oem_user_buf = base::SysWideToNativeMB(base::UTF16ToWide(username));
user_ptr = oem_user_buf.data();
user_len = oem_user_buf.length();
}
if (unicode) {
ucs_host_buf.assign(hostname.begin(), hostname.end());
host_ptr = ucs_host_buf.data();
host_len = ucs_host_buf.length() * 2;
#ifdef IS_BIG_ENDIAN
WriteUnicodeLE(const_cast<void*>(host_ptr), (const base::char16*) host_ptr,
ucs_host_buf.length());
#endif
} else {
host_ptr = hostname.data();
host_len = hostname.length();
}
*out_len = NTLM_TYPE3_HEADER_LEN + host_len + domain_len + user_len +
LM_RESP_LEN + NTLM_RESP_LEN;
*out_buf = malloc(*out_len);
if (!*out_buf)
return ERR_OUT_OF_MEMORY;
uint8 lm_resp[LM_RESP_LEN];
uint8 ntlm_resp[NTLM_RESP_LEN];
uint8 ntlm_hash[NTLM_HASH_LEN];
if (msg.flags & NTLM_NegotiateNTLM2Key) {
base::MD5Digest session_hash;
uint8 temp[16];
memcpy(lm_resp, rand_8_bytes, 8);
memset(lm_resp + 8, 0, LM_RESP_LEN - 8);
memcpy(temp, msg.challenge, 8);
memcpy(temp + 8, lm_resp, 8);
base::MD5Sum(temp, 16, &session_hash);
NTLM_Hash(password, ntlm_hash);
LM_Response(ntlm_hash, session_hash.a, ntlm_resp);
} else {
NTLM_Hash(password, ntlm_hash);
LM_Response(ntlm_hash, msg.challenge, ntlm_resp);
if (SendLM()) {
uint8 lm_hash[LM_HASH_LEN];
LM_Hash(password, lm_hash);
LM_Response(lm_hash, msg.challenge, lm_resp);
} else {
LM_Response(ntlm_hash, msg.challenge, lm_resp);
}
}
void* cursor = *out_buf;
uint32 offset;
cursor = WriteBytes(cursor, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE));
cursor = WriteBytes(cursor, NTLM_TYPE3_MARKER, sizeof(NTLM_TYPE3_MARKER));
offset = NTLM_TYPE3_HEADER_LEN + domain_len + user_len + host_len;
cursor = WriteSecBuf(cursor, LM_RESP_LEN, offset);
memcpy(static_cast<uint8*>(*out_buf) + offset, lm_resp, LM_RESP_LEN);
offset += LM_RESP_LEN;
cursor = WriteSecBuf(cursor, NTLM_RESP_LEN, offset);
memcpy(static_cast<uint8*>(*out_buf) + offset, ntlm_resp, NTLM_RESP_LEN);
offset = NTLM_TYPE3_HEADER_LEN;
cursor = WriteSecBuf(cursor, domain_len, offset);
memcpy(static_cast<uint8*>(*out_buf) + offset, domain_ptr, domain_len);
offset += domain_len;
cursor = WriteSecBuf(cursor, user_len, offset);
memcpy(static_cast<uint8*>(*out_buf) + offset, user_ptr, user_len);
offset += user_len;
cursor = WriteSecBuf(cursor, host_len, offset);
memcpy(static_cast<uint8*>(*out_buf) + offset, host_ptr, host_len);
cursor = WriteSecBuf(cursor, 0, 0);
cursor = WriteDWORD(cursor, msg.flags & NTLM_TYPE1_FLAGS);
return OK;
}
HttpAuthHandlerNTLM::GenerateRandomProc
HttpAuthHandlerNTLM::generate_random_proc_ = GenerateRandom;
HttpAuthHandlerNTLM::HostNameProc
HttpAuthHandlerNTLM::get_host_name_proc_ = GetHostName;
HttpAuthHandlerNTLM::HttpAuthHandlerNTLM() {
}
bool HttpAuthHandlerNTLM::NeedsIdentity() {
return auth_data_.empty();
}
bool HttpAuthHandlerNTLM::AllowsDefaultCredentials() {
return false;
}
int HttpAuthHandlerNTLM::InitializeBeforeFirstChallenge() {
return OK;
}
HttpAuthHandlerNTLM::~HttpAuthHandlerNTLM() {
credentials_.Zap();
}
HttpAuthHandlerNTLM::GenerateRandomProc
HttpAuthHandlerNTLM::SetGenerateRandomProc(
GenerateRandomProc proc) {
GenerateRandomProc old_proc = generate_random_proc_;
generate_random_proc_ = proc;
return old_proc;
}
HttpAuthHandlerNTLM::HostNameProc HttpAuthHandlerNTLM::SetHostNameProc(
HostNameProc proc) {
HostNameProc old_proc = get_host_name_proc_;
get_host_name_proc_ = proc;
return old_proc;
}
HttpAuthHandlerNTLM::Factory::Factory() {
}
HttpAuthHandlerNTLM::Factory::~Factory() {
}
int HttpAuthHandlerNTLM::GetNextToken(const void* in_token,
uint32 in_token_len,
void** out_token,
uint32* out_token_len) {
int rv = 0;
if (in_token) {
LogToken("in-token", in_token, in_token_len);
std::string hostname = get_host_name_proc_();
if (hostname.empty())
return ERR_UNEXPECTED;
uint8 rand_buf[8];
generate_random_proc_(rand_buf, 8);
rv = GenerateType3Msg(domain_,
credentials_.username(), credentials_.password(),
hostname, rand_buf,
in_token, in_token_len, out_token, out_token_len);
} else {
rv = GenerateType1Msg(out_token, out_token_len);
}
if (rv == OK)
LogToken("out-token", *out_token, *out_token_len);
return rv;
}
int HttpAuthHandlerNTLM::Factory::CreateAuthHandler(
HttpAuthChallengeTokenizer* challenge,
HttpAuth::Target target,
const GURL& origin,
CreateReason reason,
int digest_nonce_count,
const BoundNetLog& net_log,
scoped_ptr<HttpAuthHandler>* handler) {
if (reason == CREATE_PREEMPTIVE)
return ERR_UNSUPPORTED_AUTH_SCHEME;
scoped_ptr<HttpAuthHandler> tmp_handler(new HttpAuthHandlerNTLM);
if (!tmp_handler->InitFromChallenge(challenge, target, origin, net_log))
return ERR_INVALID_RESPONSE;
handler->swap(tmp_handler);
return OK;
}
}