1
0

http_negotiate_sspi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 http://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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef USE_WINDOWS_SSPI
  24. #if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
  25. #include "urldata.h"
  26. #include "sendf.h"
  27. #include "rawstr.h"
  28. #include "warnless.h"
  29. #include "curl_base64.h"
  30. #include "curl_sasl.h"
  31. #include "http_negotiate.h"
  32. #include "curl_multibyte.h"
  33. #include "curl_printf.h"
  34. /* The last #include files should be: */
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. CURLcode Curl_input_negotiate(struct connectdata *conn, bool proxy,
  38. const char *header)
  39. {
  40. struct SessionHandle *data = conn->data;
  41. BYTE *input_token = NULL;
  42. SecBufferDesc out_buff_desc;
  43. SecBuffer out_sec_buff;
  44. SecBufferDesc in_buff_desc;
  45. SecBuffer in_sec_buff;
  46. SECURITY_STATUS status;
  47. unsigned long attrs;
  48. TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
  49. size_t len = 0, input_token_len = 0;
  50. CURLcode result;
  51. /* Point to the username and password */
  52. const char *userp;
  53. const char *passwdp;
  54. /* Point to the correct struct with this */
  55. struct negotiatedata *neg_ctx;
  56. if(proxy) {
  57. userp = conn->proxyuser;
  58. passwdp = conn->proxypasswd;
  59. neg_ctx = &data->state.proxyneg;
  60. }
  61. else {
  62. userp = conn->user;
  63. passwdp = conn->passwd;
  64. neg_ctx = &data->state.negotiate;
  65. }
  66. /* Not set means empty */
  67. if(!userp)
  68. userp = "";
  69. if(!passwdp)
  70. passwdp = "";
  71. if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
  72. /* We finished successfully our part of authentication, but server
  73. * rejected it (since we're again here). Exit with an error since we
  74. * can't invent anything better */
  75. Curl_cleanup_negotiate(data);
  76. return CURLE_LOGIN_DENIED;
  77. }
  78. if(!neg_ctx->server_name) {
  79. /* Check proxy auth requested but no given proxy name */
  80. if(proxy && !conn->proxy.name)
  81. return CURLE_BAD_FUNCTION_ARGUMENT;
  82. /* Generate our SPN */
  83. neg_ctx->server_name = Curl_sasl_build_spn(
  84. proxy ? data->set.str[STRING_PROXY_SERVICE_NAME] :
  85. data->set.str[STRING_SERVICE_NAME],
  86. proxy ? conn->proxy.name : conn->host.name);
  87. if(!neg_ctx->server_name)
  88. return CURLE_OUT_OF_MEMORY;
  89. }
  90. if(!neg_ctx->output_token) {
  91. PSecPkgInfo SecurityPackage;
  92. status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
  93. TEXT(SP_NAME_NEGOTIATE),
  94. &SecurityPackage);
  95. if(status != SEC_E_OK)
  96. return CURLE_NOT_BUILT_IN;
  97. /* Allocate input and output buffers according to the max token size
  98. as indicated by the security package */
  99. neg_ctx->token_max = SecurityPackage->cbMaxToken;
  100. neg_ctx->output_token = malloc(neg_ctx->token_max);
  101. s_pSecFn->FreeContextBuffer(SecurityPackage);
  102. }
  103. /* Obtain the input token, if any */
  104. header += strlen("Negotiate");
  105. while(*header && ISSPACE(*header))
  106. header++;
  107. len = strlen(header);
  108. if(!len) {
  109. /* Is this the first call in a new negotiation? */
  110. if(neg_ctx->context) {
  111. /* The server rejected our authentication and hasn't suppled any more
  112. negotiation mechanisms */
  113. return CURLE_LOGIN_DENIED;
  114. }
  115. /* We have to acquire credentials and allocate memory for the context */
  116. neg_ctx->credentials = malloc(sizeof(CredHandle));
  117. neg_ctx->context = malloc(sizeof(CtxtHandle));
  118. if(!neg_ctx->credentials || !neg_ctx->context)
  119. return CURLE_OUT_OF_MEMORY;
  120. if(userp && *userp) {
  121. /* Populate our identity structure */
  122. result = Curl_create_sspi_identity(userp, passwdp, &neg_ctx->identity);
  123. if(result)
  124. return result;
  125. /* Allow proper cleanup of the identity structure */
  126. neg_ctx->p_identity = &neg_ctx->identity;
  127. }
  128. else
  129. /* Use the current Windows user */
  130. neg_ctx->p_identity = NULL;
  131. /* Acquire our credientials handle */
  132. neg_ctx->status =
  133. s_pSecFn->AcquireCredentialsHandle(NULL,
  134. (TCHAR *) TEXT(SP_NAME_NEGOTIATE),
  135. SECPKG_CRED_OUTBOUND, NULL,
  136. neg_ctx->p_identity, NULL, NULL,
  137. neg_ctx->credentials, &expiry);
  138. if(neg_ctx->status != SEC_E_OK)
  139. return CURLE_LOGIN_DENIED;
  140. }
  141. else {
  142. result = Curl_base64_decode(header,
  143. (unsigned char **)&input_token,
  144. &input_token_len);
  145. if(result)
  146. return result;
  147. if(!input_token_len) {
  148. infof(data,
  149. "Negotiate handshake failure (empty challenge message)\n");
  150. return CURLE_BAD_CONTENT_ENCODING;
  151. }
  152. }
  153. /* Setup the "output" security buffer */
  154. out_buff_desc.ulVersion = SECBUFFER_VERSION;
  155. out_buff_desc.cBuffers = 1;
  156. out_buff_desc.pBuffers = &out_sec_buff;
  157. out_sec_buff.BufferType = SECBUFFER_TOKEN;
  158. out_sec_buff.pvBuffer = neg_ctx->output_token;
  159. out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->token_max);
  160. /* Setup the "input" security buffer if present */
  161. if(input_token) {
  162. in_buff_desc.ulVersion = SECBUFFER_VERSION;
  163. in_buff_desc.cBuffers = 1;
  164. in_buff_desc.pBuffers = &in_sec_buff;
  165. in_sec_buff.BufferType = SECBUFFER_TOKEN;
  166. in_sec_buff.pvBuffer = input_token;
  167. in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
  168. }
  169. /* Generate our message */
  170. neg_ctx->status = s_pSecFn->InitializeSecurityContext(
  171. neg_ctx->credentials,
  172. input_token ? neg_ctx->context : NULL,
  173. neg_ctx->server_name,
  174. ISC_REQ_CONFIDENTIALITY,
  175. 0,
  176. SECURITY_NATIVE_DREP,
  177. input_token ? &in_buff_desc : NULL,
  178. 0,
  179. neg_ctx->context,
  180. &out_buff_desc,
  181. &attrs,
  182. &expiry);
  183. free(input_token);
  184. if(GSS_ERROR(neg_ctx->status))
  185. return CURLE_OUT_OF_MEMORY;
  186. if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
  187. neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
  188. neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
  189. &out_buff_desc);
  190. if(GSS_ERROR(neg_ctx->status))
  191. return CURLE_RECV_ERROR;
  192. }
  193. neg_ctx->output_token_length = out_sec_buff.cbBuffer;
  194. return CURLE_OK;
  195. }
  196. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  197. {
  198. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  199. &conn->data->state.negotiate;
  200. char *encoded = NULL;
  201. size_t len = 0;
  202. char *userp;
  203. CURLcode error;
  204. error = Curl_base64_encode(conn->data,
  205. (const char*)neg_ctx->output_token,
  206. neg_ctx->output_token_length,
  207. &encoded, &len);
  208. if(error)
  209. return error;
  210. if(!len)
  211. return CURLE_REMOTE_ACCESS_DENIED;
  212. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  213. encoded);
  214. if(proxy) {
  215. Curl_safefree(conn->allocptr.proxyuserpwd);
  216. conn->allocptr.proxyuserpwd = userp;
  217. }
  218. else {
  219. Curl_safefree(conn->allocptr.userpwd);
  220. conn->allocptr.userpwd = userp;
  221. }
  222. free(encoded);
  223. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  224. }
  225. static void cleanup(struct negotiatedata *neg_ctx)
  226. {
  227. /* Free our security context */
  228. if(neg_ctx->context) {
  229. s_pSecFn->DeleteSecurityContext(neg_ctx->context);
  230. free(neg_ctx->context);
  231. neg_ctx->context = NULL;
  232. }
  233. /* Free our credentials handle */
  234. if(neg_ctx->credentials) {
  235. s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
  236. free(neg_ctx->credentials);
  237. neg_ctx->credentials = NULL;
  238. }
  239. /* Free our identity */
  240. Curl_sspi_free_identity(neg_ctx->p_identity);
  241. neg_ctx->p_identity = NULL;
  242. /* Free the SPN and output token */
  243. Curl_safefree(neg_ctx->server_name);
  244. Curl_safefree(neg_ctx->output_token);
  245. /* Reset any variables */
  246. neg_ctx->token_max = 0;
  247. }
  248. void Curl_cleanup_negotiate(struct SessionHandle *data)
  249. {
  250. cleanup(&data->state.negotiate);
  251. cleanup(&data->state.proxyneg);
  252. }
  253. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
  254. #endif /* USE_WINDOWS_SSPI */