http_negotiate.c 10 KB

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