diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/http/fetch/http_digest.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/http/fetch/http_digest.cpp')
-rw-r--r-- | library/cpp/http/fetch/http_digest.cpp | 156 |
1 files changed, 78 insertions, 78 deletions
diff --git a/library/cpp/http/fetch/http_digest.cpp b/library/cpp/http/fetch/http_digest.cpp index 79c5a25319..1eaa02b7f2 100644 --- a/library/cpp/http/fetch/http_digest.cpp +++ b/library/cpp/http/fetch/http_digest.cpp @@ -1,5 +1,5 @@ -#include "http_digest.h" - +#include "http_digest.h" + #include <library/cpp/digest/md5/md5.h> #include <util/stream/output.h> #include <util/stream/str.h> @@ -9,76 +9,76 @@ static const char* WWW_PREFIX = "Authorization: Digest "; /************************************************************/ -httpDigestHandler::httpDigestHandler() - : User_(nullptr) - , Password_(nullptr) - , Nonce_(nullptr) - , NonceCount_(0) - , HeaderInstruction_(nullptr) +httpDigestHandler::httpDigestHandler() + : User_(nullptr) + , Password_(nullptr) + , Nonce_(nullptr) + , NonceCount_(0) + , HeaderInstruction_(nullptr) { } /************************************************************/ -httpDigestHandler::~httpDigestHandler() { +httpDigestHandler::~httpDigestHandler() { clear(); } /************************************************************/ -void httpDigestHandler::clear() { - free(Nonce_); - free(HeaderInstruction_); +void httpDigestHandler::clear() { + free(Nonce_); + free(HeaderInstruction_); User_ = Password_ = nullptr; Nonce_ = HeaderInstruction_ = nullptr; - NonceCount_ = 0; + NonceCount_ = 0; } /************************************************************/ -void httpDigestHandler::setAuthorization(const char* user, const char* password) { +void httpDigestHandler::setAuthorization(const char* user, const char* password) { clear(); - if (user && password) { - User_ = user; - Password_ = password; + if (user && password) { + User_ = user; + Password_ = password; } } /************************************************************/ -const char* httpDigestHandler::getHeaderInstruction() const { - return HeaderInstruction_; +const char* httpDigestHandler::getHeaderInstruction() const { + return HeaderInstruction_; } /************************************************************/ -void httpDigestHandler::generateCNonce(char* outCNonce) { +void httpDigestHandler::generateCNonce(char* outCNonce) { if (!*outCNonce) sprintf(outCNonce, "%ld", (long)time(nullptr)); } /************************************************************/ -inline void addMD5(MD5& ctx, const char* value) { +inline void addMD5(MD5& ctx, const char* value) { ctx.Update((const unsigned char*)(value), strlen(value)); } -inline void addMD5(MD5& ctx, const char* value, int len) { +inline void addMD5(MD5& ctx, const char* value, int len) { ctx.Update((const unsigned char*)(value), len); } -inline void addMD5Sep(MD5& ctx) { +inline void addMD5Sep(MD5& ctx) { addMD5(ctx, ":", 1); } /************************************************************/ /* calculate H(A1) as per spec */ -void httpDigestHandler::digestCalcHA1(const THttpAuthHeader& hd, - char* outSessionKey, - char* outCNonce) { +void httpDigestHandler::digestCalcHA1(const THttpAuthHeader& hd, + char* outSessionKey, + char* outCNonce) { MD5 ctx; ctx.Init(); - addMD5(ctx, User_); - addMD5Sep(ctx); - addMD5(ctx, hd.realm); - addMD5Sep(ctx); - addMD5(ctx, Password_); + addMD5(ctx, User_); + addMD5Sep(ctx); + addMD5(ctx, hd.realm); + addMD5Sep(ctx); + addMD5(ctx, Password_); - if (hd.algorithm == 1) { //MD5-sess + if (hd.algorithm == 1) { //MD5-sess unsigned char digest[16]; ctx.Final(digest); @@ -86,91 +86,91 @@ void httpDigestHandler::digestCalcHA1(const THttpAuthHeader& hd, ctx.Init(); ctx.Update(digest, 16); - addMD5Sep(ctx); - addMD5(ctx, hd.nonce); - addMD5Sep(ctx); - addMD5(ctx, outCNonce); + addMD5Sep(ctx); + addMD5(ctx, hd.nonce); + addMD5Sep(ctx); + addMD5(ctx, outCNonce); ctx.End(outSessionKey); } - ctx.End(outSessionKey); + ctx.End(outSessionKey); }; /************************************************************/ /* calculate request-digest/response-digest as per HTTP Digest spec */ -void httpDigestHandler::digestCalcResponse(const THttpAuthHeader& hd, - const char* path, - const char* method, - const char* nonceCount, - char* outResponse, - char* outCNonce) { +void httpDigestHandler::digestCalcResponse(const THttpAuthHeader& hd, + const char* path, + const char* method, + const char* nonceCount, + char* outResponse, + char* outCNonce) { char HA1[33]; digestCalcHA1(hd, HA1, outCNonce); char HA2[33]; MD5 ctx; ctx.Init(); - addMD5(ctx, method); - addMD5Sep(ctx); - addMD5(ctx, path); + addMD5(ctx, method); + addMD5Sep(ctx); + addMD5(ctx, path); //ignore auth-int ctx.End(HA2); ctx.Init(); - addMD5(ctx, HA1, 32); - addMD5Sep(ctx); - addMD5(ctx, Nonce_); - addMD5Sep(ctx); + addMD5(ctx, HA1, 32); + addMD5Sep(ctx); + addMD5(ctx, Nonce_); + addMD5Sep(ctx); - if (hd.qop_auth) { + if (hd.qop_auth) { if (!*outCNonce) generateCNonce(outCNonce); - addMD5(ctx, nonceCount, 8); - addMD5Sep(ctx); - addMD5(ctx, outCNonce); - addMD5Sep(ctx); - addMD5(ctx, "auth", 4); - addMD5Sep(ctx); + addMD5(ctx, nonceCount, 8); + addMD5Sep(ctx); + addMD5(ctx, outCNonce); + addMD5Sep(ctx); + addMD5(ctx, "auth", 4); + addMD5Sep(ctx); } - addMD5(ctx, HA2, 32); + addMD5(ctx, HA2, 32); ctx.End(outResponse); } /************************************************************/ -bool httpDigestHandler::processHeader(const THttpAuthHeader* header, - const char* path, - const char* method, - const char* cnonce) { - if (!User_ || !header || !header->use_auth || !header->realm || !header->nonce) +bool httpDigestHandler::processHeader(const THttpAuthHeader* header, + const char* path, + const char* method, + const char* cnonce) { + if (!User_ || !header || !header->use_auth || !header->realm || !header->nonce) return false; - if (Nonce_) { - if (strcmp(Nonce_, header->nonce)) { - free(Nonce_); + if (Nonce_) { + if (strcmp(Nonce_, header->nonce)) { + free(Nonce_); Nonce_ = nullptr; - NonceCount_ = 0; + NonceCount_ = 0; } } - if (!Nonce_) { - Nonce_ = strdup(header->nonce); - NonceCount_ = 0; + if (!Nonce_) { + Nonce_ = strdup(header->nonce); + NonceCount_ = 0; } - free(HeaderInstruction_); + free(HeaderInstruction_); HeaderInstruction_ = nullptr; - NonceCount_++; + NonceCount_++; char nonceCount[20]; - sprintf(nonceCount, "%08d", NonceCount_); + sprintf(nonceCount, "%08d", NonceCount_); char CNonce[50]; if (cnonce) strcpy(CNonce, cnonce); else - CNonce[0] = 0; + CNonce[0] = 0; char response[33]; - digestCalcResponse(*header, path, method, nonceCount, response, CNonce); + digestCalcResponse(*header, path, method, nonceCount, response, CNonce); //digest-response = 1#( username | realm | nonce | digest-uri // | response | [ algorithm ] | [cnonce] | @@ -178,11 +178,11 @@ bool httpDigestHandler::processHeader(const THttpAuthHeader* header, // [nonce-count] | [auth-param] ) TStringStream out; - out << WWW_PREFIX << "username=\"" << User_ << "\""; + out << WWW_PREFIX << "username=\"" << User_ << "\""; out << ", realm=\"" << header->realm << "\""; out << ", nonce=\"" << header->nonce << "\""; out << ", uri=\"" << path << "\""; - if (header->algorithm == 1) + if (header->algorithm == 1) out << ", algorithm=MD5-sess"; else out << ", algorithm=MD5"; @@ -197,7 +197,7 @@ bool httpDigestHandler::processHeader(const THttpAuthHeader* header, out << "\r\n"; TString s_out = out.Str(); - HeaderInstruction_ = strdup(s_out.c_str()); + HeaderInstruction_ = strdup(s_out.c_str()); return true; } |