nsHttpDigestAuth.h (3291B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef nsDigestAuth_h__ 8 #define nsDigestAuth_h__ 9 10 #include "nsICryptoHash.h" 11 #include "nsIHttpAuthenticator.h" 12 #include "nsStringFwd.h" 13 #include "nsCOMPtr.h" 14 #include "mozilla/StaticPtr.h" 15 16 namespace mozilla { 17 namespace net { 18 19 #define ALGO_SPECIFIED 0x01 20 #define ALGO_MD5 0x02 21 #define ALGO_MD5_SESS 0x04 22 #define ALGO_SHA256 0x08 23 #define ALGO_SHA256_SESS 0x10 24 #define QOP_AUTH 0x01 25 #define QOP_AUTH_INT 0x02 26 27 #define NONCE_COUNT_LENGTH 8 28 #ifndef MD5_DIGEST_LENGTH 29 # define MD5_DIGEST_LENGTH 16 30 #endif 31 #ifndef SHA256_DIGEST_LENGTH 32 # define SHA256_DIGEST_LENGTH 32 33 #endif 34 35 //----------------------------------------------------------------------------- 36 // nsHttpDigestAuth 37 //----------------------------------------------------------------------------- 38 39 class nsHttpDigestAuth final : public nsIHttpAuthenticator { 40 public: 41 NS_DECL_ISUPPORTS 42 NS_DECL_NSIHTTPAUTHENTICATOR 43 44 nsHttpDigestAuth() = default; 45 46 static already_AddRefed<nsIHttpAuthenticator> GetOrCreate(); 47 48 [[nodiscard]] static nsresult ParseChallenge( 49 const nsACString& aChallenge, nsACString& realm, nsACString& domain, 50 nsACString& nonce, nsACString& opaque, bool* stale, uint16_t* algorithm, 51 uint16_t* qop); 52 53 protected: 54 ~nsHttpDigestAuth() = default; 55 56 [[nodiscard]] nsresult ExpandToHex(const char* digest, char* result, 57 uint16_t algorithm); 58 59 [[nodiscard]] nsresult CalculateResponse( 60 const char* ha1_digest, const char* ha2_digest, uint16_t algorithm, 61 const nsCString& nonce, uint16_t qop, const char* nonce_count, 62 const nsCString& cnonce, char* result); 63 64 [[nodiscard]] nsresult CalculateHA1(const nsCString& username, 65 const nsCString& password, 66 const nsCString& realm, 67 uint16_t algorithm, 68 const nsCString& nonce, 69 const nsCString& cnonce, char* result); 70 71 [[nodiscard]] nsresult CalculateHA2(const nsCString& http_method, 72 const nsCString& http_uri_path, 73 uint16_t algorithm, uint16_t qop, 74 const char* bodyDigest, char* result); 75 76 // result is in mHashBuf 77 [[nodiscard]] nsresult DigestHash(const char* buf, uint32_t len, 78 uint16_t algorithm); 79 80 [[nodiscard]] nsresult GetMethodAndPath(nsIHttpAuthenticableChannel*, bool, 81 nsCString&, nsCString&); 82 83 // append the quoted version of value to aHeaderLine 84 [[nodiscard]] nsresult AppendQuotedString(const nsACString& value, 85 nsACString& aHeaderLine); 86 87 protected: 88 nsCOMPtr<nsICryptoHash> mVerifier; 89 char mHashBuf[SHA256_DIGEST_LENGTH]{0}; 90 91 static StaticRefPtr<nsHttpDigestAuth> gSingleton; 92 }; 93 94 } // namespace net 95 } // namespace mozilla 96 97 #endif // nsHttpDigestAuth_h__