deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [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 15:45:49 -0700 | [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 | |
| 21 | #include <algorithm> |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/arraysize.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/cryptstring.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/httpcommon.h" |
| 27 | #include "rtc_base/messagedigest.h" |
| 28 | #include "rtc_base/socketaddress.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 29 | #include "rtc_base/strings/string_builder.h" |
Artem Titov | a76af0c | 2018-07-23 17:38:12 +0200 | [diff] [blame] | 30 | #include "rtc_base/third_party/base64/base64.h" |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 31 | #include "rtc_base/zero_memory.h" |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 32 | |
| 33 | namespace rtc { |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 34 | namespace { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 35 | #if defined(WEBRTC_WIN) |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 50 | struct ConstantToLabel { |
| 51 | int value; |
| 52 | const char* label; |
| 53 | }; |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 54 | |
| 55 | const 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 | |
| 64 | std::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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | #define KLABEL(x) \ |
| 79 | { x, #x } |
| 80 | #define LASTLABEL \ |
| 81 | { 0, 0 } |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 82 | |
| 83 | const ConstantToLabel SECURITY_ERRORS[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 84 | 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}; |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 108 | #undef KLABEL |
| 109 | #undef LASTLABEL |
| 110 | #endif // defined(WEBRTC_WIN) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 111 | |
Niels Möller | 83da552 | 2018-09-26 16:18:38 +0200 | [diff] [blame^] | 112 | typedef std::pair<std::string, std::string> HttpAttribute; |
| 113 | typedef std::vector<HttpAttribute> HttpAttributeList; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 114 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 115 | inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 116 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | if ((pos + 1 < len) && (data[pos] == '=') && |
| 123 | !isspace(static_cast<unsigned char>(data[pos + 1])) && |
| 124 | (data[pos + 1] != '=')) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | void HttpParseAttributes(const char* data, |
| 131 | size_t len, |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 132 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 155 | ++pos; // Skip '=' |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 156 | // 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 168 | while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) && |
| 169 | (data[pos] != ',')) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 170 | attribute.second.append(1, data[pos++]); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | attributes.push_back(attribute); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 176 | if ((pos < len) && (data[pos] == ',')) |
| 177 | ++pos; // Skip ',' |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | bool 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 | |
| 196 | bool 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 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 210 | std::string quote(const std::string& str) { |
| 211 | std::string result; |
| 212 | result.push_back('"'); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 213 | for (size_t i = 0; i < str.size(); ++i) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 214 | 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) |
| 223 | struct 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 230 | : HttpAuthContext(auth), |
| 231 | cred(c1), |
| 232 | ctx(c2), |
| 233 | steps(0), |
| 234 | specified_credentials(false) {} |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 235 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 236 | ~NegotiateAuthContext() override { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 237 | DeleteSecurityContext(&ctx); |
| 238 | FreeCredentialsHandle(&cred); |
| 239 | } |
| 240 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 241 | #endif // WEBRTC_WIN |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 242 | |
Niels Möller | 83da552 | 2018-09-26 16:18:38 +0200 | [diff] [blame^] | 243 | } // anonymous namespace |
| 244 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 245 | HttpAuthResult 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) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 255 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 265 | return HAR_CREDENTIALS; // Bad credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 266 | if (username.empty()) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 267 | return HAR_CREDENTIALS; // Missing credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 268 | |
| 269 | context = new HttpAuthContext(auth_method); |
| 270 | |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 271 | // 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; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 275 | size_t len = username.size() + password.GetLength() + 2; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 276 | char* sensitive = new char[len]; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 277 | 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 Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 285 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 286 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 287 | return HAR_RESPONSE; |
| 288 | } |
| 289 | |
| 290 | // DIGEST |
| 291 | if (_stricmp(auth_method.c_str(), "digest") == 0) { |
| 292 | if (context) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 293 | return HAR_CREDENTIALS; // Bad credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 294 | if (username.empty()) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 295 | return HAR_CREDENTIALS; // Missing credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 296 | |
| 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 Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 311 | // 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; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 315 | size_t len = username.size() + realm.size() + password.GetLength() + 3; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 316 | char* sensitive = new char[len]; // A1 |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 317 | 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 Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 332 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 333 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 334 | std::string HA2 = MD5(A2); |
| 335 | std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2); |
| 336 | |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 337 | rtc::StringBuilder ss; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 338 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 345 | ss << ", nc=" << ncount; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 346 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 365 | #if 0 // Requires funky windows versions |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 366 | DWORD len = MAX_SPN; |
| 367 | if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr, |
| 368 | server.port(), |
| 369 | 0, &len, spn) != ERROR_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 370 | RTC_LOG_F(WARNING) << "(Negotiate) - DsMakeSpn failed"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 371 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 378 | out_sec.pvBuffer = out_buf; |
| 379 | out_sec.cbBuffer = sizeof(out_buf); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 380 | out_sec.BufferType = SECBUFFER_TOKEN; |
| 381 | |
| 382 | SecBufferDesc out_buf_desc; |
| 383 | out_buf_desc.ulVersion = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 384 | out_buf_desc.cBuffers = 1; |
| 385 | out_buf_desc.pBuffers = &out_sec; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 386 | |
| 387 | const ULONG NEG_FLAGS_DEFAULT = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 388 | // 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 | ; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 396 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 406 | NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 407 | if (neg) { |
| 408 | const size_t max_steps = 10; |
| 409 | if (++neg->steps >= max_steps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 410 | RTC_LOG(WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) " |
| 411 | "too many retries"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 412 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 421 | in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data()); |
| 422 | in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size()); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 423 | in_sec.BufferType = SECBUFFER_TOKEN; |
| 424 | |
| 425 | SecBufferDesc in_buf_desc; |
| 426 | in_buf_desc.ulVersion = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 427 | in_buf_desc.cBuffers = 1; |
| 428 | in_buf_desc.pBuffers = &in_sec; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 429 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 430 | 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); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 433 | if (FAILED(ret)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 434 | RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 435 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 436 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 450 | SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 451 | if (specify_credentials) { |
| 452 | memset(&auth_id, 0, sizeof(auth_id)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 453 | size_t len = password.GetLength() + 1; |
| 454 | char* sensitive = new char[len]; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 455 | 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 Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 482 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 483 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 484 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 489 | RTC_LOG(LS_VERBOSE) |
| 490 | << "Negotiate protocol: Using specified credentials"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 491 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 492 | RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 493 | } |
| 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); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 499 | if (ret != SEC_E_OK) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 500 | RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 501 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 502 | return HAR_IGNORE; |
| 503 | } |
| 504 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 505 | // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 506 | |
| 507 | CtxtHandle ctx; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 508 | ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0, |
| 509 | SECURITY_NATIVE_DREP, 0, 0, &ctx, |
| 510 | &out_buf_desc, &ret_flags, &lifetime); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 511 | if (FAILED(ret)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 512 | RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 513 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 514 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 524 | if ((ret == SEC_I_COMPLETE_NEEDED) || |
| 525 | (ret == SEC_I_COMPLETE_AND_CONTINUE)) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 526 | ret = CompleteAuthToken(&neg->ctx, &out_buf_desc); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 527 | RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 528 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 529 | if (FAILED(ret)) { |
| 530 | return HAR_ERROR; |
| 531 | } |
| 532 | } |
| 533 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 534 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 541 | #endif // WEBRTC_WIN |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 542 | |
| 543 | return HAR_IGNORE; |
| 544 | } |
| 545 | |
| 546 | ////////////////////////////////////////////////////////////////////// |
| 547 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 548 | } // namespace rtc |