1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.haxx.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
22 *
23 ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
28
29 #include <curl/curl.h>
30
31 #include "vauth/vauth.h"
32 #include "urldata.h"
33 #include "curl_base64.h"
34 #include "warnless.h"
35 #include "curl_multibyte.h"
36 #include "sendf.h"
37 #include "strerror.h"
38
39 /* The last #include files should be: */
40 #include "curl_memory.h"
41 #include "memdebug.h"
42
43 /*
44 * Curl_auth_is_spnego_supported()
45 *
46 * This is used to evaluate if SPNEGO (Negotiate) is supported.
47 *
48 * Parameters: None
49 *
50 * Returns TRUE if Negotiate is supported by Windows SSPI.
51 */
Curl_auth_is_spnego_supported(void)52 bool Curl_auth_is_spnego_supported(void)
53 {
54 PSecPkgInfo SecurityPackage;
55 SECURITY_STATUS status;
56
57 /* Query the security package for Negotiate */
58 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
59 TEXT(SP_NAME_NEGOTIATE),
60 &SecurityPackage);
61
62 return (status == SEC_E_OK ? TRUE : FALSE);
63 }
64
65 /*
66 * Curl_auth_decode_spnego_message()
67 *
68 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
69 * message.
70 *
71 * Parameters:
72 *
73 * data [in] - The session handle.
74 * user [in] - The user name in the format User or Domain\User.
75 * password [in] - The user's password.
76 * service [in] - The service type such as http, smtp, pop or imap.
77 * host [in] - The host name.
78 * chlg64 [in] - The optional base64 encoded challenge message.
79 * nego [in/out] - The Negotiate data struct being used and modified.
80 *
81 * Returns CURLE_OK on success.
82 */
Curl_auth_decode_spnego_message(struct Curl_easy * data,const char * user,const char * password,const char * service,const char * host,const char * chlg64,struct negotiatedata * nego)83 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
84 const char *user,
85 const char *password,
86 const char *service,
87 const char *host,
88 const char *chlg64,
89 struct negotiatedata *nego)
90 {
91 CURLcode result = CURLE_OK;
92 size_t chlglen = 0;
93 unsigned char *chlg = NULL;
94 PSecPkgInfo SecurityPackage;
95 SecBuffer chlg_buf[2];
96 SecBuffer resp_buf;
97 SecBufferDesc chlg_desc;
98 SecBufferDesc resp_desc;
99 unsigned long attrs;
100 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
101
102 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
103 (void) data;
104 #endif
105
106 if(nego->context && nego->status == SEC_E_OK) {
107 /* We finished successfully our part of authentication, but server
108 * rejected it (since we're again here). Exit with an error since we
109 * can't invent anything better */
110 Curl_auth_spnego_cleanup(nego);
111 return CURLE_LOGIN_DENIED;
112 }
113
114 if(!nego->spn) {
115 /* Generate our SPN */
116 nego->spn = Curl_auth_build_spn(service, host, NULL);
117 if(!nego->spn)
118 return CURLE_OUT_OF_MEMORY;
119 }
120
121 if(!nego->output_token) {
122 /* Query the security package for Negotiate */
123 nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
124 TEXT(SP_NAME_NEGOTIATE),
125 &SecurityPackage);
126 if(nego->status != SEC_E_OK)
127 return CURLE_NOT_BUILT_IN;
128
129 nego->token_max = SecurityPackage->cbMaxToken;
130
131 /* Release the package buffer as it is not required anymore */
132 s_pSecFn->FreeContextBuffer(SecurityPackage);
133
134 /* Allocate our output buffer */
135 nego->output_token = malloc(nego->token_max);
136 if(!nego->output_token)
137 return CURLE_OUT_OF_MEMORY;
138 }
139
140 if(!nego->credentials) {
141 /* Do we have credentials to use or are we using single sign-on? */
142 if(user && *user) {
143 /* Populate our identity structure */
144 result = Curl_create_sspi_identity(user, password, &nego->identity);
145 if(result)
146 return result;
147
148 /* Allow proper cleanup of the identity structure */
149 nego->p_identity = &nego->identity;
150 }
151 else
152 /* Use the current Windows user */
153 nego->p_identity = NULL;
154
155 /* Allocate our credentials handle */
156 nego->credentials = calloc(1, sizeof(CredHandle));
157 if(!nego->credentials)
158 return CURLE_OUT_OF_MEMORY;
159
160 /* Acquire our credentials handle */
161 nego->status =
162 s_pSecFn->AcquireCredentialsHandle(NULL,
163 (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
164 SECPKG_CRED_OUTBOUND, NULL,
165 nego->p_identity, NULL, NULL,
166 nego->credentials, &expiry);
167 if(nego->status != SEC_E_OK)
168 return CURLE_LOGIN_DENIED;
169
170 /* Allocate our new context handle */
171 nego->context = calloc(1, sizeof(CtxtHandle));
172 if(!nego->context)
173 return CURLE_OUT_OF_MEMORY;
174 }
175
176 if(chlg64 && *chlg64) {
177 /* Decode the base-64 encoded challenge message */
178 if(*chlg64 != '=') {
179 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
180 if(result)
181 return result;
182 }
183
184 /* Ensure we have a valid challenge message */
185 if(!chlg) {
186 infof(data, "SPNEGO handshake failure (empty challenge message)\n");
187
188 return CURLE_BAD_CONTENT_ENCODING;
189 }
190
191 /* Setup the challenge "input" security buffer */
192 chlg_desc.ulVersion = SECBUFFER_VERSION;
193 chlg_desc.cBuffers = 1;
194 chlg_desc.pBuffers = &chlg_buf[0];
195 chlg_buf[0].BufferType = SECBUFFER_TOKEN;
196 chlg_buf[0].pvBuffer = chlg;
197 chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
198
199 #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
200 /* ssl context comes from Schannel.
201 * When extended protection is used in IIS server,
202 * we have to pass a second SecBuffer to the SecBufferDesc
203 * otherwise IIS will not pass the authentication (401 response).
204 * Minimum supported version is Windows 7.
205 * https://docs.microsoft.com/en-us/security-updates
206 * /SecurityAdvisories/2009/973811
207 */
208 if(nego->sslContext) {
209 SEC_CHANNEL_BINDINGS channelBindings;
210 SecPkgContext_Bindings pkgBindings;
211 pkgBindings.Bindings = &channelBindings;
212 nego->status = s_pSecFn->QueryContextAttributes(
213 nego->sslContext,
214 SECPKG_ATTR_ENDPOINT_BINDINGS,
215 &pkgBindings
216 );
217 if(nego->status == SEC_E_OK) {
218 chlg_desc.cBuffers++;
219 chlg_buf[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
220 chlg_buf[1].cbBuffer = pkgBindings.BindingsLength;
221 chlg_buf[1].pvBuffer = pkgBindings.Bindings;
222 }
223 }
224 #endif
225 }
226
227 /* Setup the response "output" security buffer */
228 resp_desc.ulVersion = SECBUFFER_VERSION;
229 resp_desc.cBuffers = 1;
230 resp_desc.pBuffers = &resp_buf;
231 resp_buf.BufferType = SECBUFFER_TOKEN;
232 resp_buf.pvBuffer = nego->output_token;
233 resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
234
235 /* Generate our challenge-response message */
236 nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
237 chlg ? nego->context :
238 NULL,
239 nego->spn,
240 ISC_REQ_CONFIDENTIALITY,
241 0, SECURITY_NATIVE_DREP,
242 chlg ? &chlg_desc : NULL,
243 0, nego->context,
244 &resp_desc, &attrs,
245 &expiry);
246
247 /* Free the decoded challenge as it is not required anymore */
248 free(chlg);
249
250 if(GSS_ERROR(nego->status)) {
251 char buffer[STRERROR_LEN];
252 failf(data, "InitializeSecurityContext failed: %s",
253 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
254 return CURLE_OUT_OF_MEMORY;
255 }
256
257 if(nego->status == SEC_I_COMPLETE_NEEDED ||
258 nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
259 nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
260 if(GSS_ERROR(nego->status)) {
261 return CURLE_RECV_ERROR;
262 }
263 }
264
265 nego->output_token_length = resp_buf.cbBuffer;
266
267 return result;
268 }
269
270 /*
271 * Curl_auth_create_spnego_message()
272 *
273 * This is used to generate an already encoded SPNEGO (Negotiate) response
274 * message ready for sending to the recipient.
275 *
276 * Parameters:
277 *
278 * data [in] - The session handle.
279 * nego [in/out] - The Negotiate data struct being used and modified.
280 * outptr [in/out] - The address where a pointer to newly allocated memory
281 * holding the result will be stored upon completion.
282 * outlen [out] - The length of the output message.
283 *
284 * Returns CURLE_OK on success.
285 */
Curl_auth_create_spnego_message(struct Curl_easy * data,struct negotiatedata * nego,char ** outptr,size_t * outlen)286 CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
287 struct negotiatedata *nego,
288 char **outptr, size_t *outlen)
289 {
290 CURLcode result;
291
292 /* Base64 encode the already generated response */
293 result = Curl_base64_encode(data,
294 (const char *) nego->output_token,
295 nego->output_token_length,
296 outptr, outlen);
297
298 if(result)
299 return result;
300
301 if(!*outptr || !*outlen) {
302 free(*outptr);
303 return CURLE_REMOTE_ACCESS_DENIED;
304 }
305
306 return CURLE_OK;
307 }
308
309 /*
310 * Curl_auth_spnego_cleanup()
311 *
312 * This is used to clean up the SPNEGO (Negotiate) specific data.
313 *
314 * Parameters:
315 *
316 * nego [in/out] - The Negotiate data struct being cleaned up.
317 *
318 */
Curl_auth_spnego_cleanup(struct negotiatedata * nego)319 void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
320 {
321 /* Free our security context */
322 if(nego->context) {
323 s_pSecFn->DeleteSecurityContext(nego->context);
324 free(nego->context);
325 nego->context = NULL;
326 }
327
328 /* Free our credentials handle */
329 if(nego->credentials) {
330 s_pSecFn->FreeCredentialsHandle(nego->credentials);
331 free(nego->credentials);
332 nego->credentials = NULL;
333 }
334
335 /* Free our identity */
336 Curl_sspi_free_identity(nego->p_identity);
337 nego->p_identity = NULL;
338
339 /* Free the SPN and output token */
340 Curl_safefree(nego->spn);
341 Curl_safefree(nego->output_token);
342
343 /* Reset any variables */
344 nego->status = 0;
345 nego->token_max = 0;
346 nego->state = GSS_AUTHNONE;
347 nego->noauthpersist = FALSE;
348 nego->havenoauthpersist = FALSE;
349 nego->havenegdata = FALSE;
350 nego->havemultiplerequests = FALSE;
351 }
352
353 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */
354