http_negotiate.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
  24. #include "urldata.h"
  25. #include "sendf.h"
  26. #include "curl_gssapi.h"
  27. #include "rawstr.h"
  28. #include "curl_base64.h"
  29. #include "http_negotiate.h"
  30. #include "curl_sasl.h"
  31. #include "url.h"
  32. #include "curl_printf.h"
  33. /* The last #include files should be: */
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. CURLcode Curl_input_negotiate(struct connectdata *conn, bool proxy,
  37. const char *header)
  38. {
  39. struct SessionHandle *data = conn->data;
  40. struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
  41. &data->state.negotiate;
  42. OM_uint32 major_status, minor_status, discard_st;
  43. gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
  44. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  45. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  46. size_t len;
  47. size_t rawlen = 0;
  48. CURLcode result;
  49. if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
  50. /* We finished successfully our part of authentication, but server
  51. * rejected it (since we're again here). Exit with an error since we
  52. * can't invent anything better */
  53. Curl_cleanup_negotiate(data);
  54. return CURLE_LOGIN_DENIED;
  55. }
  56. if(!neg_ctx->server_name) {
  57. /* Generate our SPN */
  58. char *spn = Curl_sasl_build_gssapi_spn(
  59. proxy ? data->set.str[STRING_PROXY_SERVICE_NAME] :
  60. data->set.str[STRING_SERVICE_NAME],
  61. proxy ? conn->proxy.name : conn->host.name);
  62. if(!spn)
  63. return CURLE_OUT_OF_MEMORY;
  64. /* Populate the SPN structure */
  65. spn_token.value = spn;
  66. spn_token.length = strlen(spn);
  67. /* Import the SPN */
  68. major_status = gss_import_name(&minor_status, &spn_token,
  69. GSS_C_NT_HOSTBASED_SERVICE,
  70. &neg_ctx->server_name);
  71. if(GSS_ERROR(major_status)) {
  72. Curl_gss_log_error(data, minor_status, "gss_import_name() failed: ");
  73. free(spn);
  74. return CURLE_OUT_OF_MEMORY;
  75. }
  76. free(spn);
  77. }
  78. header += strlen("Negotiate");
  79. while(*header && ISSPACE(*header))
  80. header++;
  81. len = strlen(header);
  82. if(len > 0) {
  83. result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
  84. &rawlen);
  85. if(result)
  86. return result;
  87. if(!rawlen) {
  88. infof(data, "Negotiate handshake failure (empty challenge message)\n");
  89. return CURLE_BAD_CONTENT_ENCODING;
  90. }
  91. input_token.length = rawlen;
  92. DEBUGASSERT(input_token.value != NULL);
  93. }
  94. major_status = Curl_gss_init_sec_context(data,
  95. &minor_status,
  96. &neg_ctx->context,
  97. neg_ctx->server_name,
  98. &Curl_spnego_mech_oid,
  99. GSS_C_NO_CHANNEL_BINDINGS,
  100. &input_token,
  101. &output_token,
  102. TRUE,
  103. NULL);
  104. Curl_safefree(input_token.value);
  105. neg_ctx->status = major_status;
  106. if(GSS_ERROR(major_status)) {
  107. if(output_token.value)
  108. gss_release_buffer(&discard_st, &output_token);
  109. Curl_gss_log_error(conn->data, minor_status,
  110. "gss_init_sec_context() failed: ");
  111. return CURLE_OUT_OF_MEMORY;
  112. }
  113. if(!output_token.value || !output_token.length) {
  114. if(output_token.value)
  115. gss_release_buffer(&discard_st, &output_token);
  116. return CURLE_OUT_OF_MEMORY;
  117. }
  118. neg_ctx->output_token = output_token;
  119. return CURLE_OK;
  120. }
  121. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  122. {
  123. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  124. &conn->data->state.negotiate;
  125. char *encoded = NULL;
  126. size_t len = 0;
  127. char *userp;
  128. CURLcode result;
  129. OM_uint32 discard_st;
  130. result = Curl_base64_encode(conn->data,
  131. neg_ctx->output_token.value,
  132. neg_ctx->output_token.length,
  133. &encoded, &len);
  134. if(result) {
  135. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  136. neg_ctx->output_token.value = NULL;
  137. neg_ctx->output_token.length = 0;
  138. return result;
  139. }
  140. if(!encoded || !len) {
  141. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  142. neg_ctx->output_token.value = NULL;
  143. neg_ctx->output_token.length = 0;
  144. return CURLE_REMOTE_ACCESS_DENIED;
  145. }
  146. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  147. encoded);
  148. if(proxy) {
  149. Curl_safefree(conn->allocptr.proxyuserpwd);
  150. conn->allocptr.proxyuserpwd = userp;
  151. }
  152. else {
  153. Curl_safefree(conn->allocptr.userpwd);
  154. conn->allocptr.userpwd = userp;
  155. }
  156. free(encoded);
  157. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  158. }
  159. static void cleanup(struct negotiatedata *neg_ctx)
  160. {
  161. OM_uint32 minor_status;
  162. if(neg_ctx->context != GSS_C_NO_CONTEXT)
  163. gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
  164. if(neg_ctx->output_token.value)
  165. gss_release_buffer(&minor_status, &neg_ctx->output_token);
  166. if(neg_ctx->server_name != GSS_C_NO_NAME)
  167. gss_release_name(&minor_status, &neg_ctx->server_name);
  168. memset(neg_ctx, 0, sizeof(*neg_ctx));
  169. }
  170. void Curl_cleanup_negotiate(struct SessionHandle *data)
  171. {
  172. cleanup(&data->state.negotiate);
  173. cleanup(&data->state.proxyneg);
  174. }
  175. #endif /* HAVE_GSSAPI && !CURL_DISABLE_HTTP && USE_SPNEGO */