| 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 | #include "net/http_auth.h" |
| 29 | |
| 30 | #include <array> |
| 31 | #include <boost/algorithm/string/find_iterator.hpp> |
| 32 | #include <boost/algorithm/string/predicate.hpp> |
| 33 | #include <boost/fusion/adapted/std_tuple.hpp> |
| 34 | #include <boost/fusion/algorithm/iteration/for_each.hpp> |
| 35 | #include <boost/fusion/algorithm/iteration/iter_fold.hpp> |
| 36 | #include <boost/fusion/algorithm/query/any.hpp> |
| 37 | #include <boost/fusion/iterator/distance.hpp> |
| 38 | #include <boost/fusion/iterator/value_of.hpp> |
| 39 | #include <boost/fusion/sequence/intrinsic/begin.hpp> |
| 40 | #include <boost/fusion/sequence/intrinsic/size.hpp> |
| 41 | #include <boost/range/algorithm/copy.hpp> |
| 42 | #include <boost/range/algorithm/find_if.hpp> |
| 43 | #include <boost/range/iterator_range_core.hpp> |
| 44 | #include <boost/range/join.hpp> |
| 45 | #include <boost/spirit/include/karma_generate.hpp> |
| 46 | #include <boost/spirit/include/karma_uint.hpp> |
| 47 | #include <boost/spirit/include/qi_alternative.hpp> |
| 48 | #include <boost/spirit/include/qi_and_predicate.hpp> |
| 49 | #include <boost/spirit/include/qi_char.hpp> |
| 50 | #include <boost/spirit/include/qi_char_class.hpp> |
| 51 | #include <boost/spirit/include/qi_difference.hpp> |
| 52 | #include <boost/spirit/include/qi_kleene.hpp> |
| 53 | #include <boost/spirit/include/qi_parse.hpp> |
| 54 | #include <boost/spirit/include/qi_plus.hpp> |
| 55 | #include <boost/spirit/include/qi_no_case.hpp> |
| 56 | #include <boost/spirit/include/qi_not_predicate.hpp> |
| 57 | #include <boost/spirit/include/qi_raw.hpp> |
| 58 | #include <boost/spirit/include/qi_rule.hpp> |
| 59 | #include <boost/spirit/include/qi_sequence.hpp> |
| 60 | #include <boost/spirit/include/qi_string.hpp> |
| 61 | #include <boost/spirit/include/qi_symbols.hpp> |
| 62 | #include <boost/spirit/include/qi_uint.hpp> |
| 63 | #include <cassert> |
| 64 | #include <iterator> |
| 65 | #include <limits> |
| 66 | #include <openssl/evp.h> |
| 67 | #include <openssl/rand.h> |
| 68 | #include <tuple> |
| 69 | #include <type_traits> |
| 70 | |
| 71 | #include "hex.h" |
| 72 | #include "string_coding.h" |
| 73 | |
| 74 | /* This file uses the `u8` prefix and specifies all chars by ASCII numeric |
| 75 | value. This is for maximum portability - C++ does not actually specify ASCII |
| 76 | as the encoding type for unprefixed string literals, etc. Although rare, the |
| 77 | effort required to support rare compiler encoding types is low. |
| 78 | |
| 79 | Also be careful of qi::ascii character classes (`qi::asci::alpha`, etc.) - |
| 80 | non-ASCII characters will cause undefined behavior in the table lookup until |
| 81 | boost 1.60. The expression `&qi::ascii::char_` will fail on non-ASCII |
| 82 | characters without "consuming" the input character. */ |
| 83 | |
| 84 | namespace |
| 85 | { |
| 86 | namespace http = epee::net_utils::http; |
| 87 | |
| 88 | // string_ref is only constexpr if length is given |
| 89 | template<std::size_t N> |
| 90 | constexpr boost::string_ref ceref(const char (&arg)[N]) |
| 91 | { |
| 92 | return boost::string_ref(arg, N - 1); |
| 93 | } |
| 94 | |
| 95 | constexpr const auto client_auth_field = ceref(u8"Authorization"); |
| 96 | constexpr const auto server_auth_field = ceref(u8"WWW-authenticate"); |
| 97 | constexpr const auto auth_realm = ceref(u8"monero-rpc"); |
| 98 | constexpr const char comma = 44; |
| 99 | constexpr const char equal_sign = 61; |
| 100 | constexpr const char quote = 34; |
| 101 | constexpr const char zero = 48; |
| 102 | constexpr const auto sess_algo = ceref(u8"-sess"); |
| 103 | |
| 104 | constexpr const unsigned client_reserve_size = 512; //!< std::string::reserve size for clients |
| 105 | |
| 106 | //// Digest Algorithms |
| 107 | |
| 108 | struct md5_ |
| 109 | { |
| 110 | static constexpr const boost::string_ref name = ceref(u8"MD5"); |
| 111 | |
| 112 | struct update |
| 113 | { |
| 114 | template<typename T> |
| 115 | void operator()(const T& arg) const |
| 116 | { |
| 117 | const boost::iterator_range<const char*> data(boost::as_literal(arg)); |
| 118 | EVP_DigestUpdate( |
| 119 | ctx, |
| 120 | reinterpret_cast<const std::uint8_t*>(data.begin()), |
| 121 | data.size() |
| 122 | ); |
| 123 | } |
| 124 | void operator()(const std::string& arg) const |
| 125 | { |
| 126 | (*this)(boost::string_ref(arg)); |
| 127 | } |
| 128 | void operator()(const epee::wipeable_string& arg) const |
| 129 | { |
| 130 | EVP_DigestUpdate( |
| 131 | ctx, |
| 132 | reinterpret_cast<const std::uint8_t*>(arg.data()), |
| 133 | arg.size() |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | EVP_MD_CTX *ctx; |
| 138 | }; |
| 139 | |
| 140 | template<typename... T> |
| 141 | std::array<char, 32> operator()(const T&... args) const |
| 142 | { |
| 143 | std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> ctx(EVP_MD_CTX_new(), &EVP_MD_CTX_free); |
| 144 | EVP_DigestInit(ctx.get(), EVP_md5()); |
| 145 | boost::fusion::for_each(std::tie(args...), update{ctx.get()}); |
| 146 | |
| 147 | std::array<std::uint8_t, 16> digest{{}}; |
| 148 | EVP_DigestFinal(ctx.get(), digest.data(), NULL); |
| 149 | return epee::to_hex::array(digest); |
| 150 | } |
| 151 | }; |
| 152 | constexpr const boost::string_ref md5_::name; |
| 153 | |
| 154 | //! Digest Algorithms available for HTTP Digest Auth. Sort better algos to the left |
| 155 | constexpr const std::tuple<md5_> digest_algorithms{}; |
| 156 | |
| 157 | //// Various String Utilities |
| 158 | |
| 159 | struct ascii_tolower_ |
| 160 | { |
| 161 | template<typename Char> |
| 162 | constexpr Char operator()(Char value) const noexcept |
| 163 | { |
| 164 | static_assert(std::is_integral<Char>::value, "only integral types supported"); |
| 165 | return (65 <= value && value <= 90) ? (value + 32) : value; |
| 166 | } |
| 167 | }; |
| 168 | constexpr const ascii_tolower_ ascii_tolower{}; |
| 169 | |
| 170 | struct ascii_iequal_ |
| 171 | { |
| 172 | template<typename Char> |
| 173 | constexpr bool operator()(Char left, Char right) const noexcept |
| 174 | { |
| 175 | return ascii_tolower(left) == ascii_tolower(right); |
| 176 | } |
| 177 | }; |
| 178 | constexpr const ascii_iequal_ ascii_iequal{}; |
| 179 | |
| 180 | struct http_list_separator_ |
| 181 | { |
| 182 | template<typename Char> |
| 183 | bool operator()(Char value) const noexcept |
| 184 | { |
| 185 | static_assert(std::is_integral<Char>::value, "only integral types supported"); |
| 186 | return boost::spirit::char_encoding::ascii::isascii_(value) && |
| 187 | (value == comma || boost::spirit::char_encoding::ascii::isspace(value)); |
| 188 | } |
| 189 | }; |
| 190 | constexpr const http_list_separator_ http_list_separator{}; |
| 191 | |
| 192 | std::string to_string(boost::iterator_range<const char*> source) |
| 193 | { |
| 194 | return {source.begin(), source.size()}; |
| 195 | } |
| 196 | |
| 197 | template<typename T> |
| 198 | void add_first_field(std::string& str, const char* const name, const T& value) |
| 199 | { |
| 200 | str.append(name); |
| 201 | str.push_back(equal_sign); |
| 202 | boost::copy(value, std::back_inserter(str)); |
| 203 | } |
| 204 | |
| 205 | template<typename T> |
| 206 | void add_field(std::string& str, const char* const name, const T& value) |
| 207 | { |
| 208 | str.push_back(comma); |
| 209 | add_first_field(str, name, value); |
| 210 | } |
| 211 | |
| 212 | template<typename T> |
| 213 | auto quoted_(const T& arg) // avoid ADL selecting C++14 std::quoted |
| 214 | { |
| 215 | return boost::range::join(boost::range::join(ceref(u8"\""), arg), ceref(u8"\"")); |
| 216 | } |
| 217 | |
| 218 | //// Digest Authentication |
| 219 | |
| 220 | template<typename Digest> |
| 221 | typename std::result_of<Digest()>::type generate_a1( |
| 222 | Digest digest, const http::login& creds, const boost::string_ref realm) |
| 223 | { |
| 224 | return digest(creds.username, u8":", realm, u8":", creds.password); |
| 225 | } |
| 226 | |
| 227 | template<typename Digest> |
| 228 | typename std::result_of<Digest()>::type generate_a1( |
| 229 | Digest digest, const http::http_client_auth::session& user) |
| 230 | { |
| 231 | return generate_a1(std::move(digest), user.credentials, user.server.realm); |
| 232 | } |
| 233 | |
| 234 | template<typename T> |
| 235 | void init_client_value(std::string& str, |
| 236 | const boost::string_ref algorithm, const http::http_client_auth::session& user, |
| 237 | const boost::string_ref uri, const T& response) |
| 238 | { |
| 239 | str.append(u8"Digest "); |
| 240 | add_first_field(str, u8"algorithm", algorithm); |
| 241 | add_field(str, u8"nonce", quoted_(user.server.nonce)); |
| 242 | add_field(str, u8"realm", quoted_(user.server.realm)); |
| 243 | add_field(str, u8"response", quoted_(response)); |
| 244 | add_field(str, u8"uri", quoted_(uri)); |
| 245 | add_field(str, u8"username", quoted_(user.credentials.username)); |
| 246 | if (!user.server.opaque.empty()) |
| 247 | add_field(str, u8"opaque", quoted_(user.server.opaque)); |
| 248 | } |
| 249 | |
| 250 | //! Implements superseded algorithm specified in RFC 2069 |
| 251 | template<typename Digest> |
| 252 | struct old_algorithm |
| 253 | { |
| 254 | explicit old_algorithm(Digest digest_) : digest(std::move(digest_)) {} |
| 255 | |
| 256 | std::string operator()(const http::http_client_auth::session& user, |
| 257 | const boost::string_ref method, const boost::string_ref uri) const |
| 258 | { |
| 259 | const auto response = digest( |
| 260 | generate_a1(digest, user), u8":", user.server.nonce, u8":", digest(method, u8":", uri) |
| 261 | ); |
| 262 | std::string out{}; |
| 263 | out.reserve(client_reserve_size); |
| 264 | init_client_value(out, Digest::name, user, uri, response); |
| 265 | return out; |
| 266 | } |
| 267 | private: |
| 268 | Digest digest; |
| 269 | }; |
| 270 | |
| 271 | //! Implements the `qop=auth` algorithm specified in RFC 2617 |
| 272 | template<typename Digest> |
| 273 | struct auth_algorithm |
| 274 | { |
| 275 | explicit auth_algorithm(Digest digest_) : digest(std::move(digest_)) {} |
| 276 | |
| 277 | std::string operator()(const http::http_client_auth::session& user, |
| 278 | const boost::string_ref method, const boost::string_ref uri) const |
| 279 | { |
| 280 | namespace karma = boost::spirit::karma; |
| 281 | using counter_type = decltype(user.counter); |
| 282 | static_assert( |
| 283 | std::numeric_limits<counter_type>::radix == 2, "unexpected radix for counter" |
| 284 | ); |
| 285 | static_assert( |
| 286 | std::numeric_limits<counter_type>::digits <= 32, |
| 287 | "number of digits will cause underflow on padding below" |
| 288 | ); |
| 289 | |
| 290 | std::string out{}; |
| 291 | out.reserve(client_reserve_size); |
| 292 | |
| 293 | karma::generate(std::back_inserter(out), karma::hex(user.counter)); |
| 294 | out.insert(out.begin(), 8 - out.size(), zero); // zero left pad |
| 295 | if (out.size() != 8) |
| 296 | return {}; |
| 297 | |
| 298 | std::array<char, 8> nc{{}}; |
| 299 | boost::copy(out, nc.data()); |
| 300 | |
| 301 | std::array<uint8_t, 16> rbuf{{}}; |
| 302 | if (RAND_bytes(rbuf.data(), rbuf.size()) != 1) |
| 303 | return {}; |
| 304 | |
| 305 | const std::string cnonce = epee::string_encoding::base64_encode(rbuf.data(), rbuf.size()); |
| 306 | const auto response = digest( |
| 307 | generate_a1(digest, user), u8":", user.server.nonce, u8":", nc, u8":", cnonce, u8":auth:", digest(method, u8":", uri) |
| 308 | ); |
| 309 | |
| 310 | out.clear(); |
| 311 | init_client_value(out, Digest::name, user, uri, response); |
| 312 | add_field(out, u8"qop", ceref(u8"auth")); |
| 313 | add_field(out, u8"nc", nc); |
| 314 | add_field(out, u8"cnonce", quoted_(cnonce)); |
| 315 | return out; |
| 316 | } |
| 317 | |
| 318 | private: |
| 319 | Digest digest; |
| 320 | }; |
| 321 | |
| 322 | //! Processes client "Authorization" and server "WWW-authenticate" HTTP fields |
| 323 | struct auth_message |
| 324 | { |
| 325 | using iterator = const char*; |
| 326 | enum status{ kFail = 0, kStale, kPass }; |
| 327 | |
| 328 | //! \return Status of the `response` field from the client |
| 329 | static status verify(const boost::string_ref method, const boost::string_ref request, |
| 330 | const http::http_server_auth::session& user) |
| 331 | { |
| 332 | const auto parsed = parse(request); |
| 333 | if (parsed && |
| 334 | boost::equals(parsed->username, user.credentials.username) && |
| 335 | boost::fusion::any(digest_algorithms, has_valid_response{*parsed, user, method})) |
| 336 | { |
| 337 | if (boost::equals(parsed->nonce, user.nonce)) |
| 338 | { |
| 339 | // RFC 2069 format does not verify nc value - allow just once |
| 340 | if (user.counter == 1 || (!parsed->qop.empty() && parsed->counter() == user.counter)) |
| 341 | { |
| 342 | return kPass; |
| 343 | } |
| 344 | } |
| 345 | return kStale; |
| 346 | } |
| 347 | return kFail; |
| 348 | } |
| 349 | |
| 350 | //! \return Information needed to generate client authentication `response`s. |
| 351 | static http::http_client_auth::session::keys extract( |
| 352 | const http::http_response_info& response, const bool is_first) |
| 353 | { |
| 354 | using field = std::pair<std::string, std::string>; |
| 355 | |
| 356 | server_parameters best{}; |
| 357 | |
| 358 | const std::list<field>& fields = response.m_header_info.m_etc_fields; |
| 359 | auto current = fields.begin(); |
| 360 | const auto end = fields.end(); |
| 361 | while (true) |
| 362 | { |
| 363 | current = std::find_if(current, end, [] (const field& value) { |
| 364 | return boost::equals(server_auth_field, value.first, ascii_iequal); |
| 365 | }); |
| 366 | if (current == end) |
| 367 | break; |
| 368 | |
| 369 | const auto parsed = parse(current->second); |
| 370 | if (parsed) |
| 371 | { |
| 372 | server_parameters local_best = parsed->algorithm.empty() ? |
| 373 | server_parameters{*parsed, boost::fusion::find<md5_>(digest_algorithms)} : |
| 374 | boost::fusion::iter_fold(digest_algorithms, server_parameters{}, matches_algorithm{*parsed}); |
| 375 | |
| 376 | if (local_best.index < best.index) |
| 377 | best = std::move(local_best); |
| 378 | } |
| 379 | ++current; |
| 380 | } |
| 381 | if (is_first || boost::equals(best.stale, ceref(u8"true"), ascii_iequal)) |
| 382 | return best.take(); |
| 383 | return {}; // authentication failed with bad user/pass |
| 384 | } |
| 385 | |
| 386 | private: |
| 387 | explicit auth_message() |
| 388 | : algorithm() |
| 389 | , cnonce() |
| 390 | , nc() |
| 391 | , nonce() |
| 392 | , qop() |
| 393 | , realm() |
| 394 | , response() |
| 395 | , stale() |
| 396 | , uri() |
| 397 | , username() { |
| 398 | } |
| 399 | |
| 400 | static boost::optional<auth_message> parse(const boost::string_ref request) |
| 401 | { |
| 402 | struct parser |
| 403 | { |
| 404 | using field_parser = std::function<bool(const parser&, iterator&, iterator, auth_message&)>; |
| 405 | |
| 406 | explicit parser() : field_table(), skip_whitespace(), header(), quoted_string(), token(), fields() { |
| 407 | using namespace std::placeholders; |
| 408 | namespace qi = boost::spirit::qi; |
| 409 | |
| 410 | struct parse_nc |
| 411 | { |
| 412 | bool operator()(const parser&, iterator& current, const iterator end, auth_message& result) const |
| 413 | { |
| 414 | return qi::parse( |
| 415 | current, end, |
| 416 | (qi::raw[qi::uint_parser<std::uint32_t, 16, 8, 8>{}]), |
| 417 | result.nc |
| 418 | ); |
| 419 | } |
| 420 | }; |
| 421 | struct parse_token |
| 422 | { |
| 423 | bool operator()(const parser& parse, iterator& current, const iterator end, |
| 424 | boost::iterator_range<iterator>& result) const |
| 425 | { |
| 426 | return qi::parse(current, end, parse.token, result); |
| 427 | } |
| 428 | }; |
| 429 | struct parse_string |
| 430 | { |
| 431 | bool operator()(const parser& parse, iterator& current, const iterator end, |
| 432 | boost::iterator_range<iterator>& result) const |
| 433 | { |
| 434 | return qi::parse(current, end, parse.quoted_string, result); |
| 435 | } |
| 436 | bool operator()(const parser& parse, iterator& current, const iterator end) const |
| 437 | { |
| 438 | return qi::parse(current, end, parse.quoted_string); |
| 439 | } |
| 440 | }; |
| 441 | struct parse_response |
| 442 | { |
| 443 | bool operator()(const parser&, iterator& current, const iterator end, auth_message& result) const |
| 444 | { |
| 445 | using byte = qi::uint_parser<std::uint8_t, 16, 2, 2>; |
| 446 | return qi::parse( |
| 447 | current, end, |
| 448 | (qi::lit(quote) >> qi::raw[+(byte{})] >> qi::lit(quote)), |
| 449 | result.response |
| 450 | ); |
| 451 | } |
| 452 | }; |
| 453 | |
| 454 | field_table.add |
| 455 | (u8"algorithm", std::bind(parse_token{}, _1, _2, _3, std::bind(&auth_message::algorithm, _4))) |
| 456 | (u8"cnonce", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_message::cnonce, _4))) |
| 457 | (u8"domain", std::bind(parse_string{}, _1, _2, _3)) // ignore field |
| 458 | (u8"nc", parse_nc{}) |
| 459 | (u8"nonce", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_message::nonce, _4))) |
| 460 | (u8"opaque", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_message::opaque, _4))) |
| 461 | (u8"qop", std::bind(parse_token{}, _1, _2, _3, std::bind(&auth_message::qop, _4))) |
| 462 | (u8"realm", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_message::realm, _4))) |
| 463 | (u8"response", parse_response{}) |
| 464 | (u8"stale", std::bind(parse_token{}, _1, _2, _3, std::bind(&auth_message::stale, _4))) |
| 465 | (u8"uri", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_message::uri, _4))) |
| 466 | (u8"username", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_message::username, _4))); |
| 467 | |
| 468 | skip_whitespace = *(&qi::ascii::char_ >> qi::ascii::space); |
| 469 | header = skip_whitespace >> qi::ascii::no_case[u8"digest"] >> skip_whitespace; |
| 470 | quoted_string = (qi::lit(quote) >> qi::raw[+(u8"\\\"" | (qi::ascii::char_ - quote))] >> qi::lit(quote)); |
| 471 | token = |
| 472 | (!qi::lit(quote) >> qi::raw[+(&qi::ascii::char_ >> (qi::ascii::graph - qi::ascii::char_(u8"()<>@,;:\\\"/[]?={}")))]) | |
| 473 | quoted_string; |
| 474 | fields = field_table >> skip_whitespace >> equal_sign >> skip_whitespace; |
| 475 | } |
| 476 | |
| 477 | boost::optional<auth_message> operator()(const boost::string_ref request) const |
| 478 | { |
| 479 | namespace qi = boost::spirit::qi; |
| 480 | |
| 481 | iterator current = request.begin(); |
| 482 | const iterator end = request.end(); |
| 483 | |
| 484 | if (!qi::parse(current, end, header)) |
| 485 | { |
| 486 | return boost::none; |
| 487 | } |
| 488 | |
| 489 | auth_message info{}; |
| 490 | field_parser null_parser{}; |
| 491 | std::reference_wrapper<const field_parser> field = null_parser; |
| 492 | |
| 493 | do // require at least one field; require field after `,` character |
| 494 | { |
| 495 | if (!qi::parse(current, end, fields, field) || !field(*this, current, end, info)) |
| 496 | { |
| 497 | return boost::none; |
| 498 | } |
| 499 | qi::parse(current, end, skip_whitespace); |
| 500 | } while (qi::parse(current, end, qi::char_(comma) >> skip_whitespace)); |
| 501 | return boost::make_optional(current == end, info); |
| 502 | } |
| 503 | |
| 504 | private: |
| 505 | boost::spirit::qi::symbols< |
| 506 | char, field_parser, boost::spirit::qi::tst<char, field_parser>, ascii_tolower_ |
| 507 | > field_table; |
| 508 | boost::spirit::qi::rule<iterator> skip_whitespace; |
| 509 | boost::spirit::qi::rule<iterator> header; |
| 510 | boost::spirit::qi::rule<iterator, boost::iterator_range<iterator>()> quoted_string; |
| 511 | boost::spirit::qi::rule<iterator, boost::iterator_range<iterator>()> token; |
| 512 | boost::spirit::qi::rule<iterator, std::reference_wrapper<const field_parser>()> fields; |
| 513 | }; // parser |
| 514 | |
| 515 | static const parser parse_; |
| 516 | return parse_(request); |
| 517 | } |
| 518 | |
| 519 | struct has_valid_response |
| 520 | { |
| 521 | template<typename Digest, typename Result> |
| 522 | Result generate_old_response(Digest digest, const Result& key, const Result& auth) const |
| 523 | { |
| 524 | return digest(key, u8":", request.nonce, u8":", auth); |
| 525 | } |
| 526 | |
| 527 | template<typename Digest, typename Result> |
| 528 | Result generate_new_response(Digest digest, const Result& key, const Result& auth) const |
| 529 | { |
| 530 | return digest( |
| 531 | key, u8":", request.nonce, u8":", request.nc, u8":", request.cnonce, u8":", request.qop, u8":", auth |
| 532 | ); |
| 533 | } |
| 534 | |
| 535 | template<typename Result> |
| 536 | bool check(const Result& result) const |
| 537 | { |
| 538 | return boost::equals(request.response, result, ascii_iequal); |
| 539 | } |
| 540 | |
| 541 | template<typename Digest> |
| 542 | bool operator()(const Digest& digest) const |
| 543 | { |
| 544 | if (boost::starts_with(request.algorithm, Digest::name, ascii_iequal) || |
| 545 | (request.algorithm.empty() && std::is_same<md5_, Digest>::value)) |
| 546 | { |
| 547 | auto key = generate_a1(digest, user.credentials, auth_realm); |
| 548 | if (boost::ends_with(request.algorithm, sess_algo, ascii_iequal)) |
| 549 | { |
| 550 | key = digest(key, u8":", request.nonce, u8":", request.cnonce); |
| 551 | } |
| 552 | |
| 553 | auto auth = digest(method, u8":", request.uri); |
| 554 | if (request.qop.empty()) |
| 555 | { |
| 556 | return check(generate_old_response(std::move(digest), std::move(key), std::move(auth))); |
| 557 | } |
| 558 | else if (boost::equals(ceref(u8"auth"), request.qop, ascii_iequal)) |
| 559 | { |
| 560 | return check(generate_new_response(std::move(digest), std::move(key), std::move(auth))); |
| 561 | } |
| 562 | } |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | const auth_message& request; |
| 567 | const http::http_server_auth::session& user; |
| 568 | const boost::string_ref method; |
| 569 | }; |
| 570 | |
| 571 | boost::optional<std::uint32_t> counter() const |
| 572 | { |
| 573 | namespace qi = boost::spirit::qi; |
| 574 | using hex = qi::uint_parser<std::uint32_t, 16>; |
| 575 | std::uint32_t value = 0; |
| 576 | const bool converted = qi::parse(nc.begin(), nc.end(), hex{}, value); |
| 577 | return boost::make_optional(converted, value); |
| 578 | } |
| 579 | |
| 580 | struct server_parameters |
| 581 | { |
| 582 | server_parameters() |
| 583 | : nonce(), opaque(), realm(), stale(), value_generator() |
| 584 | , index(boost::fusion::size(digest_algorithms)) |
| 585 | {} |
| 586 | |
| 587 | template<typename DigestIter> |
| 588 | explicit server_parameters(const auth_message& request, const DigestIter& digest) |
| 589 | : nonce(request.nonce) |
| 590 | , opaque(request.opaque) |
| 591 | , realm(request.realm) |
| 592 | , stale(request.stale) |
| 593 | , value_generator() |
| 594 | , index(boost::fusion::distance(boost::fusion::begin(digest_algorithms), digest)) |
| 595 | { |
| 596 | using digest_type = typename boost::fusion::result_of::value_of<DigestIter>::type; |
| 597 | |
| 598 | // debug check internal state of the auth_message class |
| 599 | assert( |
| 600 | (std::is_same<digest_type, md5_>::value) || |
| 601 | boost::equals((*digest).name, request.algorithm, ascii_iequal) |
| 602 | ); |
| 603 | if (request.qop.empty()) |
| 604 | index = boost::fusion::size(digest_algorithms); |
| 605 | else |
| 606 | { |
| 607 | for (auto elem = boost::make_split_iterator(request.qop, boost::token_finder(http_list_separator)); |
| 608 | !elem.eof(); |
| 609 | ++elem) |
| 610 | { |
| 611 | if (boost::equals(ceref(u8"auth"), *elem, ascii_iequal)) |
| 612 | { |
| 613 | value_generator = auth_algorithm<digest_type>{*digest}; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | if (!value_generator) // no supported qop mode |
| 618 | index = boost::fusion::size(digest_algorithms); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | http::http_client_auth::session::keys take() |
| 623 | { |
| 624 | return {to_string(nonce), to_string(opaque), to_string(realm), std::move(value_generator)}; |
| 625 | } |
| 626 | |
| 627 | boost::iterator_range<iterator> nonce; |
| 628 | boost::iterator_range<iterator> opaque; |
| 629 | boost::iterator_range<iterator> realm; |
| 630 | boost::iterator_range<iterator> stale; |
| 631 | http::http_client_auth::session::keys::algorithm value_generator; |
| 632 | unsigned index; |
| 633 | }; |
| 634 | |
| 635 | struct matches_algorithm |
| 636 | { |
| 637 | template<typename DigestIter> |
| 638 | server_parameters operator()(server_parameters current, const DigestIter& digest) const |
| 639 | { |
| 640 | if (!current.value_generator) |
| 641 | { |
| 642 | if (boost::equals(response.algorithm, (*digest).name, ascii_iequal)) |
| 643 | { |
| 644 | current = server_parameters{response, digest}; |
| 645 | } |
| 646 | } |
| 647 | return current; |
| 648 | } |
| 649 | const auth_message& response; |
| 650 | }; |
| 651 | |
| 652 | |
| 653 | boost::iterator_range<iterator> algorithm; |
| 654 | boost::iterator_range<iterator> cnonce; |
| 655 | boost::iterator_range<iterator> nc; |
| 656 | boost::iterator_range<iterator> nonce; |
| 657 | boost::iterator_range<iterator> opaque; |
| 658 | boost::iterator_range<iterator> qop; |
| 659 | boost::iterator_range<iterator> realm; |
| 660 | boost::iterator_range<iterator> response; |
| 661 | boost::iterator_range<iterator> stale; |
| 662 | boost::iterator_range<iterator> uri; |
| 663 | boost::iterator_range<iterator> username; |
| 664 | }; // auth_message |
| 665 | |
| 666 | struct add_challenge |
| 667 | { |
| 668 | template<typename Digest> |
| 669 | void operator()(const Digest& digest) const |
| 670 | { |
| 671 | static constexpr const auto fvalue = ceref(u8"Digest qop=\"auth\""); |
| 672 | |
| 673 | for (unsigned i = 0; i < 2; ++i) |
| 674 | { |
| 675 | std::string out(fvalue); |
| 676 | |
| 677 | const auto algorithm = boost::range::join( |
| 678 | Digest::name, (i == 0 ? boost::string_ref{} : sess_algo) |
| 679 | ); |
| 680 | add_field(out, u8"algorithm", algorithm); |
| 681 | add_field(out, u8"realm", quoted_(auth_realm)); |
| 682 | add_field(out, u8"nonce", quoted_(nonce)); |
| 683 | add_field(out, u8"stale", is_stale ? ceref("true") : ceref("false")); |
| 684 | |
| 685 | fields.push_back(std::make_pair(std::string(server_auth_field), std::move(out))); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | const boost::string_ref nonce; |
| 690 | std::list<std::pair<std::string, std::string>>& fields; |
| 691 | const bool is_stale; |
| 692 | }; |
| 693 | |
| 694 | http::http_response_info create_digest_response(const boost::string_ref nonce, const bool is_stale) |
| 695 | { |
| 696 | epee::net_utils::http::http_response_info rc{}; |
| 697 | rc.m_response_code = 401; |
| 698 | rc.m_response_comment = u8"Unauthorized"; |
| 699 | rc.m_mime_tipe = u8"text/html"; |
| 700 | rc.m_body = |
| 701 | u8"<html><head><title>Unauthorized Access</title></head><body><h1>401 Unauthorized</h1></body></html>"; |
| 702 | |
| 703 | boost::fusion::for_each( |
| 704 | digest_algorithms, add_challenge{nonce, rc.m_additional_fields, is_stale} |
| 705 | ); |
| 706 | |
| 707 | return rc; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | namespace epee |
| 712 | { |
| 713 | namespace net_utils |
| 714 | { |
| 715 | namespace http |
| 716 | { |
| 717 | http_server_auth::http_server_auth(login credentials, std::function<void(size_t, uint8_t*)> r) |
| 718 | : user(session{std::move(credentials)}), rng(std::move(r)) { |
| 719 | } |
| 720 | |
| 721 | boost::optional<http_response_info> http_server_auth::do_get_response(const http_request_info& request) |
| 722 | { |
| 723 | assert(user); |
| 724 | using field = std::pair<std::string, std::string>; |
| 725 | |
| 726 | const std::list<field>& fields = request.m_header_info.m_etc_fields; |
| 727 | const auto auth = boost::find_if(fields, [] (const field& value) { |
| 728 | return boost::equals(client_auth_field, value.first, ascii_iequal); |
| 729 | }); |
| 730 | |
| 731 | bool is_stale = false; |
| 732 | if (auth != fields.end()) |
| 733 | { |
| 734 | ++(user->counter); |
| 735 | switch (auth_message::verify(request.m_http_method_str, auth->second, *user)) |
| 736 | { |
| 737 | case auth_message::kPass: |
| 738 | return boost::none; |
| 739 | |
| 740 | case auth_message::kStale: |
| 741 | is_stale = true; |
| 742 | break; |
| 743 | |
| 744 | default: |
| 745 | case auth_message::kFail: |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | user->counter = 0; |
| 750 | { |
| 751 | std::array<std::uint8_t, 16> rand_128bit{{}}; |
| 752 | rng(rand_128bit.size(), rand_128bit.data()); |
| 753 | user->nonce = string_encoding::base64_encode(rand_128bit.data(), rand_128bit.size()); |
| 754 | } |
| 755 | return create_digest_response(user->nonce, is_stale); |
| 756 | } |
| 757 | |
| 758 | http_client_auth::http_client_auth(login credentials) |
| 759 | : user(session{std::move(credentials)}) { |
| 760 | } |
| 761 | |
| 762 | http_client_auth::status http_client_auth::do_handle_401(const http_response_info& response) |
| 763 | { |
| 764 | assert(user); |
| 765 | const bool first_auth = (user->counter == 0); |
| 766 | user->server = auth_message::extract(response, first_auth); |
| 767 | if (user->server.generator) |
| 768 | { |
| 769 | user->counter = 0; |
| 770 | return kSuccess; |
| 771 | } |
| 772 | return first_auth ? kParseFailure : kBadPassword; |
| 773 | } |
| 774 | |
| 775 | boost::optional<std::pair<std::string, std::string>> http_client_auth::do_get_auth_field( |
| 776 | const boost::string_ref method, const boost::string_ref uri) |
| 777 | { |
| 778 | assert(user); |
| 779 | if (user->server.generator) |
| 780 | { |
| 781 | ++(user->counter); |
| 782 | return std::make_pair(std::string(client_auth_field), user->server.generator(*user, method, uri)); |
| 783 | } |
| 784 | return boost::none; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 |