ntlm_sspi.c 12 KB

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