spnego_sspi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 credentials 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 = calloc(1, sizeof(CredHandle));
  138. if(!nego->credentials)
  139. return CURLE_OUT_OF_MEMORY;
  140. /* Acquire our credentials handle */
  141. nego->status =
  142. s_pSecFn->AcquireCredentialsHandle(NULL,
  143. (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
  144. SECPKG_CRED_OUTBOUND, NULL,
  145. nego->p_identity, NULL, NULL,
  146. nego->credentials, &expiry);
  147. if(nego->status != SEC_E_OK)
  148. return CURLE_LOGIN_DENIED;
  149. /* Allocate our new context handle */
  150. nego->context = calloc(1, sizeof(CtxtHandle));
  151. if(!nego->context)
  152. return CURLE_OUT_OF_MEMORY;
  153. }
  154. if(chlg64 && *chlg64) {
  155. /* Decode the base-64 encoded challenge message */
  156. if(*chlg64 != '=') {
  157. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  158. if(result)
  159. return result;
  160. }
  161. /* Ensure we have a valid challenge message */
  162. if(!chlg) {
  163. infof(data, "SPNEGO handshake failure (empty challenge message)\n");
  164. return CURLE_BAD_CONTENT_ENCODING;
  165. }
  166. /* Setup the challenge "input" security buffer */
  167. chlg_desc.ulVersion = SECBUFFER_VERSION;
  168. chlg_desc.cBuffers = 1;
  169. chlg_desc.pBuffers = &chlg_buf;
  170. chlg_buf.BufferType = SECBUFFER_TOKEN;
  171. chlg_buf.pvBuffer = chlg;
  172. chlg_buf.cbBuffer = curlx_uztoul(chlglen);
  173. }
  174. /* Setup the response "output" security buffer */
  175. resp_desc.ulVersion = SECBUFFER_VERSION;
  176. resp_desc.cBuffers = 1;
  177. resp_desc.pBuffers = &resp_buf;
  178. resp_buf.BufferType = SECBUFFER_TOKEN;
  179. resp_buf.pvBuffer = nego->output_token;
  180. resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
  181. /* Generate our challenge-response message */
  182. nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
  183. chlg ? nego->context :
  184. NULL,
  185. nego->spn,
  186. ISC_REQ_CONFIDENTIALITY,
  187. 0, SECURITY_NATIVE_DREP,
  188. chlg ? &chlg_desc : NULL,
  189. 0, nego->context,
  190. &resp_desc, &attrs,
  191. &expiry);
  192. /* Free the decoded challenge as it is not required anymore */
  193. free(chlg);
  194. if(GSS_ERROR(nego->status)) {
  195. failf(data, "InitializeSecurityContext failed: %s",
  196. Curl_sspi_strerror(data->easy_conn, nego->status));
  197. return CURLE_OUT_OF_MEMORY;
  198. }
  199. if(nego->status == SEC_I_COMPLETE_NEEDED ||
  200. nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
  201. nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
  202. if(GSS_ERROR(nego->status)) {
  203. return CURLE_RECV_ERROR;
  204. }
  205. }
  206. nego->output_token_length = resp_buf.cbBuffer;
  207. return result;
  208. }
  209. /*
  210. * Curl_auth_create_spnego_message()
  211. *
  212. * This is used to generate an already encoded SPNEGO (Negotiate) response
  213. * message ready for sending to the recipient.
  214. *
  215. * Parameters:
  216. *
  217. * data [in] - The session handle.
  218. * nego [in/out] - The Negotiate data struct being used and modified.
  219. * outptr [in/out] - The address where a pointer to newly allocated memory
  220. * holding the result will be stored upon completion.
  221. * outlen [out] - The length of the output message.
  222. *
  223. * Returns CURLE_OK on success.
  224. */
  225. CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
  226. struct negotiatedata *nego,
  227. char **outptr, size_t *outlen)
  228. {
  229. CURLcode result;
  230. /* Base64 encode the already generated response */
  231. result = Curl_base64_encode(data,
  232. (const char *) nego->output_token,
  233. nego->output_token_length,
  234. outptr, outlen);
  235. if(result)
  236. return result;
  237. if(!*outptr || !*outlen) {
  238. free(*outptr);
  239. return CURLE_REMOTE_ACCESS_DENIED;
  240. }
  241. return CURLE_OK;
  242. }
  243. /*
  244. * Curl_auth_spnego_cleanup()
  245. *
  246. * This is used to clean up the SPNEGO (Negotiate) specific data.
  247. *
  248. * Parameters:
  249. *
  250. * nego [in/out] - The Negotiate data struct being cleaned up.
  251. *
  252. */
  253. void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
  254. {
  255. /* Free our security context */
  256. if(nego->context) {
  257. s_pSecFn->DeleteSecurityContext(nego->context);
  258. free(nego->context);
  259. nego->context = NULL;
  260. }
  261. /* Free our credentials handle */
  262. if(nego->credentials) {
  263. s_pSecFn->FreeCredentialsHandle(nego->credentials);
  264. free(nego->credentials);
  265. nego->credentials = NULL;
  266. }
  267. /* Free our identity */
  268. Curl_sspi_free_identity(nego->p_identity);
  269. nego->p_identity = NULL;
  270. /* Free the SPN and output token */
  271. Curl_safefree(nego->spn);
  272. Curl_safefree(nego->output_token);
  273. /* Reset any variables */
  274. nego->status = 0;
  275. nego->token_max = 0;
  276. }
  277. #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */