master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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.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 * SPDX-License-Identifier: curl
22 *
23 * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
24 *
25 ***************************************************************************/
26#include "curl_setup.h"
28#if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
30#include "vauth/vauth.h"
31#include "curlx/base64.h"
32#include "curl_trc.h"
33#include "strerror.h"
35/*
36 * Curl_auth_is_spnego_supported()
37 *
38 * This is used to evaluate if SPNEGO (Negotiate) is supported.
39 *
40 * Parameters: None
41 *
42 * Returns TRUE if Negotiate is supported by Windows SSPI.
43 */
44bool Curl_auth_is_spnego_supported(void)
45{
46 PSecPkgInfo SecurityPackage;
47 SECURITY_STATUS status;
49 /* Query the security package for Negotiate */
50 status = Curl_pSecFn->QuerySecurityPackageInfo(
51 CURL_UNCONST(TEXT(SP_NAME_NEGOTIATE)),
52 &SecurityPackage);
54 /* Release the package buffer as it is not required anymore */
55 if(status == SEC_E_OK) {
56 Curl_pSecFn->FreeContextBuffer(SecurityPackage);
57 }
59 return status == SEC_E_OK;
60}
62/*
63 * Curl_auth_decode_spnego_message()
64 *
65 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
66 * message.
67 *
68 * Parameters:
69 *
70 * data [in] - The session handle.
71 * user [in] - The username in the format User or Domain\User.
72 * password [in] - The user's password.
73 * service [in] - The service type such as http, smtp, pop or imap.
74 * host [in] - The hostname.
75 * chlg64 [in] - The optional base64 encoded challenge message.
76 * nego [in/out] - The Negotiate data struct being used and modified.
77 *
78 * Returns CURLE_OK on success.
79 */
80CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
81 struct Curl_creds *creds,
82 const char *default_service,
83 const char *host,
84 const char *chlg64,
85 struct negotiatedata *nego)
86{
87 CURLcode result = CURLE_OK;
88 size_t chlglen = 0;
89 unsigned char *chlg = NULL;
90 PSecPkgInfo SecurityPackage;
91 SecBuffer chlg_buf[2];
92 SecBuffer resp_buf;
93 SecBufferDesc chlg_desc;
94 SecBufferDesc resp_desc;
95 unsigned long attrs;
96 SecPkgContext_Bindings pkgBindings = { 0, NULL };
98 if(nego->context && nego->status == SEC_E_OK) {
99 /* We finished successfully our part of authentication, but server
100 * rejected it (since we are again here). Exit with an error since we
101 * cannot invent anything better */
102 Curl_auth_cleanup_spnego(nego);
103 return CURLE_LOGIN_DENIED;
104 }
106 if(!nego->spn) {
107 /* Generate our SPN */
108 const char *service = Curl_creds_has_sasl_service(creds) ?
109 Curl_creds_sasl_service(creds) : default_service;
110 nego->spn = Curl_auth_build_spn(service, host, NULL);
111 if(!nego->spn)
112 return CURLE_OUT_OF_MEMORY;
113 }
115 if(!nego->output_token) {
116 /* Query the security package for Negotiate */
117 nego->status = Curl_pSecFn->QuerySecurityPackageInfo(
118 CURL_UNCONST(TEXT(SP_NAME_NEGOTIATE)),
119 &SecurityPackage);
120 if(nego->status != SEC_E_OK) {
121 failf(data, "SSPI: could not get auth info");
122 return CURLE_AUTH_ERROR;
123 }
125 nego->token_max = SecurityPackage->cbMaxToken;
127 /* Release the package buffer as it is not required anymore */
128 Curl_pSecFn->FreeContextBuffer(SecurityPackage);
130 /* Allocate our output buffer */
131 nego->output_token = curlx_malloc(nego->token_max);
132 if(!nego->output_token)
133 return CURLE_OUT_OF_MEMORY;
134 }
136 if(!nego->credentials) {
137 /* Do we have credentials to use or are we using single sign-on? */
138 if(Curl_creds_has_user(creds)) {
139 /* Populate our identity structure */
140 result = Curl_create_sspi_identity(creds->user, creds->passwd,
141 &nego->identity);
142 if(result)
143 return result;
145 /* Allow proper cleanup of the identity structure */
146 nego->p_identity = &nego->identity;
147 }
148 else
149 /* Use the current Windows user */
150 nego->p_identity = NULL;
152 /* Exclude NTLM from SPNEGO negotiation via the PackageList field */
153 if(!nego->p_identity) {
154 memset(&nego->identity, 0, sizeof(nego->identity));
155 nego->identity.Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
156 nego->identity.Length = sizeof(nego->identity);
157 nego->identity.Flags = CURL_SEC_WINNT_AUTH_IDENTITY;
158 nego->p_identity = &nego->identity;
159 }
161 /* Use the special name "!ntlm" to prevent NTLM from being used:
162 * https://learn.microsoft.com/windows/win32/api/sspi/ns-sspi-sec_winnt_auth_identity_exa
163 */
164 nego->identity.PackageList = CURL_UNCONST(TEXT("!ntlm"));
165 nego->identity.PackageListLength = 5;
167 /* Allocate our credentials handle */
168 nego->credentials = curlx_calloc(1, sizeof(CredHandle));
169 if(!nego->credentials)
170 return CURLE_OUT_OF_MEMORY;
172 /* Acquire our credentials handle */
173 nego->status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
174 CURL_UNCONST(TEXT(SP_NAME_NEGOTIATE)),
175 SECPKG_CRED_OUTBOUND, NULL,
176 nego->p_identity, NULL, NULL,
177 nego->credentials, NULL);
178 if(nego->status != SEC_E_OK) {
179 curlx_safefree(nego->credentials);
180 return CURLE_AUTH_ERROR;
181 }
183 /* Allocate our new context handle */
184 nego->context = curlx_calloc(1, sizeof(CtxtHandle));
185 if(!nego->context)
186 return CURLE_OUT_OF_MEMORY;
187 }
189 chlg_desc.ulVersion = SECBUFFER_VERSION;
190 chlg_desc.cBuffers = 0;
191 chlg_desc.pBuffers = chlg_buf;
193 if(chlg64 && *chlg64) {
194 /* Decode the base-64 encoded challenge message */
195 if(*chlg64 != '=') {
196 result = curlx_base64_decode(chlg64, &chlg, &chlglen);
197 if(result)
198 return result;
199 }
201 /* Ensure we have a valid challenge message */
202 if(!chlg) {
203 infof(data, "SPNEGO handshake failure (empty challenge message)");
204 return CURLE_BAD_CONTENT_ENCODING;
205 }
207 /* Setup the challenge "input" security buffer */
208 chlg_desc.cBuffers = 1;
209 chlg_buf[0].BufferType = SECBUFFER_TOKEN;
210 chlg_buf[0].pvBuffer = chlg;
211 chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
212 }
214 /* SSL context comes from Schannel.
215 * When extended protection is used in IIS server, pass its channel
216 * bindings on the initial call too. HTTP Negotiate can create and send a
217 * Kerberos token before receiving a challenge from the server.
218 * Minimum supported version is Windows 7.
219 * https://learn.microsoft.com/security-updates/SecurityAdvisories/2009/973811
220 */
221 if(nego->sslContext) {
222 nego->status = Curl_pSecFn->QueryContextAttributes(
223 nego->sslContext,
224 SECPKG_ATTR_ENDPOINT_BINDINGS,
225 &pkgBindings);
226 if(nego->status == SEC_E_OK) {
227 SecBuffer *binding_buf = &chlg_buf[chlg_desc.cBuffers++];
228 binding_buf->BufferType = SECBUFFER_CHANNEL_BINDINGS;
229 binding_buf->cbBuffer = pkgBindings.BindingsLength;
230 binding_buf->pvBuffer = pkgBindings.Bindings;
231 }
232 }
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 {
244 DWORD sspi_flags = ISC_REQ_CONFIDENTIALITY;
245 if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_FLAG)
246 sspi_flags |= ISC_REQ_DELEGATE | ISC_REQ_MUTUAL_AUTH;
247 nego->status =
248 Curl_pSecFn->InitializeSecurityContext(nego->credentials,
249 chlg ? nego->context : NULL,
250 nego->spn,
251 sspi_flags,
252 0, SECURITY_NATIVE_DREP,
253 chlg_desc.cBuffers ?
254 &chlg_desc : NULL,
255 0, nego->context,
256 &resp_desc, &attrs, NULL);
257 }
259 if(pkgBindings.Bindings)
260 Curl_pSecFn->FreeContextBuffer(pkgBindings.Bindings);
262 /* Free the decoded challenge as it is not required anymore */
263 curlx_free(chlg);
265 if(GSS_ERROR(nego->status)) {
266 char buffer[STRERROR_LEN];
267 failf(data, "InitializeSecurityContext failed: %s",
268 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
270 if(nego->status == SEC_E_INSUFFICIENT_MEMORY)
271 return CURLE_OUT_OF_MEMORY;
273 return CURLE_AUTH_ERROR;
274 }
276 if(nego->status == SEC_I_COMPLETE_NEEDED ||
277 nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
278 nego->status = Curl_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
279 if(GSS_ERROR(nego->status)) {
280 char buffer[STRERROR_LEN];
281 failf(data, "CompleteAuthToken failed: %s",
282 Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
284 if(nego->status == SEC_E_INSUFFICIENT_MEMORY)
285 return CURLE_OUT_OF_MEMORY;
287 return CURLE_AUTH_ERROR;
288 }
289 }
291 nego->output_token_length = resp_buf.cbBuffer;
293 return result;
294}
296/*
297 * Curl_auth_create_spnego_message()
298 *
299 * This is used to generate an already encoded SPNEGO (Negotiate) response
300 * message ready for sending to the recipient.
301 *
302 * Parameters:
303 *
304 * data [in] - The session handle.
305 * nego [in/out] - The Negotiate data struct being used and modified.
306 * outptr [in/out] - The address where a pointer to newly allocated memory
307 * holding the result is stored upon completion.
308 * outlen [out] - The length of the output message.
309 *
310 * Returns CURLE_OK on success.
311 */
312CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
313 char **outptr, size_t *outlen)
314{
315 /* Base64 encode the already generated response */
316 CURLcode result = curlx_base64_encode(nego->output_token,
317 nego->output_token_length, outptr,
318 outlen);
319 if(!result && (!*outptr || !*outlen)) {
320 curlx_free(*outptr);
321 result = CURLE_REMOTE_ACCESS_DENIED;
322 }
324 return result;
325}
327/*
328 * Curl_auth_cleanup_spnego()
329 *
330 * This is used to clean up the SPNEGO (Negotiate) specific data.
331 *
332 * Parameters:
333 *
334 * nego [in/out] - The Negotiate data struct being cleaned up.
335 *
336 */
337void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
338{
339 /* Free our security context */
340 if(nego->context) {
341 Curl_pSecFn->DeleteSecurityContext(nego->context);
342 curlx_safefree(nego->context);
343 }
345 /* Free our credentials handle */
346 if(nego->credentials) {
347 Curl_pSecFn->FreeCredentialsHandle(nego->credentials);
348 curlx_safefree(nego->credentials);
349 }
351 /* Free our identity */
352 Curl_sspi_free_identity(nego->p_identity);
353 nego->p_identity = NULL;
355 /* Free the SPN and output token */
356 curlx_safefree(nego->spn);
357 curlx_safefree(nego->output_token);
359 /* Reset any variables */
360 nego->status = 0;
361 nego->token_max = 0;
362 nego->noauthpersist = FALSE;
363 nego->havenoauthpersist = FALSE;
364 nego->havenegdata = FALSE;
365 nego->havemultiplerequests = FALSE;
366}
368#endif /* USE_WINDOWS_SSPI && USE_SPNEGO */
Diagnostics4 git calls · 976 ms
CallStatusSizeTime0976 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=972 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.0 KiB972 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4073 git.response.cache=MISS git.response.duration_ms=972 git.fetch.wants=4babd4510d5d8114de7e8f874b2f3e40c82472a2 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4073 git.pkt.payload.max=4052 git.sideband.band1.bytes=4051 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5eafa6c8f3fb1bb
- Request
- d08a2abc…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format