OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | |
6 #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | |
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 #include "net/http/http_auth_sspi_win.h" | |
23 #endif | |
24 | |
25 #include <string> | |
26 | |
27 #include "base/basictypes.h" | |
28 #include "base/strings/string16.h" | |
29 #include "net/http/http_auth_handler.h" | |
30 #include "net/http/http_auth_handler_factory.h" | |
31 | |
32 namespace net { | |
33 | |
34 class URLSecurityManager; | |
35 | |
36 // Code for handling HTTP NTLM authentication. | |
37 class NET_EXPORT_PRIVATE HttpAuthHandlerNTLM : public HttpAuthHandler { | |
38 public: | |
39 class Factory : public HttpAuthHandlerFactory { | |
40 public: | |
41 Factory(); | |
42 ~Factory() override; | |
43 | |
44 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge, | |
45 HttpAuth::Target target, | |
46 const GURL& origin, | |
47 CreateReason reason, | |
48 int digest_nonce_count, | |
49 const BoundNetLog& net_log, | |
50 scoped_ptr<HttpAuthHandler>* handler) override; | |
51 #if defined(NTLM_SSPI) | |
52 // Set the SSPILibrary to use. Typically the only callers which need to use | |
53 // this are unit tests which pass in a mocked-out version of the SSPI | |
54 // library. After the call |sspi_library| will be owned by this Factory and | |
55 // will be destroyed when the Factory is destroyed. | |
56 void set_sspi_library(SSPILibrary* sspi_library) { | |
57 sspi_library_.reset(sspi_library); | |
58 } | |
59 #endif // defined(NTLM_SSPI) | |
60 private: | |
61 #if defined(NTLM_SSPI) | |
62 ULONG max_token_length_; | |
63 bool first_creation_; | |
64 bool is_unsupported_; | |
65 scoped_ptr<SSPILibrary> sspi_library_; | |
66 #endif // defined(NTLM_SSPI) | |
67 }; | |
68 | |
69 #if defined(NTLM_PORTABLE) | |
70 // A function that generates n random bytes in the output buffer. | |
71 typedef void (*GenerateRandomProc)(uint8* output, size_t n); | |
72 | |
73 // A function that returns the local host name. Returns an empty string if | |
74 // the local host name is not available. | |
75 typedef std::string (*HostNameProc)(); | |
76 | |
77 // For unit tests to override and restore the GenerateRandom and | |
78 // GetHostName functions. | |
79 class ScopedProcSetter { | |
80 public: | |
81 ScopedProcSetter(GenerateRandomProc random_proc, | |
82 HostNameProc host_name_proc) { | |
83 old_random_proc_ = SetGenerateRandomProc(random_proc); | |
84 old_host_name_proc_ = SetHostNameProc(host_name_proc); | |
85 } | |
86 | |
87 ~ScopedProcSetter() { | |
88 SetGenerateRandomProc(old_random_proc_); | |
89 SetHostNameProc(old_host_name_proc_); | |
90 } | |
91 | |
92 private: | |
93 GenerateRandomProc old_random_proc_; | |
94 HostNameProc old_host_name_proc_; | |
95 }; | |
96 #endif | |
97 | |
98 #if defined(NTLM_PORTABLE) | |
99 HttpAuthHandlerNTLM(); | |
100 #endif | |
101 #if defined(NTLM_SSPI) | |
102 HttpAuthHandlerNTLM(SSPILibrary* sspi_library, ULONG max_token_length, | |
103 URLSecurityManager* url_security_manager); | |
104 #endif | |
105 | |
106 bool NeedsIdentity() override; | |
107 | |
108 bool AllowsDefaultCredentials() override; | |
109 | |
110 HttpAuth::AuthorizationResult HandleAnotherChallenge( | |
111 HttpAuthChallengeTokenizer* challenge) override; | |
112 | |
113 protected: | |
114 // This function acquires a credentials handle in the SSPI implementation. | |
115 // It does nothing in the portable implementation. | |
116 int InitializeBeforeFirstChallenge(); | |
117 | |
118 bool Init(HttpAuthChallengeTokenizer* tok) override; | |
119 | |
120 int GenerateAuthTokenImpl(const AuthCredentials* credentials, | |
121 const HttpRequestInfo* request, | |
122 const CompletionCallback& callback, | |
123 std::string* auth_token) override; | |
124 | |
125 private: | |
126 ~HttpAuthHandlerNTLM() override; | |
127 | |
128 #if defined(NTLM_PORTABLE) | |
129 // For unit tests to override the GenerateRandom and GetHostName functions. | |
130 // Returns the old function. | |
131 static GenerateRandomProc SetGenerateRandomProc(GenerateRandomProc proc); | |
132 static HostNameProc SetHostNameProc(HostNameProc proc); | |
133 #endif | |
134 | |
135 // Parse the challenge, saving the results into this instance. | |
136 HttpAuth::AuthorizationResult ParseChallenge( | |
137 HttpAuthChallengeTokenizer* tok, bool initial_challenge); | |
138 | |
139 // Given an input token received from the server, generate the next output | |
140 // token to be sent to the server. | |
141 int GetNextToken(const void* in_token, | |
142 uint32 in_token_len, | |
143 void** out_token, | |
144 uint32* out_token_len); | |
145 | |
146 // Create an NTLM SPN to identify the |origin| server. | |
147 static std::string CreateSPN(const GURL& origin); | |
148 | |
149 #if defined(NTLM_SSPI) | |
150 HttpAuthSSPI auth_sspi_; | |
151 #endif | |
152 | |
153 #if defined(NTLM_PORTABLE) | |
154 static GenerateRandomProc generate_random_proc_; | |
155 static HostNameProc get_host_name_proc_; | |
156 #endif | |
157 | |
158 base::string16 domain_; | |
159 AuthCredentials credentials_; | |
160 | |
161 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or | |
162 // "Proxy-Authenticate" response header. | |
163 std::string auth_data_; | |
164 | |
165 #if defined(NTLM_SSPI) | |
166 URLSecurityManager* url_security_manager_; | |
167 #endif | |
168 }; | |
169 | |
170 } // namespace net | |
171 | |
172 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | |
OLD | NEW |