ntlm_sspi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 "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. return (status == SEC_E_OK ? TRUE : FALSE);
  51. }
  52. /*
  53. * Curl_auth_create_ntlm_type1_message()
  54. *
  55. * This is used to generate an already encoded NTLM type-1 message ready for
  56. * sending to the recipient.
  57. *
  58. * Parameters:
  59. *
  60. * userp [in] - The user name in the format User or Domain\User.
  61. * passdwp [in] - The user's password.
  62. * ntlm [in/out] - The NTLM data struct being used and modified.
  63. * outptr [in/out] - The address where a pointer to newly allocated memory
  64. * holding the result will be stored upon completion.
  65. * outlen [out] - The length of the output message.
  66. *
  67. * Returns CURLE_OK on success.
  68. */
  69. CURLcode Curl_auth_create_ntlm_type1_message(const char *userp,
  70. const char *passwdp,
  71. struct ntlmdata *ntlm,
  72. char **outptr, size_t *outlen)
  73. {
  74. PSecPkgInfo SecurityPackage;
  75. SecBuffer type_1_buf;
  76. SecBufferDesc type_1_desc;
  77. SECURITY_STATUS status;
  78. unsigned long attrs;
  79. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  80. /* Clean up any former leftovers and initialise to defaults */
  81. Curl_auth_ntlm_cleanup(ntlm);
  82. /* Query the security package for NTLM */
  83. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_NTLM),
  84. &SecurityPackage);
  85. if(status != SEC_E_OK)
  86. return CURLE_NOT_BUILT_IN;
  87. ntlm->token_max = SecurityPackage->cbMaxToken;
  88. /* Release the package buffer as it is not required anymore */
  89. s_pSecFn->FreeContextBuffer(SecurityPackage);
  90. /* Allocate our output buffer */
  91. ntlm->output_token = malloc(ntlm->token_max);
  92. if(!ntlm->output_token)
  93. return CURLE_OUT_OF_MEMORY;
  94. if(userp && *userp) {
  95. CURLcode result;
  96. /* Populate our identity structure */
  97. result = Curl_create_sspi_identity(userp, passwdp, &ntlm->identity);
  98. if(result)
  99. return result;
  100. /* Allow proper cleanup of the identity structure */
  101. ntlm->p_identity = &ntlm->identity;
  102. }
  103. else
  104. /* Use the current Windows user */
  105. ntlm->p_identity = NULL;
  106. /* Allocate our credentials handle */
  107. ntlm->credentials = malloc(sizeof(CredHandle));
  108. if(!ntlm->credentials)
  109. return CURLE_OUT_OF_MEMORY;
  110. memset(ntlm->credentials, 0, sizeof(CredHandle));
  111. /* Acquire our credentials handle */
  112. status = s_pSecFn->AcquireCredentialsHandle(NULL,
  113. (TCHAR *) TEXT(SP_NAME_NTLM),
  114. SECPKG_CRED_OUTBOUND, NULL,
  115. ntlm->p_identity, NULL, NULL,
  116. ntlm->credentials, &expiry);
  117. if(status != SEC_E_OK)
  118. return CURLE_LOGIN_DENIED;
  119. /* Allocate our new context handle */
  120. ntlm->context = malloc(sizeof(CtxtHandle));
  121. if(!ntlm->context)
  122. return CURLE_OUT_OF_MEMORY;
  123. memset(ntlm->context, 0, sizeof(CtxtHandle));
  124. /* Setup the type-1 "output" security buffer */
  125. type_1_desc.ulVersion = SECBUFFER_VERSION;
  126. type_1_desc.cBuffers = 1;
  127. type_1_desc.pBuffers = &type_1_buf;
  128. type_1_buf.BufferType = SECBUFFER_TOKEN;
  129. type_1_buf.pvBuffer = ntlm->output_token;
  130. type_1_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  131. /* Generate our type-1 message */
  132. status = s_pSecFn->InitializeSecurityContext(ntlm->credentials, NULL,
  133. (TCHAR *) TEXT(""),
  134. 0, 0, SECURITY_NETWORK_DREP,
  135. NULL, 0,
  136. ntlm->context, &type_1_desc,
  137. &attrs, &expiry);
  138. if(status == SEC_I_COMPLETE_NEEDED ||
  139. status == SEC_I_COMPLETE_AND_CONTINUE)
  140. s_pSecFn->CompleteAuthToken(ntlm->context, &type_1_desc);
  141. else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
  142. return CURLE_RECV_ERROR;
  143. /* Base64 encode the response */
  144. return Curl_base64_encode(NULL, (char *) ntlm->output_token,
  145. type_1_buf.cbBuffer, outptr, outlen);
  146. }
  147. /*
  148. * Curl_auth_decode_ntlm_type2_message()
  149. *
  150. * This is used to decode an already encoded NTLM type-2 message.
  151. *
  152. * Parameters:
  153. *
  154. * data [in] - The session handle.
  155. * type2msg [in] - The base64 encoded type-2 message.
  156. * ntlm [in/out] - The NTLM data struct being used and modified.
  157. *
  158. * Returns CURLE_OK on success.
  159. */
  160. CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
  161. const char *type2msg,
  162. struct ntlmdata *ntlm)
  163. {
  164. CURLcode result = CURLE_OK;
  165. unsigned char *type2 = NULL;
  166. size_t type2_len = 0;
  167. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  168. (void) data;
  169. #endif
  170. /* Decode the base-64 encoded type-2 message */
  171. if(strlen(type2msg) && *type2msg != '=') {
  172. result = Curl_base64_decode(type2msg, &type2, &type2_len);
  173. if(result)
  174. return result;
  175. }
  176. /* Ensure we have a valid type-2 message */
  177. if(!type2) {
  178. infof(data, "NTLM handshake failure (empty type-2 message)\n");
  179. return CURLE_BAD_CONTENT_ENCODING;
  180. }
  181. /* Simply store the challenge for use later */
  182. ntlm->input_token = type2;
  183. ntlm->input_token_len = type2_len;
  184. return result;
  185. }
  186. /*
  187. * Curl_auth_create_ntlm_type3_message()
  188. * Curl_auth_create_ntlm_type3_message()
  189. *
  190. * This is used to generate an already encoded NTLM type-3 message ready for
  191. * sending to the recipient.
  192. *
  193. * Parameters:
  194. *
  195. * data [in] - The session handle.
  196. * userp [in] - The user name in the format User or Domain\User.
  197. * passdwp [in] - The user's password.
  198. * ntlm [in/out] - The NTLM data struct being used and modified.
  199. * outptr [in/out] - The address where a pointer to newly allocated memory
  200. * holding the result will be stored upon completion.
  201. * outlen [out] - The length of the output message.
  202. *
  203. * Returns CURLE_OK on success.
  204. */
  205. CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
  206. const char *userp,
  207. const char *passwdp,
  208. struct ntlmdata *ntlm,
  209. char **outptr, size_t *outlen)
  210. {
  211. CURLcode result = CURLE_OK;
  212. SecBuffer type_2_buf;
  213. SecBuffer type_3_buf;
  214. SecBufferDesc type_2_desc;
  215. SecBufferDesc type_3_desc;
  216. SECURITY_STATUS status;
  217. unsigned long attrs;
  218. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  219. (void) passwdp;
  220. (void) userp;
  221. /* Setup the type-2 "input" security buffer */
  222. type_2_desc.ulVersion = SECBUFFER_VERSION;
  223. type_2_desc.cBuffers = 1;
  224. type_2_desc.pBuffers = &type_2_buf;
  225. type_2_buf.BufferType = SECBUFFER_TOKEN;
  226. type_2_buf.pvBuffer = ntlm->input_token;
  227. type_2_buf.cbBuffer = curlx_uztoul(ntlm->input_token_len);
  228. /* Setup the type-3 "output" security buffer */
  229. type_3_desc.ulVersion = SECBUFFER_VERSION;
  230. type_3_desc.cBuffers = 1;
  231. type_3_desc.pBuffers = &type_3_buf;
  232. type_3_buf.BufferType = SECBUFFER_TOKEN;
  233. type_3_buf.pvBuffer = ntlm->output_token;
  234. type_3_buf.cbBuffer = curlx_uztoul(ntlm->token_max);
  235. /* Generate our type-3 message */
  236. status = s_pSecFn->InitializeSecurityContext(ntlm->credentials,
  237. ntlm->context,
  238. (TCHAR *) TEXT(""),
  239. 0, 0, SECURITY_NETWORK_DREP,
  240. &type_2_desc,
  241. 0, ntlm->context,
  242. &type_3_desc,
  243. &attrs, &expiry);
  244. if(status != SEC_E_OK) {
  245. infof(data, "NTLM handshake failure (type-3 message): Status=%x\n",
  246. status);
  247. return CURLE_RECV_ERROR;
  248. }
  249. /* Base64 encode the response */
  250. result = Curl_base64_encode(data, (char *) ntlm->output_token,
  251. type_3_buf.cbBuffer, outptr, outlen);
  252. Curl_auth_ntlm_cleanup(ntlm);
  253. return result;
  254. }
  255. /*
  256. * Curl_auth_ntlm_cleanup()
  257. *
  258. * This is used to clean up the NTLM specific data.
  259. *
  260. * Parameters:
  261. *
  262. * ntlm [in/out] - The NTLM data struct being cleaned up.
  263. *
  264. */
  265. void Curl_auth_ntlm_cleanup(struct ntlmdata *ntlm)
  266. {
  267. /* Free our security context */
  268. if(ntlm->context) {
  269. s_pSecFn->DeleteSecurityContext(ntlm->context);
  270. free(ntlm->context);
  271. ntlm->context = NULL;
  272. }
  273. /* Free our credentials handle */
  274. if(ntlm->credentials) {
  275. s_pSecFn->FreeCredentialsHandle(ntlm->credentials);
  276. free(ntlm->credentials);
  277. ntlm->credentials = NULL;
  278. }
  279. /* Free our identity */
  280. Curl_sspi_free_identity(ntlm->p_identity);
  281. ntlm->p_identity = NULL;
  282. /* Free the input and output tokens */
  283. Curl_safefree(ntlm->input_token);
  284. Curl_safefree(ntlm->output_token);
  285. /* Reset any variables */
  286. ntlm->token_max = 0;
  287. }
  288. #endif /* USE_WINDOWS_SSPI && USE_NTLM */