blob: 4fa5f41a585cba6a2a36821affa27f370dd1081c [file] [log] [blame]
deadbeeff137e972017-03-23 15:45:49 -07001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <time.h>
12
13#if defined(WEBRTC_WIN)
deadbeeff137e972017-03-23 15:45:49 -070014#include <windows.h>
15#include <winsock2.h>
16#include <ws2tcpip.h>
17#define SECURITY_WIN32
18#include <security.h>
19#endif
20
Yves Gerey2e00abc2018-10-05 15:39:24 +020021#include <ctype.h> // for isspace
22#include <stdio.h> // for sprintf
deadbeeff137e972017-03-23 15:45:49 -070023#include <algorithm>
Yves Gerey2e00abc2018-10-05 15:39:24 +020024#include <utility> // for pair
25#include <vector>
deadbeeff137e972017-03-23 15:45:49 -070026
Niels Möller3c7d5992018-10-19 15:29:54 +020027#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/checks.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020030#include "rtc_base/cryptstring.h" // for CryptString
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/httpcommon.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020032#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/messagedigest.h"
34#include "rtc_base/socketaddress.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020035#include "rtc_base/strings/string_builder.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020036#include "rtc_base/third_party/base64/base64.h" // for Base64
37#include "rtc_base/zero_memory.h" // for ExplicitZeroMemory
deadbeeff137e972017-03-23 15:45:49 -070038
39namespace rtc {
Tommie51a0a82018-02-27 15:30:29 +010040namespace {
Robin Raymondce1b1402018-11-22 20:10:11 -050041#if defined(WEBRTC_WIN) && !defined(WINUWP)
Tommie51a0a82018-02-27 15:30:29 +010042///////////////////////////////////////////////////////////////////////////////
43// ConstantToLabel can be used to easily generate string names from constant
44// values. This can be useful for logging descriptive names of error messages.
45// Usage:
46// const ConstantToLabel LIBRARY_ERRORS[] = {
47// KLABEL(SOME_ERROR),
48// KLABEL(SOME_OTHER_ERROR),
49// ...
50// LASTLABEL
51// }
52//
53// int err = LibraryFunc();
54// LOG(LS_ERROR) << "LibraryFunc returned: "
55// << GetErrorName(err, LIBRARY_ERRORS);
Yves Gerey665174f2018-06-19 15:03:05 +020056struct ConstantToLabel {
57 int value;
58 const char* label;
59};
Tommie51a0a82018-02-27 15:30:29 +010060
61const char* LookupLabel(int value, const ConstantToLabel entries[]) {
62 for (int i = 0; entries[i].label; ++i) {
63 if (value == entries[i].value) {
64 return entries[i].label;
65 }
66 }
67 return 0;
68}
69
70std::string GetErrorName(int err, const ConstantToLabel* err_table) {
71 if (err == 0)
72 return "No error";
73
74 if (err_table != 0) {
75 if (const char* value = LookupLabel(err, err_table))
76 return value;
77 }
78
79 char buffer[16];
80 snprintf(buffer, sizeof(buffer), "0x%08x", err);
81 return buffer;
82}
83
Yves Gerey665174f2018-06-19 15:03:05 +020084#define KLABEL(x) \
85 { x, #x }
86#define LASTLABEL \
87 { 0, 0 }
Tommie51a0a82018-02-27 15:30:29 +010088
89const ConstantToLabel SECURITY_ERRORS[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020090 KLABEL(SEC_I_COMPLETE_AND_CONTINUE),
91 KLABEL(SEC_I_COMPLETE_NEEDED),
92 KLABEL(SEC_I_CONTEXT_EXPIRED),
93 KLABEL(SEC_I_CONTINUE_NEEDED),
94 KLABEL(SEC_I_INCOMPLETE_CREDENTIALS),
95 KLABEL(SEC_I_RENEGOTIATE),
96 KLABEL(SEC_E_CERT_EXPIRED),
97 KLABEL(SEC_E_INCOMPLETE_MESSAGE),
98 KLABEL(SEC_E_INSUFFICIENT_MEMORY),
99 KLABEL(SEC_E_INTERNAL_ERROR),
100 KLABEL(SEC_E_INVALID_HANDLE),
101 KLABEL(SEC_E_INVALID_TOKEN),
102 KLABEL(SEC_E_LOGON_DENIED),
103 KLABEL(SEC_E_NO_AUTHENTICATING_AUTHORITY),
104 KLABEL(SEC_E_NO_CREDENTIALS),
105 KLABEL(SEC_E_NOT_OWNER),
106 KLABEL(SEC_E_OK),
107 KLABEL(SEC_E_SECPKG_NOT_FOUND),
108 KLABEL(SEC_E_TARGET_UNKNOWN),
109 KLABEL(SEC_E_UNKNOWN_CREDENTIALS),
110 KLABEL(SEC_E_UNSUPPORTED_FUNCTION),
111 KLABEL(SEC_E_UNTRUSTED_ROOT),
112 KLABEL(SEC_E_WRONG_PRINCIPAL),
113 LASTLABEL};
Tommie51a0a82018-02-27 15:30:29 +0100114#undef KLABEL
115#undef LASTLABEL
Robin Raymondce1b1402018-11-22 20:10:11 -0500116#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700117
Niels Möller83da5522018-09-26 16:18:38 +0200118typedef std::pair<std::string, std::string> HttpAttribute;
119typedef std::vector<HttpAttribute> HttpAttributeList;
deadbeeff137e972017-03-23 15:45:49 -0700120
Yves Gerey665174f2018-06-19 15:03:05 +0200121inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) {
deadbeeff137e972017-03-23 15:45:49 -0700122 if (pos >= len)
123 return true;
124 if (isspace(static_cast<unsigned char>(data[pos])))
125 return true;
126 // The reason for this complexity is that some attributes may contain trailing
127 // equal signs (like base64 tokens in Negotiate auth headers)
Yves Gerey665174f2018-06-19 15:03:05 +0200128 if ((pos + 1 < len) && (data[pos] == '=') &&
129 !isspace(static_cast<unsigned char>(data[pos + 1])) &&
130 (data[pos + 1] != '=')) {
deadbeeff137e972017-03-23 15:45:49 -0700131 return true;
132 }
133 return false;
134}
135
Yves Gerey665174f2018-06-19 15:03:05 +0200136void HttpParseAttributes(const char* data,
137 size_t len,
deadbeeff137e972017-03-23 15:45:49 -0700138 HttpAttributeList& attributes) {
139 size_t pos = 0;
140 while (true) {
141 // Skip leading whitespace
142 while ((pos < len) && isspace(static_cast<unsigned char>(data[pos]))) {
143 ++pos;
144 }
145
146 // End of attributes?
147 if (pos >= len)
148 return;
149
150 // Find end of attribute name
151 size_t start = pos;
152 while (!IsEndOfAttributeName(pos, len, data)) {
153 ++pos;
154 }
155
156 HttpAttribute attribute;
157 attribute.first.assign(data + start, data + pos);
158
159 // Attribute has value?
160 if ((pos < len) && (data[pos] == '=')) {
Yves Gerey665174f2018-06-19 15:03:05 +0200161 ++pos; // Skip '='
deadbeeff137e972017-03-23 15:45:49 -0700162 // Check if quoted value
163 if ((pos < len) && (data[pos] == '"')) {
164 while (++pos < len) {
165 if (data[pos] == '"') {
166 ++pos;
167 break;
168 }
169 if ((data[pos] == '\\') && (pos + 1 < len))
170 ++pos;
171 attribute.second.append(1, data[pos]);
172 }
173 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200174 while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) &&
175 (data[pos] != ',')) {
deadbeeff137e972017-03-23 15:45:49 -0700176 attribute.second.append(1, data[pos++]);
177 }
178 }
179 }
180
181 attributes.push_back(attribute);
Yves Gerey665174f2018-06-19 15:03:05 +0200182 if ((pos < len) && (data[pos] == ','))
183 ++pos; // Skip ','
deadbeeff137e972017-03-23 15:45:49 -0700184 }
185}
186
187bool HttpHasAttribute(const HttpAttributeList& attributes,
188 const std::string& name,
189 std::string* value) {
190 for (HttpAttributeList::const_iterator it = attributes.begin();
191 it != attributes.end(); ++it) {
192 if (it->first == name) {
193 if (value) {
194 *value = it->second;
195 }
196 return true;
197 }
198 }
199 return false;
200}
201
202bool HttpHasNthAttribute(HttpAttributeList& attributes,
203 size_t index,
204 std::string* name,
205 std::string* value) {
206 if (index >= attributes.size())
207 return false;
208
209 if (name)
210 *name = attributes[index].first;
211 if (value)
212 *value = attributes[index].second;
213 return true;
214}
215
deadbeeff137e972017-03-23 15:45:49 -0700216std::string quote(const std::string& str) {
217 std::string result;
218 result.push_back('"');
Yves Gerey665174f2018-06-19 15:03:05 +0200219 for (size_t i = 0; i < str.size(); ++i) {
deadbeeff137e972017-03-23 15:45:49 -0700220 if ((str[i] == '"') || (str[i] == '\\'))
221 result.push_back('\\');
222 result.push_back(str[i]);
223 }
224 result.push_back('"');
225 return result;
226}
227
Robin Raymondce1b1402018-11-22 20:10:11 -0500228#if defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700229struct NegotiateAuthContext : public HttpAuthContext {
230 CredHandle cred;
231 CtxtHandle ctx;
232 size_t steps;
233 bool specified_credentials;
234
235 NegotiateAuthContext(const std::string& auth, CredHandle c1, CtxtHandle c2)
Yves Gerey665174f2018-06-19 15:03:05 +0200236 : HttpAuthContext(auth),
237 cred(c1),
238 ctx(c2),
239 steps(0),
240 specified_credentials(false) {}
deadbeeff137e972017-03-23 15:45:49 -0700241
Steve Anton9de3aac2017-10-24 10:08:26 -0700242 ~NegotiateAuthContext() override {
deadbeeff137e972017-03-23 15:45:49 -0700243 DeleteSecurityContext(&ctx);
244 FreeCredentialsHandle(&cred);
245 }
246};
Robin Raymondce1b1402018-11-22 20:10:11 -0500247#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700248
Niels Möller83da5522018-09-26 16:18:38 +0200249} // anonymous namespace
250
Yves Gerey665174f2018-06-19 15:03:05 +0200251HttpAuthResult HttpAuthenticate(const char* challenge,
252 size_t len,
253 const SocketAddress& server,
254 const std::string& method,
255 const std::string& uri,
256 const std::string& username,
257 const CryptString& password,
258 HttpAuthContext*& context,
259 std::string& response,
260 std::string& auth_method) {
deadbeeff137e972017-03-23 15:45:49 -0700261 HttpAttributeList args;
262 HttpParseAttributes(challenge, len, args);
263 HttpHasNthAttribute(args, 0, &auth_method, nullptr);
264
265 if (context && (context->auth_method != auth_method))
266 return HAR_IGNORE;
267
268 // BASIC
Niels Möller3c7d5992018-10-19 15:29:54 +0200269 if (absl::EqualsIgnoreCase(auth_method, "basic")) {
deadbeeff137e972017-03-23 15:45:49 -0700270 if (context)
Yves Gerey665174f2018-06-19 15:03:05 +0200271 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 15:45:49 -0700272 if (username.empty())
Yves Gerey665174f2018-06-19 15:03:05 +0200273 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 15:45:49 -0700274
275 context = new HttpAuthContext(auth_method);
276
Joachim Bauch5b32f232018-03-07 20:02:26 +0100277 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
278 // return response as CryptString so contents get securely deleted
279 // automatically.
280 // std::string decoded = username + ":" + password;
deadbeeff137e972017-03-23 15:45:49 -0700281 size_t len = username.size() + password.GetLength() + 2;
Yves Gerey665174f2018-06-19 15:03:05 +0200282 char* sensitive = new char[len];
deadbeeff137e972017-03-23 15:45:49 -0700283 size_t pos = strcpyn(sensitive, len, username.data(), username.size());
284 pos += strcpyn(sensitive + pos, len - pos, ":");
285 password.CopyTo(sensitive + pos, true);
286
287 response = auth_method;
288 response.append(" ");
289 // TODO: create a sensitive-source version of Base64::encode
290 response.append(Base64::Encode(sensitive));
Joachim Bauch5b32f232018-03-07 20:02:26 +0100291 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200292 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700293 return HAR_RESPONSE;
294 }
295
296 // DIGEST
Niels Möller3c7d5992018-10-19 15:29:54 +0200297 if (absl::EqualsIgnoreCase(auth_method, "digest")) {
deadbeeff137e972017-03-23 15:45:49 -0700298 if (context)
Yves Gerey665174f2018-06-19 15:03:05 +0200299 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 15:45:49 -0700300 if (username.empty())
Yves Gerey665174f2018-06-19 15:03:05 +0200301 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 15:45:49 -0700302
303 context = new HttpAuthContext(auth_method);
304
305 std::string cnonce, ncount;
306 char buffer[256];
307 sprintf(buffer, "%d", static_cast<int>(time(0)));
308 cnonce = MD5(buffer);
309 ncount = "00000001";
310
311 std::string realm, nonce, qop, opaque;
312 HttpHasAttribute(args, "realm", &realm);
313 HttpHasAttribute(args, "nonce", &nonce);
314 bool has_qop = HttpHasAttribute(args, "qop", &qop);
315 bool has_opaque = HttpHasAttribute(args, "opaque", &opaque);
316
Joachim Bauch5b32f232018-03-07 20:02:26 +0100317 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
318 // return response as CryptString so contents get securely deleted
319 // automatically.
320 // std::string A1 = username + ":" + realm + ":" + password;
deadbeeff137e972017-03-23 15:45:49 -0700321 size_t len = username.size() + realm.size() + password.GetLength() + 3;
Yves Gerey665174f2018-06-19 15:03:05 +0200322 char* sensitive = new char[len]; // A1
deadbeeff137e972017-03-23 15:45:49 -0700323 size_t pos = strcpyn(sensitive, len, username.data(), username.size());
324 pos += strcpyn(sensitive + pos, len - pos, ":");
325 pos += strcpyn(sensitive + pos, len - pos, realm.c_str());
326 pos += strcpyn(sensitive + pos, len - pos, ":");
327 password.CopyTo(sensitive + pos, true);
328
329 std::string A2 = method + ":" + uri;
330 std::string middle;
331 if (has_qop) {
332 qop = "auth";
333 middle = nonce + ":" + ncount + ":" + cnonce + ":" + qop;
334 } else {
335 middle = nonce;
336 }
337 std::string HA1 = MD5(sensitive);
Joachim Bauch5b32f232018-03-07 20:02:26 +0100338 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200339 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700340 std::string HA2 = MD5(A2);
341 std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2);
342
Jonas Olsson366a50c2018-09-06 13:41:30 +0200343 rtc::StringBuilder ss;
deadbeeff137e972017-03-23 15:45:49 -0700344 ss << auth_method;
345 ss << " username=" << quote(username);
346 ss << ", realm=" << quote(realm);
347 ss << ", nonce=" << quote(nonce);
348 ss << ", uri=" << quote(uri);
349 if (has_qop) {
350 ss << ", qop=" << qop;
Yves Gerey665174f2018-06-19 15:03:05 +0200351 ss << ", nc=" << ncount;
deadbeeff137e972017-03-23 15:45:49 -0700352 ss << ", cnonce=" << quote(cnonce);
353 }
354 ss << ", response=\"" << dig_response << "\"";
355 if (has_opaque) {
356 ss << ", opaque=" << quote(opaque);
357 }
358 response = ss.str();
359 return HAR_RESPONSE;
360 }
361
Robin Raymondce1b1402018-11-22 20:10:11 -0500362#if defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700363#if 1
Niels Möller3c7d5992018-10-19 15:29:54 +0200364 bool want_negotiate = absl::EqualsIgnoreCase(auth_method, "negotiate");
365 bool want_ntlm = absl::EqualsIgnoreCase(auth_method, "ntlm");
deadbeeff137e972017-03-23 15:45:49 -0700366 // SPNEGO & NTLM
367 if (want_negotiate || want_ntlm) {
368 const size_t MAX_MESSAGE = 12000, MAX_SPN = 256;
369 char out_buf[MAX_MESSAGE], spn[MAX_SPN];
370
Yves Gerey665174f2018-06-19 15:03:05 +0200371#if 0 // Requires funky windows versions
deadbeeff137e972017-03-23 15:45:49 -0700372 DWORD len = MAX_SPN;
373 if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr,
374 server.port(),
375 0, &len, spn) != ERROR_SUCCESS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100376 RTC_LOG_F(WARNING) << "(Negotiate) - DsMakeSpn failed";
deadbeeff137e972017-03-23 15:45:49 -0700377 return HAR_IGNORE;
378 }
379#else
Niels Mölleraba06332018-10-16 15:14:15 +0200380 snprintf(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str());
deadbeeff137e972017-03-23 15:45:49 -0700381#endif
382
383 SecBuffer out_sec;
Yves Gerey665174f2018-06-19 15:03:05 +0200384 out_sec.pvBuffer = out_buf;
385 out_sec.cbBuffer = sizeof(out_buf);
deadbeeff137e972017-03-23 15:45:49 -0700386 out_sec.BufferType = SECBUFFER_TOKEN;
387
388 SecBufferDesc out_buf_desc;
389 out_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200390 out_buf_desc.cBuffers = 1;
391 out_buf_desc.pBuffers = &out_sec;
deadbeeff137e972017-03-23 15:45:49 -0700392
393 const ULONG NEG_FLAGS_DEFAULT =
Yves Gerey665174f2018-06-19 15:03:05 +0200394 // ISC_REQ_ALLOCATE_MEMORY
395 ISC_REQ_CONFIDENTIALITY
396 //| ISC_REQ_EXTENDED_ERROR
397 //| ISC_REQ_INTEGRITY
398 | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT
399 //| ISC_REQ_STREAM
400 //| ISC_REQ_USE_SUPPLIED_CREDS
401 ;
deadbeeff137e972017-03-23 15:45:49 -0700402
403 ::TimeStamp lifetime;
404 SECURITY_STATUS ret = S_OK;
405 ULONG ret_flags = 0, flags = NEG_FLAGS_DEFAULT;
406
407 bool specify_credentials = !username.empty();
408 size_t steps = 0;
409
410 // uint32_t now = Time();
411
Yves Gerey665174f2018-06-19 15:03:05 +0200412 NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context);
deadbeeff137e972017-03-23 15:45:49 -0700413 if (neg) {
414 const size_t max_steps = 10;
415 if (++neg->steps >= max_steps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100416 RTC_LOG(WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) "
417 "too many retries";
deadbeeff137e972017-03-23 15:45:49 -0700418 return HAR_ERROR;
419 }
420 steps = neg->steps;
421
422 std::string challenge, decoded_challenge;
423 if (HttpHasNthAttribute(args, 1, &challenge, nullptr) &&
424 Base64::Decode(challenge, Base64::DO_STRICT, &decoded_challenge,
425 nullptr)) {
426 SecBuffer in_sec;
Yves Gerey665174f2018-06-19 15:03:05 +0200427 in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data());
428 in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size());
deadbeeff137e972017-03-23 15:45:49 -0700429 in_sec.BufferType = SECBUFFER_TOKEN;
430
431 SecBufferDesc in_buf_desc;
432 in_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200433 in_buf_desc.cBuffers = 1;
434 in_buf_desc.pBuffers = &in_sec;
deadbeeff137e972017-03-23 15:45:49 -0700435
Yves Gerey665174f2018-06-19 15:03:05 +0200436 ret = InitializeSecurityContextA(
437 &neg->cred, &neg->ctx, spn, flags, 0, SECURITY_NATIVE_DREP,
438 &in_buf_desc, 0, &neg->ctx, &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700439 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100440 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 15:30:29 +0100441 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700442 return HAR_ERROR;
443 }
444 } else if (neg->specified_credentials) {
445 // Try again with default credentials
446 specify_credentials = false;
447 delete context;
448 context = neg = 0;
449 } else {
450 return HAR_CREDENTIALS;
451 }
452 }
453
454 if (!neg) {
455 unsigned char userbuf[256], passbuf[256], domainbuf[16];
Yves Gerey665174f2018-06-19 15:03:05 +0200456 SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0;
deadbeeff137e972017-03-23 15:45:49 -0700457 if (specify_credentials) {
458 memset(&auth_id, 0, sizeof(auth_id));
Yves Gerey665174f2018-06-19 15:03:05 +0200459 size_t len = password.GetLength() + 1;
460 char* sensitive = new char[len];
deadbeeff137e972017-03-23 15:45:49 -0700461 password.CopyTo(sensitive, true);
462 std::string::size_type pos = username.find('\\');
463 if (pos == std::string::npos) {
464 auth_id.UserLength = static_cast<unsigned long>(
465 std::min(sizeof(userbuf) - 1, username.size()));
466 memcpy(userbuf, username.c_str(), auth_id.UserLength);
467 userbuf[auth_id.UserLength] = 0;
468 auth_id.DomainLength = 0;
469 domainbuf[auth_id.DomainLength] = 0;
470 auth_id.PasswordLength = static_cast<unsigned long>(
471 std::min(sizeof(passbuf) - 1, password.GetLength()));
472 memcpy(passbuf, sensitive, auth_id.PasswordLength);
473 passbuf[auth_id.PasswordLength] = 0;
474 } else {
475 auth_id.UserLength = static_cast<unsigned long>(
476 std::min(sizeof(userbuf) - 1, username.size() - pos - 1));
477 memcpy(userbuf, username.c_str() + pos + 1, auth_id.UserLength);
478 userbuf[auth_id.UserLength] = 0;
479 auth_id.DomainLength =
480 static_cast<unsigned long>(std::min(sizeof(domainbuf) - 1, pos));
481 memcpy(domainbuf, username.c_str(), auth_id.DomainLength);
482 domainbuf[auth_id.DomainLength] = 0;
483 auth_id.PasswordLength = static_cast<unsigned long>(
484 std::min(sizeof(passbuf) - 1, password.GetLength()));
485 memcpy(passbuf, sensitive, auth_id.PasswordLength);
486 passbuf[auth_id.PasswordLength] = 0;
487 }
Joachim Bauch5b32f232018-03-07 20:02:26 +0100488 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200489 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700490 auth_id.User = userbuf;
491 auth_id.Domain = domainbuf;
492 auth_id.Password = passbuf;
493 auth_id.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
494 pauth_id = &auth_id;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100495 RTC_LOG(LS_VERBOSE)
496 << "Negotiate protocol: Using specified credentials";
deadbeeff137e972017-03-23 15:45:49 -0700497 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100498 RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials";
deadbeeff137e972017-03-23 15:45:49 -0700499 }
500
501 CredHandle cred;
502 ret = AcquireCredentialsHandleA(
503 0, const_cast<char*>(want_negotiate ? NEGOSSP_NAME_A : NTLMSP_NAME_A),
504 SECPKG_CRED_OUTBOUND, 0, pauth_id, 0, 0, &cred, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700505 if (ret != SEC_E_OK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100506 RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: "
Tommie51a0a82018-02-27 15:30:29 +0100507 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700508 return HAR_IGNORE;
509 }
510
Yves Gerey665174f2018-06-19 15:03:05 +0200511 // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out;
deadbeeff137e972017-03-23 15:45:49 -0700512
513 CtxtHandle ctx;
Yves Gerey665174f2018-06-19 15:03:05 +0200514 ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0,
515 SECURITY_NATIVE_DREP, 0, 0, &ctx,
516 &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700517 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100518 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 15:30:29 +0100519 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700520 FreeCredentialsHandle(&cred);
521 return HAR_IGNORE;
522 }
523
524 RTC_DCHECK(!context);
525 context = neg = new NegotiateAuthContext(auth_method, cred, ctx);
526 neg->specified_credentials = specify_credentials;
527 neg->steps = steps;
528 }
529
Yves Gerey665174f2018-06-19 15:03:05 +0200530 if ((ret == SEC_I_COMPLETE_NEEDED) ||
531 (ret == SEC_I_COMPLETE_AND_CONTINUE)) {
deadbeeff137e972017-03-23 15:45:49 -0700532 ret = CompleteAuthToken(&neg->ctx, &out_buf_desc);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100533 RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: "
Tommie51a0a82018-02-27 15:30:29 +0100534 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700535 if (FAILED(ret)) {
536 return HAR_ERROR;
537 }
538 }
539
deadbeeff137e972017-03-23 15:45:49 -0700540 std::string decoded(out_buf, out_buf + out_sec.cbBuffer);
541 response = auth_method;
542 response.append(" ");
543 response.append(Base64::Encode(decoded));
544 return HAR_RESPONSE;
545 }
546#endif
Robin Raymondce1b1402018-11-22 20:10:11 -0500547#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700548
549 return HAR_IGNORE;
550}
551
552//////////////////////////////////////////////////////////////////////
553
Yves Gerey665174f2018-06-19 15:03:05 +0200554} // namespace rtc