spnego_sspi.c 10 KB

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