http_negotiate_sspi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 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_memory.h"
  33. #include "curl_multibyte.h"
  34. #define _MPRINTF_REPLACE /* use our functions only */
  35. #include <curl/mprintf.h>
  36. /* The last #include file should be: */
  37. #include "memdebug.h"
  38. /* returning zero (0) means success, everything else is treated as "failure"
  39. with no care exactly what the failure was */
  40. int Curl_input_negotiate(struct connectdata *conn, bool proxy,
  41. const char *header)
  42. {
  43. BYTE *input_token = NULL;
  44. SecBufferDesc out_buff_desc;
  45. SecBuffer out_sec_buff;
  46. SecBufferDesc in_buff_desc;
  47. SecBuffer in_sec_buff;
  48. unsigned long context_attributes;
  49. TimeStamp expiry;
  50. int ret;
  51. size_t len = 0, input_token_len = 0;
  52. CURLcode error;
  53. /* Point to the username and password */
  54. const char *userp;
  55. const char *passwdp;
  56. /* Point to the correct struct with this */
  57. struct negotiatedata *neg_ctx;
  58. if(proxy) {
  59. userp = conn->proxyuser;
  60. passwdp = conn->proxypasswd;
  61. neg_ctx = &conn->data->state.proxyneg;
  62. }
  63. else {
  64. userp = conn->user;
  65. passwdp = conn->passwd;
  66. neg_ctx = &conn->data->state.negotiate;
  67. }
  68. /* Not set means empty */
  69. if(!userp)
  70. userp = "";
  71. if(!passwdp)
  72. passwdp = "";
  73. if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
  74. /* We finished successfully our part of authentication, but server
  75. * rejected it (since we're again here). Exit with an error since we
  76. * can't invent anything better */
  77. Curl_cleanup_negotiate(conn->data);
  78. return -1;
  79. }
  80. if(!neg_ctx->server_name) {
  81. /* Check proxy auth requested but no given proxy name */
  82. if(proxy && !conn->proxy.name)
  83. return -1;
  84. /* Generate our SPN */
  85. neg_ctx->server_name = Curl_sasl_build_spn("HTTP",
  86. proxy ? conn->proxy.name :
  87. conn->host.name);
  88. if(!neg_ctx->server_name)
  89. return -1;
  90. }
  91. if(!neg_ctx->output_token) {
  92. PSecPkgInfo SecurityPackage;
  93. ret = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_NEGOTIATE),
  94. &SecurityPackage);
  95. if(ret != SEC_E_OK)
  96. return -1;
  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 -1;
  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 -1;
  120. if(userp && *userp) {
  121. /* Populate our identity structure */
  122. error = Curl_create_sspi_identity(userp, passwdp, &neg_ctx->identity);
  123. if(error)
  124. return -1;
  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 -1;
  140. }
  141. else {
  142. error = Curl_base64_decode(header,
  143. (unsigned char **)&input_token,
  144. &input_token_len);
  145. if(error || !input_token_len)
  146. return -1;
  147. }
  148. /* Setup the "output" security buffer */
  149. out_buff_desc.ulVersion = SECBUFFER_VERSION;
  150. out_buff_desc.cBuffers = 1;
  151. out_buff_desc.pBuffers = &out_sec_buff;
  152. out_sec_buff.BufferType = SECBUFFER_TOKEN;
  153. out_sec_buff.pvBuffer = neg_ctx->output_token;
  154. out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->token_max);
  155. /* Setup the "input" security buffer if present */
  156. if(input_token) {
  157. in_buff_desc.ulVersion = SECBUFFER_VERSION;
  158. in_buff_desc.cBuffers = 1;
  159. in_buff_desc.pBuffers = &in_sec_buff;
  160. in_sec_buff.BufferType = SECBUFFER_TOKEN;
  161. in_sec_buff.pvBuffer = input_token;
  162. in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
  163. }
  164. /* Generate our message */
  165. neg_ctx->status = s_pSecFn->InitializeSecurityContext(
  166. neg_ctx->credentials,
  167. input_token ? neg_ctx->context : NULL,
  168. neg_ctx->server_name,
  169. ISC_REQ_CONFIDENTIALITY,
  170. 0,
  171. SECURITY_NATIVE_DREP,
  172. input_token ? &in_buff_desc : NULL,
  173. 0,
  174. neg_ctx->context,
  175. &out_buff_desc,
  176. &context_attributes,
  177. &expiry);
  178. Curl_safefree(input_token);
  179. if(GSS_ERROR(neg_ctx->status))
  180. return -1;
  181. if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
  182. neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
  183. neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
  184. &out_buff_desc);
  185. if(GSS_ERROR(neg_ctx->status))
  186. return -1;
  187. }
  188. neg_ctx->output_token_length = out_sec_buff.cbBuffer;
  189. return 0;
  190. }
  191. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  192. {
  193. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  194. &conn->data->state.negotiate;
  195. char *encoded = NULL;
  196. size_t len = 0;
  197. char *userp;
  198. CURLcode error;
  199. error = Curl_base64_encode(conn->data,
  200. (const char*)neg_ctx->output_token,
  201. neg_ctx->output_token_length,
  202. &encoded, &len);
  203. if(error)
  204. return error;
  205. if(!len)
  206. return CURLE_REMOTE_ACCESS_DENIED;
  207. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  208. encoded);
  209. if(proxy) {
  210. Curl_safefree(conn->allocptr.proxyuserpwd);
  211. conn->allocptr.proxyuserpwd = userp;
  212. }
  213. else {
  214. Curl_safefree(conn->allocptr.userpwd);
  215. conn->allocptr.userpwd = userp;
  216. }
  217. free(encoded);
  218. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  219. }
  220. static void cleanup(struct negotiatedata *neg_ctx)
  221. {
  222. /* Free our security context */
  223. if(neg_ctx->context) {
  224. s_pSecFn->DeleteSecurityContext(neg_ctx->context);
  225. free(neg_ctx->context);
  226. neg_ctx->context = NULL;
  227. }
  228. /* Free our credentials handle */
  229. if(neg_ctx->credentials) {
  230. s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
  231. free(neg_ctx->credentials);
  232. neg_ctx->credentials = NULL;
  233. }
  234. /* Free our identity */
  235. Curl_sspi_free_identity(neg_ctx->p_identity);
  236. neg_ctx->p_identity = NULL;
  237. /* Free the SPN and output token */
  238. Curl_safefree(neg_ctx->server_name);
  239. Curl_safefree(neg_ctx->output_token);
  240. /* Reset any variables */
  241. neg_ctx->token_max = 0;
  242. }
  243. void Curl_cleanup_negotiate(struct SessionHandle *data)
  244. {
  245. cleanup(&data->state.negotiate);
  246. cleanup(&data->state.proxyneg);
  247. }
  248. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
  249. #endif /* USE_WINDOWS_SSPI */