spnego_sspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * 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. return CURLE_NOT_BUILT_IN;
  119. nego->token_max = SecurityPackage->cbMaxToken;
  120. /* Release the package buffer as it is not required anymore */
  121. s_pSecFn->FreeContextBuffer(SecurityPackage);
  122. /* Allocate our output buffer */
  123. nego->output_token = malloc(nego->token_max);
  124. if(!nego->output_token)
  125. return CURLE_OUT_OF_MEMORY;
  126. }
  127. if(!nego->credentials) {
  128. /* Do we have credentials to use or are we using single sign-on? */
  129. if(user && *user) {
  130. /* Populate our identity structure */
  131. result = Curl_create_sspi_identity(user, password, &nego->identity);
  132. if(result)
  133. return result;
  134. /* Allow proper cleanup of the identity structure */
  135. nego->p_identity = &nego->identity;
  136. }
  137. else
  138. /* Use the current Windows user */
  139. nego->p_identity = NULL;
  140. /* Allocate our credentials handle */
  141. nego->credentials = calloc(1, sizeof(CredHandle));
  142. if(!nego->credentials)
  143. return CURLE_OUT_OF_MEMORY;
  144. /* Acquire our credentials handle */
  145. nego->status =
  146. s_pSecFn->AcquireCredentialsHandle(NULL,
  147. (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
  148. SECPKG_CRED_OUTBOUND, NULL,
  149. nego->p_identity, NULL, NULL,
  150. nego->credentials, &expiry);
  151. if(nego->status != SEC_E_OK)
  152. return CURLE_AUTH_ERROR;
  153. /* Allocate our new context handle */
  154. nego->context = calloc(1, sizeof(CtxtHandle));
  155. if(!nego->context)
  156. return CURLE_OUT_OF_MEMORY;
  157. }
  158. if(chlg64 && *chlg64) {
  159. /* Decode the base-64 encoded challenge message */
  160. if(*chlg64 != '=') {
  161. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  162. if(result)
  163. return result;
  164. }
  165. /* Ensure we have a valid challenge message */
  166. if(!chlg) {
  167. infof(data, "SPNEGO handshake failure (empty challenge message)\n");
  168. return CURLE_BAD_CONTENT_ENCODING;
  169. }
  170. /* Setup the challenge "input" security buffer */
  171. chlg_desc.ulVersion = SECBUFFER_VERSION;
  172. chlg_desc.cBuffers = 1;
  173. chlg_desc.pBuffers = &chlg_buf[0];
  174. chlg_buf[0].BufferType = SECBUFFER_TOKEN;
  175. chlg_buf[0].pvBuffer = chlg;
  176. chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
  177. #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
  178. /* ssl context comes from Schannel.
  179. * When extended protection is used in IIS server,
  180. * we have to pass a second SecBuffer to the SecBufferDesc
  181. * otherwise IIS will not pass the authentication (401 response).
  182. * Minimum supported version is Windows 7.
  183. * https://docs.microsoft.com/en-us/security-updates
  184. * /SecurityAdvisories/2009/973811
  185. */
  186. if(nego->sslContext) {
  187. SEC_CHANNEL_BINDINGS channelBindings;
  188. SecPkgContext_Bindings pkgBindings;
  189. pkgBindings.Bindings = &channelBindings;
  190. nego->status = s_pSecFn->QueryContextAttributes(
  191. nego->sslContext,
  192. SECPKG_ATTR_ENDPOINT_BINDINGS,
  193. &pkgBindings
  194. );
  195. if(nego->status == SEC_E_OK) {
  196. chlg_desc.cBuffers++;
  197. chlg_buf[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
  198. chlg_buf[1].cbBuffer = pkgBindings.BindingsLength;
  199. chlg_buf[1].pvBuffer = pkgBindings.Bindings;
  200. }
  201. }
  202. #endif
  203. }
  204. /* Setup the response "output" security buffer */
  205. resp_desc.ulVersion = SECBUFFER_VERSION;
  206. resp_desc.cBuffers = 1;
  207. resp_desc.pBuffers = &resp_buf;
  208. resp_buf.BufferType = SECBUFFER_TOKEN;
  209. resp_buf.pvBuffer = nego->output_token;
  210. resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
  211. /* Generate our challenge-response message */
  212. nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
  213. chlg ? nego->context :
  214. NULL,
  215. nego->spn,
  216. ISC_REQ_CONFIDENTIALITY,
  217. 0, SECURITY_NATIVE_DREP,
  218. chlg ? &chlg_desc : NULL,
  219. 0, nego->context,
  220. &resp_desc, &attrs,
  221. &expiry);
  222. /* Free the decoded challenge as it is not required anymore */
  223. free(chlg);
  224. if(GSS_ERROR(nego->status)) {
  225. char buffer[STRERROR_LEN];
  226. failf(data, "InitializeSecurityContext failed: %s",
  227. Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
  228. if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
  229. return CURLE_OUT_OF_MEMORY;
  230. return CURLE_AUTH_ERROR;
  231. }
  232. if(nego->status == SEC_I_COMPLETE_NEEDED ||
  233. nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
  234. nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
  235. if(GSS_ERROR(nego->status)) {
  236. char buffer[STRERROR_LEN];
  237. failf(data, "CompleteAuthToken failed: %s",
  238. Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
  239. if(nego->status == (DWORD)SEC_E_INSUFFICIENT_MEMORY)
  240. return CURLE_OUT_OF_MEMORY;
  241. return CURLE_AUTH_ERROR;
  242. }
  243. }
  244. nego->output_token_length = resp_buf.cbBuffer;
  245. return result;
  246. }
  247. /*
  248. * Curl_auth_create_spnego_message()
  249. *
  250. * This is used to generate an already encoded SPNEGO (Negotiate) response
  251. * message ready for sending to the recipient.
  252. *
  253. * Parameters:
  254. *
  255. * data [in] - The session handle.
  256. * nego [in/out] - The Negotiate data struct being used and modified.
  257. * outptr [in/out] - The address where a pointer to newly allocated memory
  258. * holding the result will be stored upon completion.
  259. * outlen [out] - The length of the output message.
  260. *
  261. * Returns CURLE_OK on success.
  262. */
  263. CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
  264. struct negotiatedata *nego,
  265. char **outptr, size_t *outlen)
  266. {
  267. CURLcode result;
  268. /* Base64 encode the already generated response */
  269. result = Curl_base64_encode(data,
  270. (const char *) nego->output_token,
  271. nego->output_token_length,
  272. outptr, outlen);
  273. if(result)
  274. return result;
  275. if(!*outptr || !*outlen) {
  276. free(*outptr);
  277. return CURLE_REMOTE_ACCESS_DENIED;
  278. }
  279. return CURLE_OK;
  280. }
  281. /*
  282. * Curl_auth_cleanup_spnego()
  283. *
  284. * This is used to clean up the SPNEGO (Negotiate) specific data.
  285. *
  286. * Parameters:
  287. *
  288. * nego [in/out] - The Negotiate data struct being cleaned up.
  289. *
  290. */
  291. void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
  292. {
  293. /* Free our security context */
  294. if(nego->context) {
  295. s_pSecFn->DeleteSecurityContext(nego->context);
  296. free(nego->context);
  297. nego->context = NULL;
  298. }
  299. /* Free our credentials handle */
  300. if(nego->credentials) {
  301. s_pSecFn->FreeCredentialsHandle(nego->credentials);
  302. free(nego->credentials);
  303. nego->credentials = NULL;
  304. }
  305. /* Free our identity */
  306. Curl_sspi_free_identity(nego->p_identity);
  307. nego->p_identity = NULL;
  308. /* Free the SPN and output token */
  309. Curl_safefree(nego->spn);
  310. Curl_safefree(nego->output_token);
  311. /* Reset any variables */
  312. nego->status = 0;
  313. nego->token_max = 0;
  314. nego->noauthpersist = FALSE;
  315. nego->havenoauthpersist = FALSE;
  316. nego->havenegdata = FALSE;
  317. nego->havemultiplerequests = FALSE;
  318. }
  319. #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */