digest_sspi.c 20 KB

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