2
0

http_negotiate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifdef HAVE_GSSAPI
  25. #ifdef HAVE_OLD_GSSMIT
  26. #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
  27. #endif
  28. #ifndef CURL_DISABLE_HTTP
  29. /* -- WIN32 approved -- */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdarg.h>
  33. #include <stdlib.h>
  34. #include <ctype.h>
  35. #include "urldata.h"
  36. #include "sendf.h"
  37. #include "rawstr.h"
  38. #include "curl_base64.h"
  39. #include "http_negotiate.h"
  40. #include "curl_memory.h"
  41. #ifdef HAVE_SPNEGO
  42. # include <spnegohelp.h>
  43. # ifdef USE_SSLEAY
  44. # ifdef USE_OPENSSL
  45. # include <openssl/objects.h>
  46. # else
  47. # include <objects.h>
  48. # endif
  49. # else
  50. # error "Can't compile SPNEGO support without OpenSSL."
  51. # endif
  52. #endif
  53. #define _MPRINTF_REPLACE /* use our functions only */
  54. #include <curl/mprintf.h>
  55. /* The last #include file should be: */
  56. #include "memdebug.h"
  57. static int
  58. get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
  59. {
  60. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  61. &conn->data->state.negotiate;
  62. OM_uint32 major_status, minor_status;
  63. gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
  64. char name[2048];
  65. const char* service;
  66. /* GSSAPI implementation by Globus (known as GSI) requires the name to be
  67. of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
  68. of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
  69. Change following lines if you want to use GSI */
  70. /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
  71. if(neg_ctx->gss)
  72. service = "KHTTP";
  73. else
  74. service = "HTTP";
  75. token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
  76. conn->host.name) + 1;
  77. if(token.length + 1 > sizeof(name))
  78. return EMSGSIZE;
  79. snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
  80. conn->host.name);
  81. token.value = (void *) name;
  82. major_status = gss_import_name(&minor_status,
  83. &token,
  84. GSS_C_NT_HOSTBASED_SERVICE,
  85. server);
  86. return GSS_ERROR(major_status) ? -1 : 0;
  87. }
  88. static void
  89. log_gss_error(struct connectdata *conn, OM_uint32 error_status, const char *prefix)
  90. {
  91. OM_uint32 maj_stat, min_stat;
  92. OM_uint32 msg_ctx = 0;
  93. gss_buffer_desc status_string;
  94. char buf[1024];
  95. size_t len;
  96. snprintf(buf, sizeof(buf), "%s", prefix);
  97. len = strlen(buf);
  98. do {
  99. maj_stat = gss_display_status(&min_stat,
  100. error_status,
  101. GSS_C_MECH_CODE,
  102. GSS_C_NO_OID,
  103. &msg_ctx,
  104. &status_string);
  105. if(sizeof(buf) > len + status_string.length + 1) {
  106. snprintf(buf + len, sizeof(buf) - len,
  107. ": %s", (char*) status_string.value);
  108. len += status_string.length;
  109. }
  110. gss_release_buffer(&min_stat, &status_string);
  111. } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
  112. infof(conn->data, "%s", buf);
  113. }
  114. /* returning zero (0) means success, everything else is treated as "failure"
  115. with no care exactly what the failure was */
  116. int Curl_input_negotiate(struct connectdata *conn, bool proxy,
  117. const char *header)
  118. {
  119. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  120. &conn->data->state.negotiate;
  121. OM_uint32 major_status, minor_status, minor_status2;
  122. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  123. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  124. int ret;
  125. size_t len, rawlen;
  126. bool gss;
  127. const char* protocol;
  128. while(*header && ISSPACE(*header))
  129. header++;
  130. if(checkprefix("GSS-Negotiate", header)) {
  131. protocol = "GSS-Negotiate";
  132. gss = TRUE;
  133. }
  134. else if(checkprefix("Negotiate", header)) {
  135. protocol = "Negotiate";
  136. gss = FALSE;
  137. }
  138. else
  139. return -1;
  140. if(neg_ctx->context) {
  141. if(neg_ctx->gss != gss) {
  142. return -1;
  143. }
  144. }
  145. else {
  146. neg_ctx->protocol = protocol;
  147. neg_ctx->gss = gss;
  148. }
  149. if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
  150. /* We finished succesfully our part of authentication, but server
  151. * rejected it (since we're again here). Exit with an error since we
  152. * can't invent anything better */
  153. Curl_cleanup_negotiate(conn->data);
  154. return -1;
  155. }
  156. if(neg_ctx->server_name == NULL &&
  157. (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
  158. return ret;
  159. header += strlen(neg_ctx->protocol);
  160. while(*header && ISSPACE(*header))
  161. header++;
  162. len = strlen(header);
  163. if(len > 0) {
  164. rawlen = Curl_base64_decode(header,
  165. (unsigned char **)&input_token.value);
  166. if(rawlen == 0)
  167. return -1;
  168. input_token.length = rawlen;
  169. #ifdef HAVE_SPNEGO /* Handle SPNEGO */
  170. if(checkprefix("Negotiate", header)) {
  171. ASN1_OBJECT * object = NULL;
  172. int rc = 1;
  173. unsigned char * spnegoToken = NULL;
  174. size_t spnegoTokenLength = 0;
  175. unsigned char * mechToken = NULL;
  176. size_t mechTokenLength = 0;
  177. if(input_token.value == NULL)
  178. return CURLE_OUT_OF_MEMORY;
  179. spnegoToken = malloc(input_token.length);
  180. if(spnegoToken == NULL)
  181. return CURLE_OUT_OF_MEMORY;
  182. spnegoTokenLength = input_token.length;
  183. object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
  184. if(!parseSpnegoTargetToken(spnegoToken,
  185. spnegoTokenLength,
  186. NULL,
  187. NULL,
  188. &mechToken,
  189. &mechTokenLength,
  190. NULL,
  191. NULL)) {
  192. free(spnegoToken);
  193. spnegoToken = NULL;
  194. infof(conn->data, "Parse SPNEGO Target Token failed\n");
  195. }
  196. else {
  197. free(input_token.value);
  198. input_token.value = malloc(mechTokenLength);
  199. if (input_token.value == NULL)
  200. return CURLE_OUT_OF_MEMORY;
  201. memcpy(input_token.value, mechToken,mechTokenLength);
  202. input_token.length = mechTokenLength;
  203. free(mechToken);
  204. mechToken = NULL;
  205. infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
  206. }
  207. }
  208. #endif
  209. }
  210. major_status = gss_init_sec_context(&minor_status,
  211. GSS_C_NO_CREDENTIAL,
  212. &neg_ctx->context,
  213. neg_ctx->server_name,
  214. GSS_C_NO_OID,
  215. GSS_C_DELEG_FLAG,
  216. 0,
  217. GSS_C_NO_CHANNEL_BINDINGS,
  218. &input_token,
  219. NULL,
  220. &output_token,
  221. NULL,
  222. NULL);
  223. if(input_token.length > 0)
  224. gss_release_buffer(&minor_status2, &input_token);
  225. neg_ctx->status = major_status;
  226. if(GSS_ERROR(major_status)) {
  227. /* Curl_cleanup_negotiate(conn->data) ??? */
  228. log_gss_error(conn, minor_status,
  229. "gss_init_sec_context() failed: ");
  230. return -1;
  231. }
  232. if(output_token.length == 0) {
  233. return -1;
  234. }
  235. neg_ctx->output_token = output_token;
  236. /* conn->bits.close = FALSE; */
  237. return 0;
  238. }
  239. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  240. {
  241. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  242. &conn->data->state.negotiate;
  243. char *encoded = NULL;
  244. size_t len;
  245. #ifdef HAVE_SPNEGO /* Handle SPNEGO */
  246. if(checkprefix("Negotiate", neg_ctx->protocol)) {
  247. ASN1_OBJECT * object = NULL;
  248. int rc = 1;
  249. unsigned char * spnegoToken = NULL;
  250. size_t spnegoTokenLength = 0;
  251. unsigned char * responseToken = NULL;
  252. size_t responseTokenLength = 0;
  253. responseToken = malloc(neg_ctx->output_token.length);
  254. if( responseToken == NULL)
  255. return CURLE_OUT_OF_MEMORY;
  256. memcpy(responseToken, neg_ctx->output_token.value,
  257. neg_ctx->output_token.length);
  258. responseTokenLength = neg_ctx->output_token.length;
  259. object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
  260. if(!makeSpnegoInitialToken (object,
  261. responseToken,
  262. responseTokenLength,
  263. &spnegoToken,
  264. &spnegoTokenLength)) {
  265. free(responseToken);
  266. responseToken = NULL;
  267. infof(conn->data, "Make SPNEGO Initial Token failed\n");
  268. }
  269. else {
  270. free(neg_ctx->output_token.value);
  271. responseToken = NULL;
  272. neg_ctx->output_token.value = malloc(spnegoTokenLength);
  273. memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
  274. neg_ctx->output_token.length = spnegoTokenLength;
  275. free(spnegoToken);
  276. spnegoToken = NULL;
  277. infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
  278. }
  279. }
  280. #endif
  281. len = Curl_base64_encode(conn->data,
  282. neg_ctx->output_token.value,
  283. neg_ctx->output_token.length,
  284. &encoded);
  285. if(len == 0)
  286. return CURLE_OUT_OF_MEMORY;
  287. conn->allocptr.userpwd =
  288. aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
  289. neg_ctx->protocol, encoded);
  290. free(encoded);
  291. Curl_cleanup_negotiate (conn->data);
  292. return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  293. }
  294. static void cleanup(struct negotiatedata *neg_ctx)
  295. {
  296. OM_uint32 minor_status;
  297. if(neg_ctx->context != GSS_C_NO_CONTEXT)
  298. gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
  299. if(neg_ctx->output_token.length != 0)
  300. gss_release_buffer(&minor_status, &neg_ctx->output_token);
  301. if(neg_ctx->server_name != GSS_C_NO_NAME)
  302. gss_release_name(&minor_status, &neg_ctx->server_name);
  303. memset(neg_ctx, 0, sizeof(*neg_ctx));
  304. }
  305. void Curl_cleanup_negotiate(struct SessionHandle *data)
  306. {
  307. cleanup(&data->state.negotiate);
  308. cleanup(&data->state.proxyneg);
  309. }
  310. #endif
  311. #endif