2
0

ntlm_sspi.c 12 KB

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