ntlm_sspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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/vauth.h"
  28. #include "urldata.h"
  29. #include "curl_ntlm_core.h"
  30. #include "warnless.h"
  31. #include "curl_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((TCHAR *) TEXT(SP_NAME_NTLM),
  52. &SecurityPackage);
  53. /* Release the package buffer as it is not required anymore */
  54. if(status == SEC_E_OK) {
  55. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  56. }
  57. return (status == SEC_E_OK ? TRUE : FALSE);
  58. }
  59. /*
  60. * Curl_auth_create_ntlm_type1_message()
  61. *
  62. * This is used to generate an already encoded NTLM type-1 message ready for
  63. * sending to the recipient.
  64. *
  65. * Parameters:
  66. *
  67. * data [in] - The session handle.
  68. * userp [in] - The username in the format User or Domain\User.
  69. * passwdp [in] - The user's password.
  70. * service [in] - The service type such as http, smtp, pop or imap.
  71. * host [in] - The hostname.
  72. * ntlm [in/out] - The NTLM data struct being used and modified.
  73. * out [out] - The result storage.
  74. *
  75. * Returns CURLE_OK on success.
  76. */
  77. CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
  78. const char *userp,
  79. const char *passwdp,
  80. const char *service,
  81. const char *host,
  82. struct ntlmdata *ntlm,
  83. struct bufref *out)
  84. {
  85. PSecPkgInfo SecurityPackage;
  86. SecBuffer type_1_buf;
  87. SecBufferDesc type_1_desc;
  88. SECURITY_STATUS status;
  89. unsigned long attrs;
  90. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  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((TCHAR *) TEXT(SP_NAME_NTLM),
  95. &SecurityPackage);
  96. if(status != SEC_E_OK) {
  97. failf(data, "SSPI: could not get auth info");
  98. return CURLE_AUTH_ERROR;
  99. }
  100. ntlm->token_max = SecurityPackage->cbMaxToken;
  101. /* Release the package buffer as it is not required anymore */
  102. Curl_pSecFn->FreeContextBuffer(SecurityPackage);
  103. /* Allocate our output buffer */
  104. ntlm->output_token = malloc(ntlm->token_max);
  105. if(!ntlm->output_token)
  106. return CURLE_OUT_OF_MEMORY;
  107. if(userp && *userp) {
  108. CURLcode result;
  109. /* Populate our identity structure */
  110. result = Curl_create_sspi_identity(userp, passwdp, &ntlm->identity);
  111. if(result)
  112. return result;
  113. /* Allow proper cleanup of the identity structure */
  114. ntlm->p_identity = &ntlm->identity;
  115. }
  116. else
  117. /* Use the current Windows user */
  118. ntlm->p_identity = NULL;
  119. /* Allocate our credentials handle */
  120. ntlm->credentials = calloc(1, sizeof(CredHandle));
  121. if(!ntlm->credentials)
  122. return CURLE_OUT_OF_MEMORY;
  123. /* Acquire our credentials handle */
  124. status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
  125. (TCHAR *) TEXT(SP_NAME_NTLM),
  126. SECPKG_CRED_OUTBOUND, NULL,
  127. ntlm->p_identity, NULL, NULL,
  128. ntlm->credentials, &expiry);
  129. if(status != SEC_E_OK)
  130. return CURLE_LOGIN_DENIED;
  131. /* Allocate our new context handle */
  132. ntlm->context = calloc(1, sizeof(CtxtHandle));
  133. if(!ntlm->context)
  134. return CURLE_OUT_OF_MEMORY;
  135. ntlm->spn = Curl_auth_build_spn(service, host, NULL);
  136. if(!ntlm->spn)
  137. return CURLE_OUT_OF_MEMORY;
  138. /* Setup the type-1 "output" security buffer */
  139. type_1_desc.ulVersion = SECBUFFER_VERSION;
  140. type_1_desc.cBuffers = 1;
  141. type_1_desc.pBuffers = &type_1_buf;
  142. type_1_buf.BufferType = SECBUFFER_TOKEN;
  143. type_1_buf.pvBuffer = ntlm->output_token;
  144. type_1_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  145. /* Generate our type-1 message */
  146. status = Curl_pSecFn->InitializeSecurityContext(ntlm->credentials, NULL,
  147. ntlm->spn,
  148. 0, 0, SECURITY_NETWORK_DREP,
  149. NULL, 0,
  150. ntlm->context, &type_1_desc,
  151. &attrs, &expiry);
  152. if(status == SEC_I_COMPLETE_NEEDED ||
  153. status == SEC_I_COMPLETE_AND_CONTINUE)
  154. Curl_pSecFn->CompleteAuthToken(ntlm->context, &type_1_desc);
  155. else if(status == SEC_E_INSUFFICIENT_MEMORY)
  156. return CURLE_OUT_OF_MEMORY;
  157. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
  158. return CURLE_AUTH_ERROR;
  159. /* Return the response. */
  160. Curl_bufref_set(out, ntlm->output_token, type_1_buf.cbBuffer, NULL);
  161. return CURLE_OK;
  162. }
  163. /*
  164. * Curl_auth_decode_ntlm_type2_message()
  165. *
  166. * This is used to decode an already encoded NTLM type-2 message.
  167. *
  168. * Parameters:
  169. *
  170. * data [in] - The session handle.
  171. * type2 [in] - The type-2 message.
  172. * ntlm [in/out] - The NTLM data struct being used and modified.
  173. *
  174. * Returns CURLE_OK on success.
  175. */
  176. CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
  177. const struct bufref *type2,
  178. struct ntlmdata *ntlm)
  179. {
  180. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  181. (void) data;
  182. #endif
  183. /* Ensure we have a valid type-2 message */
  184. if(!Curl_bufref_len(type2)) {
  185. infof(data, "NTLM handshake failure (empty type-2 message)");
  186. return CURLE_BAD_CONTENT_ENCODING;
  187. }
  188. /* Store the challenge for later use */
  189. ntlm->input_token = Curl_memdup0((const char *)Curl_bufref_ptr(type2),
  190. Curl_bufref_len(type2));
  191. if(!ntlm->input_token)
  192. return CURLE_OUT_OF_MEMORY;
  193. ntlm->input_token_len = Curl_bufref_len(type2);
  194. return CURLE_OK;
  195. }
  196. /*
  197. * Curl_auth_create_ntlm_type3_message()
  198. * Curl_auth_create_ntlm_type3_message()
  199. *
  200. * This is used to generate an already encoded NTLM type-3 message ready for
  201. * sending to the recipient.
  202. *
  203. * Parameters:
  204. *
  205. * data [in] - The session handle.
  206. * userp [in] - The username in the format User or Domain\User.
  207. * passwdp [in] - The user's password.
  208. * ntlm [in/out] - The NTLM data struct being used and modified.
  209. * out [out] - The result storage.
  210. *
  211. * Returns CURLE_OK on success.
  212. */
  213. CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
  214. const char *userp,
  215. const char *passwdp,
  216. struct ntlmdata *ntlm,
  217. struct bufref *out)
  218. {
  219. CURLcode result = CURLE_OK;
  220. SecBuffer type_2_bufs[2];
  221. SecBuffer type_3_buf;
  222. SecBufferDesc type_2_desc;
  223. SecBufferDesc type_3_desc;
  224. SECURITY_STATUS status;
  225. unsigned long attrs;
  226. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  227. #if defined(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://docs.microsoft.com/en-us/security-updates
  246. * /SecurityAdvisories/2009/973811
  247. */
  248. if(ntlm->sslContext) {
  249. SEC_CHANNEL_BINDINGS channelBindings;
  250. SecPkgContext_Bindings pkgBindings;
  251. pkgBindings.Bindings = &channelBindings;
  252. status = Curl_pSecFn->QueryContextAttributes(
  253. ntlm->sslContext,
  254. SECPKG_ATTR_ENDPOINT_BINDINGS,
  255. &pkgBindings
  256. );
  257. if(status == SEC_E_OK) {
  258. type_2_desc.cBuffers++;
  259. type_2_bufs[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
  260. type_2_bufs[1].cbBuffer = pkgBindings.BindingsLength;
  261. type_2_bufs[1].pvBuffer = pkgBindings.Bindings;
  262. }
  263. }
  264. #endif
  265. /* Setup the type-3 "output" security buffer */
  266. type_3_desc.ulVersion = SECBUFFER_VERSION;
  267. type_3_desc.cBuffers = 1;
  268. type_3_desc.pBuffers = &type_3_buf;
  269. type_3_buf.BufferType = SECBUFFER_TOKEN;
  270. type_3_buf.pvBuffer = ntlm->output_token;
  271. type_3_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  272. /* Generate our type-3 message */
  273. status = Curl_pSecFn->InitializeSecurityContext(ntlm->credentials,
  274. ntlm->context,
  275. ntlm->spn,
  276. 0, 0, SECURITY_NETWORK_DREP,
  277. &type_2_desc,
  278. 0, ntlm->context,
  279. &type_3_desc,
  280. &attrs, &expiry);
  281. if(status != SEC_E_OK) {
  282. infof(data, "NTLM handshake failure (type-3 message): Status=%lx",
  283. status);
  284. if(status == SEC_E_INSUFFICIENT_MEMORY)
  285. return CURLE_OUT_OF_MEMORY;
  286. return CURLE_AUTH_ERROR;
  287. }
  288. /* Return the response. */
  289. result = Curl_bufref_memdup(out, ntlm->output_token, type_3_buf.cbBuffer);
  290. Curl_auth_cleanup_ntlm(ntlm);
  291. return result;
  292. }
  293. /*
  294. * Curl_auth_cleanup_ntlm()
  295. *
  296. * This is used to clean up the NTLM specific data.
  297. *
  298. * Parameters:
  299. *
  300. * ntlm [in/out] - The NTLM data struct being cleaned up.
  301. *
  302. */
  303. void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm)
  304. {
  305. /* Free our security context */
  306. if(ntlm->context) {
  307. Curl_pSecFn->DeleteSecurityContext(ntlm->context);
  308. free(ntlm->context);
  309. ntlm->context = NULL;
  310. }
  311. /* Free our credentials handle */
  312. if(ntlm->credentials) {
  313. Curl_pSecFn->FreeCredentialsHandle(ntlm->credentials);
  314. free(ntlm->credentials);
  315. ntlm->credentials = NULL;
  316. }
  317. /* Free our identity */
  318. Curl_sspi_free_identity(ntlm->p_identity);
  319. ntlm->p_identity = NULL;
  320. /* Free the input and output tokens */
  321. Curl_safefree(ntlm->input_token);
  322. Curl_safefree(ntlm->output_token);
  323. /* Reset any variables */
  324. ntlm->token_max = 0;
  325. Curl_safefree(ntlm->spn);
  326. }
  327. #endif /* USE_WINDOWS_SSPI && USE_NTLM */