ntlm_sspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "../curl_setup.h"
  25. #if defined(USE_WINDOWS_SSPI) && defined(USE_NTLM)
  26. #include <curl/curl.h>
  27. #include "vauth.h"
  28. #include "../urldata.h"
  29. #include "../curl_ntlm_core.h"
  30. #include "../curlx/warnless.h"
  31. #include "../curlx/multibyte.h"
  32. #include "../sendf.h"
  33. #include "../strdup.h"
  34. /* The last #include files should be: */
  35. #include "../curl_memory.h"
  36. #include "../memdebug.h"
  37. /*
  38. * Curl_auth_is_ntlm_supported()
  39. *
  40. * This is used to evaluate if NTLM is supported.
  41. *
  42. * Parameters: None
  43. *
  44. * Returns TRUE if NTLM is supported by Windows SSPI.
  45. */
  46. bool Curl_auth_is_ntlm_supported(void)
  47. {
  48. PSecPkgInfo SecurityPackage;
  49. SECURITY_STATUS status;
  50. /* Query the security package for NTLM */
  51. status = Curl_pSecFn->QuerySecurityPackageInfo(
  52. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_NTLM)),
  53. &SecurityPackage);
  54. /* Release the package buffer as it is not required anymore */
  55. if(status == SEC_E_OK) {
  56. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  57. }
  58. return status == SEC_E_OK;
  59. }
  60. /*
  61. * Curl_auth_create_ntlm_type1_message()
  62. *
  63. * This is used to generate an already encoded NTLM type-1 message ready for
  64. * sending to the recipient.
  65. *
  66. * Parameters:
  67. *
  68. * data [in] - The session handle.
  69. * userp [in] - The username in the format User or Domain\User.
  70. * passwdp [in] - The user's password.
  71. * service [in] - The service type such as http, smtp, pop or imap.
  72. * host [in] - The hostname.
  73. * ntlm [in/out] - The NTLM data struct being used and modified.
  74. * out [out] - The result storage.
  75. *
  76. * Returns CURLE_OK on success.
  77. */
  78. CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
  79. const char *userp,
  80. const char *passwdp,
  81. const char *service,
  82. const char *host,
  83. struct ntlmdata *ntlm,
  84. struct bufref *out)
  85. {
  86. PSecPkgInfo SecurityPackage;
  87. SecBuffer type_1_buf;
  88. SecBufferDesc type_1_desc;
  89. SECURITY_STATUS status;
  90. unsigned long attrs;
  91. /* Clean up any former leftovers and initialise to defaults */
  92. Curl_auth_cleanup_ntlm(ntlm);
  93. /* Query the security package for NTLM */
  94. status = Curl_pSecFn->QuerySecurityPackageInfo(
  95. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_NTLM)),
  96. &SecurityPackage);
  97. if(status != SEC_E_OK) {
  98. failf(data, "SSPI: could not get auth info");
  99. return CURLE_AUTH_ERROR;
  100. }
  101. ntlm->token_max = SecurityPackage->cbMaxToken;
  102. /* Release the package buffer as it is not required anymore */
  103. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  104. /* Allocate our output buffer */
  105. ntlm->output_token = malloc(ntlm->token_max);
  106. if(!ntlm->output_token)
  107. return CURLE_OUT_OF_MEMORY;
  108. if(userp && *userp) {
  109. CURLcode result;
  110. /* Populate our identity structure */
  111. result = Curl_create_sspi_identity(userp, passwdp, &ntlm->identity);
  112. if(result)
  113. return result;
  114. /* Allow proper cleanup of the identity structure */
  115. ntlm->p_identity = &ntlm->identity;
  116. }
  117. else
  118. /* Use the current Windows user */
  119. ntlm->p_identity = NULL;
  120. /* Allocate our credentials handle */
  121. ntlm->credentials = calloc(1, sizeof(CredHandle));
  122. if(!ntlm->credentials)
  123. return CURLE_OUT_OF_MEMORY;
  124. /* Acquire our credentials handle */
  125. status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
  126. (TCHAR *)CURL_UNCONST(TEXT(SP_NAME_NTLM)),
  127. SECPKG_CRED_OUTBOUND, NULL,
  128. ntlm->p_identity, NULL, NULL,
  129. ntlm->credentials, NULL);
  130. if(status != SEC_E_OK)
  131. return CURLE_LOGIN_DENIED;
  132. /* Allocate our new context handle */
  133. ntlm->context = calloc(1, sizeof(CtxtHandle));
  134. if(!ntlm->context)
  135. return CURLE_OUT_OF_MEMORY;
  136. ntlm->spn = Curl_auth_build_spn(service, host, NULL);
  137. if(!ntlm->spn)
  138. return CURLE_OUT_OF_MEMORY;
  139. /* Setup the type-1 "output" security buffer */
  140. type_1_desc.ulVersion = SECBUFFER_VERSION;
  141. type_1_desc.cBuffers = 1;
  142. type_1_desc.pBuffers = &type_1_buf;
  143. type_1_buf.BufferType = SECBUFFER_TOKEN;
  144. type_1_buf.pvBuffer = ntlm->output_token;
  145. type_1_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  146. /* Generate our type-1 message */
  147. status = Curl_pSecFn->InitializeSecurityContext(ntlm->credentials, NULL,
  148. ntlm->spn,
  149. 0, 0, SECURITY_NETWORK_DREP,
  150. NULL, 0,
  151. ntlm->context, &type_1_desc,
  152. &attrs, NULL);
  153. if(status == SEC_I_COMPLETE_NEEDED ||
  154. status == SEC_I_COMPLETE_AND_CONTINUE)
  155. Curl_pSecFn->CompleteAuthToken(ntlm->context, &type_1_desc);
  156. else if(status == SEC_E_INSUFFICIENT_MEMORY)
  157. return CURLE_OUT_OF_MEMORY;
  158. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
  159. return CURLE_AUTH_ERROR;
  160. /* Return the response. */
  161. Curl_bufref_set(out, ntlm->output_token, type_1_buf.cbBuffer, NULL);
  162. return CURLE_OK;
  163. }
  164. /*
  165. * Curl_auth_decode_ntlm_type2_message()
  166. *
  167. * This is used to decode an already encoded NTLM type-2 message.
  168. *
  169. * Parameters:
  170. *
  171. * data [in] - The session handle.
  172. * type2 [in] - The type-2 message.
  173. * ntlm [in/out] - The NTLM data struct being used and modified.
  174. *
  175. * Returns CURLE_OK on success.
  176. */
  177. CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
  178. const struct bufref *type2,
  179. struct ntlmdata *ntlm)
  180. {
  181. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  182. (void)data;
  183. #endif
  184. /* Ensure we have a valid type-2 message */
  185. if(!Curl_bufref_len(type2)) {
  186. infof(data, "NTLM handshake failure (empty type-2 message)");
  187. return CURLE_BAD_CONTENT_ENCODING;
  188. }
  189. /* Store the challenge for later use */
  190. ntlm->input_token = Curl_memdup0((const char *)Curl_bufref_ptr(type2),
  191. Curl_bufref_len(type2));
  192. if(!ntlm->input_token)
  193. return CURLE_OUT_OF_MEMORY;
  194. ntlm->input_token_len = Curl_bufref_len(type2);
  195. return CURLE_OK;
  196. }
  197. /*
  198. * Curl_auth_create_ntlm_type3_message()
  199. * Curl_auth_create_ntlm_type3_message()
  200. *
  201. * This is used to generate an already encoded NTLM type-3 message ready for
  202. * sending to the recipient.
  203. *
  204. * Parameters:
  205. *
  206. * data [in] - The session handle.
  207. * userp [in] - The username in the format User or Domain\User.
  208. * passwdp [in] - The user's password.
  209. * ntlm [in/out] - The NTLM data struct being used and modified.
  210. * out [out] - The result storage.
  211. *
  212. * Returns CURLE_OK on success.
  213. */
  214. CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
  215. const char *userp,
  216. const char *passwdp,
  217. struct ntlmdata *ntlm,
  218. struct bufref *out)
  219. {
  220. CURLcode result = CURLE_OK;
  221. SecBuffer type_2_bufs[2];
  222. SecBuffer type_3_buf;
  223. SecBufferDesc type_2_desc;
  224. SecBufferDesc type_3_desc;
  225. SECURITY_STATUS status;
  226. unsigned long attrs;
  227. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  228. (void)data;
  229. #endif
  230. (void)passwdp;
  231. (void)userp;
  232. /* Setup the type-2 "input" security buffer */
  233. type_2_desc.ulVersion = SECBUFFER_VERSION;
  234. type_2_desc.cBuffers = 1;
  235. type_2_desc.pBuffers = &type_2_bufs[0];
  236. type_2_bufs[0].BufferType = SECBUFFER_TOKEN;
  237. type_2_bufs[0].pvBuffer = ntlm->input_token;
  238. type_2_bufs[0].cbBuffer = curlx_uztoul(ntlm->input_token_len);
  239. #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
  240. /* ssl context comes from schannel.
  241. * When extended protection is used in IIS server,
  242. * we have to pass a second SecBuffer to the SecBufferDesc
  243. * otherwise IIS will not pass the authentication (401 response).
  244. * Minimum supported version is Windows 7.
  245. * https://learn.microsoft.com/security-updates/SecurityAdvisories/2009/973811
  246. */
  247. if(ntlm->sslContext) {
  248. SEC_CHANNEL_BINDINGS channelBindings;
  249. SecPkgContext_Bindings pkgBindings;
  250. pkgBindings.Bindings = &channelBindings;
  251. status = Curl_pSecFn->QueryContextAttributes(
  252. ntlm->sslContext,
  253. SECPKG_ATTR_ENDPOINT_BINDINGS,
  254. &pkgBindings
  255. );
  256. if(status == SEC_E_OK) {
  257. type_2_desc.cBuffers++;
  258. type_2_bufs[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
  259. type_2_bufs[1].cbBuffer = pkgBindings.BindingsLength;
  260. type_2_bufs[1].pvBuffer = pkgBindings.Bindings;
  261. }
  262. }
  263. #endif
  264. /* Setup the type-3 "output" security buffer */
  265. type_3_desc.ulVersion = SECBUFFER_VERSION;
  266. type_3_desc.cBuffers = 1;
  267. type_3_desc.pBuffers = &type_3_buf;
  268. type_3_buf.BufferType = SECBUFFER_TOKEN;
  269. type_3_buf.pvBuffer = ntlm->output_token;
  270. type_3_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  271. /* Generate our type-3 message */
  272. status = Curl_pSecFn->InitializeSecurityContext(ntlm->credentials,
  273. ntlm->context,
  274. ntlm->spn,
  275. 0, 0, SECURITY_NETWORK_DREP,
  276. &type_2_desc,
  277. 0, ntlm->context,
  278. &type_3_desc,
  279. &attrs, NULL);
  280. if(status != SEC_E_OK) {
  281. infof(data, "NTLM handshake failure (type-3 message): Status=0x%08lx",
  282. status);
  283. if(status == SEC_E_INSUFFICIENT_MEMORY)
  284. return CURLE_OUT_OF_MEMORY;
  285. return CURLE_AUTH_ERROR;
  286. }
  287. /* Return the response. */
  288. result = Curl_bufref_memdup(out, ntlm->output_token, type_3_buf.cbBuffer);
  289. Curl_auth_cleanup_ntlm(ntlm);
  290. return result;
  291. }
  292. /*
  293. * Curl_auth_cleanup_ntlm()
  294. *
  295. * This is used to clean up the NTLM specific data.
  296. *
  297. * Parameters:
  298. *
  299. * ntlm [in/out] - The NTLM data struct being cleaned up.
  300. *
  301. */
  302. void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm)
  303. {
  304. /* Free our security context */
  305. if(ntlm->context) {
  306. Curl_pSecFn->DeleteSecurityContext(ntlm->context);
  307. free(ntlm->context);
  308. ntlm->context = NULL;
  309. }
  310. /* Free our credentials handle */
  311. if(ntlm->credentials) {
  312. Curl_pSecFn->FreeCredentialsHandle(ntlm->credentials);
  313. free(ntlm->credentials);
  314. ntlm->credentials = NULL;
  315. }
  316. /* Free our identity */
  317. Curl_sspi_free_identity(ntlm->p_identity);
  318. ntlm->p_identity = NULL;
  319. /* Free the input and output tokens */
  320. Curl_safefree(ntlm->input_token);
  321. Curl_safefree(ntlm->output_token);
  322. /* Reset any variables */
  323. ntlm->token_max = 0;
  324. Curl_safefree(ntlm->spn);
  325. }
  326. #endif /* USE_WINDOWS_SSPI && USE_NTLM */