ntlm_sspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.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. failf(data, "SSPI: couldn't get auth info\n");
  98. return CURLE_AUTH_ERROR;
  99. }
  100. ntlm->token_max = SecurityPackage->cbMaxToken;
  101. /* Release the package buffer as it is not required anymore */
  102. s_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 = s_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 = s_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. s_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. /* Base64 encode the response */
  160. return Curl_base64_encode(data, (char *) ntlm->output_token,
  161. type_1_buf.cbBuffer, outptr, outlen);
  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. * type2msg [in] - The base64 encoded 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 char *type2msg,
  178. struct ntlmdata *ntlm)
  179. {
  180. CURLcode result = CURLE_OK;
  181. unsigned char *type2 = NULL;
  182. size_t type2_len = 0;
  183. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  184. (void) data;
  185. #endif
  186. /* Decode the base-64 encoded type-2 message */
  187. if(strlen(type2msg) && *type2msg != '=') {
  188. result = Curl_base64_decode(type2msg, &type2, &type2_len);
  189. if(result)
  190. return result;
  191. }
  192. /* Ensure we have a valid type-2 message */
  193. if(!type2) {
  194. infof(data, "NTLM handshake failure (empty type-2 message)\n");
  195. return CURLE_BAD_CONTENT_ENCODING;
  196. }
  197. /* Simply store the challenge for use later */
  198. ntlm->input_token = type2;
  199. ntlm->input_token_len = type2_len;
  200. return result;
  201. }
  202. /*
  203. * Curl_auth_create_ntlm_type3_message()
  204. * Curl_auth_create_ntlm_type3_message()
  205. *
  206. * This is used to generate an already encoded NTLM type-3 message ready for
  207. * sending to the recipient.
  208. *
  209. * Parameters:
  210. *
  211. * data [in] - The session handle.
  212. * userp [in] - The user name in the format User or Domain\User.
  213. * passwdp [in] - The user's password.
  214. * ntlm [in/out] - The NTLM data struct being used and modified.
  215. * outptr [in/out] - The address where a pointer to newly allocated memory
  216. * holding the result will be stored upon completion.
  217. * outlen [out] - The length of the output message.
  218. *
  219. * Returns CURLE_OK on success.
  220. */
  221. CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
  222. const char *userp,
  223. const char *passwdp,
  224. struct ntlmdata *ntlm,
  225. char **outptr, size_t *outlen)
  226. {
  227. CURLcode result = CURLE_OK;
  228. SecBuffer type_2_bufs[2];
  229. SecBuffer type_3_buf;
  230. SecBufferDesc type_2_desc;
  231. SecBufferDesc type_3_desc;
  232. SECURITY_STATUS status;
  233. unsigned long attrs;
  234. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  235. (void) passwdp;
  236. (void) userp;
  237. /* Setup the type-2 "input" security buffer */
  238. type_2_desc.ulVersion = SECBUFFER_VERSION;
  239. type_2_desc.cBuffers = 1;
  240. type_2_desc.pBuffers = &type_2_bufs[0];
  241. type_2_bufs[0].BufferType = SECBUFFER_TOKEN;
  242. type_2_bufs[0].pvBuffer = ntlm->input_token;
  243. type_2_bufs[0].cbBuffer = curlx_uztoul(ntlm->input_token_len);
  244. #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
  245. /* ssl context comes from schannel.
  246. * When extended protection is used in IIS server,
  247. * we have to pass a second SecBuffer to the SecBufferDesc
  248. * otherwise IIS will not pass the authentication (401 response).
  249. * Minimum supported version is Windows 7.
  250. * https://docs.microsoft.com/en-us/security-updates
  251. * /SecurityAdvisories/2009/973811
  252. */
  253. if(ntlm->sslContext) {
  254. SEC_CHANNEL_BINDINGS channelBindings;
  255. SecPkgContext_Bindings pkgBindings;
  256. pkgBindings.Bindings = &channelBindings;
  257. status = s_pSecFn->QueryContextAttributes(
  258. ntlm->sslContext,
  259. SECPKG_ATTR_ENDPOINT_BINDINGS,
  260. &pkgBindings
  261. );
  262. if(status == SEC_E_OK) {
  263. type_2_desc.cBuffers++;
  264. type_2_bufs[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
  265. type_2_bufs[1].cbBuffer = pkgBindings.BindingsLength;
  266. type_2_bufs[1].pvBuffer = pkgBindings.Bindings;
  267. }
  268. }
  269. #endif
  270. /* Setup the type-3 "output" security buffer */
  271. type_3_desc.ulVersion = SECBUFFER_VERSION;
  272. type_3_desc.cBuffers = 1;
  273. type_3_desc.pBuffers = &type_3_buf;
  274. type_3_buf.BufferType = SECBUFFER_TOKEN;
  275. type_3_buf.pvBuffer = ntlm->output_token;
  276. type_3_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  277. /* Generate our type-3 message */
  278. status = s_pSecFn->InitializeSecurityContext(ntlm->credentials,
  279. ntlm->context,
  280. ntlm->spn,
  281. 0, 0, SECURITY_NETWORK_DREP,
  282. &type_2_desc,
  283. 0, ntlm->context,
  284. &type_3_desc,
  285. &attrs, &expiry);
  286. if(status != SEC_E_OK) {
  287. infof(data, "NTLM handshake failure (type-3 message): Status=%x\n",
  288. status);
  289. if(status == SEC_E_INSUFFICIENT_MEMORY)
  290. return CURLE_OUT_OF_MEMORY;
  291. return CURLE_AUTH_ERROR;
  292. }
  293. /* Base64 encode the response */
  294. result = Curl_base64_encode(data, (char *) ntlm->output_token,
  295. type_3_buf.cbBuffer, outptr, outlen);
  296. Curl_auth_cleanup_ntlm(ntlm);
  297. return result;
  298. }
  299. /*
  300. * Curl_auth_cleanup_ntlm()
  301. *
  302. * This is used to clean up the NTLM specific data.
  303. *
  304. * Parameters:
  305. *
  306. * ntlm [in/out] - The NTLM data struct being cleaned up.
  307. *
  308. */
  309. void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm)
  310. {
  311. /* Free our security context */
  312. if(ntlm->context) {
  313. s_pSecFn->DeleteSecurityContext(ntlm->context);
  314. free(ntlm->context);
  315. ntlm->context = NULL;
  316. }
  317. /* Free our credentials handle */
  318. if(ntlm->credentials) {
  319. s_pSecFn->FreeCredentialsHandle(ntlm->credentials);
  320. free(ntlm->credentials);
  321. ntlm->credentials = NULL;
  322. }
  323. /* Free our identity */
  324. Curl_sspi_free_identity(ntlm->p_identity);
  325. ntlm->p_identity = NULL;
  326. /* Free the input and output tokens */
  327. Curl_safefree(ntlm->input_token);
  328. Curl_safefree(ntlm->output_token);
  329. /* Reset any variables */
  330. ntlm->token_max = 0;
  331. Curl_safefree(ntlm->spn);
  332. }
  333. #endif /* USE_WINDOWS_SSPI && USE_NTLM */