deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 1 | /* |
| 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) |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 14 | #include <windows.h> |
| 15 | #include <winsock2.h> |
| 16 | #include <ws2tcpip.h> |
| 17 | #define SECURITY_WIN32 |
| 18 | #include <security.h> |
| 19 | #endif |
| 20 | |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 21 | #include <ctype.h> // for isspace |
| 22 | #include <stdio.h> // for sprintf |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 23 | #include <algorithm> |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 24 | #include <utility> // for pair |
| 25 | #include <vector> |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 26 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 27 | #include "rtc_base/arraysize.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "rtc_base/checks.h" |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 29 | #include "rtc_base/cryptstring.h" // for CryptString |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 30 | #include "rtc_base/httpcommon.h" |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 31 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 32 | #include "rtc_base/messagedigest.h" |
| 33 | #include "rtc_base/socketaddress.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 34 | #include "rtc_base/strings/string_builder.h" |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 35 | #include "rtc_base/third_party/base64/base64.h" // for Base64 |
| 36 | #include "rtc_base/zero_memory.h" // for ExplicitZeroMemory |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 37 | |
| 38 | namespace rtc { |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 39 | namespace { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 40 | #if defined(WEBRTC_WIN) |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 41 | /////////////////////////////////////////////////////////////////////////////// |
| 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 55 | struct ConstantToLabel { |
| 56 | int value; |
| 57 | const char* label; |
| 58 | }; |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 59 | |
| 60 | const 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 | |
| 69 | std::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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 83 | #define KLABEL(x) \ |
| 84 | { x, #x } |
| 85 | #define LASTLABEL \ |
| 86 | { 0, 0 } |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 87 | |
| 88 | const ConstantToLabel SECURITY_ERRORS[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 89 | 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}; |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 113 | #undef KLABEL |
| 114 | #undef LASTLABEL |
| 115 | #endif // defined(WEBRTC_WIN) |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 116 | |
Niels Möller | 83da552 | 2018-09-26 14:18:38 | [diff] [blame] | 117 | typedef std::pair<std::string, std::string> HttpAttribute; |
| 118 | typedef std::vector<HttpAttribute> HttpAttributeList; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 119 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 120 | inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 121 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 127 | if ((pos + 1 < len) && (data[pos] == '=') && |
| 128 | !isspace(static_cast<unsigned char>(data[pos + 1])) && |
| 129 | (data[pos + 1] != '=')) { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 135 | void HttpParseAttributes(const char* data, |
| 136 | size_t len, |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 137 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 160 | ++pos; // Skip '=' |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 161 | // 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 173 | while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) && |
| 174 | (data[pos] != ',')) { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 175 | attribute.second.append(1, data[pos++]); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | attributes.push_back(attribute); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 181 | if ((pos < len) && (data[pos] == ',')) |
| 182 | ++pos; // Skip ',' |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | bool 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 | |
| 201 | bool 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 | |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 215 | std::string quote(const std::string& str) { |
| 216 | std::string result; |
| 217 | result.push_back('"'); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 218 | for (size_t i = 0; i < str.size(); ++i) { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 219 | 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) |
| 228 | struct 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 235 | : HttpAuthContext(auth), |
| 236 | cred(c1), |
| 237 | ctx(c2), |
| 238 | steps(0), |
| 239 | specified_credentials(false) {} |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 240 | |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 241 | ~NegotiateAuthContext() override { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 242 | DeleteSecurityContext(&ctx); |
| 243 | FreeCredentialsHandle(&cred); |
| 244 | } |
| 245 | }; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 246 | #endif // WEBRTC_WIN |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 247 | |
Niels Möller | 83da552 | 2018-09-26 14:18:38 | [diff] [blame] | 248 | } // anonymous namespace |
| 249 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 250 | HttpAuthResult 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) { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 260 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 270 | return HAR_CREDENTIALS; // Bad credentials |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 271 | if (username.empty()) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 272 | return HAR_CREDENTIALS; // Missing credentials |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 273 | |
| 274 | context = new HttpAuthContext(auth_method); |
| 275 | |
Joachim Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 276 | // 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; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 280 | size_t len = username.size() + password.GetLength() + 2; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 281 | char* sensitive = new char[len]; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 282 | 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 Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 290 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 291 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 292 | return HAR_RESPONSE; |
| 293 | } |
| 294 | |
| 295 | // DIGEST |
| 296 | if (_stricmp(auth_method.c_str(), "digest") == 0) { |
| 297 | if (context) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 298 | return HAR_CREDENTIALS; // Bad credentials |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 299 | if (username.empty()) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 300 | return HAR_CREDENTIALS; // Missing credentials |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 301 | |
| 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 Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 316 | // 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; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 320 | size_t len = username.size() + realm.size() + password.GetLength() + 3; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 321 | char* sensitive = new char[len]; // A1 |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 322 | 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 Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 337 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 338 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 339 | std::string HA2 = MD5(A2); |
| 340 | std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2); |
| 341 | |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 342 | rtc::StringBuilder ss; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 343 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 350 | ss << ", nc=" << ncount; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 351 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 370 | #if 0 // Requires funky windows versions |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 371 | DWORD len = MAX_SPN; |
| 372 | if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr, |
| 373 | server.port(), |
| 374 | 0, &len, spn) != ERROR_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 375 | RTC_LOG_F(WARNING) << "(Negotiate) - DsMakeSpn failed"; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 376 | return HAR_IGNORE; |
| 377 | } |
| 378 | #else |
Niels Möller | aba0633 | 2018-10-16 13:14:15 | [diff] [blame^] | 379 | snprintf(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str()); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 380 | #endif |
| 381 | |
| 382 | SecBuffer out_sec; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 383 | out_sec.pvBuffer = out_buf; |
| 384 | out_sec.cbBuffer = sizeof(out_buf); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 385 | out_sec.BufferType = SECBUFFER_TOKEN; |
| 386 | |
| 387 | SecBufferDesc out_buf_desc; |
| 388 | out_buf_desc.ulVersion = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 389 | out_buf_desc.cBuffers = 1; |
| 390 | out_buf_desc.pBuffers = &out_sec; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 391 | |
| 392 | const ULONG NEG_FLAGS_DEFAULT = |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 393 | // 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 | ; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 401 | |
| 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 411 | NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 412 | if (neg) { |
| 413 | const size_t max_steps = 10; |
| 414 | if (++neg->steps >= max_steps) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 415 | RTC_LOG(WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) " |
| 416 | "too many retries"; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 417 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 426 | in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data()); |
| 427 | in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size()); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 428 | in_sec.BufferType = SECBUFFER_TOKEN; |
| 429 | |
| 430 | SecBufferDesc in_buf_desc; |
| 431 | in_buf_desc.ulVersion = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 432 | in_buf_desc.cBuffers = 1; |
| 433 | in_buf_desc.pBuffers = &in_sec; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 434 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 435 | 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); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 438 | if (FAILED(ret)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 439 | RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: " |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 440 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 441 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 455 | SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 456 | if (specify_credentials) { |
| 457 | memset(&auth_id, 0, sizeof(auth_id)); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 458 | size_t len = password.GetLength() + 1; |
| 459 | char* sensitive = new char[len]; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 460 | 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 Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 487 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 488 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 489 | 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 Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 494 | RTC_LOG(LS_VERBOSE) |
| 495 | << "Negotiate protocol: Using specified credentials"; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 496 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 497 | RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials"; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 498 | } |
| 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); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 504 | if (ret != SEC_E_OK) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 505 | RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: " |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 506 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 507 | return HAR_IGNORE; |
| 508 | } |
| 509 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 510 | // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 511 | |
| 512 | CtxtHandle ctx; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 513 | ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0, |
| 514 | SECURITY_NATIVE_DREP, 0, 0, &ctx, |
| 515 | &out_buf_desc, &ret_flags, &lifetime); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 516 | if (FAILED(ret)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 517 | RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: " |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 518 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 519 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 529 | if ((ret == SEC_I_COMPLETE_NEEDED) || |
| 530 | (ret == SEC_I_COMPLETE_AND_CONTINUE)) { |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 531 | ret = CompleteAuthToken(&neg->ctx, &out_buf_desc); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 532 | RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: " |
Tommi | e51a0a8 | 2018-02-27 14:30:29 | [diff] [blame] | 533 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 534 | if (FAILED(ret)) { |
| 535 | return HAR_ERROR; |
| 536 | } |
| 537 | } |
| 538 | |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 539 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 546 | #endif // WEBRTC_WIN |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 547 | |
| 548 | return HAR_IGNORE; |
| 549 | } |
| 550 | |
| 551 | ////////////////////////////////////////////////////////////////////// |
| 552 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 553 | } // namespace rtc |