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"
39 /* The last #include files should be: */
40 #include "curl_memory.h"
44 * Curl_auth_is_spnego_supported()
46 * This is used to evaluate if SPNEGO (Negotiate) is supported.
50 * Returns TRUE if Negotiate is supported by Windows SSPI.
52 bool Curl_auth_is_spnego_supported(void)
54 PSecPkgInfo SecurityPackage;
55 SECURITY_STATUS status;
57 /* Query the security package for Negotiate */
58 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
59 TEXT(SP_NAME_NEGOTIATE),
62 return (status == SEC_E_OK ? TRUE : FALSE);
66 * Curl_auth_decode_spnego_message()
68 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
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.
81 * Returns CURLE_OK on success.
83 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
89 struct negotiatedata *nego)
91 CURLcode result = CURLE_OK;
93 unsigned char *chlg = NULL;
94 PSecPkgInfo SecurityPackage;
97 SecBufferDesc chlg_desc;
98 SecBufferDesc resp_desc;
100 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
102 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
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;
115 /* Generate our SPN */
116 nego->spn = Curl_auth_build_spn(service, host, NULL);
118 return CURLE_OUT_OF_MEMORY;
121 if(!nego->output_token) {
122 /* Query the security package for Negotiate */
123 nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
124 TEXT(SP_NAME_NEGOTIATE),
126 if(nego->status != SEC_E_OK)
127 return CURLE_NOT_BUILT_IN;
129 nego->token_max = SecurityPackage->cbMaxToken;
131 /* Release the package buffer as it is not required anymore */
132 s_pSecFn->FreeContextBuffer(SecurityPackage);
134 /* Allocate our output buffer */
135 nego->output_token = malloc(nego->token_max);
136 if(!nego->output_token)
137 return CURLE_OUT_OF_MEMORY;
140 if(!nego->credentials) {
141 /* Do we have credientials to use or are we using single sign-on? */
143 /* Populate our identity structure */
144 result = Curl_create_sspi_identity(user, password, &nego->identity);
148 /* Allow proper cleanup of the identity structure */
149 nego->p_identity = &nego->identity;
152 /* Use the current Windows user */
153 nego->p_identity = NULL;
155 /* Allocate our credentials handle */
156 nego->credentials = malloc(sizeof(CredHandle));
157 if(!nego->credentials)
158 return CURLE_OUT_OF_MEMORY;
160 memset(nego->credentials, 0, sizeof(CredHandle));
162 /* Acquire our credentials handle */
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;
172 /* Allocate our new context handle */
173 nego->context = malloc(sizeof(CtxtHandle));
175 return CURLE_OUT_OF_MEMORY;
177 memset(nego->context, 0, sizeof(CtxtHandle));
180 if(chlg64 && *chlg64) {
181 /* Decode the base-64 encoded challenge message */
183 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
188 /* Ensure we have a valid challenge message */
190 infof(data, "SPNEGO handshake failure (empty challenge message)\n");
192 return CURLE_BAD_CONTENT_ENCODING;
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);
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);
212 /* Generate our challenge-response message */
213 nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
214 chlg ? nego->context :
217 ISC_REQ_CONFIDENTIALITY,
218 0, SECURITY_NATIVE_DREP,
219 chlg ? &chlg_desc : NULL,
224 /* Free the decoded challenge as it is not required anymore */
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;
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;
241 nego->output_token_length = resp_buf.cbBuffer;
247 * Curl_auth_create_spnego_message()
249 * This is used to generate an already encoded SPNEGO (Negotiate) response
250 * message ready for sending to the recipient.
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.
260 * Returns CURLE_OK on success.
262 CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
263 struct negotiatedata *nego,
264 char **outptr, size_t *outlen)
268 /* Base64 encode the already generated response */
269 result = Curl_base64_encode(data,
270 (const char *) nego->output_token,
271 nego->output_token_length,
277 if(!*outptr || !*outlen) {
279 return CURLE_REMOTE_ACCESS_DENIED;
286 * Curl_auth_spnego_cleanup()
288 * This is used to clean up the SPNEGO (Negotiate) specific data.
292 * nego [in/out] - The Negotiate data struct being cleaned up.
295 void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
297 /* Free our security context */
299 s_pSecFn->DeleteSecurityContext(nego->context);
301 nego->context = NULL;
304 /* Free our credentials handle */
305 if(nego->credentials) {
306 s_pSecFn->FreeCredentialsHandle(nego->credentials);
307 free(nego->credentials);
308 nego->credentials = NULL;
311 /* Free our identity */
312 Curl_sspi_free_identity(nego->p_identity);
313 nego->p_identity = NULL;
315 /* Free the SPN and output token */
316 Curl_safefree(nego->spn);
317 Curl_safefree(nego->output_token);
319 /* Reset any variables */
324 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */