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