digest_sspi.c 21 KB

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