| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include "gtest/gtest.h" |
| 30 | #include "net/http_auth.h" |
| 31 | |
| 32 | #include <boost/algorithm/string/predicate.hpp> |
| 33 | #include <boost/algorithm/string/join.hpp> |
| 34 | #include <boost/fusion/adapted/std_pair.hpp> |
| 35 | #include <boost/range/algorithm/find_if.hpp> |
| 36 | #include <boost/range/iterator_range_core.hpp> |
| 37 | #include <boost/spirit/include/karma_char.hpp> |
| 38 | #include <boost/spirit/include/karma_list.hpp> |
| 39 | #include <boost/spirit/include/karma_generate.hpp> |
| 40 | #include <boost/spirit/include/karma_right_alignment.hpp> |
| 41 | #include <boost/spirit/include/karma_sequence.hpp> |
| 42 | #include <boost/spirit/include/karma_string.hpp> |
| 43 | #include <boost/spirit/include/karma_uint.hpp> |
| 44 | #include <boost/spirit/include/qi_alternative.hpp> |
| 45 | #include <boost/spirit/include/qi_char.hpp> |
| 46 | #include <boost/spirit/include/qi_char_class.hpp> |
| 47 | #include <boost/spirit/include/qi_difference.hpp> |
| 48 | #include <boost/spirit/include/qi_eoi.hpp> |
| 49 | #include <boost/spirit/include/qi_list.hpp> |
| 50 | #include <boost/spirit/include/qi_parse.hpp> |
| 51 | #include <boost/spirit/include/qi_plus.hpp> |
| 52 | #include <boost/spirit/include/qi_sequence.hpp> |
| 53 | #include <boost/spirit/include/qi_string.hpp> |
| 54 | #include <cstdint> |
| 55 | #include <iterator> |
| 56 | #include <openssl/evp.h> |
| 57 | #include <string> |
| 58 | #include <unordered_map> |
| 59 | #include <utility> |
| 60 | #include <vector> |
| 61 | |
| 62 | #include "string_tools.h" |
| 63 | #include "crypto/crypto.h" |
| 64 | |
| 65 | namespace { |
| 66 | namespace http = epee::net_utils::http; |
| 67 | using fields = std::unordered_map<std::string, std::string>; |
| 68 | using auth_responses = std::vector<fields>; |
| 69 | |
| 70 | void rng(size_t len, uint8_t *ptr) |
| 71 | { |
| 72 | crypto::rand(len, ptr); |
| 73 | } |
| 74 | |
| 75 | std::string quoted(std::string str) |
| 76 | { |
| 77 | str.insert(str.begin(), '"'); |
| 78 | str.push_back('"'); |
| 79 | return str; |
| 80 | } |
| 81 | |
| 82 | void write_fields(std::string& out, const fields& args) |
| 83 | { |
| 84 | namespace karma = boost::spirit::karma; |
| 85 | karma::generate( |
| 86 | std::back_inserter(out), |
| 87 | (karma::string << " = " << karma::string) % " , ", |
| 88 | args); |
| 89 | } |
| 90 | |
| 91 | std::string write_fields(const fields& args) |
| 92 | { |
| 93 | std::string out{}; |
| 94 | write_fields(out, args); |
| 95 | return out; |
| 96 | } |
| 97 | |
| 98 | http::http_request_info make_request(const fields& args) |
| 99 | { |
| 100 | std::string out{" DIGEST "}; |
| 101 | write_fields(out, args); |
| 102 | |
| 103 | http::http_request_info request{}; |
| 104 | request.m_http_method_str = "NOP"; |
| 105 | request.m_header_info.m_etc_fields.push_back( |
| 106 | std::make_pair(u8"authorization", std::move(out)) |
| 107 | ); |
| 108 | return request; |
| 109 | } |
| 110 | |
| 111 | http::http_response_info make_response(const auth_responses& choices) |
| 112 | { |
| 113 | http::http_response_info response{}; |
| 114 | for (const auto& choice : choices) |
| 115 | { |
| 116 | std::string out{" DIGEST "}; |
| 117 | write_fields(out, choice); |
| 118 | |
| 119 | response.m_header_info.m_etc_fields.push_back( |
| 120 | std::make_pair(u8"WWW-authenticate", std::move(out)) |
| 121 | ); |
| 122 | } |
| 123 | return response; |
| 124 | } |
| 125 | |
| 126 | bool has_same_fields(const auth_responses& in) |
| 127 | { |
| 128 | const std::vector<std::string> check{u8"nonce", u8"qop", u8"realm", u8"stale"}; |
| 129 | |
| 130 | auto current = in.begin(); |
| 131 | const auto end = in.end(); |
| 132 | if (current == end) |
| 133 | return true; |
| 134 | |
| 135 | ++current; |
| 136 | for ( ; current != end; ++current ) |
| 137 | { |
| 138 | for (const auto& field : check) |
| 139 | { |
| 140 | const std::string& expected = in[0].at(field); |
| 141 | const std::string& actual = current->at(field); |
| 142 | EXPECT_EQ(expected, actual); |
| 143 | if (expected != actual) |
| 144 | return false; |
| 145 | } |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool is_unauthorized(const http::http_response_info& response) |
| 151 | { |
| 152 | EXPECT_EQ(401, response.m_response_code); |
| 153 | EXPECT_STREQ(u8"Unauthorized", response.m_response_comment.c_str()); |
| 154 | EXPECT_STREQ(u8"text/html", response.m_mime_tipe.c_str()); |
| 155 | return response.m_response_code == 401 && |
| 156 | response.m_response_comment == u8"Unauthorized" && |
| 157 | response.m_mime_tipe == u8"text/html"; |
| 158 | } |
| 159 | |
| 160 | fields parse_fields(const std::string& value) |
| 161 | { |
| 162 | namespace qi = boost::spirit::qi; |
| 163 | |
| 164 | fields out{}; |
| 165 | const bool rc = qi::parse( |
| 166 | value.begin(), value.end(), |
| 167 | qi::lit(u8"Digest ") >> (( |
| 168 | +qi::ascii::alpha >> |
| 169 | qi::lit('=') >> ( |
| 170 | (qi::lit('"') >> +(qi::ascii::char_ - '"') >> qi::lit('"')) | |
| 171 | +(qi::ascii::graph - qi::ascii::char_(u8"()<>@,;:\\\"/[]?={}")) |
| 172 | ) |
| 173 | ) % ',' |
| 174 | ) >> qi::eoi, |
| 175 | out |
| 176 | ); |
| 177 | if (!rc) |
| 178 | throw std::runtime_error{"Bad field given in HTTP header"}; |
| 179 | |
| 180 | return out; |
| 181 | } |
| 182 | |
| 183 | auth_responses parse_response(const http::http_response_info& response) |
| 184 | { |
| 185 | auth_responses result{}; |
| 186 | |
| 187 | const auto end = response.m_additional_fields.end(); |
| 188 | for (auto current = response.m_additional_fields.begin();; ++current) |
| 189 | { |
| 190 | current = std::find_if(current, end, [] (const std::pair<std::string, std::string>& field) { |
| 191 | return boost::equals(u8"WWW-authenticate", field.first); |
| 192 | }); |
| 193 | |
| 194 | if (current == end) |
| 195 | return result; |
| 196 | |
| 197 | result.push_back(parse_fields(current->second)); |
| 198 | } |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | std::string md5_hex(const std::string& in) |
| 203 | { |
| 204 | std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> ctx(EVP_MD_CTX_new(), &EVP_MD_CTX_free); |
| 205 | EVP_DigestInit(ctx.get(), EVP_md5()); |
| 206 | EVP_DigestUpdate( |
| 207 | ctx.get(), |
| 208 | reinterpret_cast<const std::uint8_t*>(in.data()), |
| 209 | in.size() |
| 210 | ); |
| 211 | |
| 212 | std::array<std::uint8_t, 16> digest{{}}; |
| 213 | EVP_DigestFinal(ctx.get(), digest.data(), NULL); |
| 214 | return epee::string_tools::pod_to_hex(digest); |
| 215 | } |
| 216 | |
| 217 | std::string get_a1(const http::login& user, const fields& src) |
| 218 | { |
| 219 | const std::string& realm = src.at(u8"realm"); |
| 220 | return boost::join( |
| 221 | std::vector<std::string>{user.username, realm, std::string(user.password.data(), user.password.size())}, u8":" |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | std::string get_a1(const http::login& user, const auth_responses& responses) |
| 226 | { |
| 227 | return get_a1(user, responses.at(0)); |
| 228 | } |
| 229 | |
| 230 | std::string get_a1_sess(const http::login& user, const std::string& cnonce, const auth_responses& responses) |
| 231 | { |
| 232 | const std::string& nonce = responses.at(0).at(u8"nonce"); |
| 233 | return boost::join( |
| 234 | std::vector<std::string>{md5_hex(get_a1(user, responses)), nonce, cnonce}, u8":" |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | std::string get_a2(const std::string& uri) |
| 239 | { |
| 240 | return boost::join(std::vector<std::string>{"NOP", uri}, u8":"); |
| 241 | } |
| 242 | |
| 243 | std::string get_nc(std::uint32_t count) |
| 244 | { |
| 245 | namespace karma = boost::spirit::karma; |
| 246 | std::string out; |
| 247 | karma::generate( |
| 248 | std::back_inserter(out), |
| 249 | karma::right_align(8, '0')[karma::uint_generator<std::uint32_t, 16>{}], |
| 250 | count |
| 251 | ); |
| 252 | |
| 253 | return out; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | TEST(HTTP_Server_Auth, NotRequired) |
| 258 | { |
| 259 | http::http_server_auth auth{}; // no rng here |
| 260 | EXPECT_FALSE(auth.get_response(http::http_request_info{})); |
| 261 | } |
| 262 | |
| 263 | TEST(HTTP_Server_Auth, MissingAuth) |
| 264 | { |
| 265 | http::http_server_auth auth{{"foo", "bar"}, rng}; |
| 266 | EXPECT_TRUE(bool(auth.get_response(http::http_request_info{}))); |
| 267 | { |
| 268 | http::http_request_info request{}; |
| 269 | request.m_header_info.m_etc_fields.push_back({"\xFF", "\xFF"}); |
| 270 | EXPECT_TRUE(bool(auth.get_response(request))); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | TEST(HTTP_Server_Auth, BadSyntax) |
| 275 | { |
| 276 | http::http_server_auth auth{{"foo", "bar"}, rng}; |
| 277 | EXPECT_TRUE(bool(auth.get_response(make_request({{u8"algorithm", "fo\xFF"}})))); |
| 278 | EXPECT_TRUE(bool(auth.get_response(make_request({{u8"cnonce", "\"000\xFF\""}})))); |
| 279 | EXPECT_TRUE(bool(auth.get_response(make_request({{u8"cnonce \xFF =", "\"000\xFF\""}})))); |
| 280 | EXPECT_TRUE(bool(auth.get_response(make_request({{u8" \xFF cnonce", "\"000\xFF\""}})))); |
| 281 | } |
| 282 | |
| 283 | TEST(HTTP_Server_Auth, MD5) |
| 284 | { |
| 285 | http::login user{"foo", "bar"}; |
| 286 | http::http_server_auth auth{user, rng}; |
| 287 | |
| 288 | const auto response = auth.get_response(make_request(fields{})); |
| 289 | ASSERT_TRUE(bool(response)); |
| 290 | EXPECT_TRUE(is_unauthorized(*response)); |
| 291 | |
| 292 | const auto fields = parse_response(*response); |
| 293 | ASSERT_LE(2u, fields.size()); |
| 294 | EXPECT_TRUE(has_same_fields(fields)); |
| 295 | |
| 296 | const std::string& nonce = fields[0].at(u8"nonce"); |
| 297 | EXPECT_EQ(24, nonce.size()); |
| 298 | |
| 299 | const std::string uri{"/some_foo_thing"}; |
| 300 | |
| 301 | const std::string a1 = get_a1(user, fields); |
| 302 | const std::string a2 = get_a2(uri); |
| 303 | |
| 304 | const std::string auth_code = md5_hex( |
| 305 | boost::join(std::vector<std::string>{md5_hex(a1), nonce, md5_hex(a2)}, u8":") |
| 306 | ); |
| 307 | |
| 308 | const auto request = make_request({ |
| 309 | {u8"nonce", quoted(nonce)}, |
| 310 | {u8"realm", quoted(fields[0].at(u8"realm"))}, |
| 311 | {u8"response", quoted(auth_code)}, |
| 312 | {u8"uri", quoted(uri)}, |
| 313 | {u8"username", quoted(user.username)} |
| 314 | }); |
| 315 | |
| 316 | EXPECT_FALSE(bool(auth.get_response(request))); |
| 317 | |
| 318 | const auto response2 = auth.get_response(request); |
| 319 | ASSERT_TRUE(bool(response2)); |
| 320 | EXPECT_TRUE(is_unauthorized(*response2)); |
| 321 | |
| 322 | const auto fields2 = parse_response(*response2); |
| 323 | ASSERT_LE(2u, fields2.size()); |
| 324 | EXPECT_TRUE(has_same_fields(fields2)); |
| 325 | |
| 326 | EXPECT_NE(nonce, fields2[0].at(u8"nonce")); |
| 327 | EXPECT_STREQ(u8"true", fields2[0].at(u8"stale").c_str()); |
| 328 | } |
| 329 | |
| 330 | TEST(HTTP_Server_Auth, MD5_sess) |
| 331 | { |
| 332 | constexpr const char cnonce[] = "not a good cnonce"; |
| 333 | |
| 334 | http::login user{"foo", "bar"}; |
| 335 | http::http_server_auth auth{user, rng}; |
| 336 | |
| 337 | const auto response = auth.get_response(make_request(fields{})); |
| 338 | ASSERT_TRUE(bool(response)); |
| 339 | EXPECT_TRUE(is_unauthorized(*response)); |
| 340 | |
| 341 | const auto fields = parse_response(*response); |
| 342 | ASSERT_LE(2u, fields.size()); |
| 343 | EXPECT_TRUE(has_same_fields(fields)); |
| 344 | |
| 345 | const std::string& nonce = fields[0].at(u8"nonce"); |
| 346 | EXPECT_EQ(24, nonce.size()); |
| 347 | |
| 348 | const std::string uri{"/some_foo_thing"}; |
| 349 | |
| 350 | const std::string a1 = get_a1_sess(user, cnonce, fields); |
| 351 | const std::string a2 = get_a2(uri); |
| 352 | |
| 353 | const std::string auth_code = md5_hex( |
| 354 | boost::join(std::vector<std::string>{md5_hex(a1), nonce, md5_hex(a2)}, u8":") |
| 355 | ); |
| 356 | |
| 357 | const auto request = make_request({ |
| 358 | {u8"algorithm", u8"md5-sess"}, |
| 359 | {u8"cnonce", quoted(cnonce)}, |
| 360 | {u8"nonce", quoted(nonce)}, |
| 361 | {u8"realm", quoted(fields[0].at(u8"realm"))}, |
| 362 | {u8"response", quoted(auth_code)}, |
| 363 | {u8"uri", quoted(uri)}, |
| 364 | {u8"username", quoted(user.username)} |
| 365 | }); |
| 366 | |
| 367 | EXPECT_FALSE(bool(auth.get_response(request))); |
| 368 | |
| 369 | const auto response2 = auth.get_response(request); |
| 370 | ASSERT_TRUE(bool(response2)); |
| 371 | EXPECT_TRUE(is_unauthorized(*response2)); |
| 372 | |
| 373 | const auto fields2 = parse_response(*response2); |
| 374 | ASSERT_LE(2u, fields2.size()); |
| 375 | EXPECT_TRUE(has_same_fields(fields2)); |
| 376 | |
| 377 | EXPECT_NE(nonce, fields2[0].at(u8"nonce")); |
| 378 | EXPECT_STREQ(u8"true", fields2[0].at(u8"stale").c_str()); |
| 379 | } |
| 380 | |
| 381 | TEST(HTTP_Server_Auth, MD5_auth) |
| 382 | { |
| 383 | constexpr const char cnonce[] = "not a nonce"; |
| 384 | constexpr const char qop[] = "auth"; |
| 385 | |
| 386 | http::login user{"foo", "bar"}; |
| 387 | http::http_server_auth auth{user, rng}; |
| 388 | |
| 389 | const auto response = auth.get_response(make_request(fields{})); |
| 390 | ASSERT_TRUE(bool(response)); |
| 391 | EXPECT_TRUE(is_unauthorized(*response)); |
| 392 | |
| 393 | const auto parsed = parse_response(*response); |
| 394 | ASSERT_LE(2u, parsed.size()); |
| 395 | EXPECT_TRUE(has_same_fields(parsed)); |
| 396 | |
| 397 | const std::string& nonce = parsed[0].at(u8"nonce"); |
| 398 | EXPECT_EQ(24, nonce.size()); |
| 399 | |
| 400 | const std::string uri{"/some_foo_thing"}; |
| 401 | |
| 402 | const std::string a1 = get_a1(user, parsed); |
| 403 | const std::string a2 = get_a2(uri); |
| 404 | std::string nc = get_nc(1); |
| 405 | |
| 406 | const auto generate_auth = [&] { |
| 407 | return md5_hex( |
| 408 | boost::join( |
| 409 | std::vector<std::string>{md5_hex(a1), nonce, nc, cnonce, qop, md5_hex(a2)}, u8":" |
| 410 | ) |
| 411 | ); |
| 412 | }; |
| 413 | |
| 414 | fields args{ |
| 415 | {u8"algorithm", quoted(u8"md5")}, |
| 416 | {u8"cnonce", quoted(cnonce)}, |
| 417 | {u8"nc", nc}, |
| 418 | {u8"nonce", quoted(nonce)}, |
| 419 | {u8"qop", quoted(qop)}, |
| 420 | {u8"realm", quoted(parsed[0].at(u8"realm"))}, |
| 421 | {u8"response", quoted(generate_auth())}, |
| 422 | {u8"uri", quoted(uri)}, |
| 423 | {u8"username", quoted(user.username)} |
| 424 | }; |
| 425 | |
| 426 | const auto request = make_request(args); |
| 427 | EXPECT_FALSE(bool(auth.get_response(request))); |
| 428 | |
| 429 | for (unsigned i = 2; i < 20; ++i) |
| 430 | { |
| 431 | nc = get_nc(i); |
| 432 | args.at(u8"nc") = nc; |
| 433 | args.at(u8"response") = quoted(generate_auth()); |
| 434 | EXPECT_FALSE(auth.get_response(make_request(args))); |
| 435 | } |
| 436 | |
| 437 | const auto replay = auth.get_response(request); |
| 438 | ASSERT_TRUE(bool(replay)); |
| 439 | EXPECT_TRUE(is_unauthorized(*replay)); |
| 440 | |
| 441 | const auto parsed_replay = parse_response(*replay); |
| 442 | ASSERT_LE(2u, parsed_replay.size()); |
| 443 | EXPECT_TRUE(has_same_fields(parsed_replay)); |
| 444 | |
| 445 | EXPECT_NE(nonce, parsed_replay[0].at(u8"nonce")); |
| 446 | EXPECT_STREQ(u8"true", parsed_replay[0].at(u8"stale").c_str()); |
| 447 | } |
| 448 | |
| 449 | TEST(HTTP_Server_Auth, MD5_sess_auth) |
| 450 | { |
| 451 | constexpr const char cnonce[] = "not a nonce"; |
| 452 | constexpr const char qop[] = "auth"; |
| 453 | |
| 454 | http::login user{"foo", "bar"}; |
| 455 | http::http_server_auth auth{user, rng}; |
| 456 | |
| 457 | const auto response = auth.get_response(make_request(fields{})); |
| 458 | ASSERT_TRUE(bool(response)); |
| 459 | EXPECT_TRUE(is_unauthorized(*response)); |
| 460 | |
| 461 | const auto parsed = parse_response(*response); |
| 462 | ASSERT_LE(2u, parsed.size()); |
| 463 | EXPECT_TRUE(has_same_fields(parsed)); |
| 464 | |
| 465 | const std::string& nonce = parsed[0].at(u8"nonce"); |
| 466 | EXPECT_EQ(24, nonce.size()); |
| 467 | |
| 468 | const std::string uri{"/some_foo_thing"}; |
| 469 | |
| 470 | const std::string a1 = get_a1_sess(user, cnonce, parsed); |
| 471 | const std::string a2 = get_a2(uri); |
| 472 | std::string nc = get_nc(1); |
| 473 | |
| 474 | const auto generate_auth = [&] { |
| 475 | return md5_hex( |
| 476 | boost::join( |
| 477 | std::vector<std::string>{md5_hex(a1), nonce, nc, cnonce, qop, md5_hex(a2)}, u8":" |
| 478 | ) |
| 479 | ); |
| 480 | }; |
| 481 | |
| 482 | fields args{ |
| 483 | {u8"algorithm", u8"md5-sess"}, |
| 484 | {u8"cnonce", quoted(cnonce)}, |
| 485 | {u8"nc", nc}, |
| 486 | {u8"nonce", quoted(nonce)}, |
| 487 | {u8"qop", qop}, |
| 488 | {u8"realm", quoted(parsed[0].at(u8"realm"))}, |
| 489 | {u8"response", quoted(generate_auth())}, |
| 490 | {u8"uri", quoted(uri)}, |
| 491 | {u8"username", quoted(user.username)} |
| 492 | }; |
| 493 | |
| 494 | const auto request = make_request(args); |
| 495 | EXPECT_FALSE(bool(auth.get_response(request))); |
| 496 | |
| 497 | for (unsigned i = 2; i < 20; ++i) |
| 498 | { |
| 499 | nc = get_nc(i); |
| 500 | args.at(u8"nc") = nc; |
| 501 | args.at(u8"response") = quoted(generate_auth()); |
| 502 | EXPECT_FALSE(auth.get_response(make_request(args))); |
| 503 | } |
| 504 | |
| 505 | const auto replay = auth.get_response(request); |
| 506 | ASSERT_TRUE(bool(replay)); |
| 507 | EXPECT_TRUE(is_unauthorized(*replay)); |
| 508 | |
| 509 | const auto parsed_replay = parse_response(*replay); |
| 510 | ASSERT_LE(2u, parsed_replay.size()); |
| 511 | EXPECT_TRUE(has_same_fields(parsed_replay)); |
| 512 | |
| 513 | EXPECT_NE(nonce, parsed_replay[0].at(u8"nonce")); |
| 514 | EXPECT_STREQ(u8"true", parsed_replay[0].at(u8"stale").c_str()); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | TEST(HTTP_Auth, DogFood) |
| 519 | { |
| 520 | const auto add_auth_field = [] (http::http_request_info& request, http::http_client_auth& client) |
| 521 | { |
| 522 | auto field = client.get_auth_field(request.m_http_method_str, request.m_URI); |
| 523 | EXPECT_TRUE(bool(field)); |
| 524 | if (!field) |
| 525 | return false; |
| 526 | request.m_header_info.m_etc_fields.push_back(std::move(*field)); |
| 527 | return true; |
| 528 | }; |
| 529 | |
| 530 | const http::login user{"some_user", "ultimate password"}; |
| 531 | |
| 532 | http::http_server_auth server{user, rng}; |
| 533 | http::http_client_auth client{user}; |
| 534 | |
| 535 | http::http_request_info request{}; |
| 536 | request.m_http_method_str = "GET"; |
| 537 | request.m_URI = "/FOO"; |
| 538 | |
| 539 | auto response = server.get_response(request); |
| 540 | ASSERT_TRUE(bool(response)); |
| 541 | EXPECT_TRUE(is_unauthorized(*response)); |
| 542 | EXPECT_TRUE(response->m_header_info.m_etc_fields.empty()); |
| 543 | response->m_header_info.m_etc_fields = response->m_additional_fields; |
| 544 | |
| 545 | EXPECT_EQ(http::http_client_auth::kSuccess, client.handle_401(*response)); |
| 546 | EXPECT_TRUE(add_auth_field(request, client)); |
| 547 | EXPECT_FALSE(bool(server.get_response(request))); |
| 548 | |
| 549 | for (unsigned i = 0; i < 1000; ++i) |
| 550 | { |
| 551 | request.m_http_method_str += std::to_string(i); |
| 552 | request.m_header_info.m_etc_fields.clear(); |
| 553 | EXPECT_TRUE(add_auth_field(request, client)); |
| 554 | EXPECT_FALSE(bool(server.get_response(request))); |
| 555 | } |
| 556 | |
| 557 | // resetting counter should be rejected by server |
| 558 | request.m_header_info.m_etc_fields.clear(); |
| 559 | client = http::http_client_auth{user}; |
| 560 | EXPECT_EQ(http::http_client_auth::kSuccess, client.handle_401(*response)); |
| 561 | EXPECT_TRUE(add_auth_field(request, client)); |
| 562 | |
| 563 | auto response2 = server.get_response(request); |
| 564 | ASSERT_TRUE(bool(response2)); |
| 565 | EXPECT_TRUE(is_unauthorized(*response2)); |
| 566 | EXPECT_TRUE(response2->m_header_info.m_etc_fields.empty()); |
| 567 | response2->m_header_info.m_etc_fields = response2->m_additional_fields; |
| 568 | |
| 569 | const auth_responses parsed1 = parse_response(*response); |
| 570 | const auth_responses parsed2 = parse_response(*response2); |
| 571 | ASSERT_LE(1u, parsed1.size()); |
| 572 | ASSERT_LE(1u, parsed2.size()); |
| 573 | EXPECT_NE(parsed1[0].at(u8"nonce"), parsed2[0].at(u8"nonce")); |
| 574 | |
| 575 | // with stale=true client should reset |
| 576 | request.m_header_info.m_etc_fields.clear(); |
| 577 | EXPECT_EQ(http::http_client_auth::kSuccess, client.handle_401(*response2)); |
| 578 | EXPECT_TRUE(add_auth_field(request, client)); |
| 579 | EXPECT_FALSE(bool(server.get_response(request))); |
| 580 | |
| 581 | // client should give up if stale=false |
| 582 | EXPECT_EQ(http::http_client_auth::kBadPassword, client.handle_401(*response)); |
| 583 | } |
| 584 | |
| 585 | TEST(HTTP_Client_Auth, Unavailable) |
| 586 | { |
| 587 | http::http_client_auth auth{}; |
| 588 | EXPECT_EQ(http::http_client_auth::kBadPassword, auth.handle_401(http::http_response_info{})); |
| 589 | EXPECT_FALSE(bool(auth.get_auth_field("GET", "/file"))); |
| 590 | } |
| 591 | |
| 592 | TEST(HTTP_Client_Auth, MissingAuthenticate) |
| 593 | { |
| 594 | http::http_client_auth auth{{"foo", "bar"}}; |
| 595 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(http::http_response_info{})); |
| 596 | EXPECT_FALSE(bool(auth.get_auth_field("POST", "/\xFFname"))); |
| 597 | { |
| 598 | http::http_response_info response{}; |
| 599 | response.m_additional_fields.push_back({"\xFF", "\xFF"}); |
| 600 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(response)); |
| 601 | } |
| 602 | EXPECT_FALSE(bool(auth.get_auth_field("DELETE", "/file/does/not/exist"))); |
| 603 | } |
| 604 | |
| 605 | TEST(HTTP_Client_Auth, BadSyntax) |
| 606 | { |
| 607 | http::http_client_auth auth{{"foo", "bar"}}; |
| 608 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(make_response({{{u8"realm", "fo\xFF"}}}))); |
| 609 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(make_response({{{u8"domain", "fo\xFF"}}}))); |
| 610 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(make_response({{{u8"nonce", "fo\xFF"}}}))); |
| 611 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(make_response({{{u8"nonce \xFF =", "fo\xFF"}}}))); |
| 612 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(make_response({{{u8" \xFF nonce", "fo\xFF"}}}))); |
| 613 | } |
| 614 | |
| 615 | TEST(HTTP_Client_Auth, MD5) |
| 616 | { |
| 617 | constexpr char nonce[] = "some crazy nonce"; |
| 618 | constexpr char realm[] = "the only realm"; |
| 619 | |
| 620 | const http::login user{"foo", "bar"}; |
| 621 | http::http_client_auth auth{user}; |
| 622 | |
| 623 | auto response = make_response({ |
| 624 | { |
| 625 | {u8"domain", quoted("ignored")}, |
| 626 | {u8"nonce", quoted(nonce)}, |
| 627 | {u8"REALM", quoted(realm)} |
| 628 | }, |
| 629 | { |
| 630 | {u8"algorithm", "null"}, |
| 631 | {u8"domain", quoted("ignored")}, |
| 632 | {u8"nonce", quoted(std::string{"e"} + nonce)}, |
| 633 | {u8"realm", quoted(std::string{"e"} + realm)} |
| 634 | }, |
| 635 | }); |
| 636 | |
| 637 | EXPECT_EQ(http::http_client_auth::kParseFailure, auth.handle_401(response)); |
| 638 | } |
| 639 | |
| 640 | TEST(HTTP_Client_Auth, MD5_auth) |
| 641 | { |
| 642 | constexpr char method[] = "NOP"; |
| 643 | constexpr char nonce[] = "some crazy nonce"; |
| 644 | constexpr char opaque[] = "this is the opaque"; |
| 645 | constexpr char qop[] = u8"ignore,auth,ignore"; |
| 646 | constexpr char realm[] = "the only realm"; |
| 647 | constexpr char uri[] = "/some_file"; |
| 648 | |
| 649 | const http::login user{"foo", "bar"}; |
| 650 | http::http_client_auth auth{user}; |
| 651 | |
| 652 | auto response = make_response({ |
| 653 | { |
| 654 | {u8"algorithm", u8"MD5"}, |
| 655 | {u8"domain", quoted("ignored")}, |
| 656 | {u8"nonce", quoted(std::string{"e"} + nonce)}, |
| 657 | {u8"realm", quoted(std::string{"e"} + realm)}, |
| 658 | {u8"qop", quoted("some,thing,to,ignore")} |
| 659 | }, |
| 660 | { |
| 661 | {u8"algorIthm", quoted(u8"md5")}, |
| 662 | {u8"domain", quoted("ignored")}, |
| 663 | {u8"noNce", quoted(nonce)}, |
| 664 | {u8"opaque", quoted(opaque)}, |
| 665 | {u8"realm", quoted(realm)}, |
| 666 | {u8"QoP", quoted(qop)} |
| 667 | } |
| 668 | }); |
| 669 | |
| 670 | EXPECT_EQ(http::http_client_auth::kSuccess, auth.handle_401(response)); |
| 671 | |
| 672 | for (unsigned i = 1; i < 1000; ++i) |
| 673 | { |
| 674 | const std::string nc = get_nc(i); |
| 675 | |
| 676 | const auto auth_field = auth.get_auth_field(method, uri); |
| 677 | ASSERT_TRUE(bool(auth_field)); |
| 678 | |
| 679 | const auto parsed = parse_fields(auth_field->second); |
| 680 | EXPECT_STREQ(u8"Authorization", auth_field->first.c_str()); |
| 681 | EXPECT_STREQ(u8"MD5", parsed.at(u8"algorithm").c_str()); |
| 682 | EXPECT_STREQ(nonce, parsed.at(u8"nonce").c_str()); |
| 683 | EXPECT_STREQ(opaque, parsed.at(u8"opaque").c_str()); |
| 684 | EXPECT_STREQ(u8"auth", parsed.at(u8"qop").c_str()); |
| 685 | EXPECT_STREQ(uri, parsed.at(u8"uri").c_str()); |
| 686 | EXPECT_EQ(user.username, parsed.at(u8"username")); |
| 687 | EXPECT_STREQ(realm, parsed.at(u8"realm").c_str()); |
| 688 | EXPECT_EQ(nc, parsed.at(u8"nc")); |
| 689 | |
| 690 | const std::string a1 = get_a1(user, parsed); |
| 691 | const std::string a2 = get_a2(uri); |
| 692 | const std::string auth_code = md5_hex( |
| 693 | boost::join(std::vector<std::string>{md5_hex(a1), nonce, nc, parsed.at(u8"cnonce"), u8"auth", md5_hex(a2)}, u8":") |
| 694 | ); |
| 695 | EXPECT_TRUE(boost::iequals(auth_code, parsed.at(u8"response"))); |
| 696 | } |
| 697 | |
| 698 | EXPECT_EQ(http::http_client_auth::kBadPassword, auth.handle_401(response)); |
| 699 | response.m_header_info.m_etc_fields.back().second.append(u8"," + write_fields({{u8"stale", u8"trUe"}})); |
| 700 | EXPECT_EQ(http::http_client_auth::kSuccess, auth.handle_401(response)); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | TEST(HTTP, Add_Field) |
| 705 | { |
| 706 | std::string str{"leading text"}; |
| 707 | epee::net_utils::http::add_field(str, "foo", "bar"); |
| 708 | epee::net_utils::http::add_field(str, std::string("bar"), std::string("foo")); |
| 709 | epee::net_utils::http::add_field(str, {"moarbars", "moarfoo"}); |
| 710 | |
| 711 | EXPECT_STREQ("leading textfoo: bar\r\nbar: foo\r\nmoarbars: moarfoo\r\n", str.c_str()); |
| 712 | } |