1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2016, Steve Holme, <steve_holme@hotmail.com>.
9 * Copyright (C) 2015 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at https://curl.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 * SPDX-License-Identifier: curl
24 * RFC2831 DIGEST-MD5 authentication
26 ***************************************************************************/
28 #include "curl_setup.h"
30 #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_CRYPTO_AUTH)
32 #include <curl/curl.h>
34 #include "vauth/vauth.h"
35 #include "vauth/digest.h"
38 #include "curl_multibyte.h"
44 /* The last #include files should be: */
45 #include "curl_memory.h"
49 * Curl_auth_is_digest_supported()
51 * This is used to evaluate if DIGEST is supported.
55 * Returns TRUE if DIGEST is supported by Windows SSPI.
57 bool Curl_auth_is_digest_supported(void)
59 PSecPkgInfo SecurityPackage;
60 SECURITY_STATUS status;
62 /* Query the security package for Digest */
63 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
66 /* Release the package buffer as it is not required anymore */
67 if(status == SEC_E_OK) {
68 s_pSecFn->FreeContextBuffer(SecurityPackage);
71 return (status == SEC_E_OK ? TRUE : FALSE);
75 * Curl_auth_create_digest_md5_message()
77 * This is used to generate an already encoded DIGEST-MD5 response message
78 * ready for sending to the recipient.
82 * data [in] - The session handle.
83 * chlg [in] - The challenge message.
84 * userp [in] - The user name in the format User or Domain\User.
85 * passwdp [in] - The user's password.
86 * service [in] - The service type such as http, smtp, pop or imap.
87 * out [out] - The result storage.
89 * Returns CURLE_OK on success.
91 CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
92 const struct bufref *chlg,
98 CURLcode result = CURLE_OK;
100 size_t token_max = 0;
101 unsigned char *output_token = NULL;
102 CredHandle credentials;
104 PSecPkgInfo SecurityPackage;
105 SEC_WINNT_AUTH_IDENTITY identity;
106 SEC_WINNT_AUTH_IDENTITY *p_identity;
109 SecBufferDesc chlg_desc;
110 SecBufferDesc resp_desc;
111 SECURITY_STATUS status;
113 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
115 /* Ensure we have a valid challenge message */
116 if(!Curl_bufref_len(chlg)) {
117 infof(data, "DIGEST-MD5 handshake failure (empty challenge message)");
118 return CURLE_BAD_CONTENT_ENCODING;
121 /* Query the security package for DigestSSP */
122 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
124 if(status != SEC_E_OK) {
125 failf(data, "SSPI: couldn't get auth info");
126 return CURLE_AUTH_ERROR;
129 token_max = SecurityPackage->cbMaxToken;
131 /* Release the package buffer as it is not required anymore */
132 s_pSecFn->FreeContextBuffer(SecurityPackage);
134 /* Allocate our response buffer */
135 output_token = malloc(token_max);
137 return CURLE_OUT_OF_MEMORY;
139 /* Generate our SPN */
140 spn = Curl_auth_build_spn(service, data->conn->host.name, NULL);
143 return CURLE_OUT_OF_MEMORY;
146 if(userp && *userp) {
147 /* Populate our identity structure */
148 result = Curl_create_sspi_identity(userp, passwdp, &identity);
155 /* Allow proper cleanup of the identity structure */
156 p_identity = &identity;
159 /* Use the current Windows user */
162 /* Acquire our credentials handle */
163 status = s_pSecFn->AcquireCredentialsHandle(NULL,
164 (TCHAR *) TEXT(SP_NAME_DIGEST),
165 SECPKG_CRED_OUTBOUND, NULL,
166 p_identity, NULL, NULL,
167 &credentials, &expiry);
169 if(status != SEC_E_OK) {
170 Curl_sspi_free_identity(p_identity);
173 return CURLE_LOGIN_DENIED;
176 /* Setup the challenge "input" security buffer */
177 chlg_desc.ulVersion = SECBUFFER_VERSION;
178 chlg_desc.cBuffers = 1;
179 chlg_desc.pBuffers = &chlg_buf;
180 chlg_buf.BufferType = SECBUFFER_TOKEN;
181 chlg_buf.pvBuffer = (void *) Curl_bufref_ptr(chlg);
182 chlg_buf.cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
184 /* Setup the response "output" security buffer */
185 resp_desc.ulVersion = SECBUFFER_VERSION;
186 resp_desc.cBuffers = 1;
187 resp_desc.pBuffers = &resp_buf;
188 resp_buf.BufferType = SECBUFFER_TOKEN;
189 resp_buf.pvBuffer = output_token;
190 resp_buf.cbBuffer = curlx_uztoul(token_max);
192 /* Generate our response message */
193 status = s_pSecFn->InitializeSecurityContext(&credentials, NULL, spn,
194 0, 0, 0, &chlg_desc, 0,
195 &context, &resp_desc, &attrs,
198 if(status == SEC_I_COMPLETE_NEEDED ||
199 status == SEC_I_COMPLETE_AND_CONTINUE)
200 s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
201 else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
202 #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
203 char buffer[STRERROR_LEN];
206 s_pSecFn->FreeCredentialsHandle(&credentials);
207 Curl_sspi_free_identity(p_identity);
211 if(status == SEC_E_INSUFFICIENT_MEMORY)
212 return CURLE_OUT_OF_MEMORY;
214 infof(data, "schannel: InitializeSecurityContext failed: %s",
215 Curl_sspi_strerror(status, buffer, sizeof(buffer)));
217 return CURLE_AUTH_ERROR;
220 /* Return the response. */
221 Curl_bufref_set(out, output_token, resp_buf.cbBuffer, curl_free);
223 /* Free our handles */
224 s_pSecFn->DeleteSecurityContext(&context);
225 s_pSecFn->FreeCredentialsHandle(&credentials);
227 /* Free the identity structure */
228 Curl_sspi_free_identity(p_identity);
237 * Curl_override_sspi_http_realm()
239 * This is used to populate the domain in a SSPI identity structure
240 * The realm is extracted from the challenge message and used as the
241 * domain if it is not already explicitly set.
245 * chlg [in] - The challenge message.
246 * identity [in/out] - The identity structure.
248 * Returns CURLE_OK on success.
250 CURLcode Curl_override_sspi_http_realm(const char *chlg,
251 SEC_WINNT_AUTH_IDENTITY *identity)
253 xcharp_u domain, dup_domain;
255 /* If domain is blank or unset, check challenge message for realm */
256 if(!identity->Domain || !identity->DomainLength) {
258 char value[DIGEST_MAX_VALUE_LENGTH];
259 char content[DIGEST_MAX_CONTENT_LENGTH];
261 /* Pass all additional spaces here */
262 while(*chlg && ISBLANK(*chlg))
265 /* Extract a value=content pair */
266 if(Curl_auth_digest_get_pair(chlg, value, content, &chlg)) {
267 if(strcasecompare(value, "realm")) {
269 /* Setup identity's domain and length */
270 domain.tchar_ptr = curlx_convert_UTF8_to_tchar((char *) content);
271 if(!domain.tchar_ptr)
272 return CURLE_OUT_OF_MEMORY;
274 dup_domain.tchar_ptr = _tcsdup(domain.tchar_ptr);
275 if(!dup_domain.tchar_ptr) {
276 curlx_unicodefree(domain.tchar_ptr);
277 return CURLE_OUT_OF_MEMORY;
280 free(identity->Domain);
281 identity->Domain = dup_domain.tbyte_ptr;
282 identity->DomainLength = curlx_uztoul(_tcslen(dup_domain.tchar_ptr));
283 dup_domain.tchar_ptr = NULL;
285 curlx_unicodefree(domain.tchar_ptr);
288 /* Unknown specifier, ignore it! */
292 break; /* We're done here */
294 /* Pass all additional spaces here */
295 while(*chlg && ISBLANK(*chlg))
298 /* Allow the list to be comma-separated */
308 * Curl_auth_decode_digest_http_message()
310 * This is used to decode a HTTP DIGEST challenge message into the separate
315 * chlg [in] - The challenge message.
316 * digest [in/out] - The digest data struct being used and modified.
318 * Returns CURLE_OK on success.
320 CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
321 struct digestdata *digest)
323 size_t chlglen = strlen(chlg);
325 /* We had an input token before so if there's another one now that means we
326 provided bad credentials in the previous request or it's stale. */
327 if(digest->input_token) {
329 const char *p = chlg;
331 /* Check for the 'stale' directive */
333 char value[DIGEST_MAX_VALUE_LENGTH];
334 char content[DIGEST_MAX_CONTENT_LENGTH];
336 while(*p && ISBLANK(*p))
339 if(!Curl_auth_digest_get_pair(p, value, content, &p))
342 if(strcasecompare(value, "stale") &&
343 strcasecompare(content, "true")) {
348 while(*p && ISBLANK(*p))
356 Curl_auth_digest_cleanup(digest);
358 return CURLE_LOGIN_DENIED;
361 /* Store the challenge for use later */
362 digest->input_token = (BYTE *) Curl_memdup(chlg, chlglen + 1);
363 if(!digest->input_token)
364 return CURLE_OUT_OF_MEMORY;
366 digest->input_token_len = chlglen;
372 * Curl_auth_create_digest_http_message()
374 * This is used to generate a HTTP DIGEST response message ready for sending
379 * data [in] - The session handle.
380 * userp [in] - The user name in the format User or Domain\User.
381 * passwdp [in] - The user's password.
382 * request [in] - The HTTP request.
383 * uripath [in] - The path of the HTTP uri.
384 * digest [in/out] - The digest data struct being used and modified.
385 * outptr [in/out] - The address where a pointer to newly allocated memory
386 * holding the result will be stored upon completion.
387 * outlen [out] - The length of the output message.
389 * Returns CURLE_OK on success.
391 CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
394 const unsigned char *request,
395 const unsigned char *uripath,
396 struct digestdata *digest,
397 char **outptr, size_t *outlen)
402 size_t output_token_len = 0;
403 PSecPkgInfo SecurityPackage;
404 SecBuffer chlg_buf[5];
405 SecBufferDesc chlg_desc;
406 SECURITY_STATUS status;
410 /* Query the security package for DigestSSP */
411 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
413 if(status != SEC_E_OK) {
414 failf(data, "SSPI: couldn't get auth info");
415 return CURLE_AUTH_ERROR;
418 token_max = SecurityPackage->cbMaxToken;
420 /* Release the package buffer as it is not required anymore */
421 s_pSecFn->FreeContextBuffer(SecurityPackage);
423 /* Allocate the output buffer according to the max token size as indicated
424 by the security package */
425 output_token = malloc(token_max);
427 return CURLE_OUT_OF_MEMORY;
430 /* If the user/passwd that was used to make the identity for http_context
431 has changed then delete that context. */
432 if((userp && !digest->user) || (!userp && digest->user) ||
433 (passwdp && !digest->passwd) || (!passwdp && digest->passwd) ||
434 (userp && digest->user && Curl_timestrcmp(userp, digest->user)) ||
435 (passwdp && digest->passwd && Curl_timestrcmp(passwdp, digest->passwd))) {
436 if(digest->http_context) {
437 s_pSecFn->DeleteSecurityContext(digest->http_context);
438 Curl_safefree(digest->http_context);
440 Curl_safefree(digest->user);
441 Curl_safefree(digest->passwd);
444 if(digest->http_context) {
445 chlg_desc.ulVersion = SECBUFFER_VERSION;
446 chlg_desc.cBuffers = 5;
447 chlg_desc.pBuffers = chlg_buf;
448 chlg_buf[0].BufferType = SECBUFFER_TOKEN;
449 chlg_buf[0].pvBuffer = NULL;
450 chlg_buf[0].cbBuffer = 0;
451 chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
452 chlg_buf[1].pvBuffer = (void *) request;
453 chlg_buf[1].cbBuffer = curlx_uztoul(strlen((const char *) request));
454 chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
455 chlg_buf[2].pvBuffer = (void *) uripath;
456 chlg_buf[2].cbBuffer = curlx_uztoul(strlen((const char *) uripath));
457 chlg_buf[3].BufferType = SECBUFFER_PKG_PARAMS;
458 chlg_buf[3].pvBuffer = NULL;
459 chlg_buf[3].cbBuffer = 0;
460 chlg_buf[4].BufferType = SECBUFFER_PADDING;
461 chlg_buf[4].pvBuffer = output_token;
462 chlg_buf[4].cbBuffer = curlx_uztoul(token_max);
464 status = s_pSecFn->MakeSignature(digest->http_context, 0, &chlg_desc, 0);
465 if(status == SEC_E_OK)
466 output_token_len = chlg_buf[4].cbBuffer;
467 else { /* delete the context so a new one can be made */
468 infof(data, "digest_sspi: MakeSignature failed, error 0x%08lx",
470 s_pSecFn->DeleteSecurityContext(digest->http_context);
471 Curl_safefree(digest->http_context);
475 if(!digest->http_context) {
476 CredHandle credentials;
477 SEC_WINNT_AUTH_IDENTITY identity;
478 SEC_WINNT_AUTH_IDENTITY *p_identity;
480 SecBufferDesc resp_desc;
482 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
485 /* free the copy of user/passwd used to make the previous identity */
486 Curl_safefree(digest->user);
487 Curl_safefree(digest->passwd);
489 if(userp && *userp) {
490 /* Populate our identity structure */
491 if(Curl_create_sspi_identity(userp, passwdp, &identity)) {
493 return CURLE_OUT_OF_MEMORY;
496 /* Populate our identity domain */
497 if(Curl_override_sspi_http_realm((const char *) digest->input_token,
500 return CURLE_OUT_OF_MEMORY;
503 /* Allow proper cleanup of the identity structure */
504 p_identity = &identity;
507 /* Use the current Windows user */
511 digest->user = strdup(userp);
515 return CURLE_OUT_OF_MEMORY;
520 digest->passwd = strdup(passwdp);
522 if(!digest->passwd) {
524 Curl_safefree(digest->user);
525 return CURLE_OUT_OF_MEMORY;
529 /* Acquire our credentials handle */
530 status = s_pSecFn->AcquireCredentialsHandle(NULL,
531 (TCHAR *) TEXT(SP_NAME_DIGEST),
532 SECPKG_CRED_OUTBOUND, NULL,
533 p_identity, NULL, NULL,
534 &credentials, &expiry);
535 if(status != SEC_E_OK) {
536 Curl_sspi_free_identity(p_identity);
539 return CURLE_LOGIN_DENIED;
542 /* Setup the challenge "input" security buffer if present */
543 chlg_desc.ulVersion = SECBUFFER_VERSION;
544 chlg_desc.cBuffers = 3;
545 chlg_desc.pBuffers = chlg_buf;
546 chlg_buf[0].BufferType = SECBUFFER_TOKEN;
547 chlg_buf[0].pvBuffer = digest->input_token;
548 chlg_buf[0].cbBuffer = curlx_uztoul(digest->input_token_len);
549 chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
550 chlg_buf[1].pvBuffer = (void *) request;
551 chlg_buf[1].cbBuffer = curlx_uztoul(strlen((const char *) request));
552 chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
553 chlg_buf[2].pvBuffer = NULL;
554 chlg_buf[2].cbBuffer = 0;
556 /* Setup the response "output" security buffer */
557 resp_desc.ulVersion = SECBUFFER_VERSION;
558 resp_desc.cBuffers = 1;
559 resp_desc.pBuffers = &resp_buf;
560 resp_buf.BufferType = SECBUFFER_TOKEN;
561 resp_buf.pvBuffer = output_token;
562 resp_buf.cbBuffer = curlx_uztoul(token_max);
564 spn = curlx_convert_UTF8_to_tchar((char *) uripath);
566 s_pSecFn->FreeCredentialsHandle(&credentials);
568 Curl_sspi_free_identity(p_identity);
571 return CURLE_OUT_OF_MEMORY;
574 /* Allocate our new context handle */
575 digest->http_context = calloc(1, sizeof(CtxtHandle));
576 if(!digest->http_context)
577 return CURLE_OUT_OF_MEMORY;
579 /* Generate our response message */
580 status = s_pSecFn->InitializeSecurityContext(&credentials, NULL,
582 ISC_REQ_USE_HTTP_STYLE, 0, 0,
584 digest->http_context,
585 &resp_desc, &attrs, &expiry);
586 curlx_unicodefree(spn);
588 if(status == SEC_I_COMPLETE_NEEDED ||
589 status == SEC_I_COMPLETE_AND_CONTINUE)
590 s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
591 else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
592 #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
593 char buffer[STRERROR_LEN];
596 s_pSecFn->FreeCredentialsHandle(&credentials);
598 Curl_sspi_free_identity(p_identity);
601 Curl_safefree(digest->http_context);
603 if(status == SEC_E_INSUFFICIENT_MEMORY)
604 return CURLE_OUT_OF_MEMORY;
606 infof(data, "schannel: InitializeSecurityContext failed: %s",
607 Curl_sspi_strerror(status, buffer, sizeof(buffer)));
609 return CURLE_AUTH_ERROR;
612 output_token_len = resp_buf.cbBuffer;
614 s_pSecFn->FreeCredentialsHandle(&credentials);
615 Curl_sspi_free_identity(p_identity);
618 resp = malloc(output_token_len + 1);
622 return CURLE_OUT_OF_MEMORY;
625 /* Copy the generated response */
626 memcpy(resp, output_token, output_token_len);
627 resp[output_token_len] = 0;
629 /* Return the response */
631 *outlen = output_token_len;
633 /* Free the response buffer */
640 * Curl_auth_digest_cleanup()
642 * This is used to clean up the digest specific data.
646 * digest [in/out] - The digest data struct being cleaned up.
649 void Curl_auth_digest_cleanup(struct digestdata *digest)
651 /* Free the input token */
652 Curl_safefree(digest->input_token);
654 /* Reset any variables */
655 digest->input_token_len = 0;
657 /* Delete security context */
658 if(digest->http_context) {
659 s_pSecFn->DeleteSecurityContext(digest->http_context);
660 Curl_safefree(digest->http_context);
663 /* Free the copy of user/passwd used to make the identity for http_context */
664 Curl_safefree(digest->user);
665 Curl_safefree(digest->passwd);
668 #endif /* USE_WINDOWS_SSPI && !CURL_DISABLE_CRYPTO_AUTH */