spnego_sspi.c 10 KB

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