| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
| 6 #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 6 #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" |
| 9 |
| 10 // This contains the portable and the SSPI implementations for NTLM. |
| 11 // We use NTLM_SSPI for Windows, and NTLM_PORTABLE for other platforms. |
| 12 #if defined(OS_WIN) |
| 13 #define NTLM_SSPI |
| 14 #else |
| 15 #define NTLM_PORTABLE |
| 16 #endif |
| 17 |
| 18 #if defined(NTLM_SSPI) |
| 19 #define SECURITY_WIN32 1 |
| 20 #include <windows.h> |
| 21 #include <security.h> |
| 22 #endif |
| 23 |
| 8 #include <string> | 24 #include <string> |
| 9 | 25 |
| 10 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 11 #include "base/scoped_ptr.h" | |
| 12 #include "base/string16.h" | 27 #include "base/string16.h" |
| 13 #include "net/http/http_auth_handler.h" | 28 #include "net/http/http_auth_handler.h" |
| 14 | 29 |
| 15 namespace net { | 30 namespace net { |
| 16 | 31 |
| 17 class NTLMAuthModule; | |
| 18 | |
| 19 // Code for handling HTTP NTLM authentication. | 32 // Code for handling HTTP NTLM authentication. |
| 20 class HttpAuthHandlerNTLM : public HttpAuthHandler { | 33 class HttpAuthHandlerNTLM : public HttpAuthHandler { |
| 21 public: | 34 public: |
| 35 #if defined(NTLM_PORTABLE) |
| 22 // A function that generates n random bytes in the output buffer. | 36 // A function that generates n random bytes in the output buffer. |
| 23 typedef void (*GenerateRandomProc)(uint8* output, size_t n); | 37 typedef void (*GenerateRandomProc)(uint8* output, size_t n); |
| 24 | 38 |
| 25 // A function that returns the local host name. Returns an empty string if | 39 // A function that returns the local host name. Returns an empty string if |
| 26 // the local host name is not available. | 40 // the local host name is not available. |
| 27 typedef std::string (*HostNameProc)(); | 41 typedef std::string (*HostNameProc)(); |
| 28 | 42 |
| 29 // For unit tests to override and restore the GenerateRandom and | 43 // For unit tests to override and restore the GenerateRandom and |
| 30 // GetHostName functions. | 44 // GetHostName functions. |
| 31 class ScopedProcSetter { | 45 class ScopedProcSetter { |
| 32 public: | 46 public: |
| 33 ScopedProcSetter(GenerateRandomProc random_proc, | 47 ScopedProcSetter(GenerateRandomProc random_proc, |
| 34 HostNameProc host_name_proc) { | 48 HostNameProc host_name_proc) { |
| 35 old_random_proc_ = SetGenerateRandomProc(random_proc); | 49 old_random_proc_ = SetGenerateRandomProc(random_proc); |
| 36 old_host_name_proc_ = SetHostNameProc(host_name_proc); | 50 old_host_name_proc_ = SetHostNameProc(host_name_proc); |
| 37 } | 51 } |
| 38 | 52 |
| 39 ~ScopedProcSetter() { | 53 ~ScopedProcSetter() { |
| 40 SetGenerateRandomProc(old_random_proc_); | 54 SetGenerateRandomProc(old_random_proc_); |
| 41 SetHostNameProc(old_host_name_proc_); | 55 SetHostNameProc(old_host_name_proc_); |
| 42 } | 56 } |
| 43 | 57 |
| 44 private: | 58 private: |
| 45 GenerateRandomProc old_random_proc_; | 59 GenerateRandomProc old_random_proc_; |
| 46 HostNameProc old_host_name_proc_; | 60 HostNameProc old_host_name_proc_; |
| 47 }; | 61 }; |
| 62 #endif |
| 48 | 63 |
| 49 HttpAuthHandlerNTLM(); | 64 HttpAuthHandlerNTLM(); |
| 50 | 65 |
| 51 virtual ~HttpAuthHandlerNTLM(); | 66 virtual ~HttpAuthHandlerNTLM(); |
| 52 | 67 |
| 53 virtual bool NeedsIdentity(); | 68 virtual bool NeedsIdentity(); |
| 54 | 69 |
| 70 virtual bool IsFinalRound(); |
| 71 |
| 55 virtual std::string GenerateCredentials(const std::wstring& username, | 72 virtual std::string GenerateCredentials(const std::wstring& username, |
| 56 const std::wstring& password, | 73 const std::wstring& password, |
| 57 const HttpRequestInfo* request, | 74 const HttpRequestInfo* request, |
| 58 const ProxyInfo* proxy); | 75 const ProxyInfo* proxy); |
| 59 | 76 |
| 60 protected: | 77 protected: |
| 61 virtual bool Init(std::string::const_iterator challenge_begin, | 78 virtual bool Init(std::string::const_iterator challenge_begin, |
| 62 std::string::const_iterator challenge_end) { | 79 std::string::const_iterator challenge_end) { |
| 63 return ParseChallenge(challenge_begin, challenge_end); | 80 return ParseChallenge(challenge_begin, challenge_end); |
| 64 } | 81 } |
| 65 | 82 |
| 83 // This function acquires a credentials handle in the SSPI implementation. |
| 84 // It does nothing in the portable implementation. |
| 85 int InitializeBeforeFirstChallenge(); |
| 86 |
| 66 private: | 87 private: |
| 88 #if defined(NTLM_PORTABLE) |
| 67 // For unit tests to override the GenerateRandom and GetHostName functions. | 89 // For unit tests to override the GenerateRandom and GetHostName functions. |
| 68 // Returns the old function. | 90 // Returns the old function. |
| 69 static GenerateRandomProc SetGenerateRandomProc(GenerateRandomProc proc); | 91 static GenerateRandomProc SetGenerateRandomProc(GenerateRandomProc proc); |
| 70 static HostNameProc SetHostNameProc(HostNameProc proc); | 92 static HostNameProc SetHostNameProc(HostNameProc proc); |
| 93 #endif |
| 71 | 94 |
| 72 // Parse the challenge, saving the results into this instance. | 95 // Parse the challenge, saving the results into this instance. |
| 73 // Returns true on success. | 96 // Returns true on success. |
| 74 bool ParseChallenge(std::string::const_iterator challenge_begin, | 97 bool ParseChallenge(std::string::const_iterator challenge_begin, |
| 75 std::string::const_iterator challenge_end); | 98 std::string::const_iterator challenge_end); |
| 76 | 99 |
| 77 // Given an input token received from the server, generate the next output | 100 // Given an input token received from the server, generate the next output |
| 78 // token to be sent to the server. | 101 // token to be sent to the server. |
| 79 int GetNextToken(const void* in_token, | 102 int GetNextToken(const void* in_token, |
| 80 uint32 in_token_len, | 103 uint32 in_token_len, |
| 81 void** out_token, | 104 void** out_token, |
| 82 uint32* out_token_len); | 105 uint32* out_token_len); |
| 83 | 106 |
| 107 #if defined(NTLM_SSPI) |
| 108 void ResetSecurityContext(); |
| 109 #endif |
| 110 |
| 111 #if defined(NTLM_PORTABLE) |
| 84 static GenerateRandomProc generate_random_proc_; | 112 static GenerateRandomProc generate_random_proc_; |
| 85 static HostNameProc get_host_name_proc_; | 113 static HostNameProc get_host_name_proc_; |
| 114 #endif |
| 86 | 115 |
| 87 string16 domain_; | 116 string16 domain_; |
| 88 string16 username_; | 117 string16 username_; |
| 89 string16 password_; | 118 string16 password_; |
| 90 | 119 |
| 91 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or | 120 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or |
| 92 // "Proxy-Authenticate" response header. | 121 // "Proxy-Authenticate" response header. |
| 93 std::string auth_data_; | 122 std::string auth_data_; |
| 123 |
| 124 #if defined(NTLM_SSPI) |
| 125 ULONG max_token_len_; |
| 126 CredHandle cred_; |
| 127 CtxtHandle ctxt_; |
| 128 #endif |
| 94 }; | 129 }; |
| 95 | 130 |
| 96 } // namespace net | 131 } // namespace net |
| 97 | 132 |
| 98 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 133 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
| OLD | NEW |