Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/ntlm/ntlm_client.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "net/ntlm/ntlm.h" | |
| 12 #include "net/ntlm/ntlm_buffer_reader.h" | |
| 13 #include "net/ntlm/ntlm_buffer_writer.h" | |
| 14 | |
| 15 namespace net { | |
| 16 namespace ntlm { | |
| 17 | |
| 18 namespace { | |
| 19 // Parses the challenge message and returns the |challenge_flags| and writes | |
| 20 // the |server_challenge| into the supplied buffer. | |
| 21 // |server_challenge| must contain at least 8 bytes. | |
| 22 static bool ParseChallengeMessage(const uint8_t* challenge_message, | |
| 23 size_t challenge_message_len, | |
| 24 NegotiateFlags* challenge_flags, | |
| 25 uint8_t* server_challenge) { | |
| 26 NtlmBufferReader challenge_reader(challenge_message, challenge_message_len); | |
| 27 | |
| 28 return challenge_reader.MatchMessageHeader(MessageType::CHALLENGE) && | |
| 29 challenge_reader.SkipSecurityBufferWithValidation() && | |
| 30 challenge_reader.ReadFlags(challenge_flags) && | |
| 31 challenge_reader.ReadBytes(server_challenge, CHALLENGE_LEN); | |
| 32 } | |
| 33 | |
| 34 static bool WriteAuthenticateMessage(NtlmBufferWriter* authenticate_writer, | |
| 35 SecurityBuffer lm_payload, | |
| 36 SecurityBuffer ntlm_payload, | |
| 37 SecurityBuffer domain_payload, | |
| 38 SecurityBuffer username_payload, | |
| 39 SecurityBuffer hostname_payload, | |
| 40 NegotiateFlags authenticate_flags) { | |
| 41 return authenticate_writer->WriteMessageHeader(MessageType::AUTHENTICATE) && | |
| 42 authenticate_writer->WriteSecurityBuffer(lm_payload) && | |
| 43 authenticate_writer->WriteSecurityBuffer(ntlm_payload) && | |
| 44 authenticate_writer->WriteSecurityBuffer(domain_payload) && | |
| 45 authenticate_writer->WriteSecurityBuffer(username_payload) && | |
| 46 authenticate_writer->WriteSecurityBuffer(hostname_payload) && | |
| 47 authenticate_writer->WriteSecurityBuffer( | |
| 48 SecurityBuffer(AUTHENTICATE_HEADER_V1_LEN, 0)) && | |
| 49 authenticate_writer->WriteFlags(authenticate_flags); | |
| 50 } | |
| 51 | |
| 52 static bool WriteResponsePayloads(NtlmBufferWriter* authenticate_writer, | |
| 53 const uint8_t* lm_response, | |
| 54 size_t lm_response_len, | |
| 55 const uint8_t* ntlm_response, | |
| 56 size_t ntlm_response_len) { | |
| 57 return authenticate_writer->WriteBytes(lm_response, lm_response_len) && | |
| 58 authenticate_writer->WriteBytes(ntlm_response, ntlm_response_len); | |
| 59 } | |
| 60 | |
| 61 static bool WriteStringPayloads(NtlmBufferWriter* authenticate_writer, | |
| 62 bool is_unicode, | |
| 63 const base::string16& domain, | |
| 64 const base::string16& username, | |
| 65 const std::string hostname) { | |
| 66 if (is_unicode) { | |
| 67 return authenticate_writer->WriteUtf16String(domain) && | |
| 68 authenticate_writer->WriteUtf16String(username) && | |
| 69 authenticate_writer->WriteUtf8AsUtf16String(hostname); | |
| 70 } else { | |
| 71 return authenticate_writer->WriteUtf16AsUtf8String(domain) && | |
| 72 authenticate_writer->WriteUtf16AsUtf8String(username) && | |
| 73 authenticate_writer->WriteUtf8String(hostname); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 // Returns the size in bytes of a string16 depending whether unicode | |
| 78 // was negotiated. | |
| 79 static size_t GetStringPayloadLength(const base::string16& str, | |
| 80 bool is_unicode) { | |
| 81 if (is_unicode) | |
| 82 return str.length() * 2; | |
| 83 | |
| 84 // When |WriteUtf16AsUtf8String| is called with a |base::string16|, the string | |
| 85 // is converted to UTF8. Do the conversion to ensure that the character | |
| 86 // count is correct. | |
| 87 return base::UTF16ToUTF8(str).length(); | |
| 88 } | |
| 89 | |
| 90 // Returns the size in bytes of a std::string depending whether unicode | |
| 91 // was negotiated. | |
| 92 static size_t GetStringPayloadLength(const std::string& str, bool is_unicode) { | |
| 93 if (!is_unicode) | |
| 94 return str.length(); | |
| 95 | |
| 96 return base::UTF8ToUTF16(str).length() * 2; | |
| 97 } | |
| 98 | |
| 99 } // namespace | |
| 100 | |
| 101 NtlmClient::NtlmClient() : negotiate_flags_(NEGOTIATE_MESSAGE_FLAGS) { | |
| 102 // Just generate the negotiate message once and hold on to it. It never | |
| 103 // changes and in a NTLMv2 it's used as an input | |
| 104 // to the Message Integrity Check in the Authenticate message. | |
| 105 GenerateNegotiateMessage(); | |
| 106 } | |
| 107 | |
| 108 NtlmClient::~NtlmClient() {} | |
| 109 | |
| 110 void NtlmClient::GetNegotiateMessage(uint8_t** negotiate_message, | |
| 111 size_t* negotiate_message_len) const { | |
| 112 *negotiate_message_len = NEGOTIATE_MESSAGE_LEN; | |
| 113 *negotiate_message = new uint8_t[NEGOTIATE_MESSAGE_LEN]; | |
|
asanka
2017/07/12 20:38:37
HttpAuthHandlerNTLM::GenerateAuthTokenImpl ultimat
zentaro
2017/07/13 20:27:18
I like that latter option in the long run but don'
asanka
2017/07/14 16:52:38
I apologize for not pushing harder for something b
| |
| 114 memcpy(*negotiate_message, negotiate_message_.get(), NEGOTIATE_MESSAGE_LEN); | |
| 115 } | |
| 116 | |
| 117 void NtlmClient::GenerateNegotiateMessage() { | |
| 118 NtlmBufferWriter writer(NEGOTIATE_MESSAGE_LEN); | |
| 119 bool result = | |
| 120 writer.WriteMessageHeader(MessageType::NEGOTIATE) && | |
| 121 writer.WriteFlags(negotiate_flags_) && | |
| 122 writer.WriteSecurityBuffer(SecurityBuffer(NEGOTIATE_MESSAGE_LEN, 0)) && | |
| 123 writer.WriteSecurityBuffer(SecurityBuffer(NEGOTIATE_MESSAGE_LEN, 0)) && | |
| 124 writer.IsEndOfBuffer(); | |
| 125 | |
| 126 DCHECK(result); | |
| 127 | |
| 128 negotiate_message_ = writer.ReleaseBuffer(); | |
| 129 } | |
| 130 | |
| 131 bool NtlmClient::GenerateAuthenticateMessage( | |
| 132 const base::string16& domain, | |
| 133 const base::string16& username, | |
| 134 const base::string16& password, | |
| 135 const std::string& hostname, | |
| 136 const uint8_t* client_challenge, | |
| 137 const uint8_t* challenge_message, | |
| 138 size_t challenge_message_len, | |
| 139 uint8_t** authenticate_message, | |
| 140 size_t* authenticate_message_len) const { | |
| 141 *authenticate_message = nullptr; | |
| 142 *authenticate_message_len = 0; | |
| 143 NegotiateFlags challenge_flags; | |
| 144 uint8_t server_challenge[CHALLENGE_LEN]; | |
| 145 | |
| 146 if (!ParseChallengeMessage(challenge_message, challenge_message_len, | |
| 147 &challenge_flags, server_challenge)) { | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 // Calculate the responses for the authenticate message. | |
| 152 uint8_t lm_response[RESPONSE_V1_LEN]; | |
| 153 uint8_t ntlm_response[RESPONSE_V1_LEN]; | |
| 154 | |
| 155 // Always use extended session security even if the server tries to downgrade. | |
| 156 NegotiateFlags authenticate_flags = (challenge_flags & negotiate_flags_) | | |
| 157 NegotiateFlags::EXTENDED_SESSIONSECURITY; | |
| 158 | |
| 159 // Generate the LM and NTLM responses. | |
| 160 GenerateResponsesV1WithSS(password, server_challenge, client_challenge, | |
| 161 lm_response, ntlm_response); | |
| 162 | |
| 163 // Calculate all the payload lengths and offsets. | |
| 164 bool is_unicode = | |
| 165 static_cast<bool>(authenticate_flags & NegotiateFlags::UNICODE); | |
| 166 | |
| 167 SecurityBuffer lm_info; | |
| 168 SecurityBuffer ntlm_info; | |
| 169 SecurityBuffer domain_info; | |
| 170 SecurityBuffer username_info; | |
| 171 SecurityBuffer hostname_info; | |
| 172 CalculatePayloadSizes(is_unicode, domain, username, hostname, &lm_info, | |
| 173 &ntlm_info, &domain_info, &username_info, | |
| 174 &hostname_info, authenticate_message_len); | |
| 175 | |
| 176 // Write the authenticate message header. | |
| 177 NtlmBufferWriter authenticate_writer(*authenticate_message_len); | |
| 178 bool writer_result = WriteAuthenticateMessage( | |
| 179 &authenticate_writer, lm_info, ntlm_info, domain_info, username_info, | |
| 180 hostname_info, authenticate_flags); | |
| 181 DCHECK(writer_result); | |
| 182 DCHECK(authenticate_writer.GetCursor() == GetAuthenticateHeaderLength()); | |
| 183 | |
| 184 // Write the response payloads. | |
| 185 writer_result = | |
| 186 WriteResponsePayloads(&authenticate_writer, lm_response, lm_info.length, | |
| 187 ntlm_response, ntlm_info.length); | |
| 188 DCHECK(writer_result); | |
| 189 DCHECK(authenticate_writer.GetCursor() == domain_info.offset); | |
| 190 | |
| 191 // Write the string field payloads. | |
| 192 writer_result = WriteStringPayloads(&authenticate_writer, is_unicode, domain, | |
| 193 username, hostname); | |
| 194 DCHECK(writer_result); | |
| 195 DCHECK(authenticate_writer.IsEndOfBuffer()); | |
| 196 | |
| 197 *authenticate_message = authenticate_writer.ReleaseBuffer().release(); | |
| 198 return true; | |
| 199 } | |
| 200 | |
| 201 size_t NtlmClient::CalculateAuthenticateMessageLength( | |
| 202 bool is_unicode, | |
| 203 const base::string16& domain, | |
| 204 const base::string16& username, | |
| 205 const std::string& hostname) const { | |
| 206 return GetAuthenticateHeaderLength() + | |
| 207 GetStringPayloadLength(domain, is_unicode) + | |
| 208 GetStringPayloadLength(username, is_unicode) + | |
| 209 GetStringPayloadLength(hostname, is_unicode) + RESPONSE_V1_LEN + | |
| 210 GetNtlmResponseLength(); | |
| 211 } | |
| 212 | |
| 213 void NtlmClient::CalculatePayloadSizes(bool is_unicode, | |
|
asanka
2017/07/12 20:38:37
This is calculating payload layout.
zentaro
2017/07/13 20:27:18
Done.
| |
| 214 const base::string16& domain, | |
| 215 const base::string16& username, | |
| 216 const std::string& hostname, | |
| 217 SecurityBuffer* lm_info, | |
| 218 SecurityBuffer* ntlm_info, | |
| 219 SecurityBuffer* domain_info, | |
| 220 SecurityBuffer* username_info, | |
| 221 SecurityBuffer* hostname_info, | |
| 222 size_t* authenticate_message_len) const { | |
| 223 size_t upto = GetAuthenticateHeaderLength(); | |
| 224 | |
| 225 lm_info->offset = upto; | |
| 226 lm_info->length = RESPONSE_V1_LEN; | |
| 227 upto += lm_info->length; | |
| 228 | |
| 229 ntlm_info->offset = upto; | |
| 230 ntlm_info->length = GetNtlmResponseLength(); | |
| 231 upto += ntlm_info->length; | |
| 232 | |
| 233 domain_info->offset = upto; | |
| 234 domain_info->length = GetStringPayloadLength(domain, is_unicode); | |
| 235 upto += domain_info->length; | |
| 236 | |
| 237 username_info->offset = upto; | |
| 238 username_info->length = GetStringPayloadLength(username, is_unicode); | |
| 239 upto += username_info->length; | |
| 240 | |
| 241 hostname_info->offset = upto; | |
| 242 hostname_info->length = GetStringPayloadLength(hostname, is_unicode); | |
| 243 upto += hostname_info->length; | |
| 244 | |
| 245 // Calculate the full message size including payload. | |
| 246 *authenticate_message_len = CalculateAuthenticateMessageLength( | |
| 247 is_unicode, domain, username, hostname); | |
| 248 | |
| 249 // Double check the end of the payloads matches the end of the message. | |
| 250 DCHECK(*authenticate_message_len == upto); | |
|
asanka
2017/07/12 20:38:37
Why call CalculateAuthenticateMessageLength() inst
zentaro
2017/07/13 20:27:18
Done.
| |
| 251 } | |
| 252 | |
| 253 size_t NtlmClient::GetAuthenticateHeaderLength() const { | |
| 254 return AUTHENTICATE_HEADER_V1_LEN; | |
| 255 } | |
| 256 | |
| 257 size_t NtlmClient::GetNtlmResponseLength() const { | |
| 258 return RESPONSE_V1_LEN; | |
| 259 } | |
| 260 | |
| 261 } // namespace ntlm | |
| 262 } // namespace net | |
| OLD | NEW |