mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-29 21:18:29 +00:00
Match Digest algorithm and qop values case-insensitively
The Digest client compared the algorithm and qop values of a challenge case-sensitively, so algorithm=sha-256 was answered with an MD5 hash and qop="AUTH" without qop. A challenge whose algorithm it doesn't implement (SHA-512-256, the -sess variants, or a value that isn't a token) was answered with an MD5 hash under the server's algorithm name, and only the first Digest challenge was ever looked at. - Match algorithm and qop options case-insensitively. qop is split into tokens instead of searched as a substring, and sent as auth/auth-int. - Collect params per Digest challenge and use the first one the client can answer (RFC 7616 Sections 3.3 and 3.7): realm and nonce present and an algorithm it implements. If there is none, the 401 is returned without a retry. - digest_hash() is the single list of supported algorithms, used by both parse_www_authenticate() and make_digest_authentication_header(). The algorithm is sent back as the server wrote it (RFC 7616 Section 3.4). parse_www_authenticate() moves into the SSL block, next to the hash functions and its callers.
This commit is contained in:
1 parent
58cd21449c
commit
c5faf03dfe
2 files changed
+211
-185
No files matched your search
@@ -11224,76 +11224,6 @@ inline std::string escape_quoted_pairs(const std::string &s) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// RFC 9110 Section 11.6.1 defines a challenge list as
|
||||
// WWW-Authenticate = #challenge
|
||||
// challenge = auth-scheme [ 1*SP ( token68 / [ #auth-param ] ) ]
|
||||
// auth-param = token BWS "=" BWS ( token / quoted-string )
|
||||
// so a server may offer several schemes, each with its own comma-separated
|
||||
// auth-param list, in either order and either as separate field lines or
|
||||
// packed into one. Splitting on every comma would break apart a challenge's
|
||||
// own param list; splitting only on the first space would miss a Digest
|
||||
// challenge that isn't first. Split on commas that aren't inside a
|
||||
// quoted-string instead (split_unquoted), then track which scheme each
|
||||
// resulting segment belongs to: a segment whose text before "=" contains
|
||||
// whitespace (or that has no "=" at all) starts a new challenge named by its
|
||||
// leading token.
|
||||
inline bool parse_www_authenticate(const Response &res,
|
||||
std::map<std::string, std::string> &auth,
|
||||
bool is_proxy) {
|
||||
auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
|
||||
auto combined = get_combined_header_value(res.headers, auth_key);
|
||||
if (combined.empty()) { return false; }
|
||||
|
||||
auto found_digest = false;
|
||||
auto in_digest_challenge = false;
|
||||
split_unquoted(
|
||||
combined.data(), combined.data() + combined.size(), ',',
|
||||
[&](const char *b, const char *e) {
|
||||
std::string segment(b, e);
|
||||
auto eq_pos = segment.find('=');
|
||||
// BWS is allowed on both sides of "=", so the text naming the key (or,
|
||||
// for the first segment of a challenge, "<scheme> <key>") must be
|
||||
// trimmed before its boundaries are inspected.
|
||||
auto key_part = trim_copy(
|
||||
eq_pos == std::string::npos ? segment : segment.substr(0, eq_pos));
|
||||
auto space_pos = key_part.find_last_of(" \t");
|
||||
if (space_pos != std::string::npos || eq_pos == std::string::npos) {
|
||||
// "<scheme>[ <key>]" starts a new challenge.
|
||||
auto scheme_end =
|
||||
space_pos == std::string::npos ? key_part.size() : space_pos;
|
||||
// RFC 7616 Section 3.7: a server may offer more than one Digest
|
||||
// challenge (e.g. SHA-256 and MD5); keep only the first so a nonce
|
||||
// from one challenge is never paired with another's algorithm.
|
||||
in_digest_challenge =
|
||||
!found_digest &&
|
||||
case_ignore::equal(key_part.substr(0, scheme_end), "Digest");
|
||||
if (in_digest_challenge) { found_digest = true; }
|
||||
if (space_pos == std::string::npos) {
|
||||
// Bare scheme (or a token68), no auth-param on this segment.
|
||||
return;
|
||||
}
|
||||
key_part = key_part.substr(space_pos + 1);
|
||||
}
|
||||
|
||||
if (!in_digest_challenge) { return; }
|
||||
|
||||
auto val = trim_copy(segment.substr(eq_pos + 1));
|
||||
auto unquoted = trim_double_quotes_copy(val);
|
||||
if (unquoted.size() != val.size()) {
|
||||
unquoted = unescape_quoted_pairs(unquoted);
|
||||
}
|
||||
// auth-param names are case-insensitive (RFC 9110 Section 11.2)
|
||||
auth[case_ignore::to_lower(key_part)] = std::move(unquoted);
|
||||
});
|
||||
|
||||
// RFC 7616 Section 3.3 requires realm and nonce on every Digest challenge;
|
||||
// make_digest_authentication_header() dereferences both unconditionally, so
|
||||
// a challenge missing either can't produce a usable Authorization header.
|
||||
// Treat it the same as no Digest challenge at all.
|
||||
return found_digest && auth.find("realm") != auth.end() &&
|
||||
auth.find("nonce") != auth.end();
|
||||
}
|
||||
|
||||
class ContentProviderAdapter {
|
||||
public:
|
||||
explicit ContentProviderAdapter(
|
||||
@@ -11762,6 +11692,99 @@ inline bool process_client_socket_ssl(
|
||||
return callback(strm);
|
||||
}
|
||||
|
||||
using DigestHash = std::string (*)(const std::string &);
|
||||
|
||||
// The Digest algorithms the client implements, matched case-insensitively;
|
||||
// nullptr for any other.
|
||||
inline DigestHash digest_hash(const std::string &algorithm) {
|
||||
if (case_ignore::equal(algorithm, "MD5")) { return detail::MD5; }
|
||||
if (case_ignore::equal(algorithm, "SHA-256")) { return detail::SHA_256; }
|
||||
if (case_ignore::equal(algorithm, "SHA-512")) { return detail::SHA_512; }
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// RFC 9110 Section 11.6.1 defines a challenge list as
|
||||
// WWW-Authenticate = #challenge
|
||||
// challenge = auth-scheme [ 1*SP ( token68 / [ #auth-param ] ) ]
|
||||
// auth-param = token BWS "=" BWS ( token / quoted-string )
|
||||
// so a server may offer several schemes, each with its own comma-separated
|
||||
// auth-param list, in either order and either as separate field lines or
|
||||
// packed into one. Splitting on every comma would break apart a challenge's
|
||||
// own param list; splitting only on the first space would miss a Digest
|
||||
// challenge that isn't first. Split on commas that aren't inside a
|
||||
// quoted-string instead (split_unquoted), then track which scheme each
|
||||
// resulting segment belongs to: a segment whose text before "=" contains
|
||||
// whitespace (or that has no "=" at all) starts a new challenge named by its
|
||||
// leading token.
|
||||
inline bool parse_www_authenticate(const Response &res,
|
||||
std::map<std::string, std::string> &auth,
|
||||
bool is_proxy) {
|
||||
auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
|
||||
auto combined = get_combined_header_value(res.headers, auth_key);
|
||||
if (combined.empty()) { return false; }
|
||||
|
||||
// RFC 7616 Section 3.7: a server may offer more than one Digest challenge
|
||||
// (e.g. SHA-256 and MD5), and the client uses the first one it supports.
|
||||
// A challenge whose algorithm the client doesn't implement is ignored
|
||||
// (Section 3.3), and one without realm and nonce can't be answered. Params
|
||||
// are collected per challenge, so a nonce from one is never paired with
|
||||
// another's algorithm.
|
||||
auto found = false;
|
||||
auto in_digest_challenge = false;
|
||||
std::map<std::string, std::string> challenge;
|
||||
auto take_challenge_if_answerable = [&] {
|
||||
if (!found && in_digest_challenge &&
|
||||
challenge.find("realm") != challenge.end() &&
|
||||
challenge.find("nonce") != challenge.end()) {
|
||||
auto it = challenge.find("algorithm");
|
||||
if (it == challenge.end() || digest_hash(it->second)) {
|
||||
auth = std::move(challenge);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
challenge.clear();
|
||||
};
|
||||
|
||||
split_unquoted(
|
||||
combined.data(), combined.data() + combined.size(), ',',
|
||||
[&](const char *b, const char *e) {
|
||||
std::string segment(b, e);
|
||||
auto eq_pos = segment.find('=');
|
||||
// BWS is allowed on both sides of "=", so the text naming the key (or,
|
||||
// for the first segment of a challenge, "<scheme> <key>") must be
|
||||
// trimmed before its boundaries are inspected.
|
||||
auto key_part = trim_copy(
|
||||
eq_pos == std::string::npos ? segment : segment.substr(0, eq_pos));
|
||||
auto space_pos = key_part.find_last_of(" \t");
|
||||
if (space_pos != std::string::npos || eq_pos == std::string::npos) {
|
||||
// "<scheme>[ <key>]" starts a new challenge.
|
||||
take_challenge_if_answerable();
|
||||
auto scheme_end =
|
||||
space_pos == std::string::npos ? key_part.size() : space_pos;
|
||||
in_digest_challenge =
|
||||
case_ignore::equal(key_part.substr(0, scheme_end), "Digest");
|
||||
if (space_pos == std::string::npos) {
|
||||
// Bare scheme (or a token68), no auth-param on this segment.
|
||||
return;
|
||||
}
|
||||
key_part = key_part.substr(space_pos + 1);
|
||||
}
|
||||
|
||||
if (!in_digest_challenge) { return; }
|
||||
|
||||
auto val = trim_copy(segment.substr(eq_pos + 1));
|
||||
auto unquoted = trim_double_quotes_copy(val);
|
||||
if (unquoted.size() != val.size()) {
|
||||
unquoted = unescape_quoted_pairs(unquoted);
|
||||
}
|
||||
// auth-param names are case-insensitive (RFC 9110 Section 11.2)
|
||||
challenge[case_ignore::to_lower(key_part)] = std::move(unquoted);
|
||||
});
|
||||
take_challenge_if_answerable();
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// `uri` is req's request-target in origin form, as curl sends it, also when
|
||||
// the request line carries the absolute form for a proxy.
|
||||
inline std::pair<std::string, std::string> make_digest_authentication_header(
|
||||
@@ -11776,33 +11799,31 @@ inline std::pair<std::string, std::string> make_digest_authentication_header(
|
||||
nc = ss.str();
|
||||
}
|
||||
|
||||
// auth-int is preferred over auth; other qop options are ignored.
|
||||
std::string qop;
|
||||
if (auth.find("qop") != auth.end()) {
|
||||
qop = auth.at("qop");
|
||||
if (qop.find("auth-int") != std::string::npos) {
|
||||
qop = "auth-int";
|
||||
} else if (qop.find("auth") != std::string::npos) {
|
||||
qop = "auth";
|
||||
} else {
|
||||
qop.clear();
|
||||
}
|
||||
auto qop_it = auth.find("qop");
|
||||
if (qop_it != auth.end()) {
|
||||
const auto &options = qop_it->second;
|
||||
split(options.data(), options.data() + options.size(), ',',
|
||||
[&](const char *b, const char *e) {
|
||||
std::string option(b, e);
|
||||
if (case_ignore::equal(option, "auth-int")) {
|
||||
qop = "auth-int";
|
||||
} else if (qop.empty() && case_ignore::equal(option, "auth")) {
|
||||
qop = "auth";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::string algo = "MD5";
|
||||
if (auth.find("algorithm") != auth.end()) {
|
||||
// algorithm is an unquoted token (RFC 7616 §3.4). A server value that is
|
||||
// not a token would otherwise be emitted verbatim and could carry commas
|
||||
// or quotes that inject further auth-params into the header below.
|
||||
const auto &a = auth.at("algorithm");
|
||||
if (fields::is_token(a)) { algo = a; }
|
||||
}
|
||||
// Sent back as the server wrote it (RFC 7616 Section 3.4).
|
||||
// parse_www_authenticate() accepts only one digest_hash() knows.
|
||||
auto algo_it = auth.find("algorithm");
|
||||
auto algo = algo_it == auth.end() ? std::string("MD5") : algo_it->second;
|
||||
auto H = digest_hash(algo);
|
||||
assert(H);
|
||||
|
||||
std::string response;
|
||||
{
|
||||
auto H = algo == "SHA-256" ? detail::SHA_256
|
||||
: algo == "SHA-512" ? detail::SHA_512
|
||||
: detail::MD5;
|
||||
|
||||
auto A1 = username + ":" + auth.at("realm") + ":" + password;
|
||||
|
||||
auto A2 = req.method + ":" + uri;
|
||||
|
||||
Reference in new issue
Block a user