1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
30 * https://davenport.sourceforge.io/ntlm.html
31 * https://www.innovation.ch/java/ntlm.html
39 #include "http_ntlm.h"
40 #include "curl_ntlm_core.h"
41 #include "curl_ntlm_wb.h"
42 #include "curl_base64.h"
43 #include "vauth/vauth.h"
46 /* SSL backend-specific #if branches in this file must be kept in the order
47 documented in curl_ntlm_core. */
48 #if defined(USE_WINDOWS_SSPI)
49 #include "curl_sspi.h"
52 /* The last 3 #include files should be in this order */
53 #include "curl_printf.h"
54 #include "curl_memory.h"
58 # define DEBUG_OUT(x) x
60 # define DEBUG_OUT(x) Curl_nop_stmt
63 CURLcode Curl_input_ntlm(struct Curl_easy *data,
64 bool proxy, /* if proxy or not */
65 const char *header) /* rest of the www-authenticate:
68 /* point to the correct struct with this */
69 struct ntlmdata *ntlm;
71 CURLcode result = CURLE_OK;
72 struct connectdata *conn = data->conn;
74 ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
75 state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
77 if(checkprefix("NTLM", header)) {
78 header += strlen("NTLM");
80 while(*header && ISSPACE(*header))
87 result = Curl_base64_decode(header, &hdr, &hdrlen);
91 Curl_bufref_init(&hdrbuf);
92 Curl_bufref_set(&hdrbuf, hdr, hdrlen, curl_free);
93 result = Curl_auth_decode_ntlm_type2_message(data, &hdrbuf, ntlm);
94 Curl_bufref_free(&hdrbuf);
99 *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
102 if(*state == NTLMSTATE_LAST) {
103 infof(data, "NTLM auth restarted");
104 Curl_http_auth_cleanup_ntlm(conn);
106 else if(*state == NTLMSTATE_TYPE3) {
107 infof(data, "NTLM handshake rejected");
108 Curl_http_auth_cleanup_ntlm(conn);
109 *state = NTLMSTATE_NONE;
110 return CURLE_REMOTE_ACCESS_DENIED;
112 else if(*state >= NTLMSTATE_TYPE1) {
113 infof(data, "NTLM handshake failure (internal error)");
114 return CURLE_REMOTE_ACCESS_DENIED;
117 *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
125 * This is for creating ntlm header output
127 CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
131 CURLcode result = CURLE_OK;
132 struct bufref ntlmmsg;
134 /* point to the address of the pointer that holds the string to send to the
135 server, which is for a plain host or for a HTTP proxy */
138 /* point to the username, password, service and host */
141 const char *service = NULL;
142 const char *hostname = NULL;
144 /* point to the correct struct with this */
145 struct ntlmdata *ntlm;
148 struct connectdata *conn = data->conn;
154 #ifndef CURL_DISABLE_PROXY
155 allocuserpwd = &data->state.aptr.proxyuserpwd;
156 userp = data->state.aptr.proxyuser;
157 passwdp = data->state.aptr.proxypasswd;
158 service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
159 data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
160 hostname = conn->http_proxy.host.name;
161 ntlm = &conn->proxyntlm;
162 state = &conn->proxy_ntlm_state;
163 authp = &data->state.authproxy;
165 return CURLE_NOT_BUILT_IN;
169 allocuserpwd = &data->state.aptr.userpwd;
170 userp = data->state.aptr.user;
171 passwdp = data->state.aptr.passwd;
172 service = data->set.str[STRING_SERVICE_NAME] ?
173 data->set.str[STRING_SERVICE_NAME] : "HTTP";
174 hostname = conn->host.name;
176 state = &conn->http_ntlm_state;
177 authp = &data->state.authhost;
181 /* not set means empty */
188 #ifdef USE_WINDOWS_SSPI
190 /* not thread safe and leaks - use curl_global_init() to avoid */
191 CURLcode err = Curl_sspi_global_init();
195 #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
196 ntlm->sslContext = conn->sslContext;
200 Curl_bufref_init(&ntlmmsg);
202 /* connection is already authenticated, don't send a header in future
203 * requests so go directly to NTLMSTATE_LAST */
204 if(*state == NTLMSTATE_TYPE3)
205 *state = NTLMSTATE_LAST;
208 case NTLMSTATE_TYPE1:
209 default: /* for the weird cases we (re)start here */
210 /* Create a type-1 message */
211 result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp,
215 DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
216 result = Curl_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
217 Curl_bufref_len(&ntlmmsg), &base64, &len);
220 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
221 proxy ? "Proxy-" : "",
225 result = CURLE_OUT_OF_MEMORY;
230 case NTLMSTATE_TYPE2:
231 /* We already received the type-2 message, create a type-3 message */
232 result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
234 if(!result && Curl_bufref_len(&ntlmmsg)) {
235 result = Curl_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
236 Curl_bufref_len(&ntlmmsg), &base64, &len);
239 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
240 proxy ? "Proxy-" : "",
244 result = CURLE_OUT_OF_MEMORY;
246 *state = NTLMSTATE_TYPE3; /* we send a type-3 */
254 Curl_safefree(*allocuserpwd);
258 Curl_bufref_free(&ntlmmsg);
263 void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
265 Curl_auth_cleanup_ntlm(&conn->ntlm);
266 Curl_auth_cleanup_ntlm(&conn->proxyntlm);
268 #if defined(NTLM_WB_ENABLED)
269 Curl_http_auth_cleanup_ntlm_wb(conn);
273 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */