| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "build/build_config.h" |
| 11 // This contains the portable and the SSPI implementation for NTLM. |
| 12 // We use NTLM_PORTABLE for Linux and OSX, for windows NTLM_SSPI is preferred. |
| 13 #if defined(OS_WIN) |
| 14 #define NTLM_SSPI |
| 15 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 16 #define NTLM_PORTABLE |
| 17 #endif |
| 18 |
| 19 #if defined(NTLM_SSPI) |
| 20 #include <windows.h> |
| 21 #define SECURITY_WIN32 |
| 22 #include <security.h> |
| 23 #endif |
| 24 |
| 10 #include "base/basictypes.h" | 25 #include "base/basictypes.h" |
| 11 #include "base/scoped_ptr.h" | 26 #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" |
| 29 #include "net/base/net_errors.h" |
| 14 | 30 |
| 15 namespace net { | 31 namespace net { |
| 16 | 32 |
| 33 static inline void ZapBuf(void* buf, size_t buf_len) { |
| 34 memset(buf, 0, buf_len); |
| 35 } |
| 36 |
| 37 // TODO(wtc): Can we implement ZapString as |
| 38 // s.replace(0, s.size(), s.size(), '\0)? |
| 39 static inline void ZapString(std::string* s) { |
| 40 ZapBuf(&(*s)[0], s->length()); |
| 41 } |
| 42 |
| 43 static inline void ZapString(string16* s) { |
| 44 ZapBuf(&(*s)[0], s->length() * 2); |
| 45 } |
| 46 |
| 17 class NTLMAuthModule; | 47 class NTLMAuthModule; |
| 18 | 48 |
| 19 // Code for handling HTTP NTLM authentication. | 49 // Code for handling HTTP NTLM authentication. |
| 20 class HttpAuthHandlerNTLM : public HttpAuthHandler { | 50 class HttpAuthHandlerNTLM : public HttpAuthHandler { |
| 21 public: | 51 public: |
| 52 |
| 53 #if defined(NTLM_PORTABLE) |
| 22 // A function that generates n random bytes in the output buffer. | 54 // A function that generates n random bytes in the output buffer. |
| 23 typedef void (*GenerateRandomProc)(uint8* output, size_t n); | 55 typedef void (*GenerateRandomProc)(uint8* output, size_t n); |
| 24 | 56 |
| 25 // A function that returns the local host name. Returns an empty string if | 57 // A function that returns the local host name. Returns an empty string if |
| 26 // the local host name is not available. | 58 // the local host name is not available. |
| 27 typedef std::string (*HostNameProc)(); | 59 typedef std::string (*HostNameProc)(); |
| 28 | 60 |
| 29 // For unit tests to override and restore the GenerateRandom and | 61 // For unit tests to override and restore the GenerateRandom and |
| 30 // GetHostName functions. | 62 // GetHostName functions. |
| 31 class ScopedProcSetter { | 63 class ScopedProcSetter { |
| 32 public: | 64 public: |
| 33 ScopedProcSetter(GenerateRandomProc random_proc, | 65 ScopedProcSetter(GenerateRandomProc random_proc, |
| 34 HostNameProc host_name_proc) { | 66 HostNameProc host_name_proc) { |
| 35 old_random_proc_ = SetGenerateRandomProc(random_proc); | 67 old_random_proc_ = SetGenerateRandomProc(random_proc); |
| 36 old_host_name_proc_ = SetHostNameProc(host_name_proc); | 68 old_host_name_proc_ = SetHostNameProc(host_name_proc); |
| 37 } | 69 } |
| 38 | 70 |
| 39 ~ScopedProcSetter() { | 71 ~ScopedProcSetter() { |
| 40 SetGenerateRandomProc(old_random_proc_); | 72 SetGenerateRandomProc(old_random_proc_); |
| 41 SetHostNameProc(old_host_name_proc_); | 73 SetHostNameProc(old_host_name_proc_); |
| 42 } | 74 } |
| 43 | 75 |
| 44 private: | 76 private: |
| 45 GenerateRandomProc old_random_proc_; | 77 GenerateRandomProc old_random_proc_; |
| 46 HostNameProc old_host_name_proc_; | 78 HostNameProc old_host_name_proc_; |
| 47 }; | 79 }; |
| 80 #endif |
| 48 | 81 |
| 49 HttpAuthHandlerNTLM(); | 82 HttpAuthHandlerNTLM(); |
| 50 | 83 |
| 51 virtual ~HttpAuthHandlerNTLM(); | 84 virtual ~HttpAuthHandlerNTLM(); |
| 52 | 85 |
| 53 virtual bool NeedsIdentity(); | 86 virtual bool NeedsIdentity(); |
| 54 | 87 |
| 55 virtual std::string GenerateCredentials(const std::wstring& username, | 88 virtual std::string GenerateCredentials(const std::wstring& username, |
| 56 const std::wstring& password, | 89 const std::wstring& password, |
| 57 const HttpRequestInfo* request, | 90 const HttpRequestInfo* request, |
| 58 const ProxyInfo* proxy); | 91 const ProxyInfo* proxy); |
| 59 | 92 |
| 60 protected: | 93 protected: |
| 61 virtual bool Init(std::string::const_iterator challenge_begin, | 94 virtual bool Init(std::string::const_iterator challenge_begin, |
| 62 std::string::const_iterator challenge_end) { | 95 std::string::const_iterator challenge_end) { |
| 63 return ParseChallenge(challenge_begin, challenge_end); | 96 return ParseChallenge(challenge_begin, challenge_end); |
| 64 } | 97 } |
| 65 | 98 |
| 99 // This function is implemented in the SSPI layer to get Credentials |
| 100 int InitializeBeforeFirstChallenge(); |
| 101 |
| 66 private: | 102 private: |
| 103 #if defined(NTLM_PORTABLE) |
| 67 // For unit tests to override the GenerateRandom and GetHostName functions. | 104 // For unit tests to override the GenerateRandom and GetHostName functions. |
| 68 // Returns the old function. | 105 // Returns the old function. |
| 69 static GenerateRandomProc SetGenerateRandomProc(GenerateRandomProc proc); | 106 static GenerateRandomProc SetGenerateRandomProc(GenerateRandomProc proc); |
| 70 static HostNameProc SetHostNameProc(HostNameProc proc); | 107 static HostNameProc SetHostNameProc(HostNameProc proc); |
| 108 #endif |
| 71 | 109 |
| 72 // Parse the challenge, saving the results into this instance. | 110 // Parse the challenge, saving the results into this instance. |
| 73 // Returns true on success. | 111 // Returns true on success. |
| 74 bool ParseChallenge(std::string::const_iterator challenge_begin, | 112 bool ParseChallenge(std::string::const_iterator challenge_begin, |
| 75 std::string::const_iterator challenge_end); | 113 std::string::const_iterator challenge_end); |
| 76 | 114 |
| 77 // Given an input token received from the server, generate the next output | 115 // Given an input token received from the server, generate the next output |
| 78 // token to be sent to the server. | 116 // token to be sent to the server. |
| 79 int GetNextToken(const void* in_token, | 117 int GetNextToken(const void* in_token, |
| 80 uint32 in_token_len, | 118 uint32 in_token_len, |
| 81 void** out_token, | 119 void** out_token, |
| 82 uint32* out_token_len); | 120 uint32* out_token_len); |
| 83 | 121 |
| 122 #if defined(NTLM_SSPI) |
| 123 void ResetSecurityContext(); |
| 124 #endif |
| 125 |
| 126 #if defined(NTLM_PORTABLE) |
| 84 static GenerateRandomProc generate_random_proc_; | 127 static GenerateRandomProc generate_random_proc_; |
| 85 static HostNameProc get_host_name_proc_; | 128 static HostNameProc get_host_name_proc_; |
| 129 #endif |
| 86 | 130 |
| 131 protected: |
| 87 string16 domain_; | 132 string16 domain_; |
| 88 string16 username_; | 133 string16 username_; |
| 89 string16 password_; | 134 string16 password_; |
| 90 | 135 |
| 91 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or | 136 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or |
| 92 // "Proxy-Authenticate" response header. | 137 // "Proxy-Authenticate" response header. |
| 93 std::string auth_data_; | 138 std::string auth_data_; |
| 139 |
| 140 #if defined(NTLM_SSPI) |
| 141 ULONG max_token_len_; |
| 142 CredHandle cred_; |
| 143 CtxtHandle ctxt_; |
| 144 #endif |
| 94 }; | 145 }; |
| 95 | 146 |
| 96 } // namespace net | 147 } // namespace net |
| 97 | 148 |
| 98 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 149 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
| OLD | NEW |