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.
5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_
6 #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_
8 #include "build/build_config.h"
10 // This contains the portable and the SSPI implementations for NTLM.
11 // We use NTLM_SSPI for Windows, and NTLM_PORTABLE for other platforms.
18 #if defined(NTLM_SSPI)
19 #define SECURITY_WIN32 1
22 #include "net/http/http_auth_sspi_win.h"
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"
34 class URLSecurityManager;
36 // Code for handling HTTP NTLM authentication.
37 class NET_EXPORT_PRIVATE HttpAuthHandlerNTLM : public HttpAuthHandler {
39 class Factory : public HttpAuthHandlerFactory {
44 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge,
45 HttpAuth::Target target,
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);
59 #endif // defined(NTLM_SSPI)
61 #if defined(NTLM_SSPI)
62 ULONG max_token_length_;
65 scoped_ptr<SSPILibrary> sspi_library_;
66 #endif // defined(NTLM_SSPI)
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);
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)();
77 // For unit tests to override and restore the GenerateRandom and
78 // GetHostName functions.
79 class ScopedProcSetter {
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);
88 SetGenerateRandomProc(old_random_proc_);
89 SetHostNameProc(old_host_name_proc_);
93 GenerateRandomProc old_random_proc_;
94 HostNameProc old_host_name_proc_;
98 #if defined(NTLM_PORTABLE)
99 HttpAuthHandlerNTLM();
101 #if defined(NTLM_SSPI)
102 HttpAuthHandlerNTLM(SSPILibrary* sspi_library, ULONG max_token_length,
103 URLSecurityManager* url_security_manager);
106 bool NeedsIdentity() override;
108 bool AllowsDefaultCredentials() override;
110 HttpAuth::AuthorizationResult HandleAnotherChallenge(
111 HttpAuthChallengeTokenizer* challenge) override;
114 // This function acquires a credentials handle in the SSPI implementation.
115 // It does nothing in the portable implementation.
116 int InitializeBeforeFirstChallenge();
118 bool Init(HttpAuthChallengeTokenizer* tok) override;
120 int GenerateAuthTokenImpl(const AuthCredentials* credentials,
121 const HttpRequestInfo* request,
122 const CompletionCallback& callback,
123 std::string* auth_token) override;
126 ~HttpAuthHandlerNTLM() override;
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);
135 // Parse the challenge, saving the results into this instance.
136 HttpAuth::AuthorizationResult ParseChallenge(
137 HttpAuthChallengeTokenizer* tok, bool initial_challenge);
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,
144 uint32* out_token_len);
146 // Create an NTLM SPN to identify the |origin| server.
147 static std::string CreateSPN(const GURL& origin);
149 #if defined(NTLM_SSPI)
150 HttpAuthSSPI auth_sspi_;
153 #if defined(NTLM_PORTABLE)
154 static GenerateRandomProc generate_random_proc_;
155 static HostNameProc get_host_name_proc_;
158 base::string16 domain_;
159 AuthCredentials credentials_;
161 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or
162 // "Proxy-Authenticate" response header.
163 std::string auth_data_;
165 #if defined(NTLM_SSPI)
166 URLSecurityManager* url_security_manager_;
172 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_