blob: 716a41e16e93e72c6e90c162be625ee221458b35 [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
21#include <algorithm>
22
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
25#include "rtc_base/cryptstring.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/httpcommon.h"
27#include "rtc_base/messagedigest.h"
28#include "rtc_base/socketaddress.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020029#include "rtc_base/strings/string_builder.h"
Artem Titova76af0c2018-07-23 17:38:12 +020030#include "rtc_base/third_party/base64/base64.h"
Joachim Bauch5b32f232018-03-07 20:02:26 +010031#include "rtc_base/zero_memory.h"
deadbeeff137e972017-03-23 15:45:49 -070032
33namespace rtc {
Tommie51a0a82018-02-27 15:30:29 +010034namespace {
deadbeeff137e972017-03-23 15:45:49 -070035#if defined(WEBRTC_WIN)
Tommie51a0a82018-02-27 15:30:29 +010036///////////////////////////////////////////////////////////////////////////////
37// ConstantToLabel can be used to easily generate string names from constant
38// values. This can be useful for logging descriptive names of error messages.
39// Usage:
40// const ConstantToLabel LIBRARY_ERRORS[] = {
41// KLABEL(SOME_ERROR),
42// KLABEL(SOME_OTHER_ERROR),
43// ...
44// LASTLABEL
45// }
46//
47// int err = LibraryFunc();
48// LOG(LS_ERROR) << "LibraryFunc returned: "
49// << GetErrorName(err, LIBRARY_ERRORS);
Yves Gerey665174f2018-06-19 15:03:05 +020050struct ConstantToLabel {
51 int value;
52 const char* label;
53};
Tommie51a0a82018-02-27 15:30:29 +010054
55const char* LookupLabel(int value, const ConstantToLabel entries[]) {
56 for (int i = 0; entries[i].label; ++i) {
57 if (value == entries[i].value) {
58 return entries[i].label;
59 }
60 }
61 return 0;
62}
63
64std::string GetErrorName(int err, const ConstantToLabel* err_table) {
65 if (err == 0)
66 return "No error";
67
68 if (err_table != 0) {
69 if (const char* value = LookupLabel(err, err_table))
70 return value;
71 }
72
73 char buffer[16];
74 snprintf(buffer, sizeof(buffer), "0x%08x", err);
75 return buffer;
76}
77
Yves Gerey665174f2018-06-19 15:03:05 +020078#define KLABEL(x) \
79 { x, #x }
80#define LASTLABEL \
81 { 0, 0 }
Tommie51a0a82018-02-27 15:30:29 +010082
83const ConstantToLabel SECURITY_ERRORS[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020084 KLABEL(SEC_I_COMPLETE_AND_CONTINUE),
85 KLABEL(SEC_I_COMPLETE_NEEDED),
86 KLABEL(SEC_I_CONTEXT_EXPIRED),
87 KLABEL(SEC_I_CONTINUE_NEEDED),
88 KLABEL(SEC_I_INCOMPLETE_CREDENTIALS),
89 KLABEL(SEC_I_RENEGOTIATE),
90 KLABEL(SEC_E_CERT_EXPIRED),
91 KLABEL(SEC_E_INCOMPLETE_MESSAGE),
92 KLABEL(SEC_E_INSUFFICIENT_MEMORY),
93 KLABEL(SEC_E_INTERNAL_ERROR),
94 KLABEL(SEC_E_INVALID_HANDLE),
95 KLABEL(SEC_E_INVALID_TOKEN),
96 KLABEL(SEC_E_LOGON_DENIED),
97 KLABEL(SEC_E_NO_AUTHENTICATING_AUTHORITY),
98 KLABEL(SEC_E_NO_CREDENTIALS),
99 KLABEL(SEC_E_NOT_OWNER),
100 KLABEL(SEC_E_OK),
101 KLABEL(SEC_E_SECPKG_NOT_FOUND),
102 KLABEL(SEC_E_TARGET_UNKNOWN),
103 KLABEL(SEC_E_UNKNOWN_CREDENTIALS),
104 KLABEL(SEC_E_UNSUPPORTED_FUNCTION),
105 KLABEL(SEC_E_UNTRUSTED_ROOT),
106 KLABEL(SEC_E_WRONG_PRINCIPAL),
107 LASTLABEL};
Tommie51a0a82018-02-27 15:30:29 +0100108#undef KLABEL
109#undef LASTLABEL
110#endif // defined(WEBRTC_WIN)
deadbeeff137e972017-03-23 15:45:49 -0700111
Niels Möller83da5522018-09-26 16:18:38 +0200112typedef std::pair<std::string, std::string> HttpAttribute;
113typedef std::vector<HttpAttribute> HttpAttributeList;
deadbeeff137e972017-03-23 15:45:49 -0700114
Yves Gerey665174f2018-06-19 15:03:05 +0200115inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) {
deadbeeff137e972017-03-23 15:45:49 -0700116 if (pos >= len)
117 return true;
118 if (isspace(static_cast<unsigned char>(data[pos])))
119 return true;
120 // The reason for this complexity is that some attributes may contain trailing
121 // equal signs (like base64 tokens in Negotiate auth headers)
Yves Gerey665174f2018-06-19 15:03:05 +0200122 if ((pos + 1 < len) && (data[pos] == '=') &&
123 !isspace(static_cast<unsigned char>(data[pos + 1])) &&
124 (data[pos + 1] != '=')) {
deadbeeff137e972017-03-23 15:45:49 -0700125 return true;
126 }
127 return false;
128}
129
Yves Gerey665174f2018-06-19 15:03:05 +0200130void HttpParseAttributes(const char* data,
131 size_t len,
deadbeeff137e972017-03-23 15:45:49 -0700132 HttpAttributeList& attributes) {
133 size_t pos = 0;
134 while (true) {
135 // Skip leading whitespace
136 while ((pos < len) && isspace(static_cast<unsigned char>(data[pos]))) {
137 ++pos;
138 }
139
140 // End of attributes?
141 if (pos >= len)
142 return;
143
144 // Find end of attribute name
145 size_t start = pos;
146 while (!IsEndOfAttributeName(pos, len, data)) {
147 ++pos;
148 }
149
150 HttpAttribute attribute;
151 attribute.first.assign(data + start, data + pos);
152
153 // Attribute has value?
154 if ((pos < len) && (data[pos] == '=')) {
Yves Gerey665174f2018-06-19 15:03:05 +0200155 ++pos; // Skip '='
deadbeeff137e972017-03-23 15:45:49 -0700156 // Check if quoted value
157 if ((pos < len) && (data[pos] == '"')) {
158 while (++pos < len) {
159 if (data[pos] == '"') {
160 ++pos;
161 break;
162 }
163 if ((data[pos] == '\\') && (pos + 1 < len))
164 ++pos;
165 attribute.second.append(1, data[pos]);
166 }
167 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200168 while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) &&
169 (data[pos] != ',')) {
deadbeeff137e972017-03-23 15:45:49 -0700170 attribute.second.append(1, data[pos++]);
171 }
172 }
173 }
174
175 attributes.push_back(attribute);
Yves Gerey665174f2018-06-19 15:03:05 +0200176 if ((pos < len) && (data[pos] == ','))
177 ++pos; // Skip ','
deadbeeff137e972017-03-23 15:45:49 -0700178 }
179}
180
181bool HttpHasAttribute(const HttpAttributeList& attributes,
182 const std::string& name,
183 std::string* value) {
184 for (HttpAttributeList::const_iterator it = attributes.begin();
185 it != attributes.end(); ++it) {
186 if (it->first == name) {
187 if (value) {
188 *value = it->second;
189 }
190 return true;
191 }
192 }
193 return false;
194}
195
196bool HttpHasNthAttribute(HttpAttributeList& attributes,
197 size_t index,
198 std::string* name,
199 std::string* value) {
200 if (index >= attributes.size())
201 return false;
202
203 if (name)
204 *name = attributes[index].first;
205 if (value)
206 *value = attributes[index].second;
207 return true;
208}
209
deadbeeff137e972017-03-23 15:45:49 -0700210std::string quote(const std::string& str) {
211 std::string result;
212 result.push_back('"');
Yves Gerey665174f2018-06-19 15:03:05 +0200213 for (size_t i = 0; i < str.size(); ++i) {
deadbeeff137e972017-03-23 15:45:49 -0700214 if ((str[i] == '"') || (str[i] == '\\'))
215 result.push_back('\\');
216 result.push_back(str[i]);
217 }
218 result.push_back('"');
219 return result;
220}
221
222#if defined(WEBRTC_WIN)
223struct NegotiateAuthContext : public HttpAuthContext {
224 CredHandle cred;
225 CtxtHandle ctx;
226 size_t steps;
227 bool specified_credentials;
228
229 NegotiateAuthContext(const std::string& auth, CredHandle c1, CtxtHandle c2)
Yves Gerey665174f2018-06-19 15:03:05 +0200230 : HttpAuthContext(auth),
231 cred(c1),
232 ctx(c2),
233 steps(0),
234 specified_credentials(false) {}
deadbeeff137e972017-03-23 15:45:49 -0700235
Steve Anton9de3aac2017-10-24 10:08:26 -0700236 ~NegotiateAuthContext() override {
deadbeeff137e972017-03-23 15:45:49 -0700237 DeleteSecurityContext(&ctx);
238 FreeCredentialsHandle(&cred);
239 }
240};
Yves Gerey665174f2018-06-19 15:03:05 +0200241#endif // WEBRTC_WIN
deadbeeff137e972017-03-23 15:45:49 -0700242
Niels Möller83da5522018-09-26 16:18:38 +0200243} // anonymous namespace
244
Yves Gerey665174f2018-06-19 15:03:05 +0200245HttpAuthResult HttpAuthenticate(const char* challenge,
246 size_t len,
247 const SocketAddress& server,
248 const std::string& method,
249 const std::string& uri,
250 const std::string& username,
251 const CryptString& password,
252 HttpAuthContext*& context,
253 std::string& response,
254 std::string& auth_method) {
deadbeeff137e972017-03-23 15:45:49 -0700255 HttpAttributeList args;
256 HttpParseAttributes(challenge, len, args);
257 HttpHasNthAttribute(args, 0, &auth_method, nullptr);
258
259 if (context && (context->auth_method != auth_method))
260 return HAR_IGNORE;
261
262 // BASIC
263 if (_stricmp(auth_method.c_str(), "basic") == 0) {
264 if (context)
Yves Gerey665174f2018-06-19 15:03:05 +0200265 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 15:45:49 -0700266 if (username.empty())
Yves Gerey665174f2018-06-19 15:03:05 +0200267 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 15:45:49 -0700268
269 context = new HttpAuthContext(auth_method);
270
Joachim Bauch5b32f232018-03-07 20:02:26 +0100271 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
272 // return response as CryptString so contents get securely deleted
273 // automatically.
274 // std::string decoded = username + ":" + password;
deadbeeff137e972017-03-23 15:45:49 -0700275 size_t len = username.size() + password.GetLength() + 2;
Yves Gerey665174f2018-06-19 15:03:05 +0200276 char* sensitive = new char[len];
deadbeeff137e972017-03-23 15:45:49 -0700277 size_t pos = strcpyn(sensitive, len, username.data(), username.size());
278 pos += strcpyn(sensitive + pos, len - pos, ":");
279 password.CopyTo(sensitive + pos, true);
280
281 response = auth_method;
282 response.append(" ");
283 // TODO: create a sensitive-source version of Base64::encode
284 response.append(Base64::Encode(sensitive));
Joachim Bauch5b32f232018-03-07 20:02:26 +0100285 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200286 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700287 return HAR_RESPONSE;
288 }
289
290 // DIGEST
291 if (_stricmp(auth_method.c_str(), "digest") == 0) {
292 if (context)
Yves Gerey665174f2018-06-19 15:03:05 +0200293 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 15:45:49 -0700294 if (username.empty())
Yves Gerey665174f2018-06-19 15:03:05 +0200295 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 15:45:49 -0700296
297 context = new HttpAuthContext(auth_method);
298
299 std::string cnonce, ncount;
300 char buffer[256];
301 sprintf(buffer, "%d", static_cast<int>(time(0)));
302 cnonce = MD5(buffer);
303 ncount = "00000001";
304
305 std::string realm, nonce, qop, opaque;
306 HttpHasAttribute(args, "realm", &realm);
307 HttpHasAttribute(args, "nonce", &nonce);
308 bool has_qop = HttpHasAttribute(args, "qop", &qop);
309 bool has_opaque = HttpHasAttribute(args, "opaque", &opaque);
310
Joachim Bauch5b32f232018-03-07 20:02:26 +0100311 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
312 // return response as CryptString so contents get securely deleted
313 // automatically.
314 // std::string A1 = username + ":" + realm + ":" + password;
deadbeeff137e972017-03-23 15:45:49 -0700315 size_t len = username.size() + realm.size() + password.GetLength() + 3;
Yves Gerey665174f2018-06-19 15:03:05 +0200316 char* sensitive = new char[len]; // A1
deadbeeff137e972017-03-23 15:45:49 -0700317 size_t pos = strcpyn(sensitive, len, username.data(), username.size());
318 pos += strcpyn(sensitive + pos, len - pos, ":");
319 pos += strcpyn(sensitive + pos, len - pos, realm.c_str());
320 pos += strcpyn(sensitive + pos, len - pos, ":");
321 password.CopyTo(sensitive + pos, true);
322
323 std::string A2 = method + ":" + uri;
324 std::string middle;
325 if (has_qop) {
326 qop = "auth";
327 middle = nonce + ":" + ncount + ":" + cnonce + ":" + qop;
328 } else {
329 middle = nonce;
330 }
331 std::string HA1 = MD5(sensitive);
Joachim Bauch5b32f232018-03-07 20:02:26 +0100332 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200333 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700334 std::string HA2 = MD5(A2);
335 std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2);
336
Jonas Olsson366a50c2018-09-06 13:41:30 +0200337 rtc::StringBuilder ss;
deadbeeff137e972017-03-23 15:45:49 -0700338 ss << auth_method;
339 ss << " username=" << quote(username);
340 ss << ", realm=" << quote(realm);
341 ss << ", nonce=" << quote(nonce);
342 ss << ", uri=" << quote(uri);
343 if (has_qop) {
344 ss << ", qop=" << qop;
Yves Gerey665174f2018-06-19 15:03:05 +0200345 ss << ", nc=" << ncount;
deadbeeff137e972017-03-23 15:45:49 -0700346 ss << ", cnonce=" << quote(cnonce);
347 }
348 ss << ", response=\"" << dig_response << "\"";
349 if (has_opaque) {
350 ss << ", opaque=" << quote(opaque);
351 }
352 response = ss.str();
353 return HAR_RESPONSE;
354 }
355
356#if defined(WEBRTC_WIN)
357#if 1
358 bool want_negotiate = (_stricmp(auth_method.c_str(), "negotiate") == 0);
359 bool want_ntlm = (_stricmp(auth_method.c_str(), "ntlm") == 0);
360 // SPNEGO & NTLM
361 if (want_negotiate || want_ntlm) {
362 const size_t MAX_MESSAGE = 12000, MAX_SPN = 256;
363 char out_buf[MAX_MESSAGE], spn[MAX_SPN];
364
Yves Gerey665174f2018-06-19 15:03:05 +0200365#if 0 // Requires funky windows versions
deadbeeff137e972017-03-23 15:45:49 -0700366 DWORD len = MAX_SPN;
367 if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr,
368 server.port(),
369 0, &len, spn) != ERROR_SUCCESS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100370 RTC_LOG_F(WARNING) << "(Negotiate) - DsMakeSpn failed";
deadbeeff137e972017-03-23 15:45:49 -0700371 return HAR_IGNORE;
372 }
373#else
374 sprintfn(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str());
375#endif
376
377 SecBuffer out_sec;
Yves Gerey665174f2018-06-19 15:03:05 +0200378 out_sec.pvBuffer = out_buf;
379 out_sec.cbBuffer = sizeof(out_buf);
deadbeeff137e972017-03-23 15:45:49 -0700380 out_sec.BufferType = SECBUFFER_TOKEN;
381
382 SecBufferDesc out_buf_desc;
383 out_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200384 out_buf_desc.cBuffers = 1;
385 out_buf_desc.pBuffers = &out_sec;
deadbeeff137e972017-03-23 15:45:49 -0700386
387 const ULONG NEG_FLAGS_DEFAULT =
Yves Gerey665174f2018-06-19 15:03:05 +0200388 // ISC_REQ_ALLOCATE_MEMORY
389 ISC_REQ_CONFIDENTIALITY
390 //| ISC_REQ_EXTENDED_ERROR
391 //| ISC_REQ_INTEGRITY
392 | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT
393 //| ISC_REQ_STREAM
394 //| ISC_REQ_USE_SUPPLIED_CREDS
395 ;
deadbeeff137e972017-03-23 15:45:49 -0700396
397 ::TimeStamp lifetime;
398 SECURITY_STATUS ret = S_OK;
399 ULONG ret_flags = 0, flags = NEG_FLAGS_DEFAULT;
400
401 bool specify_credentials = !username.empty();
402 size_t steps = 0;
403
404 // uint32_t now = Time();
405
Yves Gerey665174f2018-06-19 15:03:05 +0200406 NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context);
deadbeeff137e972017-03-23 15:45:49 -0700407 if (neg) {
408 const size_t max_steps = 10;
409 if (++neg->steps >= max_steps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100410 RTC_LOG(WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) "
411 "too many retries";
deadbeeff137e972017-03-23 15:45:49 -0700412 return HAR_ERROR;
413 }
414 steps = neg->steps;
415
416 std::string challenge, decoded_challenge;
417 if (HttpHasNthAttribute(args, 1, &challenge, nullptr) &&
418 Base64::Decode(challenge, Base64::DO_STRICT, &decoded_challenge,
419 nullptr)) {
420 SecBuffer in_sec;
Yves Gerey665174f2018-06-19 15:03:05 +0200421 in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data());
422 in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size());
deadbeeff137e972017-03-23 15:45:49 -0700423 in_sec.BufferType = SECBUFFER_TOKEN;
424
425 SecBufferDesc in_buf_desc;
426 in_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200427 in_buf_desc.cBuffers = 1;
428 in_buf_desc.pBuffers = &in_sec;
deadbeeff137e972017-03-23 15:45:49 -0700429
Yves Gerey665174f2018-06-19 15:03:05 +0200430 ret = InitializeSecurityContextA(
431 &neg->cred, &neg->ctx, spn, flags, 0, SECURITY_NATIVE_DREP,
432 &in_buf_desc, 0, &neg->ctx, &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700433 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100434 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 15:30:29 +0100435 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700436 return HAR_ERROR;
437 }
438 } else if (neg->specified_credentials) {
439 // Try again with default credentials
440 specify_credentials = false;
441 delete context;
442 context = neg = 0;
443 } else {
444 return HAR_CREDENTIALS;
445 }
446 }
447
448 if (!neg) {
449 unsigned char userbuf[256], passbuf[256], domainbuf[16];
Yves Gerey665174f2018-06-19 15:03:05 +0200450 SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0;
deadbeeff137e972017-03-23 15:45:49 -0700451 if (specify_credentials) {
452 memset(&auth_id, 0, sizeof(auth_id));
Yves Gerey665174f2018-06-19 15:03:05 +0200453 size_t len = password.GetLength() + 1;
454 char* sensitive = new char[len];
deadbeeff137e972017-03-23 15:45:49 -0700455 password.CopyTo(sensitive, true);
456 std::string::size_type pos = username.find('\\');
457 if (pos == std::string::npos) {
458 auth_id.UserLength = static_cast<unsigned long>(
459 std::min(sizeof(userbuf) - 1, username.size()));
460 memcpy(userbuf, username.c_str(), auth_id.UserLength);
461 userbuf[auth_id.UserLength] = 0;
462 auth_id.DomainLength = 0;
463 domainbuf[auth_id.DomainLength] = 0;
464 auth_id.PasswordLength = static_cast<unsigned long>(
465 std::min(sizeof(passbuf) - 1, password.GetLength()));
466 memcpy(passbuf, sensitive, auth_id.PasswordLength);
467 passbuf[auth_id.PasswordLength] = 0;
468 } else {
469 auth_id.UserLength = static_cast<unsigned long>(
470 std::min(sizeof(userbuf) - 1, username.size() - pos - 1));
471 memcpy(userbuf, username.c_str() + pos + 1, auth_id.UserLength);
472 userbuf[auth_id.UserLength] = 0;
473 auth_id.DomainLength =
474 static_cast<unsigned long>(std::min(sizeof(domainbuf) - 1, pos));
475 memcpy(domainbuf, username.c_str(), auth_id.DomainLength);
476 domainbuf[auth_id.DomainLength] = 0;
477 auth_id.PasswordLength = static_cast<unsigned long>(
478 std::min(sizeof(passbuf) - 1, password.GetLength()));
479 memcpy(passbuf, sensitive, auth_id.PasswordLength);
480 passbuf[auth_id.PasswordLength] = 0;
481 }
Joachim Bauch5b32f232018-03-07 20:02:26 +0100482 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 15:03:05 +0200483 delete[] sensitive;
deadbeeff137e972017-03-23 15:45:49 -0700484 auth_id.User = userbuf;
485 auth_id.Domain = domainbuf;
486 auth_id.Password = passbuf;
487 auth_id.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
488 pauth_id = &auth_id;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100489 RTC_LOG(LS_VERBOSE)
490 << "Negotiate protocol: Using specified credentials";
deadbeeff137e972017-03-23 15:45:49 -0700491 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100492 RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials";
deadbeeff137e972017-03-23 15:45:49 -0700493 }
494
495 CredHandle cred;
496 ret = AcquireCredentialsHandleA(
497 0, const_cast<char*>(want_negotiate ? NEGOSSP_NAME_A : NTLMSP_NAME_A),
498 SECPKG_CRED_OUTBOUND, 0, pauth_id, 0, 0, &cred, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700499 if (ret != SEC_E_OK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100500 RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: "
Tommie51a0a82018-02-27 15:30:29 +0100501 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700502 return HAR_IGNORE;
503 }
504
Yves Gerey665174f2018-06-19 15:03:05 +0200505 // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out;
deadbeeff137e972017-03-23 15:45:49 -0700506
507 CtxtHandle ctx;
Yves Gerey665174f2018-06-19 15:03:05 +0200508 ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0,
509 SECURITY_NATIVE_DREP, 0, 0, &ctx,
510 &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 15:45:49 -0700511 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100512 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 15:30:29 +0100513 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700514 FreeCredentialsHandle(&cred);
515 return HAR_IGNORE;
516 }
517
518 RTC_DCHECK(!context);
519 context = neg = new NegotiateAuthContext(auth_method, cred, ctx);
520 neg->specified_credentials = specify_credentials;
521 neg->steps = steps;
522 }
523
Yves Gerey665174f2018-06-19 15:03:05 +0200524 if ((ret == SEC_I_COMPLETE_NEEDED) ||
525 (ret == SEC_I_COMPLETE_AND_CONTINUE)) {
deadbeeff137e972017-03-23 15:45:49 -0700526 ret = CompleteAuthToken(&neg->ctx, &out_buf_desc);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100527 RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: "
Tommie51a0a82018-02-27 15:30:29 +0100528 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 15:45:49 -0700529 if (FAILED(ret)) {
530 return HAR_ERROR;
531 }
532 }
533
deadbeeff137e972017-03-23 15:45:49 -0700534 std::string decoded(out_buf, out_buf + out_sec.cbBuffer);
535 response = auth_method;
536 response.append(" ");
537 response.append(Base64::Encode(decoded));
538 return HAR_RESPONSE;
539 }
540#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200541#endif // WEBRTC_WIN
deadbeeff137e972017-03-23 15:45:49 -0700542
543 return HAR_IGNORE;
544}
545
546//////////////////////////////////////////////////////////////////////
547
Yves Gerey665174f2018-06-19 15:03:05 +0200548} // namespace rtc