http_negotiate_sspi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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. #ifndef CURL_DISABLE_HTTP
  25. #include "urldata.h"
  26. #include "sendf.h"
  27. #include "rawstr.h"
  28. #include "warnless.h"
  29. #include "curl_base64.h"
  30. #include "http_negotiate.h"
  31. #include "curl_memory.h"
  32. #include "curl_multibyte.h"
  33. #define _MPRINTF_REPLACE /* use our functions only */
  34. #include <curl/mprintf.h>
  35. /* The last #include file should be: */
  36. #include "memdebug.h"
  37. static int
  38. get_gss_name(struct connectdata *conn, bool proxy,
  39. struct negotiatedata *neg_ctx)
  40. {
  41. const char* service;
  42. size_t length;
  43. if(proxy && !conn->proxy.name)
  44. /* proxy auth requested but no given proxy name, error out! */
  45. return -1;
  46. /* GSSAPI implementation by Globus (known as GSI) requires the name to be
  47. of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
  48. of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
  49. Change following lines if you want to use GSI */
  50. /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name,
  51. and SSPI then generates an NTLM token. When using <service>/<fqdn> a
  52. Kerberos token is generated. */
  53. if(neg_ctx->gss)
  54. service = "KHTTP";
  55. else
  56. service = "HTTP";
  57. length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
  58. conn->host.name) + 1;
  59. if(length + 1 > sizeof(neg_ctx->server_name))
  60. return EMSGSIZE;
  61. snprintf(neg_ctx->server_name, sizeof(neg_ctx->server_name), "%s/%s",
  62. service, proxy ? conn->proxy.name : conn->host.name);
  63. return 0;
  64. }
  65. /* returning zero (0) means success, everything else is treated as "failure"
  66. with no care exactly what the failure was */
  67. int Curl_input_negotiate(struct connectdata *conn, bool proxy,
  68. const char *header)
  69. {
  70. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  71. &conn->data->state.negotiate;
  72. BYTE *input_token = 0;
  73. SecBufferDesc out_buff_desc;
  74. SecBuffer out_sec_buff;
  75. SecBufferDesc in_buff_desc;
  76. SecBuffer in_sec_buff;
  77. unsigned long context_attributes;
  78. TimeStamp lifetime;
  79. TCHAR *sname;
  80. int ret;
  81. size_t len = 0, input_token_len = 0;
  82. bool gss = FALSE;
  83. const char* protocol;
  84. CURLcode error;
  85. while(*header && ISSPACE(*header))
  86. header++;
  87. if(checkprefix("GSS-Negotiate", header)) {
  88. protocol = "GSS-Negotiate";
  89. gss = TRUE;
  90. }
  91. else if(checkprefix("Negotiate", header)) {
  92. protocol = "Negotiate";
  93. gss = FALSE;
  94. }
  95. else
  96. return -1;
  97. if(neg_ctx->context) {
  98. if(neg_ctx->gss != gss) {
  99. return -1;
  100. }
  101. }
  102. else {
  103. neg_ctx->protocol = protocol;
  104. neg_ctx->gss = gss;
  105. }
  106. if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
  107. /* We finished successfully our part of authentication, but server
  108. * rejected it (since we're again here). Exit with an error since we
  109. * can't invent anything better */
  110. Curl_cleanup_negotiate(conn->data);
  111. return -1;
  112. }
  113. if(0 == strlen(neg_ctx->server_name)) {
  114. ret = get_gss_name(conn, proxy, neg_ctx);
  115. if(ret)
  116. return ret;
  117. }
  118. if(!neg_ctx->output_token) {
  119. PSecPkgInfo SecurityPackage;
  120. ret = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT("Negotiate"),
  121. &SecurityPackage);
  122. if(ret != SEC_E_OK)
  123. return -1;
  124. /* Allocate input and output buffers according to the max token size
  125. as indicated by the security package */
  126. neg_ctx->max_token_length = SecurityPackage->cbMaxToken;
  127. neg_ctx->output_token = malloc(neg_ctx->max_token_length);
  128. s_pSecFn->FreeContextBuffer(SecurityPackage);
  129. }
  130. /* Obtain the input token, if any */
  131. header += strlen(neg_ctx->protocol);
  132. while(*header && ISSPACE(*header))
  133. header++;
  134. len = strlen(header);
  135. if(!len) {
  136. /* first call in a new negotation, we have to acquire credentials,
  137. and allocate memory for the context */
  138. neg_ctx->credentials = malloc(sizeof(CredHandle));
  139. neg_ctx->context = malloc(sizeof(CtxtHandle));
  140. if(!neg_ctx->credentials || !neg_ctx->context)
  141. return -1;
  142. neg_ctx->status =
  143. s_pSecFn->AcquireCredentialsHandle(NULL,
  144. (TCHAR *) TEXT("Negotiate"),
  145. SECPKG_CRED_OUTBOUND, NULL, NULL,
  146. NULL, NULL, neg_ctx->credentials,
  147. &lifetime);
  148. if(neg_ctx->status != SEC_E_OK)
  149. return -1;
  150. }
  151. else {
  152. input_token = malloc(neg_ctx->max_token_length);
  153. if(!input_token)
  154. return -1;
  155. error = Curl_base64_decode(header,
  156. (unsigned char **)&input_token,
  157. &input_token_len);
  158. if(error || input_token_len == 0)
  159. return -1;
  160. }
  161. /* prepare the output buffers, and input buffers if present */
  162. out_buff_desc.ulVersion = 0;
  163. out_buff_desc.cBuffers = 1;
  164. out_buff_desc.pBuffers = &out_sec_buff;
  165. out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->max_token_length);
  166. out_sec_buff.BufferType = SECBUFFER_TOKEN;
  167. out_sec_buff.pvBuffer = neg_ctx->output_token;
  168. if(input_token) {
  169. in_buff_desc.ulVersion = 0;
  170. in_buff_desc.cBuffers = 1;
  171. in_buff_desc.pBuffers = &in_sec_buff;
  172. in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
  173. in_sec_buff.BufferType = SECBUFFER_TOKEN;
  174. in_sec_buff.pvBuffer = input_token;
  175. }
  176. sname = Curl_convert_UTF8_to_tchar(neg_ctx->server_name);
  177. if(!sname)
  178. return CURLE_OUT_OF_MEMORY;
  179. neg_ctx->status = s_pSecFn->InitializeSecurityContext(
  180. neg_ctx->credentials,
  181. input_token ? neg_ctx->context : 0,
  182. sname,
  183. ISC_REQ_CONFIDENTIALITY,
  184. 0,
  185. SECURITY_NATIVE_DREP,
  186. input_token ? &in_buff_desc : 0,
  187. 0,
  188. neg_ctx->context,
  189. &out_buff_desc,
  190. &context_attributes,
  191. &lifetime);
  192. Curl_unicodefree(sname);
  193. if(GSS_ERROR(neg_ctx->status))
  194. return -1;
  195. if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
  196. neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
  197. neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
  198. &out_buff_desc);
  199. if(GSS_ERROR(neg_ctx->status))
  200. return -1;
  201. }
  202. neg_ctx->output_token_length = out_sec_buff.cbBuffer;
  203. return 0;
  204. }
  205. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  206. {
  207. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  208. &conn->data->state.negotiate;
  209. char *encoded = NULL;
  210. size_t len = 0;
  211. char *userp;
  212. CURLcode error;
  213. error = Curl_base64_encode(conn->data,
  214. (const char*)neg_ctx->output_token,
  215. neg_ctx->output_token_length,
  216. &encoded, &len);
  217. if(error)
  218. return error;
  219. if(len == 0)
  220. return CURLE_REMOTE_ACCESS_DENIED;
  221. userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
  222. neg_ctx->protocol, encoded);
  223. if(proxy)
  224. conn->allocptr.proxyuserpwd = userp;
  225. else
  226. conn->allocptr.userpwd = userp;
  227. free(encoded);
  228. Curl_cleanup_negotiate (conn->data);
  229. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  230. }
  231. static void cleanup(struct negotiatedata *neg_ctx)
  232. {
  233. if(neg_ctx->context) {
  234. s_pSecFn->DeleteSecurityContext(neg_ctx->context);
  235. free(neg_ctx->context);
  236. neg_ctx->context = 0;
  237. }
  238. if(neg_ctx->credentials) {
  239. s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
  240. free(neg_ctx->credentials);
  241. neg_ctx->credentials = 0;
  242. }
  243. if(neg_ctx->output_token) {
  244. free(neg_ctx->output_token);
  245. neg_ctx->output_token = 0;
  246. }
  247. neg_ctx->max_token_length = 0;
  248. }
  249. void Curl_cleanup_negotiate(struct SessionHandle *data)
  250. {
  251. cleanup(&data->state.negotiate);
  252. cleanup(&data->state.proxyneg);
  253. }
  254. #endif
  255. #endif