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 * SPDX-License-Identifier: curl
23 * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
25 ***************************************************************************/
27 #include "curl_setup.h"
29 #if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
31 #include <curl/curl.h>
33 #include "vauth/vauth.h"
35 #include "curl_base64.h"
37 #include "curl_multibyte.h"
41 /* The last #include files should be: */
42 #include "curl_memory.h"
46 * Curl_auth_is_spnego_supported()
48 * This is used to evaluate if SPNEGO (Negotiate) is supported.
52 * Returns TRUE if Negotiate is supported by Windows SSPI.
54 bool Curl_auth_is_spnego_supported(void)
56 PSecPkgInfo SecurityPackage;
57 SECURITY_STATUS status;
59 /* Query the security package for Negotiate */
60 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
61 TEXT(SP_NAME_NEGOTIATE),
64 /* Release the package buffer as it is not required anymore */
65 if(status == SEC_E_OK) {
66 s_pSecFn->FreeContextBuffer(SecurityPackage);
70 return (status == SEC_E_OK ? TRUE : FALSE);
74 * Curl_auth_decode_spnego_message()
76 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
81 * data [in] - The session handle.
82 * user [in] - The user name in the format User or Domain\User.
83 * password [in] - The user's password.
84 * service [in] - The service type such as http, smtp, pop or imap.
85 * host [in] - The host name.
86 * chlg64 [in] - The optional base64 encoded challenge message.
87 * nego [in/out] - The Negotiate data struct being used and modified.
89 * Returns CURLE_OK on success.
91 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
97 struct negotiatedata *nego)
99 CURLcode result = CURLE_OK;
101 unsigned char *chlg = NULL;
102 PSecPkgInfo SecurityPackage;
103 SecBuffer chlg_buf[2];
105 SecBufferDesc chlg_desc;
106 SecBufferDesc resp_desc;
108 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
110 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
114 if(nego->context && nego->status == SEC_E_OK) {
115 /* We finished successfully our part of authentication, but server
116 * rejected it (since we're again here). Exit with an error since we
117 * can't invent anything better */
118 Curl_auth_cleanup_spnego(nego);
119 return CURLE_LOGIN_DENIED;
123 /* Generate our SPN */
124 nego->spn = Curl_auth_build_spn(service, host, NULL);
126 return CURLE_OUT_OF_MEMORY;
129 if(!nego->output_token) {
130 /* Query the security package for Negotiate */
131 nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
132 TEXT(SP_NAME_NEGOTIATE),
134 if(nego->status != SEC_E_OK) {
135 failf(data, "SSPI: couldn't get auth info");
136 return CURLE_AUTH_ERROR;
139 nego->token_max = SecurityPackage->cbMaxToken;
141 /* Release the package buffer as it is not required anymore */
142 s_pSecFn->FreeContextBuffer(SecurityPackage);
144 /* Allocate our output buffer */
145 nego->output_token = malloc(nego->token_max);
146 if(!nego->output_token)
147 return CURLE_OUT_OF_MEMORY;
150 if(!nego->credentials) {
151 /* Do we have credentials to use or are we using single sign-on? */
153 /* Populate our identity structure */
154 result = Curl_create_sspi_identity(user, password, &nego->identity);
158 /* Allow proper cleanup of the identity structure */
159 nego->p_identity = &nego->identity;
162 /* Use the current Windows user */
163 nego->p_identity = NULL;
165 /* Allocate our credentials handle */
166 nego->credentials = calloc(1, sizeof(CredHandle));
167 if(!nego->credentials)
168 return CURLE_OUT_OF_MEMORY;
170 /* Acquire our credentials handle */
172 s_pSecFn->AcquireCredentialsHandle(NULL,
173 (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
174 SECPKG_CRED_OUTBOUND, NULL,
175 nego->p_identity, NULL, NULL,
176 nego->credentials, &expiry);
177 if(nego->status != SEC_E_OK)
178 return CURLE_AUTH_ERROR;
180 /* Allocate our new context handle */
181 nego->context = calloc(1, sizeof(CtxtHandle));
183 return CURLE_OUT_OF_MEMORY;
186 if(chlg64 && *chlg64) {
187 /* Decode the base-64 encoded challenge message */
189 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
194 /* Ensure we have a valid challenge message */
196 infof(data, "SPNEGO handshake failure (empty challenge message)");
197 return CURLE_BAD_CONTENT_ENCODING;
200 /* Setup the challenge "input" security buffer */
201 chlg_desc.ulVersion = SECBUFFER_VERSION;
202 chlg_desc.cBuffers = 1;
203 chlg_desc.pBuffers = &chlg_buf[0];
204 chlg_buf[0].BufferType = SECBUFFER_TOKEN;
205 chlg_buf[0].pvBuffer = chlg;
206 chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
208 #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
209 /* ssl context comes from Schannel.
210 * When extended protection is used in IIS server,
211 * we have to pass a second SecBuffer to the SecBufferDesc
212 * otherwise IIS will not pass the authentication (401 response).
213 * Minimum supported version is Windows 7.
214 * https://docs.microsoft.com/en-us/security-updates
215 * /SecurityAdvisories/2009/973811
217 if(nego->sslContext) {
218 SEC_CHANNEL_BINDINGS channelBindings;
219 SecPkgContext_Bindings pkgBindings;
220 pkgBindings.Bindings = &channelBindings;
221 nego->status = s_pSecFn->QueryContextAttributes(
223 SECPKG_ATTR_ENDPOINT_BINDINGS,
226 if(nego->status == SEC_E_OK) {
227 chlg_desc.cBuffers++;
228 chlg_buf[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
229 chlg_buf[1].cbBuffer = pkgBindings.BindingsLength;
230 chlg_buf[1].pvBuffer = pkgBindings.Bindings;
236 /* Setup the response "output" security buffer */
237 resp_desc.ulVersion = SECBUFFER_VERSION;
238 resp_desc.cBuffers = 1;
239 resp_desc.pBuffers = &resp_buf;
240 resp_buf.BufferType = SECBUFFER_TOKEN;
241 resp_buf.pvBuffer = nego->output_token;
242 resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
244 /* Generate our challenge-response message */
245 nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
246 chlg ? nego->context :
249 ISC_REQ_CONFIDENTIALITY,
250 0, SECURITY_NATIVE_DREP,
251 chlg ? &chlg_desc : NULL,
256 /* Free the decoded challenge as it is not required anymore */
259 if(GSS_ERROR(nego->status)) {
260 char buffer[STRERROR_LEN];
261 failf(data, "InitializeSecurityContext failed: %s",
262 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
264 if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
265 return CURLE_OUT_OF_MEMORY;
267 return CURLE_AUTH_ERROR;
270 if(nego->status == SEC_I_COMPLETE_NEEDED ||
271 nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
272 nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
273 if(GSS_ERROR(nego->status)) {
274 char buffer[STRERROR_LEN];
275 failf(data, "CompleteAuthToken failed: %s",
276 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
278 if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
279 return CURLE_OUT_OF_MEMORY;
281 return CURLE_AUTH_ERROR;
285 nego->output_token_length = resp_buf.cbBuffer;
291 * Curl_auth_create_spnego_message()
293 * This is used to generate an already encoded SPNEGO (Negotiate) response
294 * message ready for sending to the recipient.
298 * data [in] - The session handle.
299 * nego [in/out] - The Negotiate data struct being used and modified.
300 * outptr [in/out] - The address where a pointer to newly allocated memory
301 * holding the result will be stored upon completion.
302 * outlen [out] - The length of the output message.
304 * Returns CURLE_OK on success.
306 CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
307 char **outptr, size_t *outlen)
309 /* Base64 encode the already generated response */
310 CURLcode result = Curl_base64_encode((const char *) nego->output_token,
311 nego->output_token_length, outptr,
313 if(!result && (!*outptr || !*outlen)) {
315 result = CURLE_REMOTE_ACCESS_DENIED;
322 * Curl_auth_cleanup_spnego()
324 * This is used to clean up the SPNEGO (Negotiate) specific data.
328 * nego [in/out] - The Negotiate data struct being cleaned up.
331 void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
333 /* Free our security context */
335 s_pSecFn->DeleteSecurityContext(nego->context);
337 nego->context = NULL;
340 /* Free our credentials handle */
341 if(nego->credentials) {
342 s_pSecFn->FreeCredentialsHandle(nego->credentials);
343 free(nego->credentials);
344 nego->credentials = NULL;
347 /* Free our identity */
348 Curl_sspi_free_identity(nego->p_identity);
349 nego->p_identity = NULL;
351 /* Free the SPN and output token */
352 Curl_safefree(nego->spn);
353 Curl_safefree(nego->output_token);
355 /* Reset any variables */
358 nego->noauthpersist = FALSE;
359 nego->havenoauthpersist = FALSE;
360 nego->havenegdata = FALSE;
361 nego->havemultiplerequests = FALSE;
364 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */