digest_sspi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014 - 2016, Steve Holme, <[email protected]>.
  9. * Copyright (C) 2015, 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. return (status == SEC_E_OK ? TRUE : FALSE);
  57. }
  58. /*
  59. * Curl_auth_create_digest_md5_message()
  60. *
  61. * This is used to generate an already encoded DIGEST-MD5 response message
  62. * ready for sending to the recipient.
  63. *
  64. * Parameters:
  65. *
  66. * data [in] - The session handle.
  67. * chlg64 [in] - The base64 encoded challenge message.
  68. * userp [in] - The user name in the format User or Domain\User.
  69. * passdwp [in] - The user's password.
  70. * service [in] - The service type such as http, smtp, pop or imap.
  71. * outptr [in/out] - The address where a pointer to newly allocated memory
  72. * holding the result will be stored upon completion.
  73. * outlen [out] - The length of the output message.
  74. *
  75. * Returns CURLE_OK on success.
  76. */
  77. CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
  78. const char *chlg64,
  79. const char *userp,
  80. const char *passwdp,
  81. const char *service,
  82. char **outptr, size_t *outlen)
  83. {
  84. CURLcode result = CURLE_OK;
  85. TCHAR *spn = NULL;
  86. size_t chlglen = 0;
  87. size_t token_max = 0;
  88. unsigned char *input_token = NULL;
  89. unsigned char *output_token = NULL;
  90. CredHandle credentials;
  91. CtxtHandle context;
  92. PSecPkgInfo SecurityPackage;
  93. SEC_WINNT_AUTH_IDENTITY identity;
  94. SEC_WINNT_AUTH_IDENTITY *p_identity;
  95. SecBuffer chlg_buf;
  96. SecBuffer resp_buf;
  97. SecBufferDesc chlg_desc;
  98. SecBufferDesc resp_desc;
  99. SECURITY_STATUS status;
  100. unsigned long attrs;
  101. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  102. /* Decode the base-64 encoded challenge message */
  103. if(strlen(chlg64) && *chlg64 != '=') {
  104. result = Curl_base64_decode(chlg64, &input_token, &chlglen);
  105. if(result)
  106. return result;
  107. }
  108. /* Ensure we have a valid challenge message */
  109. if(!input_token) {
  110. infof(data, "DIGEST-MD5 handshake failure (empty challenge message)\n");
  111. return CURLE_BAD_CONTENT_ENCODING;
  112. }
  113. /* Query the security package for DigestSSP */
  114. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
  115. &SecurityPackage);
  116. if(status != SEC_E_OK) {
  117. free(input_token);
  118. return CURLE_NOT_BUILT_IN;
  119. }
  120. token_max = SecurityPackage->cbMaxToken;
  121. /* Release the package buffer as it is not required anymore */
  122. s_pSecFn->FreeContextBuffer(SecurityPackage);
  123. /* Allocate our response buffer */
  124. output_token = malloc(token_max);
  125. if(!output_token) {
  126. free(input_token);
  127. return CURLE_OUT_OF_MEMORY;
  128. }
  129. /* Generate our SPN */
  130. spn = Curl_auth_build_spn(service, data->easy_conn->host.name, NULL);
  131. if(!spn) {
  132. free(output_token);
  133. free(input_token);
  134. return CURLE_OUT_OF_MEMORY;
  135. }
  136. if(userp && *userp) {
  137. /* Populate our identity structure */
  138. result = Curl_create_sspi_identity(userp, passwdp, &identity);
  139. if(result) {
  140. free(spn);
  141. free(output_token);
  142. free(input_token);
  143. return result;
  144. }
  145. /* Allow proper cleanup of the identity structure */
  146. p_identity = &identity;
  147. }
  148. else
  149. /* Use the current Windows user */
  150. p_identity = NULL;
  151. /* Acquire our credentials handle */
  152. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  153. (TCHAR *) TEXT(SP_NAME_DIGEST),
  154. SECPKG_CRED_OUTBOUND, NULL,
  155. p_identity, NULL, NULL,
  156. &credentials, &expiry);
  157. if(status != SEC_E_OK) {
  158. Curl_sspi_free_identity(p_identity);
  159. free(spn);
  160. free(output_token);
  161. free(input_token);
  162. return CURLE_LOGIN_DENIED;
  163. }
  164. /* Setup the challenge "input" security buffer */
  165. chlg_desc.ulVersion = SECBUFFER_VERSION;
  166. chlg_desc.cBuffers = 1;
  167. chlg_desc.pBuffers = &chlg_buf;
  168. chlg_buf.BufferType = SECBUFFER_TOKEN;
  169. chlg_buf.pvBuffer = input_token;
  170. chlg_buf.cbBuffer = curlx_uztoul(chlglen);
  171. /* Setup the response "output" security buffer */
  172. resp_desc.ulVersion = SECBUFFER_VERSION;
  173. resp_desc.cBuffers = 1;
  174. resp_desc.pBuffers = &resp_buf;
  175. resp_buf.BufferType = SECBUFFER_TOKEN;
  176. resp_buf.pvBuffer = output_token;
  177. resp_buf.cbBuffer = curlx_uztoul(token_max);
  178. /* Generate our response message */
  179. status = s_pSecFn->InitializeSecurityContext(&credentials, NULL, spn,
  180. 0, 0, 0, &chlg_desc, 0,
  181. &context, &resp_desc, &attrs,
  182. &expiry);
  183. if(status == SEC_I_COMPLETE_NEEDED ||
  184. status == SEC_I_COMPLETE_AND_CONTINUE)
  185. s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
  186. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  187. s_pSecFn->FreeCredentialsHandle(&credentials);
  188. Curl_sspi_free_identity(p_identity);
  189. free(spn);
  190. free(output_token);
  191. free(input_token);
  192. return CURLE_RECV_ERROR;
  193. }
  194. /* Base64 encode the response */
  195. result = Curl_base64_encode(data, (char *) output_token, resp_buf.cbBuffer,
  196. outptr, outlen);
  197. /* Free our handles */
  198. s_pSecFn->DeleteSecurityContext(&context);
  199. s_pSecFn->FreeCredentialsHandle(&credentials);
  200. /* Free the identity structure */
  201. Curl_sspi_free_identity(p_identity);
  202. /* Free the SPN */
  203. free(spn);
  204. /* Free the response buffer */
  205. free(output_token);
  206. /* Free the decoded challenge message */
  207. free(input_token);
  208. return result;
  209. }
  210. /*
  211. * Curl_override_sspi_http_realm()
  212. *
  213. * This is used to populate the domain in a SSPI identity structure
  214. * The realm is extracted from the challenge message and used as the
  215. * domain if it is not already explicitly set.
  216. *
  217. * Parameters:
  218. *
  219. * chlg [in] - The challenge message.
  220. * identity [in/out] - The identity structure.
  221. *
  222. * Returns CURLE_OK on success.
  223. */
  224. CURLcode Curl_override_sspi_http_realm(const char *chlg,
  225. SEC_WINNT_AUTH_IDENTITY *identity)
  226. {
  227. xcharp_u domain, dup_domain;
  228. /* If domain is blank or unset, check challenge message for realm */
  229. if(!identity->Domain || !identity->DomainLength) {
  230. for(;;) {
  231. char value[DIGEST_MAX_VALUE_LENGTH];
  232. char content[DIGEST_MAX_CONTENT_LENGTH];
  233. /* Pass all additional spaces here */
  234. while(*chlg && ISSPACE(*chlg))
  235. chlg++;
  236. /* Extract a value=content pair */
  237. if(Curl_auth_digest_get_pair(chlg, value, content, &chlg)) {
  238. if(strcasecompare(value, "realm")) {
  239. /* Setup identity's domain and length */
  240. domain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *) content);
  241. if(!domain.tchar_ptr)
  242. return CURLE_OUT_OF_MEMORY;
  243. dup_domain.tchar_ptr = _tcsdup(domain.tchar_ptr);
  244. if(!dup_domain.tchar_ptr) {
  245. Curl_unicodefree(domain.tchar_ptr);
  246. return CURLE_OUT_OF_MEMORY;
  247. }
  248. free(identity->Domain);
  249. identity->Domain = dup_domain.tbyte_ptr;
  250. identity->DomainLength = curlx_uztoul(_tcslen(dup_domain.tchar_ptr));
  251. dup_domain.tchar_ptr = NULL;
  252. Curl_unicodefree(domain.tchar_ptr);
  253. }
  254. else {
  255. /* Unknown specifier, ignore it! */
  256. }
  257. }
  258. else
  259. break; /* We're done here */
  260. /* Pass all additional spaces here */
  261. while(*chlg && ISSPACE(*chlg))
  262. chlg++;
  263. /* Allow the list to be comma-separated */
  264. if(',' == *chlg)
  265. chlg++;
  266. }
  267. }
  268. return CURLE_OK;
  269. }
  270. /*
  271. * Curl_auth_decode_digest_http_message()
  272. *
  273. * This is used to decode a HTTP DIGEST challenge message into the seperate
  274. * attributes.
  275. *
  276. * Parameters:
  277. *
  278. * chlg [in] - The challenge message.
  279. * digest [in/out] - The digest data struct being used and modified.
  280. *
  281. * Returns CURLE_OK on success.
  282. */
  283. CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
  284. struct digestdata *digest)
  285. {
  286. size_t chlglen = strlen(chlg);
  287. /* We had an input token before and we got another one now. This means we
  288. provided bad credentials in the previous request. */
  289. if(digest->input_token)
  290. return CURLE_BAD_CONTENT_ENCODING;
  291. /* Simply store the challenge for use later */
  292. digest->input_token = (BYTE *) Curl_memdup(chlg, chlglen);
  293. if(!digest->input_token)
  294. return CURLE_OUT_OF_MEMORY;
  295. digest->input_token_len = chlglen;
  296. return CURLE_OK;
  297. }
  298. /*
  299. * Curl_auth_create_digest_http_message()
  300. *
  301. * This is used to generate a HTTP DIGEST response message ready for sending
  302. * to the recipient.
  303. *
  304. * Parameters:
  305. *
  306. * data [in] - The session handle.
  307. * userp [in] - The user name in the format User or Domain\User.
  308. * passdwp [in] - The user's password.
  309. * request [in] - The HTTP request.
  310. * uripath [in] - The path of the HTTP uri.
  311. * digest [in/out] - The digest data struct being used and modified.
  312. * outptr [in/out] - The address where a pointer to newly allocated memory
  313. * holding the result will be stored upon completion.
  314. * outlen [out] - The length of the output message.
  315. *
  316. * Returns CURLE_OK on success.
  317. */
  318. CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
  319. const char *userp,
  320. const char *passwdp,
  321. const unsigned char *request,
  322. const unsigned char *uripath,
  323. struct digestdata *digest,
  324. char **outptr, size_t *outlen)
  325. {
  326. size_t token_max;
  327. CredHandle credentials;
  328. CtxtHandle context;
  329. char *resp;
  330. BYTE *output_token;
  331. PSecPkgInfo SecurityPackage;
  332. SEC_WINNT_AUTH_IDENTITY identity;
  333. SEC_WINNT_AUTH_IDENTITY *p_identity;
  334. SecBuffer chlg_buf[3];
  335. SecBuffer resp_buf;
  336. SecBufferDesc chlg_desc;
  337. SecBufferDesc resp_desc;
  338. SECURITY_STATUS status;
  339. unsigned long attrs;
  340. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  341. TCHAR *spn;
  342. (void) data;
  343. /* Query the security package for DigestSSP */
  344. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
  345. &SecurityPackage);
  346. if(status != SEC_E_OK)
  347. return CURLE_NOT_BUILT_IN;
  348. token_max = SecurityPackage->cbMaxToken;
  349. /* Release the package buffer as it is not required anymore */
  350. s_pSecFn->FreeContextBuffer(SecurityPackage);
  351. if(userp && *userp) {
  352. /* Populate our identity structure */
  353. if(Curl_create_sspi_identity(userp, passwdp, &identity))
  354. return CURLE_OUT_OF_MEMORY;
  355. /* Populate our identity domain */
  356. if(Curl_override_sspi_http_realm((const char*) digest->input_token,
  357. &identity))
  358. return CURLE_OUT_OF_MEMORY;
  359. /* Allow proper cleanup of the identity structure */
  360. p_identity = &identity;
  361. }
  362. else
  363. /* Use the current Windows user */
  364. p_identity = NULL;
  365. /* Acquire our credentials handle */
  366. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  367. (TCHAR *) TEXT(SP_NAME_DIGEST),
  368. SECPKG_CRED_OUTBOUND, NULL,
  369. p_identity, NULL, NULL,
  370. &credentials, &expiry);
  371. if(status != SEC_E_OK) {
  372. Curl_sspi_free_identity(p_identity);
  373. return CURLE_LOGIN_DENIED;
  374. }
  375. /* Allocate the output buffer according to the max token size as indicated
  376. by the security package */
  377. output_token = malloc(token_max);
  378. if(!output_token) {
  379. s_pSecFn->FreeCredentialsHandle(&credentials);
  380. Curl_sspi_free_identity(p_identity);
  381. return CURLE_OUT_OF_MEMORY;
  382. }
  383. /* Setup the challenge "input" security buffer if present */
  384. chlg_desc.ulVersion = SECBUFFER_VERSION;
  385. chlg_desc.cBuffers = 3;
  386. chlg_desc.pBuffers = chlg_buf;
  387. chlg_buf[0].BufferType = SECBUFFER_TOKEN;
  388. chlg_buf[0].pvBuffer = digest->input_token;
  389. chlg_buf[0].cbBuffer = curlx_uztoul(digest->input_token_len);
  390. chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
  391. chlg_buf[1].pvBuffer = (void *) request;
  392. chlg_buf[1].cbBuffer = curlx_uztoul(strlen((const char *) request));
  393. chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
  394. chlg_buf[2].pvBuffer = NULL;
  395. chlg_buf[2].cbBuffer = 0;
  396. /* Setup the response "output" security buffer */
  397. resp_desc.ulVersion = SECBUFFER_VERSION;
  398. resp_desc.cBuffers = 1;
  399. resp_desc.pBuffers = &resp_buf;
  400. resp_buf.BufferType = SECBUFFER_TOKEN;
  401. resp_buf.pvBuffer = output_token;
  402. resp_buf.cbBuffer = curlx_uztoul(token_max);
  403. spn = Curl_convert_UTF8_to_tchar((char *) uripath);
  404. if(!spn) {
  405. s_pSecFn->FreeCredentialsHandle(&credentials);
  406. Curl_sspi_free_identity(p_identity);
  407. free(output_token);
  408. return CURLE_OUT_OF_MEMORY;
  409. }
  410. /* Generate our reponse message */
  411. status = s_pSecFn->InitializeSecurityContext(&credentials, NULL,
  412. spn,
  413. ISC_REQ_USE_HTTP_STYLE, 0, 0,
  414. &chlg_desc, 0, &context,
  415. &resp_desc, &attrs, &expiry);
  416. Curl_unicodefree(spn);
  417. if(status == SEC_I_COMPLETE_NEEDED ||
  418. status == SEC_I_COMPLETE_AND_CONTINUE)
  419. s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
  420. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  421. s_pSecFn->FreeCredentialsHandle(&credentials);
  422. Curl_sspi_free_identity(p_identity);
  423. free(output_token);
  424. return CURLE_OUT_OF_MEMORY;
  425. }
  426. resp = malloc(resp_buf.cbBuffer + 1);
  427. if(!resp) {
  428. s_pSecFn->DeleteSecurityContext(&context);
  429. s_pSecFn->FreeCredentialsHandle(&credentials);
  430. Curl_sspi_free_identity(p_identity);
  431. free(output_token);
  432. return CURLE_OUT_OF_MEMORY;
  433. }
  434. /* Copy the generated reponse */
  435. memcpy(resp, resp_buf.pvBuffer, resp_buf.cbBuffer);
  436. resp[resp_buf.cbBuffer] = 0x00;
  437. /* Return the response */
  438. *outptr = resp;
  439. *outlen = resp_buf.cbBuffer;
  440. /* Free our handles */
  441. s_pSecFn->DeleteSecurityContext(&context);
  442. s_pSecFn->FreeCredentialsHandle(&credentials);
  443. /* Free the identity structure */
  444. Curl_sspi_free_identity(p_identity);
  445. /* Free the response buffer */
  446. free(output_token);
  447. return CURLE_OK;
  448. }
  449. /*
  450. * Curl_auth_digest_cleanup()
  451. *
  452. * This is used to clean up the digest specific data.
  453. *
  454. * Parameters:
  455. *
  456. * digest [in/out] - The digest data struct being cleaned up.
  457. *
  458. */
  459. void Curl_auth_digest_cleanup(struct digestdata *digest)
  460. {
  461. /* Free the input token */
  462. Curl_safefree(digest->input_token);
  463. /* Reset any variables */
  464. digest->input_token_len = 0;
  465. }
  466. #endif /* USE_WINDOWS_SSPI && !CURL_DISABLE_CRYPTO_AUTH */