blob: 621b854b531051e5ff089d5003e78f5a3541c7ed [file] [log] [blame]
deadbeeff137e972017-03-23 22:45:491/*
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
Ali Tofigh7fa90572022-03-17 14:47:4913#include "absl/strings/string_view.h"
14
deadbeeff137e972017-03-23 22:45:4915#if defined(WEBRTC_WIN)
deadbeeff137e972017-03-23 22:45:4916#include <windows.h>
17#include <winsock2.h>
18#include <ws2tcpip.h>
Yves Gerey3e707812018-11-28 15:47:4919
deadbeeff137e972017-03-23 22:45:4920#define SECURITY_WIN32
21#include <security.h>
22#endif
23
Yves Gerey2e00abc2018-10-05 13:39:2424#include <ctype.h> // for isspace
25#include <stdio.h> // for sprintf
Jonas Olssona4d87372019-07-05 17:08:3326
Yves Gerey2e00abc2018-10-05 13:39:2427#include <utility> // for pair
28#include <vector>
deadbeeff137e972017-03-23 22:45:4929
Niels Möller3c7d5992018-10-19 13:29:5430#include "absl/strings/match.h"
Steve Anton10542f22019-01-11 17:11:0031#include "rtc_base/crypt_string.h" // for CryptString
32#include "rtc_base/http_common.h"
Yves Gerey2e00abc2018-10-05 13:39:2433#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0034#include "rtc_base/message_digest.h"
35#include "rtc_base/socket_address.h"
Niels Möller198cf002019-05-10 10:29:1336#include "rtc_base/string_utils.h"
Jonas Olsson366a50c2018-09-06 11:41:3037#include "rtc_base/strings/string_builder.h"
Yves Gerey2e00abc2018-10-05 13:39:2438#include "rtc_base/third_party/base64/base64.h" // for Base64
39#include "rtc_base/zero_memory.h" // for ExplicitZeroMemory
deadbeeff137e972017-03-23 22:45:4940
41namespace rtc {
Tommie51a0a82018-02-27 14:30:2942namespace {
Robin Raymondce1b1402018-11-23 01:10:1143#if defined(WEBRTC_WIN) && !defined(WINUWP)
Tommie51a0a82018-02-27 14:30:2944///////////////////////////////////////////////////////////////////////////////
45// ConstantToLabel can be used to easily generate string names from constant
46// values. This can be useful for logging descriptive names of error messages.
47// Usage:
48// const ConstantToLabel LIBRARY_ERRORS[] = {
49// KLABEL(SOME_ERROR),
50// KLABEL(SOME_OTHER_ERROR),
51// ...
52// LASTLABEL
53// }
54//
55// int err = LibraryFunc();
56// LOG(LS_ERROR) << "LibraryFunc returned: "
57// << GetErrorName(err, LIBRARY_ERRORS);
Yves Gerey665174f2018-06-19 13:03:0558struct ConstantToLabel {
59 int value;
60 const char* label;
61};
Tommie51a0a82018-02-27 14:30:2962
63const char* LookupLabel(int value, const ConstantToLabel entries[]) {
64 for (int i = 0; entries[i].label; ++i) {
65 if (value == entries[i].value) {
66 return entries[i].label;
67 }
68 }
69 return 0;
70}
71
72std::string GetErrorName(int err, const ConstantToLabel* err_table) {
73 if (err == 0)
74 return "No error";
75
76 if (err_table != 0) {
77 if (const char* value = LookupLabel(err, err_table))
78 return value;
79 }
80
81 char buffer[16];
82 snprintf(buffer, sizeof(buffer), "0x%08x", err);
83 return buffer;
84}
85
Yves Gerey665174f2018-06-19 13:03:0586#define KLABEL(x) \
87 { x, #x }
88#define LASTLABEL \
89 { 0, 0 }
Tommie51a0a82018-02-27 14:30:2990
91const ConstantToLabel SECURITY_ERRORS[] = {
Yves Gerey665174f2018-06-19 13:03:0592 KLABEL(SEC_I_COMPLETE_AND_CONTINUE),
93 KLABEL(SEC_I_COMPLETE_NEEDED),
94 KLABEL(SEC_I_CONTEXT_EXPIRED),
95 KLABEL(SEC_I_CONTINUE_NEEDED),
96 KLABEL(SEC_I_INCOMPLETE_CREDENTIALS),
97 KLABEL(SEC_I_RENEGOTIATE),
98 KLABEL(SEC_E_CERT_EXPIRED),
99 KLABEL(SEC_E_INCOMPLETE_MESSAGE),
100 KLABEL(SEC_E_INSUFFICIENT_MEMORY),
101 KLABEL(SEC_E_INTERNAL_ERROR),
102 KLABEL(SEC_E_INVALID_HANDLE),
103 KLABEL(SEC_E_INVALID_TOKEN),
104 KLABEL(SEC_E_LOGON_DENIED),
105 KLABEL(SEC_E_NO_AUTHENTICATING_AUTHORITY),
106 KLABEL(SEC_E_NO_CREDENTIALS),
107 KLABEL(SEC_E_NOT_OWNER),
108 KLABEL(SEC_E_OK),
109 KLABEL(SEC_E_SECPKG_NOT_FOUND),
110 KLABEL(SEC_E_TARGET_UNKNOWN),
111 KLABEL(SEC_E_UNKNOWN_CREDENTIALS),
112 KLABEL(SEC_E_UNSUPPORTED_FUNCTION),
113 KLABEL(SEC_E_UNTRUSTED_ROOT),
114 KLABEL(SEC_E_WRONG_PRINCIPAL),
115 LASTLABEL};
Tommie51a0a82018-02-27 14:30:29116#undef KLABEL
117#undef LASTLABEL
Robin Raymondce1b1402018-11-23 01:10:11118#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 22:45:49119
Niels Möller83da5522018-09-26 14:18:38120typedef std::pair<std::string, std::string> HttpAttribute;
121typedef std::vector<HttpAttribute> HttpAttributeList;
deadbeeff137e972017-03-23 22:45:49122
Ali Tofigh2ab914c2022-04-13 10:55:15123inline bool IsEndOfAttributeName(size_t pos, absl::string_view data) {
124 if (pos >= data.size())
deadbeeff137e972017-03-23 22:45:49125 return true;
126 if (isspace(static_cast<unsigned char>(data[pos])))
127 return true;
128 // The reason for this complexity is that some attributes may contain trailing
129 // equal signs (like base64 tokens in Negotiate auth headers)
Ali Tofigh2ab914c2022-04-13 10:55:15130 if ((pos + 1 < data.size()) && (data[pos] == '=') &&
Yves Gerey665174f2018-06-19 13:03:05131 !isspace(static_cast<unsigned char>(data[pos + 1])) &&
132 (data[pos + 1] != '=')) {
deadbeeff137e972017-03-23 22:45:49133 return true;
134 }
135 return false;
136}
137
Ali Tofigh2ab914c2022-04-13 10:55:15138void HttpParseAttributes(absl::string_view data,
deadbeeff137e972017-03-23 22:45:49139 HttpAttributeList& attributes) {
140 size_t pos = 0;
Ali Tofigh2ab914c2022-04-13 10:55:15141 const size_t len = data.size();
deadbeeff137e972017-03-23 22:45:49142 while (true) {
143 // Skip leading whitespace
144 while ((pos < len) && isspace(static_cast<unsigned char>(data[pos]))) {
145 ++pos;
146 }
147
148 // End of attributes?
149 if (pos >= len)
150 return;
151
152 // Find end of attribute name
153 size_t start = pos;
Ali Tofigh2ab914c2022-04-13 10:55:15154 while (!IsEndOfAttributeName(pos, data)) {
deadbeeff137e972017-03-23 22:45:49155 ++pos;
156 }
157
158 HttpAttribute attribute;
Ali Tofigh2ab914c2022-04-13 10:55:15159 attribute.first.assign(data.data() + start, data.data() + pos);
deadbeeff137e972017-03-23 22:45:49160
161 // Attribute has value?
162 if ((pos < len) && (data[pos] == '=')) {
Yves Gerey665174f2018-06-19 13:03:05163 ++pos; // Skip '='
deadbeeff137e972017-03-23 22:45:49164 // Check if quoted value
165 if ((pos < len) && (data[pos] == '"')) {
166 while (++pos < len) {
167 if (data[pos] == '"') {
168 ++pos;
169 break;
170 }
171 if ((data[pos] == '\\') && (pos + 1 < len))
172 ++pos;
173 attribute.second.append(1, data[pos]);
174 }
175 } else {
Yves Gerey665174f2018-06-19 13:03:05176 while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) &&
177 (data[pos] != ',')) {
deadbeeff137e972017-03-23 22:45:49178 attribute.second.append(1, data[pos++]);
179 }
180 }
181 }
182
183 attributes.push_back(attribute);
Yves Gerey665174f2018-06-19 13:03:05184 if ((pos < len) && (data[pos] == ','))
185 ++pos; // Skip ','
deadbeeff137e972017-03-23 22:45:49186 }
187}
188
189bool HttpHasAttribute(const HttpAttributeList& attributes,
Ali Tofigh7fa90572022-03-17 14:47:49190 absl::string_view name,
deadbeeff137e972017-03-23 22:45:49191 std::string* value) {
192 for (HttpAttributeList::const_iterator it = attributes.begin();
193 it != attributes.end(); ++it) {
194 if (it->first == name) {
195 if (value) {
196 *value = it->second;
197 }
198 return true;
199 }
200 }
201 return false;
202}
203
204bool HttpHasNthAttribute(HttpAttributeList& attributes,
205 size_t index,
206 std::string* name,
207 std::string* value) {
208 if (index >= attributes.size())
209 return false;
210
211 if (name)
212 *name = attributes[index].first;
213 if (value)
214 *value = attributes[index].second;
215 return true;
216}
217
Ali Tofigh7fa90572022-03-17 14:47:49218std::string quote(absl::string_view str) {
deadbeeff137e972017-03-23 22:45:49219 std::string result;
220 result.push_back('"');
Yves Gerey665174f2018-06-19 13:03:05221 for (size_t i = 0; i < str.size(); ++i) {
deadbeeff137e972017-03-23 22:45:49222 if ((str[i] == '"') || (str[i] == '\\'))
223 result.push_back('\\');
224 result.push_back(str[i]);
225 }
226 result.push_back('"');
227 return result;
228}
229
Robin Raymondce1b1402018-11-23 01:10:11230#if defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 22:45:49231struct NegotiateAuthContext : public HttpAuthContext {
232 CredHandle cred;
233 CtxtHandle ctx;
234 size_t steps;
235 bool specified_credentials;
236
Ali Tofigh7fa90572022-03-17 14:47:49237 NegotiateAuthContext(absl::string_view auth, CredHandle c1, CtxtHandle c2)
Yves Gerey665174f2018-06-19 13:03:05238 : HttpAuthContext(auth),
239 cred(c1),
240 ctx(c2),
241 steps(0),
242 specified_credentials(false) {}
deadbeeff137e972017-03-23 22:45:49243
Steve Anton9de3aac2017-10-24 17:08:26244 ~NegotiateAuthContext() override {
deadbeeff137e972017-03-23 22:45:49245 DeleteSecurityContext(&ctx);
246 FreeCredentialsHandle(&cred);
247 }
248};
Robin Raymondce1b1402018-11-23 01:10:11249#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 22:45:49250
Niels Möller83da5522018-09-26 14:18:38251} // anonymous namespace
252
Ali Tofigh2ab914c2022-04-13 10:55:15253HttpAuthResult HttpAuthenticate(absl::string_view challenge,
Yves Gerey665174f2018-06-19 13:03:05254 const SocketAddress& server,
Ali Tofigh7fa90572022-03-17 14:47:49255 absl::string_view method,
256 absl::string_view uri,
257 absl::string_view username,
Yves Gerey665174f2018-06-19 13:03:05258 const CryptString& password,
259 HttpAuthContext*& context,
260 std::string& response,
261 std::string& auth_method) {
deadbeeff137e972017-03-23 22:45:49262 HttpAttributeList args;
Ali Tofigh2ab914c2022-04-13 10:55:15263 HttpParseAttributes(challenge, args);
deadbeeff137e972017-03-23 22:45:49264 HttpHasNthAttribute(args, 0, &auth_method, nullptr);
265
266 if (context && (context->auth_method != auth_method))
267 return HAR_IGNORE;
268
269 // BASIC
Niels Möller3c7d5992018-10-19 13:29:54270 if (absl::EqualsIgnoreCase(auth_method, "basic")) {
deadbeeff137e972017-03-23 22:45:49271 if (context)
Yves Gerey665174f2018-06-19 13:03:05272 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 22:45:49273 if (username.empty())
Yves Gerey665174f2018-06-19 13:03:05274 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 22:45:49275
276 context = new HttpAuthContext(auth_method);
277
Joachim Bauch5b32f232018-03-07 19:02:26278 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
279 // return response as CryptString so contents get securely deleted
280 // automatically.
281 // std::string decoded = username + ":" + password;
deadbeeff137e972017-03-23 22:45:49282 size_t len = username.size() + password.GetLength() + 2;
Yves Gerey665174f2018-06-19 13:03:05283 char* sensitive = new char[len];
Ali Tofighe5b22202022-03-24 23:08:32284 size_t pos = strcpyn(sensitive, len, username);
deadbeeff137e972017-03-23 22:45:49285 pos += strcpyn(sensitive + pos, len - pos, ":");
286 password.CopyTo(sensitive + pos, true);
287
288 response = auth_method;
289 response.append(" ");
290 // TODO: create a sensitive-source version of Base64::encode
291 response.append(Base64::Encode(sensitive));
Joachim Bauch5b32f232018-03-07 19:02:26292 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 13:03:05293 delete[] sensitive;
deadbeeff137e972017-03-23 22:45:49294 return HAR_RESPONSE;
295 }
296
297 // DIGEST
Niels Möller3c7d5992018-10-19 13:29:54298 if (absl::EqualsIgnoreCase(auth_method, "digest")) {
deadbeeff137e972017-03-23 22:45:49299 if (context)
Yves Gerey665174f2018-06-19 13:03:05300 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 22:45:49301 if (username.empty())
Yves Gerey665174f2018-06-19 13:03:05302 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 22:45:49303
304 context = new HttpAuthContext(auth_method);
305
306 std::string cnonce, ncount;
307 char buffer[256];
Justin Cohen45d66a52022-06-07 02:51:50308 snprintf(buffer, sizeof(buffer), "%d", static_cast<int>(time(0)));
deadbeeff137e972017-03-23 22:45:49309 cnonce = MD5(buffer);
310 ncount = "00000001";
311
312 std::string realm, nonce, qop, opaque;
313 HttpHasAttribute(args, "realm", &realm);
314 HttpHasAttribute(args, "nonce", &nonce);
315 bool has_qop = HttpHasAttribute(args, "qop", &qop);
316 bool has_opaque = HttpHasAttribute(args, "opaque", &opaque);
317
Joachim Bauch5b32f232018-03-07 19:02:26318 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
319 // return response as CryptString so contents get securely deleted
320 // automatically.
321 // std::string A1 = username + ":" + realm + ":" + password;
deadbeeff137e972017-03-23 22:45:49322 size_t len = username.size() + realm.size() + password.GetLength() + 3;
Yves Gerey665174f2018-06-19 13:03:05323 char* sensitive = new char[len]; // A1
Ali Tofighe5b22202022-03-24 23:08:32324 size_t pos = strcpyn(sensitive, len, username);
deadbeeff137e972017-03-23 22:45:49325 pos += strcpyn(sensitive + pos, len - pos, ":");
Ali Tofighe5b22202022-03-24 23:08:32326 pos += strcpyn(sensitive + pos, len - pos, realm);
deadbeeff137e972017-03-23 22:45:49327 pos += strcpyn(sensitive + pos, len - pos, ":");
328 password.CopyTo(sensitive + pos, true);
329
Ali Tofigh7fa90572022-03-17 14:47:49330 std::string A2 = std::string(method) + ":" + std::string(uri);
deadbeeff137e972017-03-23 22:45:49331 std::string middle;
332 if (has_qop) {
333 qop = "auth";
334 middle = nonce + ":" + ncount + ":" + cnonce + ":" + qop;
335 } else {
336 middle = nonce;
337 }
338 std::string HA1 = MD5(sensitive);
Joachim Bauch5b32f232018-03-07 19:02:26339 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 13:03:05340 delete[] sensitive;
deadbeeff137e972017-03-23 22:45:49341 std::string HA2 = MD5(A2);
342 std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2);
343
Jonas Olsson366a50c2018-09-06 11:41:30344 rtc::StringBuilder ss;
deadbeeff137e972017-03-23 22:45:49345 ss << auth_method;
346 ss << " username=" << quote(username);
347 ss << ", realm=" << quote(realm);
348 ss << ", nonce=" << quote(nonce);
349 ss << ", uri=" << quote(uri);
350 if (has_qop) {
351 ss << ", qop=" << qop;
Yves Gerey665174f2018-06-19 13:03:05352 ss << ", nc=" << ncount;
deadbeeff137e972017-03-23 22:45:49353 ss << ", cnonce=" << quote(cnonce);
354 }
355 ss << ", response=\"" << dig_response << "\"";
356 if (has_opaque) {
357 ss << ", opaque=" << quote(opaque);
358 }
359 response = ss.str();
360 return HAR_RESPONSE;
361 }
362
Robin Raymondce1b1402018-11-23 01:10:11363#if defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 22:45:49364#if 1
Niels Möller3c7d5992018-10-19 13:29:54365 bool want_negotiate = absl::EqualsIgnoreCase(auth_method, "negotiate");
366 bool want_ntlm = absl::EqualsIgnoreCase(auth_method, "ntlm");
deadbeeff137e972017-03-23 22:45:49367 // SPNEGO & NTLM
368 if (want_negotiate || want_ntlm) {
369 const size_t MAX_MESSAGE = 12000, MAX_SPN = 256;
370 char out_buf[MAX_MESSAGE], spn[MAX_SPN];
371
Yves Gerey665174f2018-06-19 13:03:05372#if 0 // Requires funky windows versions
deadbeeff137e972017-03-23 22:45:49373 DWORD len = MAX_SPN;
374 if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr,
375 server.port(),
376 0, &len, spn) != ERROR_SUCCESS) {
Harald Alvestrandef5b21e2021-11-27 21:31:08377 RTC_LOG_F(LS_WARNING) << "(Negotiate) - DsMakeSpn failed";
deadbeeff137e972017-03-23 22:45:49378 return HAR_IGNORE;
379 }
380#else
Niels Mölleraba06332018-10-16 13:14:15381 snprintf(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str());
deadbeeff137e972017-03-23 22:45:49382#endif
383
384 SecBuffer out_sec;
Yves Gerey665174f2018-06-19 13:03:05385 out_sec.pvBuffer = out_buf;
386 out_sec.cbBuffer = sizeof(out_buf);
deadbeeff137e972017-03-23 22:45:49387 out_sec.BufferType = SECBUFFER_TOKEN;
388
389 SecBufferDesc out_buf_desc;
390 out_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 13:03:05391 out_buf_desc.cBuffers = 1;
392 out_buf_desc.pBuffers = &out_sec;
deadbeeff137e972017-03-23 22:45:49393
394 const ULONG NEG_FLAGS_DEFAULT =
Yves Gerey665174f2018-06-19 13:03:05395 // ISC_REQ_ALLOCATE_MEMORY
396 ISC_REQ_CONFIDENTIALITY
397 //| ISC_REQ_EXTENDED_ERROR
398 //| ISC_REQ_INTEGRITY
399 | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT
400 //| ISC_REQ_STREAM
401 //| ISC_REQ_USE_SUPPLIED_CREDS
402 ;
deadbeeff137e972017-03-23 22:45:49403
404 ::TimeStamp lifetime;
405 SECURITY_STATUS ret = S_OK;
406 ULONG ret_flags = 0, flags = NEG_FLAGS_DEFAULT;
407
408 bool specify_credentials = !username.empty();
409 size_t steps = 0;
410
411 // uint32_t now = Time();
412
Yves Gerey665174f2018-06-19 13:03:05413 NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context);
deadbeeff137e972017-03-23 22:45:49414 if (neg) {
415 const size_t max_steps = 10;
416 if (++neg->steps >= max_steps) {
Harald Alvestrandef5b21e2021-11-27 21:31:08417 RTC_LOG(LS_WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) "
418 "too many retries";
deadbeeff137e972017-03-23 22:45:49419 return HAR_ERROR;
420 }
421 steps = neg->steps;
422
423 std::string challenge, decoded_challenge;
424 if (HttpHasNthAttribute(args, 1, &challenge, nullptr) &&
425 Base64::Decode(challenge, Base64::DO_STRICT, &decoded_challenge,
426 nullptr)) {
427 SecBuffer in_sec;
Yves Gerey665174f2018-06-19 13:03:05428 in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data());
429 in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size());
deadbeeff137e972017-03-23 22:45:49430 in_sec.BufferType = SECBUFFER_TOKEN;
431
432 SecBufferDesc in_buf_desc;
433 in_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 13:03:05434 in_buf_desc.cBuffers = 1;
435 in_buf_desc.pBuffers = &in_sec;
deadbeeff137e972017-03-23 22:45:49436
Yves Gerey665174f2018-06-19 13:03:05437 ret = InitializeSecurityContextA(
438 &neg->cred, &neg->ctx, spn, flags, 0, SECURITY_NATIVE_DREP,
439 &in_buf_desc, 0, &neg->ctx, &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 22:45:49440 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 10:09:25441 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 14:30:29442 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49443 return HAR_ERROR;
444 }
445 } else if (neg->specified_credentials) {
446 // Try again with default credentials
447 specify_credentials = false;
448 delete context;
449 context = neg = 0;
450 } else {
451 return HAR_CREDENTIALS;
452 }
453 }
454
455 if (!neg) {
456 unsigned char userbuf[256], passbuf[256], domainbuf[16];
Yves Gerey665174f2018-06-19 13:03:05457 SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0;
deadbeeff137e972017-03-23 22:45:49458 if (specify_credentials) {
459 memset(&auth_id, 0, sizeof(auth_id));
Yves Gerey665174f2018-06-19 13:03:05460 size_t len = password.GetLength() + 1;
461 char* sensitive = new char[len];
deadbeeff137e972017-03-23 22:45:49462 password.CopyTo(sensitive, true);
Ali Tofigh7fa90572022-03-17 14:47:49463 absl::string_view::size_type pos = username.find('\\');
464 if (pos == absl::string_view::npos) {
deadbeeff137e972017-03-23 22:45:49465 auth_id.UserLength = static_cast<unsigned long>(
466 std::min(sizeof(userbuf) - 1, username.size()));
Ali Tofigh7fa90572022-03-17 14:47:49467 memcpy(userbuf, username.data(), auth_id.UserLength);
deadbeeff137e972017-03-23 22:45:49468 userbuf[auth_id.UserLength] = 0;
469 auth_id.DomainLength = 0;
470 domainbuf[auth_id.DomainLength] = 0;
471 auth_id.PasswordLength = static_cast<unsigned long>(
472 std::min(sizeof(passbuf) - 1, password.GetLength()));
473 memcpy(passbuf, sensitive, auth_id.PasswordLength);
474 passbuf[auth_id.PasswordLength] = 0;
475 } else {
476 auth_id.UserLength = static_cast<unsigned long>(
477 std::min(sizeof(userbuf) - 1, username.size() - pos - 1));
Ali Tofigh7fa90572022-03-17 14:47:49478 memcpy(userbuf, username.data() + pos + 1, auth_id.UserLength);
deadbeeff137e972017-03-23 22:45:49479 userbuf[auth_id.UserLength] = 0;
480 auth_id.DomainLength =
481 static_cast<unsigned long>(std::min(sizeof(domainbuf) - 1, pos));
Ali Tofigh7fa90572022-03-17 14:47:49482 memcpy(domainbuf, username.data(), auth_id.DomainLength);
deadbeeff137e972017-03-23 22:45:49483 domainbuf[auth_id.DomainLength] = 0;
484 auth_id.PasswordLength = static_cast<unsigned long>(
485 std::min(sizeof(passbuf) - 1, password.GetLength()));
486 memcpy(passbuf, sensitive, auth_id.PasswordLength);
487 passbuf[auth_id.PasswordLength] = 0;
488 }
Joachim Bauch5b32f232018-03-07 19:02:26489 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 13:03:05490 delete[] sensitive;
deadbeeff137e972017-03-23 22:45:49491 auth_id.User = userbuf;
492 auth_id.Domain = domainbuf;
493 auth_id.Password = passbuf;
494 auth_id.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
495 pauth_id = &auth_id;
Mirko Bonadei675513b2017-11-09 10:09:25496 RTC_LOG(LS_VERBOSE)
497 << "Negotiate protocol: Using specified credentials";
deadbeeff137e972017-03-23 22:45:49498 } else {
Mirko Bonadei675513b2017-11-09 10:09:25499 RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials";
deadbeeff137e972017-03-23 22:45:49500 }
501
502 CredHandle cred;
503 ret = AcquireCredentialsHandleA(
504 0, const_cast<char*>(want_negotiate ? NEGOSSP_NAME_A : NTLMSP_NAME_A),
505 SECPKG_CRED_OUTBOUND, 0, pauth_id, 0, 0, &cred, &lifetime);
deadbeeff137e972017-03-23 22:45:49506 if (ret != SEC_E_OK) {
Mirko Bonadei675513b2017-11-09 10:09:25507 RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: "
Tommie51a0a82018-02-27 14:30:29508 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49509 return HAR_IGNORE;
510 }
511
Yves Gerey665174f2018-06-19 13:03:05512 // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out;
deadbeeff137e972017-03-23 22:45:49513
514 CtxtHandle ctx;
Yves Gerey665174f2018-06-19 13:03:05515 ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0,
516 SECURITY_NATIVE_DREP, 0, 0, &ctx,
517 &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 22:45:49518 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 10:09:25519 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 14:30:29520 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49521 FreeCredentialsHandle(&cred);
522 return HAR_IGNORE;
523 }
524
525 RTC_DCHECK(!context);
526 context = neg = new NegotiateAuthContext(auth_method, cred, ctx);
527 neg->specified_credentials = specify_credentials;
528 neg->steps = steps;
529 }
530
Yves Gerey665174f2018-06-19 13:03:05531 if ((ret == SEC_I_COMPLETE_NEEDED) ||
532 (ret == SEC_I_COMPLETE_AND_CONTINUE)) {
deadbeeff137e972017-03-23 22:45:49533 ret = CompleteAuthToken(&neg->ctx, &out_buf_desc);
Mirko Bonadei675513b2017-11-09 10:09:25534 RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: "
Tommie51a0a82018-02-27 14:30:29535 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49536 if (FAILED(ret)) {
537 return HAR_ERROR;
538 }
539 }
540
deadbeeff137e972017-03-23 22:45:49541 std::string decoded(out_buf, out_buf + out_sec.cbBuffer);
542 response = auth_method;
543 response.append(" ");
544 response.append(Base64::Encode(decoded));
545 return HAR_RESPONSE;
546 }
547#endif
Robin Raymondce1b1402018-11-23 01:10:11548#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 22:45:49549
550 return HAR_IGNORE;
551}
552
553//////////////////////////////////////////////////////////////////////
554
Yves Gerey665174f2018-06-19 13:03:05555} // namespace rtc