1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, 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.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 /* Release the package buffer as it is not required anymore */
63 if(status == SEC_E_OK) {
64 s_pSecFn->FreeContextBuffer(SecurityPackage);
68 return (status == SEC_E_OK ? TRUE : FALSE);
72 * Curl_auth_decode_spnego_message()
74 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
79 * data [in] - The session handle.
80 * user [in] - The user name in the format User or Domain\User.
81 * password [in] - The user's password.
82 * service [in] - The service type such as http, smtp, pop or imap.
83 * host [in] - The host name.
84 * chlg64 [in] - The optional base64 encoded challenge message.
85 * nego [in/out] - The Negotiate data struct being used and modified.
87 * Returns CURLE_OK on success.
89 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
95 struct negotiatedata *nego)
97 CURLcode result = CURLE_OK;
99 unsigned char *chlg = NULL;
100 PSecPkgInfo SecurityPackage;
101 SecBuffer chlg_buf[2];
103 SecBufferDesc chlg_desc;
104 SecBufferDesc resp_desc;
106 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
108 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
112 if(nego->context && nego->status == SEC_E_OK) {
113 /* We finished successfully our part of authentication, but server
114 * rejected it (since we're again here). Exit with an error since we
115 * can't invent anything better */
116 Curl_auth_cleanup_spnego(nego);
117 return CURLE_LOGIN_DENIED;
121 /* Generate our SPN */
122 nego->spn = Curl_auth_build_spn(service, host, NULL);
124 return CURLE_OUT_OF_MEMORY;
127 if(!nego->output_token) {
128 /* Query the security package for Negotiate */
129 nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
130 TEXT(SP_NAME_NEGOTIATE),
132 if(nego->status != SEC_E_OK) {
133 failf(data, "SSPI: couldn't get auth info");
134 return CURLE_AUTH_ERROR;
137 nego->token_max = SecurityPackage->cbMaxToken;
139 /* Release the package buffer as it is not required anymore */
140 s_pSecFn->FreeContextBuffer(SecurityPackage);
142 /* Allocate our output buffer */
143 nego->output_token = malloc(nego->token_max);
144 if(!nego->output_token)
145 return CURLE_OUT_OF_MEMORY;
148 if(!nego->credentials) {
149 /* Do we have credentials to use or are we using single sign-on? */
151 /* Populate our identity structure */
152 result = Curl_create_sspi_identity(user, password, &nego->identity);
156 /* Allow proper cleanup of the identity structure */
157 nego->p_identity = &nego->identity;
160 /* Use the current Windows user */
161 nego->p_identity = NULL;
163 /* Allocate our credentials handle */
164 nego->credentials = calloc(1, sizeof(CredHandle));
165 if(!nego->credentials)
166 return CURLE_OUT_OF_MEMORY;
168 /* Acquire our credentials handle */
170 s_pSecFn->AcquireCredentialsHandle(NULL,
171 (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
172 SECPKG_CRED_OUTBOUND, NULL,
173 nego->p_identity, NULL, NULL,
174 nego->credentials, &expiry);
175 if(nego->status != SEC_E_OK)
176 return CURLE_AUTH_ERROR;
178 /* Allocate our new context handle */
179 nego->context = calloc(1, sizeof(CtxtHandle));
181 return CURLE_OUT_OF_MEMORY;
184 if(chlg64 && *chlg64) {
185 /* Decode the base-64 encoded challenge message */
187 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
192 /* Ensure we have a valid challenge message */
194 infof(data, "SPNEGO handshake failure (empty challenge message)");
195 return CURLE_BAD_CONTENT_ENCODING;
198 /* Setup the challenge "input" security buffer */
199 chlg_desc.ulVersion = SECBUFFER_VERSION;
200 chlg_desc.cBuffers = 1;
201 chlg_desc.pBuffers = &chlg_buf[0];
202 chlg_buf[0].BufferType = SECBUFFER_TOKEN;
203 chlg_buf[0].pvBuffer = chlg;
204 chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
206 #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
207 /* ssl context comes from Schannel.
208 * When extended protection is used in IIS server,
209 * we have to pass a second SecBuffer to the SecBufferDesc
210 * otherwise IIS will not pass the authentication (401 response).
211 * Minimum supported version is Windows 7.
212 * https://docs.microsoft.com/en-us/security-updates
213 * /SecurityAdvisories/2009/973811
215 if(nego->sslContext) {
216 SEC_CHANNEL_BINDINGS channelBindings;
217 SecPkgContext_Bindings pkgBindings;
218 pkgBindings.Bindings = &channelBindings;
219 nego->status = s_pSecFn->QueryContextAttributes(
221 SECPKG_ATTR_ENDPOINT_BINDINGS,
224 if(nego->status == SEC_E_OK) {
225 chlg_desc.cBuffers++;
226 chlg_buf[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
227 chlg_buf[1].cbBuffer = pkgBindings.BindingsLength;
228 chlg_buf[1].pvBuffer = pkgBindings.Bindings;
234 /* Setup the response "output" security buffer */
235 resp_desc.ulVersion = SECBUFFER_VERSION;
236 resp_desc.cBuffers = 1;
237 resp_desc.pBuffers = &resp_buf;
238 resp_buf.BufferType = SECBUFFER_TOKEN;
239 resp_buf.pvBuffer = nego->output_token;
240 resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
242 /* Generate our challenge-response message */
243 nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
244 chlg ? nego->context :
247 ISC_REQ_CONFIDENTIALITY,
248 0, SECURITY_NATIVE_DREP,
249 chlg ? &chlg_desc : NULL,
254 /* Free the decoded challenge as it is not required anymore */
257 if(GSS_ERROR(nego->status)) {
258 char buffer[STRERROR_LEN];
259 failf(data, "InitializeSecurityContext failed: %s",
260 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
262 if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
263 return CURLE_OUT_OF_MEMORY;
265 return CURLE_AUTH_ERROR;
268 if(nego->status == SEC_I_COMPLETE_NEEDED ||
269 nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
270 nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
271 if(GSS_ERROR(nego->status)) {
272 char buffer[STRERROR_LEN];
273 failf(data, "CompleteAuthToken failed: %s",
274 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
276 if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
277 return CURLE_OUT_OF_MEMORY;
279 return CURLE_AUTH_ERROR;
283 nego->output_token_length = resp_buf.cbBuffer;
289 * Curl_auth_create_spnego_message()
291 * This is used to generate an already encoded SPNEGO (Negotiate) response
292 * message ready for sending to the recipient.
296 * data [in] - The session handle.
297 * nego [in/out] - The Negotiate data struct being used and modified.
298 * outptr [in/out] - The address where a pointer to newly allocated memory
299 * holding the result will be stored upon completion.
300 * outlen [out] - The length of the output message.
302 * Returns CURLE_OK on success.
304 CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
305 char **outptr, size_t *outlen)
307 /* Base64 encode the already generated response */
308 CURLcode result = Curl_base64_encode((const char *) nego->output_token,
309 nego->output_token_length, outptr,
311 if(!result && (!*outptr || !*outlen)) {
313 result = CURLE_REMOTE_ACCESS_DENIED;
320 * Curl_auth_cleanup_spnego()
322 * This is used to clean up the SPNEGO (Negotiate) specific data.
326 * nego [in/out] - The Negotiate data struct being cleaned up.
329 void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
331 /* Free our security context */
333 s_pSecFn->DeleteSecurityContext(nego->context);
335 nego->context = NULL;
338 /* Free our credentials handle */
339 if(nego->credentials) {
340 s_pSecFn->FreeCredentialsHandle(nego->credentials);
341 free(nego->credentials);
342 nego->credentials = NULL;
345 /* Free our identity */
346 Curl_sspi_free_identity(nego->p_identity);
347 nego->p_identity = NULL;
349 /* Free the SPN and output token */
350 Curl_safefree(nego->spn);
351 Curl_safefree(nego->output_token);
353 /* Reset any variables */
356 nego->noauthpersist = FALSE;
357 nego->havenoauthpersist = FALSE;
358 nego->havenegdata = FALSE;
359 nego->havemultiplerequests = FALSE;
362 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */