digest_sspi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Steve Holme, <[email protected]>.
  9. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  10. *
  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.
  14. *
  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.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. * RFC2831 DIGEST-MD5 authentication
  25. *
  26. ***************************************************************************/
  27. #include "../curl_setup.h"
  28. #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_DIGEST_AUTH)
  29. #include <curl/curl.h>
  30. #include "vauth.h"
  31. #include "digest.h"
  32. #include "../urldata.h"
  33. #include "../curlx/warnless.h"
  34. #include "../curlx/multibyte.h"
  35. #include "../sendf.h"
  36. #include "../strdup.h"
  37. #include "../strcase.h"
  38. #include "../strerror.h"
  39. /* The last #include files should be: */
  40. #include "../curl_memory.h"
  41. #include "../memdebug.h"
  42. /*
  43. * Curl_auth_is_digest_supported()
  44. *
  45. * This is used to evaluate if DIGEST is supported.
  46. *
  47. * Parameters: None
  48. *
  49. * Returns TRUE if DIGEST is supported by Windows SSPI.
  50. */
  51. bool Curl_auth_is_digest_supported(void)
  52. {
  53. PSecPkgInfo SecurityPackage;
  54. SECURITY_STATUS status;
  55. /* Query the security package for Digest */
  56. status =
  57. Curl_pSecFn->QuerySecurityPackageInfo(
  58. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_DIGEST)),
  59. &SecurityPackage);
  60. /* Release the package buffer as it is not required anymore */
  61. if(status == SEC_E_OK) {
  62. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  63. }
  64. return status == SEC_E_OK;
  65. }
  66. /*
  67. * Curl_auth_create_digest_md5_message()
  68. *
  69. * This is used to generate an already encoded DIGEST-MD5 response message
  70. * ready for sending to the recipient.
  71. *
  72. * Parameters:
  73. *
  74. * data [in] - The session handle.
  75. * chlg [in] - The challenge message.
  76. * userp [in] - The username in the format User or Domain\User.
  77. * passwdp [in] - The user's password.
  78. * service [in] - The service type such as http, smtp, pop or imap.
  79. * out [out] - The result storage.
  80. *
  81. * Returns CURLE_OK on success.
  82. */
  83. CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
  84. const struct bufref *chlg,
  85. const char *userp,
  86. const char *passwdp,
  87. const char *service,
  88. struct bufref *out)
  89. {
  90. CURLcode result = CURLE_OK;
  91. TCHAR *spn = NULL;
  92. size_t token_max = 0;
  93. unsigned char *output_token = NULL;
  94. CredHandle credentials;
  95. CtxtHandle context;
  96. PSecPkgInfo SecurityPackage;
  97. SEC_WINNT_AUTH_IDENTITY identity;
  98. SEC_WINNT_AUTH_IDENTITY *p_identity;
  99. SecBuffer chlg_buf;
  100. SecBuffer resp_buf;
  101. SecBufferDesc chlg_desc;
  102. SecBufferDesc resp_desc;
  103. SECURITY_STATUS status;
  104. unsigned long attrs;
  105. /* Ensure we have a valid challenge message */
  106. if(!Curl_bufref_len(chlg)) {
  107. infof(data, "DIGEST-MD5 handshake failure (empty challenge message)");
  108. return CURLE_BAD_CONTENT_ENCODING;
  109. }
  110. /* Query the security package for DigestSSP */
  111. status =
  112. Curl_pSecFn->QuerySecurityPackageInfo(
  113. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_DIGEST)),
  114. &SecurityPackage);
  115. if(status != SEC_E_OK) {
  116. failf(data, "SSPI: could not get auth info");
  117. return CURLE_AUTH_ERROR;
  118. }
  119. token_max = SecurityPackage->cbMaxToken;
  120. /* Release the package buffer as it is not required anymore */
  121. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  122. /* Allocate our response buffer */
  123. output_token = malloc(token_max);
  124. if(!output_token)
  125. return CURLE_OUT_OF_MEMORY;
  126. /* Generate our SPN */
  127. spn = Curl_auth_build_spn(service, data->conn->host.name, NULL);
  128. if(!spn) {
  129. free(output_token);
  130. return CURLE_OUT_OF_MEMORY;
  131. }
  132. if(userp && *userp) {
  133. /* Populate our identity structure */
  134. result = Curl_create_sspi_identity(userp, passwdp, &identity);
  135. if(result) {
  136. free(spn);
  137. free(output_token);
  138. return result;
  139. }
  140. /* Allow proper cleanup of the identity structure */
  141. p_identity = &identity;
  142. }
  143. else
  144. /* Use the current Windows user */
  145. p_identity = NULL;
  146. /* Acquire our credentials handle */
  147. status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
  148. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_DIGEST)),
  149. SECPKG_CRED_OUTBOUND, NULL,
  150. p_identity, NULL, NULL,
  151. &credentials, NULL);
  152. if(status != SEC_E_OK) {
  153. Curl_sspi_free_identity(p_identity);
  154. free(spn);
  155. free(output_token);
  156. return CURLE_LOGIN_DENIED;
  157. }
  158. /* Setup the challenge "input" security buffer */
  159. chlg_desc.ulVersion = SECBUFFER_VERSION;
  160. chlg_desc.cBuffers = 1;
  161. chlg_desc.pBuffers = &chlg_buf;
  162. chlg_buf.BufferType = SECBUFFER_TOKEN;
  163. chlg_buf.pvBuffer = CURL_UNCONST(Curl_bufref_ptr(chlg));
  164. chlg_buf.cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
  165. /* Setup the response "output" security buffer */
  166. resp_desc.ulVersion = SECBUFFER_VERSION;
  167. resp_desc.cBuffers = 1;
  168. resp_desc.pBuffers = &resp_buf;
  169. resp_buf.BufferType = SECBUFFER_TOKEN;
  170. resp_buf.pvBuffer = output_token;
  171. resp_buf.cbBuffer = curlx_uztoul(token_max);
  172. /* Generate our response message */
  173. status = Curl_pSecFn->InitializeSecurityContext(&credentials, NULL, spn,
  174. 0, 0, 0, &chlg_desc, 0,
  175. &context, &resp_desc, &attrs,
  176. NULL);
  177. if(status == SEC_I_COMPLETE_NEEDED ||
  178. status == SEC_I_COMPLETE_AND_CONTINUE)
  179. Curl_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
  180. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  181. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  182. char buffer[STRERROR_LEN];
  183. #endif
  184. Curl_pSecFn->FreeCredentialsHandle(&credentials);
  185. Curl_sspi_free_identity(p_identity);
  186. free(spn);
  187. free(output_token);
  188. if(status == SEC_E_INSUFFICIENT_MEMORY)
  189. return CURLE_OUT_OF_MEMORY;
  190. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  191. infof(data, "schannel: InitializeSecurityContext failed: %s",
  192. Curl_sspi_strerror(status, buffer, sizeof(buffer)));
  193. #endif
  194. return CURLE_AUTH_ERROR;
  195. }
  196. /* Return the response. */
  197. Curl_bufref_set(out, output_token, resp_buf.cbBuffer, curl_free);
  198. /* Free our handles */
  199. Curl_pSecFn->DeleteSecurityContext(&context);
  200. Curl_pSecFn->FreeCredentialsHandle(&credentials);
  201. /* Free the identity structure */
  202. Curl_sspi_free_identity(p_identity);
  203. /* Free the SPN */
  204. free(spn);
  205. return result;
  206. }
  207. /*
  208. * Curl_override_sspi_http_realm()
  209. *
  210. * This is used to populate the domain in an SSPI identity structure
  211. * The realm is extracted from the challenge message and used as the
  212. * domain if it is not already explicitly set.
  213. *
  214. * Parameters:
  215. *
  216. * chlg [in] - The challenge message.
  217. * identity [in/out] - The identity structure.
  218. *
  219. * Returns CURLE_OK on success.
  220. */
  221. CURLcode Curl_override_sspi_http_realm(const char *chlg,
  222. SEC_WINNT_AUTH_IDENTITY *identity)
  223. {
  224. xcharp_u domain, dup_domain;
  225. /* If domain is blank or unset, check challenge message for realm */
  226. if(!identity->Domain || !identity->DomainLength) {
  227. for(;;) {
  228. char value[DIGEST_MAX_VALUE_LENGTH];
  229. char content[DIGEST_MAX_CONTENT_LENGTH];
  230. /* Pass all additional spaces here */
  231. while(*chlg && ISBLANK(*chlg))
  232. chlg++;
  233. /* Extract a value=content pair */
  234. if(Curl_auth_digest_get_pair(chlg, value, content, &chlg)) {
  235. if(curl_strequal(value, "realm")) {
  236. /* Setup identity's domain and length */
  237. domain.tchar_ptr = curlx_convert_UTF8_to_tchar(content);
  238. if(!domain.tchar_ptr)
  239. return CURLE_OUT_OF_MEMORY;
  240. dup_domain.tchar_ptr = Curl_tcsdup(domain.tchar_ptr);
  241. if(!dup_domain.tchar_ptr) {
  242. curlx_unicodefree(domain.tchar_ptr);
  243. return CURLE_OUT_OF_MEMORY;
  244. }
  245. free(identity->Domain);
  246. identity->Domain = dup_domain.tbyte_ptr;
  247. identity->DomainLength = curlx_uztoul(_tcslen(dup_domain.tchar_ptr));
  248. dup_domain.tchar_ptr = NULL;
  249. curlx_unicodefree(domain.tchar_ptr);
  250. }
  251. else {
  252. /* Unknown specifier, ignore it! */
  253. }
  254. }
  255. else
  256. break; /* We are done here */
  257. /* Pass all additional spaces here */
  258. while(*chlg && ISBLANK(*chlg))
  259. chlg++;
  260. /* Allow the list to be comma-separated */
  261. if(',' == *chlg)
  262. chlg++;
  263. }
  264. }
  265. return CURLE_OK;
  266. }
  267. /*
  268. * Curl_auth_decode_digest_http_message()
  269. *
  270. * This is used to decode an HTTP DIGEST challenge message into the separate
  271. * attributes.
  272. *
  273. * Parameters:
  274. *
  275. * chlg [in] - The challenge message.
  276. * digest [in/out] - The digest data struct being used and modified.
  277. *
  278. * Returns CURLE_OK on success.
  279. */
  280. CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
  281. struct digestdata *digest)
  282. {
  283. size_t chlglen = strlen(chlg);
  284. /* We had an input token before so if there is another one now that means we
  285. provided bad credentials in the previous request or it is stale. */
  286. if(digest->input_token) {
  287. bool stale = FALSE;
  288. const char *p = chlg;
  289. /* Check for the 'stale' directive */
  290. for(;;) {
  291. char value[DIGEST_MAX_VALUE_LENGTH];
  292. char content[DIGEST_MAX_CONTENT_LENGTH];
  293. while(*p && ISBLANK(*p))
  294. p++;
  295. if(!Curl_auth_digest_get_pair(p, value, content, &p))
  296. break;
  297. if(curl_strequal(value, "stale") &&
  298. curl_strequal(content, "true")) {
  299. stale = TRUE;
  300. break;
  301. }
  302. while(*p && ISBLANK(*p))
  303. p++;
  304. if(',' == *p)
  305. p++;
  306. }
  307. if(stale)
  308. Curl_auth_digest_cleanup(digest);
  309. else
  310. return CURLE_LOGIN_DENIED;
  311. }
  312. /* Store the challenge for use later */
  313. digest->input_token = (BYTE *)Curl_memdup(chlg, chlglen + 1);
  314. if(!digest->input_token)
  315. return CURLE_OUT_OF_MEMORY;
  316. digest->input_token_len = chlglen;
  317. return CURLE_OK;
  318. }
  319. /*
  320. * Curl_auth_create_digest_http_message()
  321. *
  322. * This is used to generate an HTTP DIGEST response message ready for sending
  323. * to the recipient.
  324. *
  325. * Parameters:
  326. *
  327. * data [in] - The session handle.
  328. * userp [in] - The username in the format User or Domain\User.
  329. * passwdp [in] - The user's password.
  330. * request [in] - The HTTP request.
  331. * uripath [in] - The path of the HTTP uri.
  332. * digest [in/out] - The digest data struct being used and modified.
  333. * outptr [in/out] - The address where a pointer to newly allocated memory
  334. * holding the result will be stored upon completion.
  335. * outlen [out] - The length of the output message.
  336. *
  337. * Returns CURLE_OK on success.
  338. */
  339. CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
  340. const char *userp,
  341. const char *passwdp,
  342. const unsigned char *request,
  343. const unsigned char *uripath,
  344. struct digestdata *digest,
  345. char **outptr, size_t *outlen)
  346. {
  347. size_t token_max;
  348. char *resp;
  349. BYTE *output_token;
  350. size_t output_token_len = 0;
  351. PSecPkgInfo SecurityPackage;
  352. SecBuffer chlg_buf[5];
  353. SecBufferDesc chlg_desc;
  354. SECURITY_STATUS status;
  355. (void)data;
  356. /* Query the security package for DigestSSP */
  357. status =
  358. Curl_pSecFn->QuerySecurityPackageInfo(
  359. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_DIGEST)),
  360. &SecurityPackage);
  361. if(status != SEC_E_OK) {
  362. failf(data, "SSPI: could not get auth info");
  363. return CURLE_AUTH_ERROR;
  364. }
  365. token_max = SecurityPackage->cbMaxToken;
  366. /* Release the package buffer as it is not required anymore */
  367. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  368. /* Allocate the output buffer according to the max token size as indicated
  369. by the security package */
  370. output_token = malloc(token_max);
  371. if(!output_token) {
  372. return CURLE_OUT_OF_MEMORY;
  373. }
  374. /* If the user/passwd that was used to make the identity for http_context
  375. has changed then delete that context. */
  376. if((userp && !digest->user) || (!userp && digest->user) ||
  377. (passwdp && !digest->passwd) || (!passwdp && digest->passwd) ||
  378. (userp && digest->user && Curl_timestrcmp(userp, digest->user)) ||
  379. (passwdp && digest->passwd && Curl_timestrcmp(passwdp, digest->passwd))) {
  380. if(digest->http_context) {
  381. Curl_pSecFn->DeleteSecurityContext(digest->http_context);
  382. Curl_safefree(digest->http_context);
  383. }
  384. Curl_safefree(digest->user);
  385. Curl_safefree(digest->passwd);
  386. }
  387. if(digest->http_context) {
  388. chlg_desc.ulVersion = SECBUFFER_VERSION;
  389. chlg_desc.cBuffers = 5;
  390. chlg_desc.pBuffers = chlg_buf;
  391. chlg_buf[0].BufferType = SECBUFFER_TOKEN;
  392. chlg_buf[0].pvBuffer = NULL;
  393. chlg_buf[0].cbBuffer = 0;
  394. chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
  395. chlg_buf[1].pvBuffer = CURL_UNCONST(request);
  396. chlg_buf[1].cbBuffer = curlx_uztoul(strlen((const char *) request));
  397. chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
  398. chlg_buf[2].pvBuffer = CURL_UNCONST(uripath);
  399. chlg_buf[2].cbBuffer = curlx_uztoul(strlen((const char *) uripath));
  400. chlg_buf[3].BufferType = SECBUFFER_PKG_PARAMS;
  401. chlg_buf[3].pvBuffer = NULL;
  402. chlg_buf[3].cbBuffer = 0;
  403. chlg_buf[4].BufferType = SECBUFFER_PADDING;
  404. chlg_buf[4].pvBuffer = output_token;
  405. chlg_buf[4].cbBuffer = curlx_uztoul(token_max);
  406. status = Curl_pSecFn->MakeSignature(digest->http_context, 0, &chlg_desc,
  407. 0);
  408. if(status == SEC_E_OK)
  409. output_token_len = chlg_buf[4].cbBuffer;
  410. else { /* delete the context so a new one can be made */
  411. infof(data, "digest_sspi: MakeSignature failed, error 0x%08lx", status);
  412. Curl_pSecFn->DeleteSecurityContext(digest->http_context);
  413. Curl_safefree(digest->http_context);
  414. }
  415. }
  416. if(!digest->http_context) {
  417. CredHandle credentials;
  418. SEC_WINNT_AUTH_IDENTITY identity;
  419. SEC_WINNT_AUTH_IDENTITY *p_identity;
  420. SecBuffer resp_buf;
  421. SecBufferDesc resp_desc;
  422. unsigned long attrs;
  423. TCHAR *spn;
  424. /* free the copy of user/passwd used to make the previous identity */
  425. Curl_safefree(digest->user);
  426. Curl_safefree(digest->passwd);
  427. if(userp && *userp) {
  428. /* Populate our identity structure */
  429. if(Curl_create_sspi_identity(userp, passwdp, &identity)) {
  430. free(output_token);
  431. return CURLE_OUT_OF_MEMORY;
  432. }
  433. /* Populate our identity domain */
  434. if(Curl_override_sspi_http_realm((const char *) digest->input_token,
  435. &identity)) {
  436. free(output_token);
  437. return CURLE_OUT_OF_MEMORY;
  438. }
  439. /* Allow proper cleanup of the identity structure */
  440. p_identity = &identity;
  441. }
  442. else
  443. /* Use the current Windows user */
  444. p_identity = NULL;
  445. if(userp) {
  446. digest->user = strdup(userp);
  447. if(!digest->user) {
  448. free(output_token);
  449. Curl_sspi_free_identity(p_identity);
  450. return CURLE_OUT_OF_MEMORY;
  451. }
  452. }
  453. if(passwdp) {
  454. digest->passwd = strdup(passwdp);
  455. if(!digest->passwd) {
  456. free(output_token);
  457. Curl_sspi_free_identity(p_identity);
  458. Curl_safefree(digest->user);
  459. return CURLE_OUT_OF_MEMORY;
  460. }
  461. }
  462. /* Acquire our credentials handle */
  463. status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
  464. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_DIGEST)),
  465. SECPKG_CRED_OUTBOUND, NULL,
  466. p_identity, NULL, NULL,
  467. &credentials, NULL);
  468. if(status != SEC_E_OK) {
  469. Curl_sspi_free_identity(p_identity);
  470. free(output_token);
  471. return CURLE_LOGIN_DENIED;
  472. }
  473. /* Setup the challenge "input" security buffer if present */
  474. chlg_desc.ulVersion = SECBUFFER_VERSION;
  475. chlg_desc.cBuffers = 3;
  476. chlg_desc.pBuffers = chlg_buf;
  477. chlg_buf[0].BufferType = SECBUFFER_TOKEN;
  478. chlg_buf[0].pvBuffer = digest->input_token;
  479. chlg_buf[0].cbBuffer = curlx_uztoul(digest->input_token_len);
  480. chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
  481. chlg_buf[1].pvBuffer = CURL_UNCONST(request);
  482. chlg_buf[1].cbBuffer = curlx_uztoul(strlen((const char *) request));
  483. chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
  484. chlg_buf[2].pvBuffer = NULL;
  485. chlg_buf[2].cbBuffer = 0;
  486. /* Setup the response "output" security buffer */
  487. resp_desc.ulVersion = SECBUFFER_VERSION;
  488. resp_desc.cBuffers = 1;
  489. resp_desc.pBuffers = &resp_buf;
  490. resp_buf.BufferType = SECBUFFER_TOKEN;
  491. resp_buf.pvBuffer = output_token;
  492. resp_buf.cbBuffer = curlx_uztoul(token_max);
  493. spn = curlx_convert_UTF8_to_tchar((const char *) uripath);
  494. if(!spn) {
  495. Curl_pSecFn->FreeCredentialsHandle(&credentials);
  496. Curl_sspi_free_identity(p_identity);
  497. free(output_token);
  498. return CURLE_OUT_OF_MEMORY;
  499. }
  500. /* Allocate our new context handle */
  501. digest->http_context = calloc(1, sizeof(CtxtHandle));
  502. if(!digest->http_context) {
  503. curlx_unicodefree(spn);
  504. Curl_sspi_free_identity(p_identity);
  505. free(output_token);
  506. return CURLE_OUT_OF_MEMORY;
  507. }
  508. /* Generate our response message */
  509. status = Curl_pSecFn->InitializeSecurityContext(&credentials, NULL,
  510. spn,
  511. ISC_REQ_USE_HTTP_STYLE, 0, 0,
  512. &chlg_desc, 0,
  513. digest->http_context,
  514. &resp_desc, &attrs, NULL);
  515. curlx_unicodefree(spn);
  516. if(status == SEC_I_COMPLETE_NEEDED ||
  517. status == SEC_I_COMPLETE_AND_CONTINUE)
  518. Curl_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
  519. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  520. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  521. char buffer[STRERROR_LEN];
  522. #endif
  523. Curl_pSecFn->FreeCredentialsHandle(&credentials);
  524. Curl_sspi_free_identity(p_identity);
  525. free(output_token);
  526. Curl_safefree(digest->http_context);
  527. if(status == SEC_E_INSUFFICIENT_MEMORY)
  528. return CURLE_OUT_OF_MEMORY;
  529. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  530. infof(data, "schannel: InitializeSecurityContext failed: %s",
  531. Curl_sspi_strerror(status, buffer, sizeof(buffer)));
  532. #endif
  533. return CURLE_AUTH_ERROR;
  534. }
  535. output_token_len = resp_buf.cbBuffer;
  536. Curl_pSecFn->FreeCredentialsHandle(&credentials);
  537. Curl_sspi_free_identity(p_identity);
  538. }
  539. resp = Curl_memdup0((const char *)output_token, output_token_len);
  540. free(output_token);
  541. if(!resp) {
  542. return CURLE_OUT_OF_MEMORY;
  543. }
  544. /* Return the response */
  545. *outptr = resp;
  546. *outlen = output_token_len;
  547. return CURLE_OK;
  548. }
  549. /*
  550. * Curl_auth_digest_cleanup()
  551. *
  552. * This is used to clean up the digest specific data.
  553. *
  554. * Parameters:
  555. *
  556. * digest [in/out] - The digest data struct being cleaned up.
  557. *
  558. */
  559. void Curl_auth_digest_cleanup(struct digestdata *digest)
  560. {
  561. /* Free the input token */
  562. Curl_safefree(digest->input_token);
  563. /* Reset any variables */
  564. digest->input_token_len = 0;
  565. /* Delete security context */
  566. if(digest->http_context) {
  567. Curl_pSecFn->DeleteSecurityContext(digest->http_context);
  568. Curl_safefree(digest->http_context);
  569. }
  570. /* Free the copy of user/passwd used to make the identity for http_context */
  571. Curl_safefree(digest->user);
  572. Curl_safefree(digest->passwd);
  573. }
  574. #endif /* USE_WINDOWS_SSPI && !CURL_DISABLE_DIGEST_AUTH */