blob: 5ac222f468fc6f648f97fc97bacb535eabe8c856 [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
Ali Tofigh7fa90572022-03-17 15:47:49 +010013#include "absl/strings/string_view.h"
14
deadbeeff137e972017-03-23 15:45:49 -070015#if defined(WEBRTC_WIN)
deadbeeff137e972017-03-23 15:45:49 -070016#include <windows.h>
17#include <winsock2.h>
18#include <ws2tcpip.h>
Yves Gerey3e707812018-11-28 16:47:49 +010019
deadbeeff137e972017-03-23 15:45:49 -070020#define SECURITY_WIN32
21#include <security.h>
22#endif
23
Yves Gerey2e00abc2018-10-05 15:39:24 +020024#include <ctype.h> // for isspace
25#include <stdio.h> // for sprintf
Jonas Olssona4d87372019-07-05 19:08:33 +020026
Yves Gerey2e00abc2018-10-05 15:39:24 +020027#include <utility> // for pair
28#include <vector>
deadbeeff137e972017-03-23 15:45:49 -070029
Niels Möller3c7d5992018-10-19 15:29:54 +020030#include "absl/strings/match.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/crypt_string.h" // for CryptString
32#include "rtc_base/http_common.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020033#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/message_digest.h"
35#include "rtc_base/socket_address.h"
Niels Möller198cf002019-05-10 12:29:13 +020036#include "rtc_base/string_utils.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020037#include "rtc_base/strings/string_builder.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020038#include "rtc_base/third_party/base64/base64.h" // for Base64
39#include "rtc_base/zero_memory.h" // for ExplicitZeroMemory
deadbeeff137e972017-03-23 15:45:49 -070040
41namespace rtc {
Tommie51a0a82018-02-27 15:30:29 +010042namespace {
Robin Raymondce1b1402018-11-22 20:10:11 -050043#if defined(WEBRTC_WIN) && !defined(WINUWP)
Tommie51a0a82018-02-27 15:30:29 +010044///////////////////////////////////////////////////////////////////////////////
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 15:03:05 +020058struct ConstantToLabel {
59 int value;
60 const char* label;
61};
Tommie51a0a82018-02-27 15:30:29 +010062
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 15:03:05 +020086#define KLABEL(x) \
87 { x, #x }
88#define LASTLABEL \
89 { 0, 0 }
Tommie51a0a82018-02-27 15:30:29 +010090
91const ConstantToLabel SECURITY_ERRORS[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020092 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 15:30:29 +0100116#undef KLABEL
117#undef LASTLABEL
Robin Raymondce1b1402018-11-22 20:10:11 -0500118#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700119
Niels Möller83da5522018-09-26 16:18:38 +0200120typedef std::pair<std::string, std::string> HttpAttribute;
121typedef std::vector<HttpAttribute> HttpAttributeList;
deadbeeff137e972017-03-23 15:45:49 -0700122
Yves Gerey665174f2018-06-19 15:03:05 +0200123inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) {
deadbeeff137e972017-03-23 15:45:49 -0700124 if (pos >= len)
125 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)
Yves Gerey665174f2018-06-19 15:03:05 +0200130 if ((pos + 1 < len) && (data[pos] == '=') &&
131 !isspace(static_cast<unsigned char>(data[pos + 1])) &&
132 (data[pos + 1] != '=')) {
deadbeeff137e972017-03-23 15:45:49 -0700133 return true;
134 }
135 return false;
136}
137
Yves Gerey665174f2018-06-19 15:03:05 +0200138void HttpParseAttributes(const char* data,
139 size_t len,
deadbeeff137e972017-03-23 15:45:49 -0700140 HttpAttributeList& attributes) {
141 size_t pos = 0;
142 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;
154 while (!IsEndOfAttributeName(pos, len, data)) {
155 ++pos;
156 }
157
158 HttpAttribute attribute;
159 attribute.first.assign(data + start, data + pos);
160
161 // Attribute has value?
162 if ((pos < len) && (data[pos] == '=')) {
Yves Gerey665174f2018-06-19 15:03:05 +0200163 ++pos; // Skip '='
deadbeeff137e972017-03-23 15:45:49 -0700164 // 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 15:03:05 +0200176 while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) &&
177 (data[pos] != ',')) {
deadbeeff137e972017-03-23 15:45:49 -0700178 attribute.second.append(1, data[pos++]);
179 }
180 }
181 }
182
183 attributes.push_back(attribute);
Yves Gerey665174f2018-06-19 15:03:05 +0200184 if ((pos < len) && (data[pos] == ','))
185 ++pos; // Skip ','
deadbeeff137e972017-03-23 15:45:49 -0700186 }
187}
188
189bool HttpHasAttribute(const HttpAttributeList& attributes,
Ali Tofigh7fa90572022-03-17 15:47:49 +0100190 absl::string_view name,
deadbeeff137e972017-03-23 15:45:49 -0700191 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 15:47:49 +0100218std::string quote(absl::string_view str) {
deadbeeff137e972017-03-23 15:45:49 -0700219 std::string result;
220 result.push_back('"');
Yves Gerey665174f2018-06-19 15:03:05 +0200221 for (size_t i = 0; i < str.size(); ++i) {
deadbeeff137e972017-03-23 15:45:49 -0700222 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-22 20:10:11 -0500230#if defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700231struct NegotiateAuthContext : public HttpAuthContext {
232 CredHandle cred;
233 CtxtHandle ctx;
234 size_t steps;
235 bool specified_credentials;
236
Ali Tofigh7fa90572022-03-17 15:47:49 +0100237 NegotiateAuthContext(absl::string_view auth, CredHandle c1, CtxtHandle c2)
Yves Gerey665174f2018-06-19 15:03:05 +0200238 : HttpAuthContext(auth),
239 cred(c1),
240 ctx(c2),
241 steps(0),
242 specified_credentials(false) {}
deadbeeff137e972017-03-23 15:45:49 -0700243
Steve Anton9de3aac2017-10-24 10:08:26 -0700244 ~NegotiateAuthContext() override {
deadbeeff137e972017-03-23 15:45:49 -0700245 DeleteSecurityContext(&ctx);
246 FreeCredentialsHandle(&cred);
247 }
248};
Robin Raymondce1b1402018-11-22 20:10:11 -0500249#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700250
Niels Möller83da5522018-09-26 16:18:38 +0200251} // anonymous namespace
252
Yves Gerey665174f2018-06-19 15:03:05 +0200253HttpAuthResult HttpAuthenticate(const char* challenge,
254 size_t len,
255 const SocketAddress& server,
Ali Tofigh7fa90572022-03-17 15:47:49 +0100256 absl::string_view method,
257 absl::string_view uri,
258 absl::string_view username,
Yves Gerey665174f2018-06-19 15:03:05 +0200259 const CryptString& password,
260 HttpAuthContext*& context,
261 std::string& response,
262 std::string& auth_method) {
deadbeeff137e972017-03-23 15:45:49 -0700263 HttpAttributeList args;
264 HttpParseAttributes(challenge, len, args);
265 HttpHasNthAttribute(args, 0, &auth_method, nullptr);
266
267 if (context && (context->auth_method != auth_method))
268 return HAR_IGNORE;
269
270 // BASIC
Niels Möller3c7d5992018-10-19 15:29:54 +0200271 if (absl::EqualsIgnoreCase(auth_method, "basic")) {
deadbeeff137e972017-03-23 15:45:49 -0700272 if (context)
Yves Gerey665174f2018-06-19 15:03:05 +0200273 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 15:45:49 -0700274 if (username.empty())
Yves Gerey665174f2018-06-19 15:03:05 +0200275 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 15:45:49 -0700276
277 context = new HttpAuthContext(auth_method);
278
Joachim Bauch5b32f232018-03-07 20:02:26 +0100279 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
280 // return response as CryptString so contents get securely deleted
281 // automatically.
282 // std::string decoded = username + ":" + password;
deadbeeff137e972017-03-23 15:45:49 -0700283 size_t len = username.size() + password.GetLength() + 2;
Yves Gerey665174f2018-06-19 15:03:05 +0200284 char* sensitive = new char[len];
Ali Tofighe5b22202022-03-25 00:08:32 +0100285 size_t pos = strcpyn(sensitive, len, username);
deadbeeff137e972017-03-23 15:45:49 -0700286 pos += strcpyn(sensitive + pos, len - pos, ":");
287 password.CopyTo(sensitive + pos, true);
288
289 response = auth_method;
290 response.append(" ");
291 // TODO: create a sensitive-source version of Base64::encode
292 response.append(Base64::Encode(sensitive));
Joachim Bauch5b32f232018-03-07 20:02:26 +0100293 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200294 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700295 return HAR_RESPONSE;
296 }
297
298 // DIGEST
Niels Möller3c7d5992018-10-19 15:29:54 +0200299 if (absl::EqualsIgnoreCase(auth_method, "digest")) {
deadbeeff137e972017-03-23 15:45:49 -0700300 if (context)
Yves Gerey665174f2018-06-19 15:03:05 +0200301 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 15:45:49 -0700302 if (username.empty())
Yves Gerey665174f2018-06-19 15:03:05 +0200303 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 15:45:49 -0700304
305 context = new HttpAuthContext(auth_method);
306
307 std::string cnonce, ncount;
308 char buffer[256];
309 sprintf(buffer, "%d", static_cast<int>(time(0)));
310 cnonce = MD5(buffer);
311 ncount = "00000001";
312
313 std::string realm, nonce, qop, opaque;
314 HttpHasAttribute(args, "realm", &realm);
315 HttpHasAttribute(args, "nonce", &nonce);
316 bool has_qop = HttpHasAttribute(args, "qop", &qop);
317 bool has_opaque = HttpHasAttribute(args, "opaque", &opaque);
318
Joachim Bauch5b32f232018-03-07 20:02:26 +0100319 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
320 // return response as CryptString so contents get securely deleted
321 // automatically.
322 // std::string A1 = username + ":" + realm + ":" + password;
deadbeeff137e972017-03-23 15:45:49 -0700323 size_t len = username.size() + realm.size() + password.GetLength() + 3;
Yves Gerey665174f2018-06-19 15:03:05 +0200324 char* sensitive = new char[len]; // A1
Ali Tofighe5b22202022-03-25 00:08:32 +0100325 size_t pos = strcpyn(sensitive, len, username);
deadbeeff137e972017-03-23 15:45:49 -0700326 pos += strcpyn(sensitive + pos, len - pos, ":");
Ali Tofighe5b22202022-03-25 00:08:32 +0100327 pos += strcpyn(sensitive + pos, len - pos, realm);
deadbeeff137e972017-03-23 15:45:49 -0700328 pos += strcpyn(sensitive + pos, len - pos, ":");
329 password.CopyTo(sensitive + pos, true);
330
Ali Tofigh7fa90572022-03-17 15:47:49 +0100331 std::string A2 = std::string(method) + ":" + std::string(uri);
deadbeeff137e972017-03-23 15:45:49 -0700332 std::string middle;
333 if (has_qop) {
334 qop = "auth";
335 middle = nonce + ":" + ncount + ":" + cnonce + ":" + qop;
336 } else {
337 middle = nonce;
338 }
339 std::string HA1 = MD5(sensitive);
Joachim Bauch5b32f232018-03-07 20:02:26 +0100340 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200341 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700342 std::string HA2 = MD5(A2);
343 std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2);
344
Jonas Olsson366a50c2018-09-06 13:41:30 +0200345 rtc::StringBuilder ss;
deadbeeff137e972017-03-23 15:45:49 -0700346 ss << auth_method;
347 ss << " username=" << quote(username);
348 ss << ", realm=" << quote(realm);
349 ss << ", nonce=" << quote(nonce);
350 ss << ", uri=" << quote(uri);
351 if (has_qop) {
352 ss << ", qop=" << qop;
Yves Gerey665174f2018-06-19 15:03:05 +0200353 ss << ", nc=" << ncount;
deadbeeff137e972017-03-23 15:45:49 -0700354 ss << ", cnonce=" << quote(cnonce);
355 }
356 ss << ", response=\"" << dig_response << "\"";
357 if (has_opaque) {
358 ss << ", opaque=" << quote(opaque);
359 }
360 response = ss.str();
361 return HAR_RESPONSE;
362 }
363
Robin Raymondce1b1402018-11-22 20:10:11 -0500364#if defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700365#if 1
Niels Möller3c7d5992018-10-19 15:29:54 +0200366 bool want_negotiate = absl::EqualsIgnoreCase(auth_method, "negotiate");
367 bool want_ntlm = absl::EqualsIgnoreCase(auth_method, "ntlm");
deadbeeff137e972017-03-23 15:45:49 -0700368 // SPNEGO & NTLM
369 if (want_negotiate || want_ntlm) {
370 const size_t MAX_MESSAGE = 12000, MAX_SPN = 256;
371 char out_buf[MAX_MESSAGE], spn[MAX_SPN];
372
Yves Gerey665174f2018-06-19 15:03:05 +0200373#if 0 // Requires funky windows versions
deadbeeff137e972017-03-23 15:45:49 -0700374 DWORD len = MAX_SPN;
375 if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr,
376 server.port(),
377 0, &len, spn) != ERROR_SUCCESS) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000378 RTC_LOG_F(LS_WARNING) << "(Negotiate) - DsMakeSpn failed";
deadbeeff137e972017-03-23 15:45:49 -0700379 return HAR_IGNORE;
380 }
381#else
Niels Mölleraba06332018-10-16 15:14:15 +0200382 snprintf(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str());
deadbeeff137e972017-03-23 15:45:49 -0700383#endif
384
385 SecBuffer out_sec;
Yves Gerey665174f2018-06-19 15:03:05 +0200386 out_sec.pvBuffer = out_buf;
387 out_sec.cbBuffer = sizeof(out_buf);
deadbeeff137e972017-03-23 15:45:49 -0700388 out_sec.BufferType = SECBUFFER_TOKEN;
389
390 SecBufferDesc out_buf_desc;
391 out_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200392 out_buf_desc.cBuffers = 1;
393 out_buf_desc.pBuffers = &out_sec;
deadbeeff137e972017-03-23 15:45:49 -0700394
395 const ULONG NEG_FLAGS_DEFAULT =
Yves Gerey665174f2018-06-19 15:03:05 +0200396 // ISC_REQ_ALLOCATE_MEMORY
397 ISC_REQ_CONFIDENTIALITY
398 //| ISC_REQ_EXTENDED_ERROR
399 //| ISC_REQ_INTEGRITY
400 | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT
401 //| ISC_REQ_STREAM
402 //| ISC_REQ_USE_SUPPLIED_CREDS
403 ;
deadbeeff137e972017-03-23 15:45:49 -0700404
405 ::TimeStamp lifetime;
406 SECURITY_STATUS ret = S_OK;
407 ULONG ret_flags = 0, flags = NEG_FLAGS_DEFAULT;
408
409 bool specify_credentials = !username.empty();
410 size_t steps = 0;
411
412 // uint32_t now = Time();
413
Yves Gerey665174f2018-06-19 15:03:05 +0200414 NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context);
deadbeeff137e972017-03-23 15:45:49 -0700415 if (neg) {
416 const size_t max_steps = 10;
417 if (++neg->steps >= max_steps) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000418 RTC_LOG(LS_WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) "
419 "too many retries";
deadbeeff137e972017-03-23 15:45:49 -0700420 return HAR_ERROR;
421 }
422 steps = neg->steps;
423
424 std::string challenge, decoded_challenge;
425 if (HttpHasNthAttribute(args, 1, &challenge, nullptr) &&
426 Base64::Decode(challenge, Base64::DO_STRICT, &decoded_challenge,
427 nullptr)) {
428 SecBuffer in_sec;
Yves Gerey665174f2018-06-19 15:03:05 +0200429 in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data());
430 in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size());
deadbeeff137e972017-03-23 15:45:49 -0700431 in_sec.BufferType = SECBUFFER_TOKEN;
432
433 SecBufferDesc in_buf_desc;
434 in_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200435 in_buf_desc.cBuffers = 1;
436 in_buf_desc.pBuffers = &in_sec;
deadbeeff137e972017-03-23 15:45:49 -0700437
Yves Gerey665174f2018-06-19 15:03:05 +0200438 ret = InitializeSecurityContextA(
439 &neg->cred, &neg->ctx, spn, flags, 0, SECURITY_NATIVE_DREP,
440 &in_buf_desc, 0, &neg->ctx, &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700441 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100442 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 15:30:29 +0100443 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700444 return HAR_ERROR;
445 }
446 } else if (neg->specified_credentials) {
447 // Try again with default credentials
448 specify_credentials = false;
449 delete context;
450 context = neg = 0;
451 } else {
452 return HAR_CREDENTIALS;
453 }
454 }
455
456 if (!neg) {
457 unsigned char userbuf[256], passbuf[256], domainbuf[16];
Yves Gerey665174f2018-06-19 15:03:05 +0200458 SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0;
deadbeeff137e972017-03-23 15:45:49 -0700459 if (specify_credentials) {
460 memset(&auth_id, 0, sizeof(auth_id));
Yves Gerey665174f2018-06-19 15:03:05 +0200461 size_t len = password.GetLength() + 1;
462 char* sensitive = new char[len];
deadbeeff137e972017-03-23 15:45:49 -0700463 password.CopyTo(sensitive, true);
Ali Tofigh7fa90572022-03-17 15:47:49 +0100464 absl::string_view::size_type pos = username.find('\\');
465 if (pos == absl::string_view::npos) {
deadbeeff137e972017-03-23 15:45:49 -0700466 auth_id.UserLength = static_cast<unsigned long>(
467 std::min(sizeof(userbuf) - 1, username.size()));
Ali Tofigh7fa90572022-03-17 15:47:49 +0100468 memcpy(userbuf, username.data(), auth_id.UserLength);
deadbeeff137e972017-03-23 15:45:49 -0700469 userbuf[auth_id.UserLength] = 0;
470 auth_id.DomainLength = 0;
471 domainbuf[auth_id.DomainLength] = 0;
472 auth_id.PasswordLength = static_cast<unsigned long>(
473 std::min(sizeof(passbuf) - 1, password.GetLength()));
474 memcpy(passbuf, sensitive, auth_id.PasswordLength);
475 passbuf[auth_id.PasswordLength] = 0;
476 } else {
477 auth_id.UserLength = static_cast<unsigned long>(
478 std::min(sizeof(userbuf) - 1, username.size() - pos - 1));
Ali Tofigh7fa90572022-03-17 15:47:49 +0100479 memcpy(userbuf, username.data() + pos + 1, auth_id.UserLength);
deadbeeff137e972017-03-23 15:45:49 -0700480 userbuf[auth_id.UserLength] = 0;
481 auth_id.DomainLength =
482 static_cast<unsigned long>(std::min(sizeof(domainbuf) - 1, pos));
Ali Tofigh7fa90572022-03-17 15:47:49 +0100483 memcpy(domainbuf, username.data(), auth_id.DomainLength);
deadbeeff137e972017-03-23 15:45:49 -0700484 domainbuf[auth_id.DomainLength] = 0;
485 auth_id.PasswordLength = static_cast<unsigned long>(
486 std::min(sizeof(passbuf) - 1, password.GetLength()));
487 memcpy(passbuf, sensitive, auth_id.PasswordLength);
488 passbuf[auth_id.PasswordLength] = 0;
489 }
Joachim Bauch5b32f232018-03-07 20:02:26 +0100490 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200491 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700492 auth_id.User = userbuf;
493 auth_id.Domain = domainbuf;
494 auth_id.Password = passbuf;
495 auth_id.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
496 pauth_id = &auth_id;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100497 RTC_LOG(LS_VERBOSE)
498 << "Negotiate protocol: Using specified credentials";
deadbeeff137e972017-03-23 15:45:49 -0700499 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100500 RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials";
deadbeeff137e972017-03-23 15:45:49 -0700501 }
502
503 CredHandle cred;
504 ret = AcquireCredentialsHandleA(
505 0, const_cast<char*>(want_negotiate ? NEGOSSP_NAME_A : NTLMSP_NAME_A),
506 SECPKG_CRED_OUTBOUND, 0, pauth_id, 0, 0, &cred, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700507 if (ret != SEC_E_OK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100508 RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: "
Tommie51a0a82018-02-27 15:30:29 +0100509 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700510 return HAR_IGNORE;
511 }
512
Yves Gerey665174f2018-06-19 15:03:05 +0200513 // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out;
deadbeeff137e972017-03-23 15:45:49 -0700514
515 CtxtHandle ctx;
Yves Gerey665174f2018-06-19 15:03:05 +0200516 ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0,
517 SECURITY_NATIVE_DREP, 0, 0, &ctx,
518 &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700519 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100520 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 15:30:29 +0100521 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700522 FreeCredentialsHandle(&cred);
523 return HAR_IGNORE;
524 }
525
526 RTC_DCHECK(!context);
527 context = neg = new NegotiateAuthContext(auth_method, cred, ctx);
528 neg->specified_credentials = specify_credentials;
529 neg->steps = steps;
530 }
531
Yves Gerey665174f2018-06-19 15:03:05 +0200532 if ((ret == SEC_I_COMPLETE_NEEDED) ||
533 (ret == SEC_I_COMPLETE_AND_CONTINUE)) {
deadbeeff137e972017-03-23 15:45:49 -0700534 ret = CompleteAuthToken(&neg->ctx, &out_buf_desc);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100535 RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: "
Tommie51a0a82018-02-27 15:30:29 +0100536 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700537 if (FAILED(ret)) {
538 return HAR_ERROR;
539 }
540 }
541
deadbeeff137e972017-03-23 15:45:49 -0700542 std::string decoded(out_buf, out_buf + out_sec.cbBuffer);
543 response = auth_method;
544 response.append(" ");
545 response.append(Base64::Encode(decoded));
546 return HAR_RESPONSE;
547 }
548#endif
Robin Raymondce1b1402018-11-22 20:10:11 -0500549#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
deadbeeff137e972017-03-23 15:45:49 -0700550
551 return HAR_IGNORE;
552}
553
554//////////////////////////////////////////////////////////////////////
555
Yves Gerey665174f2018-06-19 15:03:05 +0200556} // namespace rtc