blob: baf843d07c0d53b462783a7d967bcf9dca3bfbd0 [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
13#if defined(WEBRTC_WIN)
deadbeeff137e972017-03-23 22:45:4914#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 13:39:2421#include <ctype.h> // for isspace
22#include <stdio.h> // for sprintf
deadbeeff137e972017-03-23 22:45:4923#include <algorithm>
Yves Gerey2e00abc2018-10-05 13:39:2424#include <utility> // for pair
25#include <vector>
deadbeeff137e972017-03-23 22:45:4926
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "rtc_base/checks.h"
Yves Gerey2e00abc2018-10-05 13:39:2429#include "rtc_base/cryptstring.h" // for CryptString
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "rtc_base/httpcommon.h"
Yves Gerey2e00abc2018-10-05 13:39:2431#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3132#include "rtc_base/messagedigest.h"
33#include "rtc_base/socketaddress.h"
Jonas Olsson366a50c2018-09-06 11:41:3034#include "rtc_base/strings/string_builder.h"
Yves Gerey2e00abc2018-10-05 13:39:2435#include "rtc_base/third_party/base64/base64.h" // for Base64
36#include "rtc_base/zero_memory.h" // for ExplicitZeroMemory
deadbeeff137e972017-03-23 22:45:4937
38namespace rtc {
Tommie51a0a82018-02-27 14:30:2939namespace {
deadbeeff137e972017-03-23 22:45:4940#if defined(WEBRTC_WIN)
Tommie51a0a82018-02-27 14:30:2941///////////////////////////////////////////////////////////////////////////////
42// ConstantToLabel can be used to easily generate string names from constant
43// values. This can be useful for logging descriptive names of error messages.
44// Usage:
45// const ConstantToLabel LIBRARY_ERRORS[] = {
46// KLABEL(SOME_ERROR),
47// KLABEL(SOME_OTHER_ERROR),
48// ...
49// LASTLABEL
50// }
51//
52// int err = LibraryFunc();
53// LOG(LS_ERROR) << "LibraryFunc returned: "
54// << GetErrorName(err, LIBRARY_ERRORS);
Yves Gerey665174f2018-06-19 13:03:0555struct ConstantToLabel {
56 int value;
57 const char* label;
58};
Tommie51a0a82018-02-27 14:30:2959
60const char* LookupLabel(int value, const ConstantToLabel entries[]) {
61 for (int i = 0; entries[i].label; ++i) {
62 if (value == entries[i].value) {
63 return entries[i].label;
64 }
65 }
66 return 0;
67}
68
69std::string GetErrorName(int err, const ConstantToLabel* err_table) {
70 if (err == 0)
71 return "No error";
72
73 if (err_table != 0) {
74 if (const char* value = LookupLabel(err, err_table))
75 return value;
76 }
77
78 char buffer[16];
79 snprintf(buffer, sizeof(buffer), "0x%08x", err);
80 return buffer;
81}
82
Yves Gerey665174f2018-06-19 13:03:0583#define KLABEL(x) \
84 { x, #x }
85#define LASTLABEL \
86 { 0, 0 }
Tommie51a0a82018-02-27 14:30:2987
88const ConstantToLabel SECURITY_ERRORS[] = {
Yves Gerey665174f2018-06-19 13:03:0589 KLABEL(SEC_I_COMPLETE_AND_CONTINUE),
90 KLABEL(SEC_I_COMPLETE_NEEDED),
91 KLABEL(SEC_I_CONTEXT_EXPIRED),
92 KLABEL(SEC_I_CONTINUE_NEEDED),
93 KLABEL(SEC_I_INCOMPLETE_CREDENTIALS),
94 KLABEL(SEC_I_RENEGOTIATE),
95 KLABEL(SEC_E_CERT_EXPIRED),
96 KLABEL(SEC_E_INCOMPLETE_MESSAGE),
97 KLABEL(SEC_E_INSUFFICIENT_MEMORY),
98 KLABEL(SEC_E_INTERNAL_ERROR),
99 KLABEL(SEC_E_INVALID_HANDLE),
100 KLABEL(SEC_E_INVALID_TOKEN),
101 KLABEL(SEC_E_LOGON_DENIED),
102 KLABEL(SEC_E_NO_AUTHENTICATING_AUTHORITY),
103 KLABEL(SEC_E_NO_CREDENTIALS),
104 KLABEL(SEC_E_NOT_OWNER),
105 KLABEL(SEC_E_OK),
106 KLABEL(SEC_E_SECPKG_NOT_FOUND),
107 KLABEL(SEC_E_TARGET_UNKNOWN),
108 KLABEL(SEC_E_UNKNOWN_CREDENTIALS),
109 KLABEL(SEC_E_UNSUPPORTED_FUNCTION),
110 KLABEL(SEC_E_UNTRUSTED_ROOT),
111 KLABEL(SEC_E_WRONG_PRINCIPAL),
112 LASTLABEL};
Tommie51a0a82018-02-27 14:30:29113#undef KLABEL
114#undef LASTLABEL
115#endif // defined(WEBRTC_WIN)
deadbeeff137e972017-03-23 22:45:49116
Niels Möller83da5522018-09-26 14:18:38117typedef std::pair<std::string, std::string> HttpAttribute;
118typedef std::vector<HttpAttribute> HttpAttributeList;
deadbeeff137e972017-03-23 22:45:49119
Yves Gerey665174f2018-06-19 13:03:05120inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) {
deadbeeff137e972017-03-23 22:45:49121 if (pos >= len)
122 return true;
123 if (isspace(static_cast<unsigned char>(data[pos])))
124 return true;
125 // The reason for this complexity is that some attributes may contain trailing
126 // equal signs (like base64 tokens in Negotiate auth headers)
Yves Gerey665174f2018-06-19 13:03:05127 if ((pos + 1 < len) && (data[pos] == '=') &&
128 !isspace(static_cast<unsigned char>(data[pos + 1])) &&
129 (data[pos + 1] != '=')) {
deadbeeff137e972017-03-23 22:45:49130 return true;
131 }
132 return false;
133}
134
Yves Gerey665174f2018-06-19 13:03:05135void HttpParseAttributes(const char* data,
136 size_t len,
deadbeeff137e972017-03-23 22:45:49137 HttpAttributeList& attributes) {
138 size_t pos = 0;
139 while (true) {
140 // Skip leading whitespace
141 while ((pos < len) && isspace(static_cast<unsigned char>(data[pos]))) {
142 ++pos;
143 }
144
145 // End of attributes?
146 if (pos >= len)
147 return;
148
149 // Find end of attribute name
150 size_t start = pos;
151 while (!IsEndOfAttributeName(pos, len, data)) {
152 ++pos;
153 }
154
155 HttpAttribute attribute;
156 attribute.first.assign(data + start, data + pos);
157
158 // Attribute has value?
159 if ((pos < len) && (data[pos] == '=')) {
Yves Gerey665174f2018-06-19 13:03:05160 ++pos; // Skip '='
deadbeeff137e972017-03-23 22:45:49161 // Check if quoted value
162 if ((pos < len) && (data[pos] == '"')) {
163 while (++pos < len) {
164 if (data[pos] == '"') {
165 ++pos;
166 break;
167 }
168 if ((data[pos] == '\\') && (pos + 1 < len))
169 ++pos;
170 attribute.second.append(1, data[pos]);
171 }
172 } else {
Yves Gerey665174f2018-06-19 13:03:05173 while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) &&
174 (data[pos] != ',')) {
deadbeeff137e972017-03-23 22:45:49175 attribute.second.append(1, data[pos++]);
176 }
177 }
178 }
179
180 attributes.push_back(attribute);
Yves Gerey665174f2018-06-19 13:03:05181 if ((pos < len) && (data[pos] == ','))
182 ++pos; // Skip ','
deadbeeff137e972017-03-23 22:45:49183 }
184}
185
186bool HttpHasAttribute(const HttpAttributeList& attributes,
187 const std::string& name,
188 std::string* value) {
189 for (HttpAttributeList::const_iterator it = attributes.begin();
190 it != attributes.end(); ++it) {
191 if (it->first == name) {
192 if (value) {
193 *value = it->second;
194 }
195 return true;
196 }
197 }
198 return false;
199}
200
201bool HttpHasNthAttribute(HttpAttributeList& attributes,
202 size_t index,
203 std::string* name,
204 std::string* value) {
205 if (index >= attributes.size())
206 return false;
207
208 if (name)
209 *name = attributes[index].first;
210 if (value)
211 *value = attributes[index].second;
212 return true;
213}
214
deadbeeff137e972017-03-23 22:45:49215std::string quote(const std::string& str) {
216 std::string result;
217 result.push_back('"');
Yves Gerey665174f2018-06-19 13:03:05218 for (size_t i = 0; i < str.size(); ++i) {
deadbeeff137e972017-03-23 22:45:49219 if ((str[i] == '"') || (str[i] == '\\'))
220 result.push_back('\\');
221 result.push_back(str[i]);
222 }
223 result.push_back('"');
224 return result;
225}
226
227#if defined(WEBRTC_WIN)
228struct NegotiateAuthContext : public HttpAuthContext {
229 CredHandle cred;
230 CtxtHandle ctx;
231 size_t steps;
232 bool specified_credentials;
233
234 NegotiateAuthContext(const std::string& auth, CredHandle c1, CtxtHandle c2)
Yves Gerey665174f2018-06-19 13:03:05235 : HttpAuthContext(auth),
236 cred(c1),
237 ctx(c2),
238 steps(0),
239 specified_credentials(false) {}
deadbeeff137e972017-03-23 22:45:49240
Steve Anton9de3aac2017-10-24 17:08:26241 ~NegotiateAuthContext() override {
deadbeeff137e972017-03-23 22:45:49242 DeleteSecurityContext(&ctx);
243 FreeCredentialsHandle(&cred);
244 }
245};
Yves Gerey665174f2018-06-19 13:03:05246#endif // WEBRTC_WIN
deadbeeff137e972017-03-23 22:45:49247
Niels Möller83da5522018-09-26 14:18:38248} // anonymous namespace
249
Yves Gerey665174f2018-06-19 13:03:05250HttpAuthResult HttpAuthenticate(const char* challenge,
251 size_t len,
252 const SocketAddress& server,
253 const std::string& method,
254 const std::string& uri,
255 const std::string& username,
256 const CryptString& password,
257 HttpAuthContext*& context,
258 std::string& response,
259 std::string& auth_method) {
deadbeeff137e972017-03-23 22:45:49260 HttpAttributeList args;
261 HttpParseAttributes(challenge, len, args);
262 HttpHasNthAttribute(args, 0, &auth_method, nullptr);
263
264 if (context && (context->auth_method != auth_method))
265 return HAR_IGNORE;
266
267 // BASIC
268 if (_stricmp(auth_method.c_str(), "basic") == 0) {
269 if (context)
Yves Gerey665174f2018-06-19 13:03:05270 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 22:45:49271 if (username.empty())
Yves Gerey665174f2018-06-19 13:03:05272 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 22:45:49273
274 context = new HttpAuthContext(auth_method);
275
Joachim Bauch5b32f232018-03-07 19:02:26276 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
277 // return response as CryptString so contents get securely deleted
278 // automatically.
279 // std::string decoded = username + ":" + password;
deadbeeff137e972017-03-23 22:45:49280 size_t len = username.size() + password.GetLength() + 2;
Yves Gerey665174f2018-06-19 13:03:05281 char* sensitive = new char[len];
deadbeeff137e972017-03-23 22:45:49282 size_t pos = strcpyn(sensitive, len, username.data(), username.size());
283 pos += strcpyn(sensitive + pos, len - pos, ":");
284 password.CopyTo(sensitive + pos, true);
285
286 response = auth_method;
287 response.append(" ");
288 // TODO: create a sensitive-source version of Base64::encode
289 response.append(Base64::Encode(sensitive));
Joachim Bauch5b32f232018-03-07 19:02:26290 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 13:03:05291 delete[] sensitive;
deadbeeff137e972017-03-23 22:45:49292 return HAR_RESPONSE;
293 }
294
295 // DIGEST
296 if (_stricmp(auth_method.c_str(), "digest") == 0) {
297 if (context)
Yves Gerey665174f2018-06-19 13:03:05298 return HAR_CREDENTIALS; // Bad credentials
deadbeeff137e972017-03-23 22:45:49299 if (username.empty())
Yves Gerey665174f2018-06-19 13:03:05300 return HAR_CREDENTIALS; // Missing credentials
deadbeeff137e972017-03-23 22:45:49301
302 context = new HttpAuthContext(auth_method);
303
304 std::string cnonce, ncount;
305 char buffer[256];
306 sprintf(buffer, "%d", static_cast<int>(time(0)));
307 cnonce = MD5(buffer);
308 ncount = "00000001";
309
310 std::string realm, nonce, qop, opaque;
311 HttpHasAttribute(args, "realm", &realm);
312 HttpHasAttribute(args, "nonce", &nonce);
313 bool has_qop = HttpHasAttribute(args, "qop", &qop);
314 bool has_opaque = HttpHasAttribute(args, "opaque", &opaque);
315
Joachim Bauch5b32f232018-03-07 19:02:26316 // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also
317 // return response as CryptString so contents get securely deleted
318 // automatically.
319 // std::string A1 = username + ":" + realm + ":" + password;
deadbeeff137e972017-03-23 22:45:49320 size_t len = username.size() + realm.size() + password.GetLength() + 3;
Yves Gerey665174f2018-06-19 13:03:05321 char* sensitive = new char[len]; // A1
deadbeeff137e972017-03-23 22:45:49322 size_t pos = strcpyn(sensitive, len, username.data(), username.size());
323 pos += strcpyn(sensitive + pos, len - pos, ":");
324 pos += strcpyn(sensitive + pos, len - pos, realm.c_str());
325 pos += strcpyn(sensitive + pos, len - pos, ":");
326 password.CopyTo(sensitive + pos, true);
327
328 std::string A2 = method + ":" + uri;
329 std::string middle;
330 if (has_qop) {
331 qop = "auth";
332 middle = nonce + ":" + ncount + ":" + cnonce + ":" + qop;
333 } else {
334 middle = nonce;
335 }
336 std::string HA1 = MD5(sensitive);
Joachim Bauch5b32f232018-03-07 19:02:26337 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 13:03:05338 delete[] sensitive;
deadbeeff137e972017-03-23 22:45:49339 std::string HA2 = MD5(A2);
340 std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2);
341
Jonas Olsson366a50c2018-09-06 11:41:30342 rtc::StringBuilder ss;
deadbeeff137e972017-03-23 22:45:49343 ss << auth_method;
344 ss << " username=" << quote(username);
345 ss << ", realm=" << quote(realm);
346 ss << ", nonce=" << quote(nonce);
347 ss << ", uri=" << quote(uri);
348 if (has_qop) {
349 ss << ", qop=" << qop;
Yves Gerey665174f2018-06-19 13:03:05350 ss << ", nc=" << ncount;
deadbeeff137e972017-03-23 22:45:49351 ss << ", cnonce=" << quote(cnonce);
352 }
353 ss << ", response=\"" << dig_response << "\"";
354 if (has_opaque) {
355 ss << ", opaque=" << quote(opaque);
356 }
357 response = ss.str();
358 return HAR_RESPONSE;
359 }
360
361#if defined(WEBRTC_WIN)
362#if 1
363 bool want_negotiate = (_stricmp(auth_method.c_str(), "negotiate") == 0);
364 bool want_ntlm = (_stricmp(auth_method.c_str(), "ntlm") == 0);
365 // SPNEGO & NTLM
366 if (want_negotiate || want_ntlm) {
367 const size_t MAX_MESSAGE = 12000, MAX_SPN = 256;
368 char out_buf[MAX_MESSAGE], spn[MAX_SPN];
369
Yves Gerey665174f2018-06-19 13:03:05370#if 0 // Requires funky windows versions
deadbeeff137e972017-03-23 22:45:49371 DWORD len = MAX_SPN;
372 if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr,
373 server.port(),
374 0, &len, spn) != ERROR_SUCCESS) {
Mirko Bonadei675513b2017-11-09 10:09:25375 RTC_LOG_F(WARNING) << "(Negotiate) - DsMakeSpn failed";
deadbeeff137e972017-03-23 22:45:49376 return HAR_IGNORE;
377 }
378#else
Niels Mölleraba06332018-10-16 13:14:15379 snprintf(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str());
deadbeeff137e972017-03-23 22:45:49380#endif
381
382 SecBuffer out_sec;
Yves Gerey665174f2018-06-19 13:03:05383 out_sec.pvBuffer = out_buf;
384 out_sec.cbBuffer = sizeof(out_buf);
deadbeeff137e972017-03-23 22:45:49385 out_sec.BufferType = SECBUFFER_TOKEN;
386
387 SecBufferDesc out_buf_desc;
388 out_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 13:03:05389 out_buf_desc.cBuffers = 1;
390 out_buf_desc.pBuffers = &out_sec;
deadbeeff137e972017-03-23 22:45:49391
392 const ULONG NEG_FLAGS_DEFAULT =
Yves Gerey665174f2018-06-19 13:03:05393 // ISC_REQ_ALLOCATE_MEMORY
394 ISC_REQ_CONFIDENTIALITY
395 //| ISC_REQ_EXTENDED_ERROR
396 //| ISC_REQ_INTEGRITY
397 | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT
398 //| ISC_REQ_STREAM
399 //| ISC_REQ_USE_SUPPLIED_CREDS
400 ;
deadbeeff137e972017-03-23 22:45:49401
402 ::TimeStamp lifetime;
403 SECURITY_STATUS ret = S_OK;
404 ULONG ret_flags = 0, flags = NEG_FLAGS_DEFAULT;
405
406 bool specify_credentials = !username.empty();
407 size_t steps = 0;
408
409 // uint32_t now = Time();
410
Yves Gerey665174f2018-06-19 13:03:05411 NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context);
deadbeeff137e972017-03-23 22:45:49412 if (neg) {
413 const size_t max_steps = 10;
414 if (++neg->steps >= max_steps) {
Mirko Bonadei675513b2017-11-09 10:09:25415 RTC_LOG(WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) "
416 "too many retries";
deadbeeff137e972017-03-23 22:45:49417 return HAR_ERROR;
418 }
419 steps = neg->steps;
420
421 std::string challenge, decoded_challenge;
422 if (HttpHasNthAttribute(args, 1, &challenge, nullptr) &&
423 Base64::Decode(challenge, Base64::DO_STRICT, &decoded_challenge,
424 nullptr)) {
425 SecBuffer in_sec;
Yves Gerey665174f2018-06-19 13:03:05426 in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data());
427 in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size());
deadbeeff137e972017-03-23 22:45:49428 in_sec.BufferType = SECBUFFER_TOKEN;
429
430 SecBufferDesc in_buf_desc;
431 in_buf_desc.ulVersion = 0;
Yves Gerey665174f2018-06-19 13:03:05432 in_buf_desc.cBuffers = 1;
433 in_buf_desc.pBuffers = &in_sec;
deadbeeff137e972017-03-23 22:45:49434
Yves Gerey665174f2018-06-19 13:03:05435 ret = InitializeSecurityContextA(
436 &neg->cred, &neg->ctx, spn, flags, 0, SECURITY_NATIVE_DREP,
437 &in_buf_desc, 0, &neg->ctx, &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 22:45:49438 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 10:09:25439 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 14:30:29440 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49441 return HAR_ERROR;
442 }
443 } else if (neg->specified_credentials) {
444 // Try again with default credentials
445 specify_credentials = false;
446 delete context;
447 context = neg = 0;
448 } else {
449 return HAR_CREDENTIALS;
450 }
451 }
452
453 if (!neg) {
454 unsigned char userbuf[256], passbuf[256], domainbuf[16];
Yves Gerey665174f2018-06-19 13:03:05455 SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0;
deadbeeff137e972017-03-23 22:45:49456 if (specify_credentials) {
457 memset(&auth_id, 0, sizeof(auth_id));
Yves Gerey665174f2018-06-19 13:03:05458 size_t len = password.GetLength() + 1;
459 char* sensitive = new char[len];
deadbeeff137e972017-03-23 22:45:49460 password.CopyTo(sensitive, true);
461 std::string::size_type pos = username.find('\\');
462 if (pos == std::string::npos) {
463 auth_id.UserLength = static_cast<unsigned long>(
464 std::min(sizeof(userbuf) - 1, username.size()));
465 memcpy(userbuf, username.c_str(), auth_id.UserLength);
466 userbuf[auth_id.UserLength] = 0;
467 auth_id.DomainLength = 0;
468 domainbuf[auth_id.DomainLength] = 0;
469 auth_id.PasswordLength = static_cast<unsigned long>(
470 std::min(sizeof(passbuf) - 1, password.GetLength()));
471 memcpy(passbuf, sensitive, auth_id.PasswordLength);
472 passbuf[auth_id.PasswordLength] = 0;
473 } else {
474 auth_id.UserLength = static_cast<unsigned long>(
475 std::min(sizeof(userbuf) - 1, username.size() - pos - 1));
476 memcpy(userbuf, username.c_str() + pos + 1, auth_id.UserLength);
477 userbuf[auth_id.UserLength] = 0;
478 auth_id.DomainLength =
479 static_cast<unsigned long>(std::min(sizeof(domainbuf) - 1, pos));
480 memcpy(domainbuf, username.c_str(), auth_id.DomainLength);
481 domainbuf[auth_id.DomainLength] = 0;
482 auth_id.PasswordLength = static_cast<unsigned long>(
483 std::min(sizeof(passbuf) - 1, password.GetLength()));
484 memcpy(passbuf, sensitive, auth_id.PasswordLength);
485 passbuf[auth_id.PasswordLength] = 0;
486 }
Joachim Bauch5b32f232018-03-07 19:02:26487 ExplicitZeroMemory(sensitive, len);
Yves Gerey665174f2018-06-19 13:03:05488 delete[] sensitive;
deadbeeff137e972017-03-23 22:45:49489 auth_id.User = userbuf;
490 auth_id.Domain = domainbuf;
491 auth_id.Password = passbuf;
492 auth_id.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
493 pauth_id = &auth_id;
Mirko Bonadei675513b2017-11-09 10:09:25494 RTC_LOG(LS_VERBOSE)
495 << "Negotiate protocol: Using specified credentials";
deadbeeff137e972017-03-23 22:45:49496 } else {
Mirko Bonadei675513b2017-11-09 10:09:25497 RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials";
deadbeeff137e972017-03-23 22:45:49498 }
499
500 CredHandle cred;
501 ret = AcquireCredentialsHandleA(
502 0, const_cast<char*>(want_negotiate ? NEGOSSP_NAME_A : NTLMSP_NAME_A),
503 SECPKG_CRED_OUTBOUND, 0, pauth_id, 0, 0, &cred, &lifetime);
deadbeeff137e972017-03-23 22:45:49504 if (ret != SEC_E_OK) {
Mirko Bonadei675513b2017-11-09 10:09:25505 RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: "
Tommie51a0a82018-02-27 14:30:29506 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49507 return HAR_IGNORE;
508 }
509
Yves Gerey665174f2018-06-19 13:03:05510 // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out;
deadbeeff137e972017-03-23 22:45:49511
512 CtxtHandle ctx;
Yves Gerey665174f2018-06-19 13:03:05513 ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0,
514 SECURITY_NATIVE_DREP, 0, 0, &ctx,
515 &out_buf_desc, &ret_flags, &lifetime);
deadbeeff137e972017-03-23 22:45:49516 if (FAILED(ret)) {
Mirko Bonadei675513b2017-11-09 10:09:25517 RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: "
Tommie51a0a82018-02-27 14:30:29518 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49519 FreeCredentialsHandle(&cred);
520 return HAR_IGNORE;
521 }
522
523 RTC_DCHECK(!context);
524 context = neg = new NegotiateAuthContext(auth_method, cred, ctx);
525 neg->specified_credentials = specify_credentials;
526 neg->steps = steps;
527 }
528
Yves Gerey665174f2018-06-19 13:03:05529 if ((ret == SEC_I_COMPLETE_NEEDED) ||
530 (ret == SEC_I_COMPLETE_AND_CONTINUE)) {
deadbeeff137e972017-03-23 22:45:49531 ret = CompleteAuthToken(&neg->ctx, &out_buf_desc);
Mirko Bonadei675513b2017-11-09 10:09:25532 RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: "
Tommie51a0a82018-02-27 14:30:29533 << GetErrorName(ret, SECURITY_ERRORS);
deadbeeff137e972017-03-23 22:45:49534 if (FAILED(ret)) {
535 return HAR_ERROR;
536 }
537 }
538
deadbeeff137e972017-03-23 22:45:49539 std::string decoded(out_buf, out_buf + out_sec.cbBuffer);
540 response = auth_method;
541 response.append(" ");
542 response.append(Base64::Encode(decoded));
543 return HAR_RESPONSE;
544 }
545#endif
Yves Gerey665174f2018-06-19 13:03:05546#endif // WEBRTC_WIN
deadbeeff137e972017-03-23 22:45:49547
548 return HAR_IGNORE;
549}
550
551//////////////////////////////////////////////////////////////////////
552
Yves Gerey665174f2018-06-19 13:03:05553} // namespace rtc