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_
;
64 scoped_ptr
<SSPILibrary
> sspi_library_
;
65 #endif // defined(NTLM_SSPI)
68 #if defined(NTLM_PORTABLE)
69 // A function that generates n random bytes in the output buffer.
70 typedef void (*GenerateRandomProc
)(uint8
* output
, size_t n
);
72 // A function that returns the local host name. Returns an empty string if
73 // the local host name is not available.
74 typedef std::string (*HostNameProc
)();
76 // For unit tests to override and restore the GenerateRandom and
77 // GetHostName functions.
78 class ScopedProcSetter
{
80 ScopedProcSetter(GenerateRandomProc random_proc
,
81 HostNameProc host_name_proc
) {
82 old_random_proc_
= SetGenerateRandomProc(random_proc
);
83 old_host_name_proc_
= SetHostNameProc(host_name_proc
);
87 SetGenerateRandomProc(old_random_proc_
);
88 SetHostNameProc(old_host_name_proc_
);
92 GenerateRandomProc old_random_proc_
;
93 HostNameProc old_host_name_proc_
;
97 #if defined(NTLM_PORTABLE)
98 HttpAuthHandlerNTLM();
100 #if defined(NTLM_SSPI)
101 HttpAuthHandlerNTLM(SSPILibrary
* sspi_library
, ULONG max_token_length
,
102 URLSecurityManager
* url_security_manager
);
105 bool NeedsIdentity() override
;
107 bool AllowsDefaultCredentials() override
;
109 HttpAuth::AuthorizationResult
HandleAnotherChallenge(
110 HttpAuthChallengeTokenizer
* challenge
) override
;
113 // This function acquires a credentials handle in the SSPI implementation.
114 // It does nothing in the portable implementation.
115 int InitializeBeforeFirstChallenge();
117 bool Init(HttpAuthChallengeTokenizer
* tok
) override
;
119 int GenerateAuthTokenImpl(const AuthCredentials
* credentials
,
120 const HttpRequestInfo
* request
,
121 const CompletionCallback
& callback
,
122 std::string
* auth_token
) override
;
125 ~HttpAuthHandlerNTLM() override
;
127 #if defined(NTLM_PORTABLE)
128 // For unit tests to override the GenerateRandom and GetHostName functions.
129 // Returns the old function.
130 static GenerateRandomProc
SetGenerateRandomProc(GenerateRandomProc proc
);
131 static HostNameProc
SetHostNameProc(HostNameProc proc
);
134 // Parse the challenge, saving the results into this instance.
135 HttpAuth::AuthorizationResult
ParseChallenge(
136 HttpAuthChallengeTokenizer
* tok
, bool initial_challenge
);
138 // Given an input token received from the server, generate the next output
139 // token to be sent to the server.
140 int GetNextToken(const void* in_token
,
143 uint32
* out_token_len
);
145 // Create an NTLM SPN to identify the |origin| server.
146 static std::string
CreateSPN(const GURL
& origin
);
148 #if defined(NTLM_SSPI)
149 HttpAuthSSPI auth_sspi_
;
152 #if defined(NTLM_PORTABLE)
153 static GenerateRandomProc generate_random_proc_
;
154 static HostNameProc get_host_name_proc_
;
157 base::string16 domain_
;
158 AuthCredentials credentials_
;
160 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or
161 // "Proxy-Authenticate" response header.
162 std::string auth_data_
;
164 #if defined(NTLM_SSPI)
165 URLSecurityManager
* url_security_manager_
;
171 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_