• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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  * userp       [in]     - The user name in the format User or Domain\User.
75  * passdwp     [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;
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 credientials 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 = malloc(sizeof(CredHandle));
157     if(!nego->credentials)
158       return CURLE_OUT_OF_MEMORY;
159 
160     memset(nego->credentials, 0, sizeof(CredHandle));
161 
162     /* Acquire our credentials handle */
163     nego->status =
164       s_pSecFn->AcquireCredentialsHandle(NULL,
165                                          (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
166                                          SECPKG_CRED_OUTBOUND, NULL,
167                                          nego->p_identity, NULL, NULL,
168                                          nego->credentials, &expiry);
169     if(nego->status != SEC_E_OK)
170       return CURLE_LOGIN_DENIED;
171 
172     /* Allocate our new context handle */
173     nego->context = malloc(sizeof(CtxtHandle));
174     if(!nego->context)
175       return CURLE_OUT_OF_MEMORY;
176 
177     memset(nego->context, 0, sizeof(CtxtHandle));
178   }
179 
180   if(chlg64 && *chlg64) {
181     /* Decode the base-64 encoded challenge message */
182     if(*chlg64 != '=') {
183       result = Curl_base64_decode(chlg64, &chlg, &chlglen);
184       if(result)
185         return result;
186     }
187 
188     /* Ensure we have a valid challenge message */
189     if(!chlg) {
190       infof(data, "SPNEGO handshake failure (empty challenge message)\n");
191 
192       return CURLE_BAD_CONTENT_ENCODING;
193     }
194 
195     /* Setup the challenge "input" security buffer */
196     chlg_desc.ulVersion = SECBUFFER_VERSION;
197     chlg_desc.cBuffers  = 1;
198     chlg_desc.pBuffers  = &chlg_buf;
199     chlg_buf.BufferType = SECBUFFER_TOKEN;
200     chlg_buf.pvBuffer   = chlg;
201     chlg_buf.cbBuffer   = curlx_uztoul(chlglen);
202   }
203 
204   /* Setup the response "output" security buffer */
205   resp_desc.ulVersion = SECBUFFER_VERSION;
206   resp_desc.cBuffers  = 1;
207   resp_desc.pBuffers  = &resp_buf;
208   resp_buf.BufferType = SECBUFFER_TOKEN;
209   resp_buf.pvBuffer   = nego->output_token;
210   resp_buf.cbBuffer   = curlx_uztoul(nego->token_max);
211 
212   /* Generate our challenge-response message */
213   nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
214                                                      chlg ? nego->context :
215                                                             NULL,
216                                                      nego->spn,
217                                                      ISC_REQ_CONFIDENTIALITY,
218                                                      0, SECURITY_NATIVE_DREP,
219                                                      chlg ? &chlg_desc : NULL,
220                                                      0, nego->context,
221                                                      &resp_desc, &attrs,
222                                                      &expiry);
223 
224   /* Free the decoded challenge as it is not required anymore */
225   free(chlg);
226 
227   if(GSS_ERROR(nego->status)) {
228     failf(data, "InitializeSecurityContext failed: %s",
229           Curl_sspi_strerror(data->easy_conn, nego->status));
230     return CURLE_OUT_OF_MEMORY;
231   }
232 
233   if(nego->status == SEC_I_COMPLETE_NEEDED ||
234      nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
235     nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
236     if(GSS_ERROR(nego->status)) {
237       return CURLE_RECV_ERROR;
238     }
239   }
240 
241   nego->output_token_length = resp_buf.cbBuffer;
242 
243   return result;
244 }
245 
246 /*
247  * Curl_auth_create_spnego_message()
248  *
249  * This is used to generate an already encoded SPNEGO (Negotiate) response
250  * message ready for sending to the recipient.
251  *
252  * Parameters:
253  *
254  * data        [in]     - The session handle.
255  * nego        [in/out] - The Negotiate data struct being used and modified.
256  * outptr      [in/out] - The address where a pointer to newly allocated memory
257  *                        holding the result will be stored upon completion.
258  * outlen      [out]    - The length of the output message.
259  *
260  * Returns CURLE_OK on success.
261  */
Curl_auth_create_spnego_message(struct Curl_easy * data,struct negotiatedata * nego,char ** outptr,size_t * outlen)262 CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
263                                          struct negotiatedata *nego,
264                                          char **outptr, size_t *outlen)
265 {
266   CURLcode result;
267 
268   /* Base64 encode the already generated response */
269   result = Curl_base64_encode(data,
270                               (const char *) nego->output_token,
271                               nego->output_token_length,
272                               outptr, outlen);
273 
274   if(result)
275     return result;
276 
277   if(!*outptr || !*outlen) {
278     free(*outptr);
279     return CURLE_REMOTE_ACCESS_DENIED;
280   }
281 
282   return CURLE_OK;
283 }
284 
285 /*
286  * Curl_auth_spnego_cleanup()
287  *
288  * This is used to clean up the SPNEGO (Negotiate) specific data.
289  *
290  * Parameters:
291  *
292  * nego     [in/out] - The Negotiate data struct being cleaned up.
293  *
294  */
Curl_auth_spnego_cleanup(struct negotiatedata * nego)295 void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
296 {
297   /* Free our security context */
298   if(nego->context) {
299     s_pSecFn->DeleteSecurityContext(nego->context);
300     free(nego->context);
301     nego->context = NULL;
302   }
303 
304   /* Free our credentials handle */
305   if(nego->credentials) {
306     s_pSecFn->FreeCredentialsHandle(nego->credentials);
307     free(nego->credentials);
308     nego->credentials = NULL;
309   }
310 
311   /* Free our identity */
312   Curl_sspi_free_identity(nego->p_identity);
313   nego->p_identity = NULL;
314 
315   /* Free the SPN and output token */
316   Curl_safefree(nego->spn);
317   Curl_safefree(nego->output_token);
318 
319   /* Reset any variables */
320   nego->status = 0;
321   nego->token_max = 0;
322 }
323 
324 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */
325