1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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.haxx.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 * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
29 #include <curl/curl.h>
31 #include "vauth/vauth.h"
33 #include "curl_base64.h"
35 #include "curl_multibyte.h"
38 /* The last #include files should be: */
39 #include "curl_memory.h"
43 * Curl_auth_is_spnego_supported()
45 * This is used to evaluate if SPNEGO (Negotiate) is supported.
49 * Returns TRUE if Negotiate is supported by Windows SSPI.
51 bool Curl_auth_is_spnego_supported(void)
53 PSecPkgInfo SecurityPackage;
54 SECURITY_STATUS status;
56 /* Query the security package for Negotiate */
57 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
58 TEXT(SP_NAME_NEGOTIATE),
61 return (status == SEC_E_OK ? TRUE : FALSE);
65 * Curl_auth_decode_spnego_message()
67 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
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.
80 * Returns CURLE_OK on success.
82 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
88 struct negotiatedata *nego)
90 CURLcode result = CURLE_OK;
92 unsigned char *chlg = NULL;
93 PSecPkgInfo SecurityPackage;
96 SecBufferDesc chlg_desc;
97 SecBufferDesc resp_desc;
99 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
101 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
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;
114 /* Generate our SPN */
115 nego->spn = Curl_auth_build_spn(service, host, NULL);
117 return CURLE_OUT_OF_MEMORY;
120 if(!nego->output_token) {
121 /* Query the security package for Negotiate */
122 nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
123 TEXT(SP_NAME_NEGOTIATE),
125 if(nego->status != SEC_E_OK)
126 return CURLE_NOT_BUILT_IN;
128 nego->token_max = SecurityPackage->cbMaxToken;
130 /* Release the package buffer as it is not required anymore */
131 s_pSecFn->FreeContextBuffer(SecurityPackage);
133 /* Allocate our output buffer */
134 nego->output_token = malloc(nego->token_max);
135 if(!nego->output_token)
136 return CURLE_OUT_OF_MEMORY;
139 if(!nego->credentials) {
140 /* Do we have credientials to use or are we using single sign-on? */
142 /* Populate our identity structure */
143 result = Curl_create_sspi_identity(user, password, &nego->identity);
147 /* Allow proper cleanup of the identity structure */
148 nego->p_identity = &nego->identity;
151 /* Use the current Windows user */
152 nego->p_identity = NULL;
154 /* Allocate our credentials handle */
155 nego->credentials = malloc(sizeof(CredHandle));
156 if(!nego->credentials)
157 return CURLE_OUT_OF_MEMORY;
159 memset(nego->credentials, 0, sizeof(CredHandle));
161 /* Acquire our credentials handle */
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;
171 /* Allocate our new context handle */
172 nego->context = malloc(sizeof(CtxtHandle));
174 return CURLE_OUT_OF_MEMORY;
176 memset(nego->context, 0, sizeof(CtxtHandle));
179 if(chlg64 && *chlg64) {
180 /* Decode the base-64 encoded challenge message */
182 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
187 /* Ensure we have a valid challenge message */
189 infof(data, "SPNEGO handshake failure (empty challenge message)\n");
191 return CURLE_BAD_CONTENT_ENCODING;
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);
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);
211 /* Generate our challenge-response message */
212 nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
213 chlg ? nego->context :
216 ISC_REQ_CONFIDENTIALITY,
217 0, SECURITY_NATIVE_DREP,
218 chlg ? &chlg_desc : NULL,
223 /* Free the decoded challenge as it is not required anymore */
226 if(GSS_ERROR(nego->status)) {
227 return CURLE_OUT_OF_MEMORY;
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;
238 nego->output_token_length = resp_buf.cbBuffer;
244 * Curl_auth_create_spnego_message()
246 * This is used to generate an already encoded SPNEGO (Negotiate) response
247 * message ready for sending to the recipient.
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.
257 * Returns CURLE_OK on success.
259 CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
260 struct negotiatedata *nego,
261 char **outptr, size_t *outlen)
265 /* Base64 encode the already generated response */
266 result = Curl_base64_encode(data,
267 (const char *) nego->output_token,
268 nego->output_token_length,
274 if(!*outptr || !*outlen) {
276 return CURLE_REMOTE_ACCESS_DENIED;
283 * Curl_auth_spnego_cleanup()
285 * This is used to clean up the SPNEGO (Negotiate) specific data.
289 * nego [in/out] - The Negotiate data struct being cleaned up.
292 void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
294 /* Free our security context */
296 s_pSecFn->DeleteSecurityContext(nego->context);
298 nego->context = NULL;
301 /* Free our credentials handle */
302 if(nego->credentials) {
303 s_pSecFn->FreeCredentialsHandle(nego->credentials);
304 free(nego->credentials);
305 nego->credentials = NULL;
308 /* Free our identity */
309 Curl_sspi_free_identity(nego->p_identity);
310 nego->p_identity = NULL;
312 /* Free the SPN and output token */
313 Curl_safefree(nego->spn);
314 Curl_safefree(nego->output_token);
316 /* Reset any variables */
321 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */