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 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 13 | #include "absl/strings/string_view.h" |
| 14 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 15 | #if defined(WEBRTC_WIN) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 16 | #include <windows.h> |
| 17 | #include <winsock2.h> |
| 18 | #include <ws2tcpip.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 20 | #define SECURITY_WIN32 |
| 21 | #include <security.h> |
| 22 | #endif |
| 23 | |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 24 | #include <ctype.h> // for isspace |
| 25 | #include <stdio.h> // for sprintf |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 26 | |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 27 | #include <utility> // for pair |
| 28 | #include <vector> |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 29 | |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 30 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "rtc_base/crypt_string.h" // for CryptString |
| 32 | #include "rtc_base/http_common.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 33 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "rtc_base/message_digest.h" |
| 35 | #include "rtc_base/socket_address.h" |
Niels Möller | 198cf00 | 2019-05-10 12:29:13 +0200 | [diff] [blame] | 36 | #include "rtc_base/string_utils.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 37 | #include "rtc_base/strings/string_builder.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 38 | #include "rtc_base/third_party/base64/base64.h" // for Base64 |
| 39 | #include "rtc_base/zero_memory.h" // for ExplicitZeroMemory |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 40 | |
| 41 | namespace rtc { |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 42 | namespace { |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 43 | #if defined(WEBRTC_WIN) && !defined(WINUWP) |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | // ConstantToLabel can be used to easily generate string names from constant |
| 46 | // values. This can be useful for logging descriptive names of error messages. |
| 47 | // Usage: |
| 48 | // const ConstantToLabel LIBRARY_ERRORS[] = { |
| 49 | // KLABEL(SOME_ERROR), |
| 50 | // KLABEL(SOME_OTHER_ERROR), |
| 51 | // ... |
| 52 | // LASTLABEL |
| 53 | // } |
| 54 | // |
| 55 | // int err = LibraryFunc(); |
| 56 | // LOG(LS_ERROR) << "LibraryFunc returned: " |
| 57 | // << GetErrorName(err, LIBRARY_ERRORS); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | struct ConstantToLabel { |
| 59 | int value; |
| 60 | const char* label; |
| 61 | }; |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 62 | |
| 63 | const char* LookupLabel(int value, const ConstantToLabel entries[]) { |
| 64 | for (int i = 0; entries[i].label; ++i) { |
| 65 | if (value == entries[i].value) { |
| 66 | return entries[i].label; |
| 67 | } |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | std::string GetErrorName(int err, const ConstantToLabel* err_table) { |
| 73 | if (err == 0) |
| 74 | return "No error"; |
| 75 | |
| 76 | if (err_table != 0) { |
| 77 | if (const char* value = LookupLabel(err, err_table)) |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | char buffer[16]; |
| 82 | snprintf(buffer, sizeof(buffer), "0x%08x", err); |
| 83 | return buffer; |
| 84 | } |
| 85 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 86 | #define KLABEL(x) \ |
| 87 | { x, #x } |
| 88 | #define LASTLABEL \ |
| 89 | { 0, 0 } |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 90 | |
| 91 | const ConstantToLabel SECURITY_ERRORS[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | KLABEL(SEC_I_COMPLETE_AND_CONTINUE), |
| 93 | KLABEL(SEC_I_COMPLETE_NEEDED), |
| 94 | KLABEL(SEC_I_CONTEXT_EXPIRED), |
| 95 | KLABEL(SEC_I_CONTINUE_NEEDED), |
| 96 | KLABEL(SEC_I_INCOMPLETE_CREDENTIALS), |
| 97 | KLABEL(SEC_I_RENEGOTIATE), |
| 98 | KLABEL(SEC_E_CERT_EXPIRED), |
| 99 | KLABEL(SEC_E_INCOMPLETE_MESSAGE), |
| 100 | KLABEL(SEC_E_INSUFFICIENT_MEMORY), |
| 101 | KLABEL(SEC_E_INTERNAL_ERROR), |
| 102 | KLABEL(SEC_E_INVALID_HANDLE), |
| 103 | KLABEL(SEC_E_INVALID_TOKEN), |
| 104 | KLABEL(SEC_E_LOGON_DENIED), |
| 105 | KLABEL(SEC_E_NO_AUTHENTICATING_AUTHORITY), |
| 106 | KLABEL(SEC_E_NO_CREDENTIALS), |
| 107 | KLABEL(SEC_E_NOT_OWNER), |
| 108 | KLABEL(SEC_E_OK), |
| 109 | KLABEL(SEC_E_SECPKG_NOT_FOUND), |
| 110 | KLABEL(SEC_E_TARGET_UNKNOWN), |
| 111 | KLABEL(SEC_E_UNKNOWN_CREDENTIALS), |
| 112 | KLABEL(SEC_E_UNSUPPORTED_FUNCTION), |
| 113 | KLABEL(SEC_E_UNTRUSTED_ROOT), |
| 114 | KLABEL(SEC_E_WRONG_PRINCIPAL), |
| 115 | LASTLABEL}; |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 116 | #undef KLABEL |
| 117 | #undef LASTLABEL |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 118 | #endif // defined(WEBRTC_WIN) && !defined(WINUWP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 119 | |
Niels Möller | 83da552 | 2018-09-26 16:18:38 +0200 | [diff] [blame] | 120 | typedef std::pair<std::string, std::string> HttpAttribute; |
| 121 | typedef std::vector<HttpAttribute> HttpAttributeList; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 122 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 123 | inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 124 | if (pos >= len) |
| 125 | return true; |
| 126 | if (isspace(static_cast<unsigned char>(data[pos]))) |
| 127 | return true; |
| 128 | // The reason for this complexity is that some attributes may contain trailing |
| 129 | // equal signs (like base64 tokens in Negotiate auth headers) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | if ((pos + 1 < len) && (data[pos] == '=') && |
| 131 | !isspace(static_cast<unsigned char>(data[pos + 1])) && |
| 132 | (data[pos + 1] != '=')) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 138 | void HttpParseAttributes(const char* data, |
| 139 | size_t len, |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 140 | HttpAttributeList& attributes) { |
| 141 | size_t pos = 0; |
| 142 | while (true) { |
| 143 | // Skip leading whitespace |
| 144 | while ((pos < len) && isspace(static_cast<unsigned char>(data[pos]))) { |
| 145 | ++pos; |
| 146 | } |
| 147 | |
| 148 | // End of attributes? |
| 149 | if (pos >= len) |
| 150 | return; |
| 151 | |
| 152 | // Find end of attribute name |
| 153 | size_t start = pos; |
| 154 | while (!IsEndOfAttributeName(pos, len, data)) { |
| 155 | ++pos; |
| 156 | } |
| 157 | |
| 158 | HttpAttribute attribute; |
| 159 | attribute.first.assign(data + start, data + pos); |
| 160 | |
| 161 | // Attribute has value? |
| 162 | if ((pos < len) && (data[pos] == '=')) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 163 | ++pos; // Skip '=' |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 164 | // Check if quoted value |
| 165 | if ((pos < len) && (data[pos] == '"')) { |
| 166 | while (++pos < len) { |
| 167 | if (data[pos] == '"') { |
| 168 | ++pos; |
| 169 | break; |
| 170 | } |
| 171 | if ((data[pos] == '\\') && (pos + 1 < len)) |
| 172 | ++pos; |
| 173 | attribute.second.append(1, data[pos]); |
| 174 | } |
| 175 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 176 | while ((pos < len) && !isspace(static_cast<unsigned char>(data[pos])) && |
| 177 | (data[pos] != ',')) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 178 | attribute.second.append(1, data[pos++]); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | attributes.push_back(attribute); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 184 | if ((pos < len) && (data[pos] == ',')) |
| 185 | ++pos; // Skip ',' |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | bool HttpHasAttribute(const HttpAttributeList& attributes, |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 190 | absl::string_view name, |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 191 | std::string* value) { |
| 192 | for (HttpAttributeList::const_iterator it = attributes.begin(); |
| 193 | it != attributes.end(); ++it) { |
| 194 | if (it->first == name) { |
| 195 | if (value) { |
| 196 | *value = it->second; |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | bool HttpHasNthAttribute(HttpAttributeList& attributes, |
| 205 | size_t index, |
| 206 | std::string* name, |
| 207 | std::string* value) { |
| 208 | if (index >= attributes.size()) |
| 209 | return false; |
| 210 | |
| 211 | if (name) |
| 212 | *name = attributes[index].first; |
| 213 | if (value) |
| 214 | *value = attributes[index].second; |
| 215 | return true; |
| 216 | } |
| 217 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 218 | std::string quote(absl::string_view str) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 219 | std::string result; |
| 220 | result.push_back('"'); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 221 | for (size_t i = 0; i < str.size(); ++i) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 222 | if ((str[i] == '"') || (str[i] == '\\')) |
| 223 | result.push_back('\\'); |
| 224 | result.push_back(str[i]); |
| 225 | } |
| 226 | result.push_back('"'); |
| 227 | return result; |
| 228 | } |
| 229 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 230 | #if defined(WEBRTC_WIN) && !defined(WINUWP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 231 | struct NegotiateAuthContext : public HttpAuthContext { |
| 232 | CredHandle cred; |
| 233 | CtxtHandle ctx; |
| 234 | size_t steps; |
| 235 | bool specified_credentials; |
| 236 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 237 | NegotiateAuthContext(absl::string_view auth, CredHandle c1, CtxtHandle c2) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 238 | : HttpAuthContext(auth), |
| 239 | cred(c1), |
| 240 | ctx(c2), |
| 241 | steps(0), |
| 242 | specified_credentials(false) {} |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 243 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 244 | ~NegotiateAuthContext() override { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 245 | DeleteSecurityContext(&ctx); |
| 246 | FreeCredentialsHandle(&cred); |
| 247 | } |
| 248 | }; |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 249 | #endif // defined(WEBRTC_WIN) && !defined(WINUWP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 250 | |
Niels Möller | 83da552 | 2018-09-26 16:18:38 +0200 | [diff] [blame] | 251 | } // anonymous namespace |
| 252 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 253 | HttpAuthResult HttpAuthenticate(const char* challenge, |
| 254 | size_t len, |
| 255 | const SocketAddress& server, |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 256 | absl::string_view method, |
| 257 | absl::string_view uri, |
| 258 | absl::string_view username, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 259 | const CryptString& password, |
| 260 | HttpAuthContext*& context, |
| 261 | std::string& response, |
| 262 | std::string& auth_method) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 263 | HttpAttributeList args; |
| 264 | HttpParseAttributes(challenge, len, args); |
| 265 | HttpHasNthAttribute(args, 0, &auth_method, nullptr); |
| 266 | |
| 267 | if (context && (context->auth_method != auth_method)) |
| 268 | return HAR_IGNORE; |
| 269 | |
| 270 | // BASIC |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 271 | if (absl::EqualsIgnoreCase(auth_method, "basic")) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 272 | if (context) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 273 | return HAR_CREDENTIALS; // Bad credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 274 | if (username.empty()) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 275 | return HAR_CREDENTIALS; // Missing credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 276 | |
| 277 | context = new HttpAuthContext(auth_method); |
| 278 | |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 279 | // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also |
| 280 | // return response as CryptString so contents get securely deleted |
| 281 | // automatically. |
| 282 | // std::string decoded = username + ":" + password; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 283 | size_t len = username.size() + password.GetLength() + 2; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 284 | char* sensitive = new char[len]; |
Ali Tofigh | e5b2220 | 2022-03-25 00:08:32 +0100 | [diff] [blame] | 285 | size_t pos = strcpyn(sensitive, len, username); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 286 | pos += strcpyn(sensitive + pos, len - pos, ":"); |
| 287 | password.CopyTo(sensitive + pos, true); |
| 288 | |
| 289 | response = auth_method; |
| 290 | response.append(" "); |
| 291 | // TODO: create a sensitive-source version of Base64::encode |
| 292 | response.append(Base64::Encode(sensitive)); |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 293 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 294 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 295 | return HAR_RESPONSE; |
| 296 | } |
| 297 | |
| 298 | // DIGEST |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 299 | if (absl::EqualsIgnoreCase(auth_method, "digest")) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 300 | if (context) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 301 | return HAR_CREDENTIALS; // Bad credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 302 | if (username.empty()) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 303 | return HAR_CREDENTIALS; // Missing credentials |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 304 | |
| 305 | context = new HttpAuthContext(auth_method); |
| 306 | |
| 307 | std::string cnonce, ncount; |
| 308 | char buffer[256]; |
| 309 | sprintf(buffer, "%d", static_cast<int>(time(0))); |
| 310 | cnonce = MD5(buffer); |
| 311 | ncount = "00000001"; |
| 312 | |
| 313 | std::string realm, nonce, qop, opaque; |
| 314 | HttpHasAttribute(args, "realm", &realm); |
| 315 | HttpHasAttribute(args, "nonce", &nonce); |
| 316 | bool has_qop = HttpHasAttribute(args, "qop", &qop); |
| 317 | bool has_opaque = HttpHasAttribute(args, "opaque", &opaque); |
| 318 | |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 319 | // TODO(bugs.webrtc.org/8905): Convert sensitive to a CryptString and also |
| 320 | // return response as CryptString so contents get securely deleted |
| 321 | // automatically. |
| 322 | // std::string A1 = username + ":" + realm + ":" + password; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 323 | size_t len = username.size() + realm.size() + password.GetLength() + 3; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 324 | char* sensitive = new char[len]; // A1 |
Ali Tofigh | e5b2220 | 2022-03-25 00:08:32 +0100 | [diff] [blame] | 325 | size_t pos = strcpyn(sensitive, len, username); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 326 | pos += strcpyn(sensitive + pos, len - pos, ":"); |
Ali Tofigh | e5b2220 | 2022-03-25 00:08:32 +0100 | [diff] [blame] | 327 | pos += strcpyn(sensitive + pos, len - pos, realm); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 328 | pos += strcpyn(sensitive + pos, len - pos, ":"); |
| 329 | password.CopyTo(sensitive + pos, true); |
| 330 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 331 | std::string A2 = std::string(method) + ":" + std::string(uri); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 332 | std::string middle; |
| 333 | if (has_qop) { |
| 334 | qop = "auth"; |
| 335 | middle = nonce + ":" + ncount + ":" + cnonce + ":" + qop; |
| 336 | } else { |
| 337 | middle = nonce; |
| 338 | } |
| 339 | std::string HA1 = MD5(sensitive); |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 340 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 341 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 342 | std::string HA2 = MD5(A2); |
| 343 | std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2); |
| 344 | |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 345 | rtc::StringBuilder ss; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 346 | ss << auth_method; |
| 347 | ss << " username=" << quote(username); |
| 348 | ss << ", realm=" << quote(realm); |
| 349 | ss << ", nonce=" << quote(nonce); |
| 350 | ss << ", uri=" << quote(uri); |
| 351 | if (has_qop) { |
| 352 | ss << ", qop=" << qop; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 353 | ss << ", nc=" << ncount; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 354 | ss << ", cnonce=" << quote(cnonce); |
| 355 | } |
| 356 | ss << ", response=\"" << dig_response << "\""; |
| 357 | if (has_opaque) { |
| 358 | ss << ", opaque=" << quote(opaque); |
| 359 | } |
| 360 | response = ss.str(); |
| 361 | return HAR_RESPONSE; |
| 362 | } |
| 363 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 364 | #if defined(WEBRTC_WIN) && !defined(WINUWP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 365 | #if 1 |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 366 | bool want_negotiate = absl::EqualsIgnoreCase(auth_method, "negotiate"); |
| 367 | bool want_ntlm = absl::EqualsIgnoreCase(auth_method, "ntlm"); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 368 | // SPNEGO & NTLM |
| 369 | if (want_negotiate || want_ntlm) { |
| 370 | const size_t MAX_MESSAGE = 12000, MAX_SPN = 256; |
| 371 | char out_buf[MAX_MESSAGE], spn[MAX_SPN]; |
| 372 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 373 | #if 0 // Requires funky windows versions |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 374 | DWORD len = MAX_SPN; |
| 375 | if (DsMakeSpn("HTTP", server.HostAsURIString().c_str(), nullptr, |
| 376 | server.port(), |
| 377 | 0, &len, spn) != ERROR_SUCCESS) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 378 | RTC_LOG_F(LS_WARNING) << "(Negotiate) - DsMakeSpn failed"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 379 | return HAR_IGNORE; |
| 380 | } |
| 381 | #else |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 382 | snprintf(spn, MAX_SPN, "HTTP/%s", server.ToString().c_str()); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 383 | #endif |
| 384 | |
| 385 | SecBuffer out_sec; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 386 | out_sec.pvBuffer = out_buf; |
| 387 | out_sec.cbBuffer = sizeof(out_buf); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 388 | out_sec.BufferType = SECBUFFER_TOKEN; |
| 389 | |
| 390 | SecBufferDesc out_buf_desc; |
| 391 | out_buf_desc.ulVersion = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 392 | out_buf_desc.cBuffers = 1; |
| 393 | out_buf_desc.pBuffers = &out_sec; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 394 | |
| 395 | const ULONG NEG_FLAGS_DEFAULT = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 396 | // ISC_REQ_ALLOCATE_MEMORY |
| 397 | ISC_REQ_CONFIDENTIALITY |
| 398 | //| ISC_REQ_EXTENDED_ERROR |
| 399 | //| ISC_REQ_INTEGRITY |
| 400 | | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT |
| 401 | //| ISC_REQ_STREAM |
| 402 | //| ISC_REQ_USE_SUPPLIED_CREDS |
| 403 | ; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 404 | |
| 405 | ::TimeStamp lifetime; |
| 406 | SECURITY_STATUS ret = S_OK; |
| 407 | ULONG ret_flags = 0, flags = NEG_FLAGS_DEFAULT; |
| 408 | |
| 409 | bool specify_credentials = !username.empty(); |
| 410 | size_t steps = 0; |
| 411 | |
| 412 | // uint32_t now = Time(); |
| 413 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 414 | NegotiateAuthContext* neg = static_cast<NegotiateAuthContext*>(context); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 415 | if (neg) { |
| 416 | const size_t max_steps = 10; |
| 417 | if (++neg->steps >= max_steps) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 418 | RTC_LOG(LS_WARNING) << "AsyncHttpsProxySocket::Authenticate(Negotiate) " |
| 419 | "too many retries"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 420 | return HAR_ERROR; |
| 421 | } |
| 422 | steps = neg->steps; |
| 423 | |
| 424 | std::string challenge, decoded_challenge; |
| 425 | if (HttpHasNthAttribute(args, 1, &challenge, nullptr) && |
| 426 | Base64::Decode(challenge, Base64::DO_STRICT, &decoded_challenge, |
| 427 | nullptr)) { |
| 428 | SecBuffer in_sec; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 429 | in_sec.pvBuffer = const_cast<char*>(decoded_challenge.data()); |
| 430 | in_sec.cbBuffer = static_cast<unsigned long>(decoded_challenge.size()); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 431 | in_sec.BufferType = SECBUFFER_TOKEN; |
| 432 | |
| 433 | SecBufferDesc in_buf_desc; |
| 434 | in_buf_desc.ulVersion = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 435 | in_buf_desc.cBuffers = 1; |
| 436 | in_buf_desc.pBuffers = &in_sec; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 437 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 438 | ret = InitializeSecurityContextA( |
| 439 | &neg->cred, &neg->ctx, spn, flags, 0, SECURITY_NATIVE_DREP, |
| 440 | &in_buf_desc, 0, &neg->ctx, &out_buf_desc, &ret_flags, &lifetime); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 441 | if (FAILED(ret)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 442 | RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 443 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 444 | return HAR_ERROR; |
| 445 | } |
| 446 | } else if (neg->specified_credentials) { |
| 447 | // Try again with default credentials |
| 448 | specify_credentials = false; |
| 449 | delete context; |
| 450 | context = neg = 0; |
| 451 | } else { |
| 452 | return HAR_CREDENTIALS; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (!neg) { |
| 457 | unsigned char userbuf[256], passbuf[256], domainbuf[16]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 458 | SEC_WINNT_AUTH_IDENTITY_A auth_id, *pauth_id = 0; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 459 | if (specify_credentials) { |
| 460 | memset(&auth_id, 0, sizeof(auth_id)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 461 | size_t len = password.GetLength() + 1; |
| 462 | char* sensitive = new char[len]; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 463 | password.CopyTo(sensitive, true); |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 464 | absl::string_view::size_type pos = username.find('\\'); |
| 465 | if (pos == absl::string_view::npos) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 466 | auth_id.UserLength = static_cast<unsigned long>( |
| 467 | std::min(sizeof(userbuf) - 1, username.size())); |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 468 | memcpy(userbuf, username.data(), auth_id.UserLength); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 469 | userbuf[auth_id.UserLength] = 0; |
| 470 | auth_id.DomainLength = 0; |
| 471 | domainbuf[auth_id.DomainLength] = 0; |
| 472 | auth_id.PasswordLength = static_cast<unsigned long>( |
| 473 | std::min(sizeof(passbuf) - 1, password.GetLength())); |
| 474 | memcpy(passbuf, sensitive, auth_id.PasswordLength); |
| 475 | passbuf[auth_id.PasswordLength] = 0; |
| 476 | } else { |
| 477 | auth_id.UserLength = static_cast<unsigned long>( |
| 478 | std::min(sizeof(userbuf) - 1, username.size() - pos - 1)); |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 479 | memcpy(userbuf, username.data() + pos + 1, auth_id.UserLength); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 480 | userbuf[auth_id.UserLength] = 0; |
| 481 | auth_id.DomainLength = |
| 482 | static_cast<unsigned long>(std::min(sizeof(domainbuf) - 1, pos)); |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 483 | memcpy(domainbuf, username.data(), auth_id.DomainLength); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 484 | domainbuf[auth_id.DomainLength] = 0; |
| 485 | auth_id.PasswordLength = static_cast<unsigned long>( |
| 486 | std::min(sizeof(passbuf) - 1, password.GetLength())); |
| 487 | memcpy(passbuf, sensitive, auth_id.PasswordLength); |
| 488 | passbuf[auth_id.PasswordLength] = 0; |
| 489 | } |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 490 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 491 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 492 | auth_id.User = userbuf; |
| 493 | auth_id.Domain = domainbuf; |
| 494 | auth_id.Password = passbuf; |
| 495 | auth_id.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI; |
| 496 | pauth_id = &auth_id; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 497 | RTC_LOG(LS_VERBOSE) |
| 498 | << "Negotiate protocol: Using specified credentials"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 499 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 500 | RTC_LOG(LS_VERBOSE) << "Negotiate protocol: Using default credentials"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | CredHandle cred; |
| 504 | ret = AcquireCredentialsHandleA( |
| 505 | 0, const_cast<char*>(want_negotiate ? NEGOSSP_NAME_A : NTLMSP_NAME_A), |
| 506 | SECPKG_CRED_OUTBOUND, 0, pauth_id, 0, 0, &cred, &lifetime); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 507 | if (ret != SEC_E_OK) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 508 | RTC_LOG(LS_ERROR) << "AcquireCredentialsHandle error: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 509 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 510 | return HAR_IGNORE; |
| 511 | } |
| 512 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 513 | // CSecBufferBundle<5, CSecBufferBase::FreeSSPI> sb_out; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 514 | |
| 515 | CtxtHandle ctx; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 516 | ret = InitializeSecurityContextA(&cred, 0, spn, flags, 0, |
| 517 | SECURITY_NATIVE_DREP, 0, 0, &ctx, |
| 518 | &out_buf_desc, &ret_flags, &lifetime); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 519 | if (FAILED(ret)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 520 | RTC_LOG(LS_ERROR) << "InitializeSecurityContext returned: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 521 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 522 | FreeCredentialsHandle(&cred); |
| 523 | return HAR_IGNORE; |
| 524 | } |
| 525 | |
| 526 | RTC_DCHECK(!context); |
| 527 | context = neg = new NegotiateAuthContext(auth_method, cred, ctx); |
| 528 | neg->specified_credentials = specify_credentials; |
| 529 | neg->steps = steps; |
| 530 | } |
| 531 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 532 | if ((ret == SEC_I_COMPLETE_NEEDED) || |
| 533 | (ret == SEC_I_COMPLETE_AND_CONTINUE)) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 534 | ret = CompleteAuthToken(&neg->ctx, &out_buf_desc); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 535 | RTC_LOG(LS_VERBOSE) << "CompleteAuthToken returned: " |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 536 | << GetErrorName(ret, SECURITY_ERRORS); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 537 | if (FAILED(ret)) { |
| 538 | return HAR_ERROR; |
| 539 | } |
| 540 | } |
| 541 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 542 | std::string decoded(out_buf, out_buf + out_sec.cbBuffer); |
| 543 | response = auth_method; |
| 544 | response.append(" "); |
| 545 | response.append(Base64::Encode(decoded)); |
| 546 | return HAR_RESPONSE; |
| 547 | } |
| 548 | #endif |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 549 | #endif // defined(WEBRTC_WIN) && !defined(WINUWP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 550 | |
| 551 | return HAR_IGNORE; |
| 552 | } |
| 553 | |
| 554 | ////////////////////////////////////////////////////////////////////// |
| 555 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 556 | } // namespace rtc |