spnego_sspi.c 12 KB

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