http_negotiate.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
  26. #include "urldata.h"
  27. #include "cfilters.h"
  28. #include "sendf.h"
  29. #include "http_negotiate.h"
  30. #include "vauth/vauth.h"
  31. #include "vtls/vtls.h"
  32. #include "curlx/strparse.h"
  33. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. static void http_auth_nego_reset(struct connectdata *conn,
  38. struct negotiatedata *neg_ctx,
  39. bool proxy)
  40. {
  41. if(proxy)
  42. conn->proxy_negotiate_state = GSS_AUTHNONE;
  43. else
  44. conn->http_negotiate_state = GSS_AUTHNONE;
  45. if(neg_ctx)
  46. Curl_auth_cleanup_spnego(neg_ctx);
  47. }
  48. CURLcode Curl_input_negotiate(struct Curl_easy *data, struct connectdata *conn,
  49. bool proxy, const char *header)
  50. {
  51. CURLcode result;
  52. size_t len;
  53. /* Point to the username, password, service and host */
  54. const char *userp;
  55. const char *passwdp;
  56. const char *service;
  57. const char *host;
  58. /* Point to the correct struct with this */
  59. struct negotiatedata *neg_ctx;
  60. curlnegotiate state;
  61. if(proxy) {
  62. #ifndef CURL_DISABLE_PROXY
  63. userp = conn->http_proxy.user;
  64. passwdp = conn->http_proxy.passwd;
  65. service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
  66. data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
  67. host = conn->http_proxy.host.name;
  68. state = conn->proxy_negotiate_state;
  69. #else
  70. return CURLE_NOT_BUILT_IN;
  71. #endif
  72. }
  73. else {
  74. userp = conn->user;
  75. passwdp = conn->passwd;
  76. service = data->set.str[STRING_SERVICE_NAME] ?
  77. data->set.str[STRING_SERVICE_NAME] : "HTTP";
  78. host = conn->host.name;
  79. state = conn->http_negotiate_state;
  80. }
  81. neg_ctx = Curl_auth_nego_get(conn, proxy);
  82. if(!neg_ctx)
  83. return CURLE_OUT_OF_MEMORY;
  84. /* Not set means empty */
  85. if(!userp)
  86. userp = "";
  87. if(!passwdp)
  88. passwdp = "";
  89. /* Obtain the input token, if any */
  90. header += strlen("Negotiate");
  91. curlx_str_passblanks(&header);
  92. len = strlen(header);
  93. neg_ctx->havenegdata = len != 0;
  94. if(!len) {
  95. if(state == GSS_AUTHSUCC) {
  96. infof(data, "Negotiate auth restarted");
  97. http_auth_nego_reset(conn, neg_ctx, proxy);
  98. }
  99. else if(state != GSS_AUTHNONE) {
  100. /* The server rejected our authentication and has not supplied any more
  101. negotiation mechanisms */
  102. http_auth_nego_reset(conn, neg_ctx, proxy);
  103. return CURLE_LOGIN_DENIED;
  104. }
  105. }
  106. /* Supports SSL channel binding for Windows ISS extended protection */
  107. #if defined(USE_WINDOWS_SSPI) && defined(SECPKG_ATTR_ENDPOINT_BINDINGS)
  108. neg_ctx->sslContext = conn->sslContext;
  109. #endif
  110. /* Check if the connection is using SSL and get the channel binding data */
  111. #ifdef HAVE_GSSAPI
  112. #ifdef USE_SSL
  113. curlx_dyn_init(&neg_ctx->channel_binding_data, SSL_CB_MAX_SIZE + 1);
  114. if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
  115. result = Curl_ssl_get_channel_binding(
  116. data, FIRSTSOCKET, &neg_ctx->channel_binding_data);
  117. if(result) {
  118. http_auth_nego_reset(conn, neg_ctx, proxy);
  119. return result;
  120. }
  121. }
  122. #else
  123. curlx_dyn_init(&neg_ctx->channel_binding_data, 1);
  124. #endif /* USE_SSL */
  125. #endif /* HAVE_GSSAPI */
  126. /* Initialize the security context and decode our challenge */
  127. result = Curl_auth_decode_spnego_message(data, userp, passwdp, service,
  128. host, header, neg_ctx);
  129. #ifdef HAVE_GSSAPI
  130. curlx_dyn_free(&neg_ctx->channel_binding_data);
  131. #endif
  132. if(result)
  133. http_auth_nego_reset(conn, neg_ctx, proxy);
  134. return result;
  135. }
  136. CURLcode Curl_output_negotiate(struct Curl_easy *data,
  137. struct connectdata *conn, bool proxy)
  138. {
  139. struct negotiatedata *neg_ctx;
  140. struct auth *authp;
  141. curlnegotiate *state;
  142. char *base64 = NULL;
  143. size_t len = 0;
  144. char *userp;
  145. CURLcode result;
  146. if(proxy) {
  147. #ifndef CURL_DISABLE_PROXY
  148. authp = &data->state.authproxy;
  149. state = &conn->proxy_negotiate_state;
  150. #else
  151. return CURLE_NOT_BUILT_IN;
  152. #endif
  153. }
  154. else {
  155. authp = &data->state.authhost;
  156. state = &conn->http_negotiate_state;
  157. }
  158. neg_ctx = Curl_auth_nego_get(conn, proxy);
  159. if(!neg_ctx)
  160. return CURLE_OUT_OF_MEMORY;
  161. authp->done = FALSE;
  162. if(*state == GSS_AUTHRECV) {
  163. if(neg_ctx->havenegdata) {
  164. neg_ctx->havemultiplerequests = TRUE;
  165. }
  166. }
  167. else if(*state == GSS_AUTHSUCC) {
  168. if(!neg_ctx->havenoauthpersist) {
  169. neg_ctx->noauthpersist = !neg_ctx->havemultiplerequests;
  170. }
  171. }
  172. if(neg_ctx->noauthpersist ||
  173. (*state != GSS_AUTHDONE && *state != GSS_AUTHSUCC)) {
  174. if(neg_ctx->noauthpersist && *state == GSS_AUTHSUCC) {
  175. infof(data, "Curl_output_negotiate, "
  176. "no persistent authentication: cleanup existing context");
  177. http_auth_nego_reset(conn, neg_ctx, proxy);
  178. }
  179. if(!neg_ctx->context) {
  180. result = Curl_input_negotiate(data, conn, proxy, "Negotiate");
  181. if(result == CURLE_AUTH_ERROR) {
  182. /* negotiate auth failed, let's continue unauthenticated to stay
  183. * compatible with the behavior before curl-7_64_0-158-g6c6035532 */
  184. authp->done = TRUE;
  185. return CURLE_OK;
  186. }
  187. else if(result)
  188. return result;
  189. }
  190. result = Curl_auth_create_spnego_message(neg_ctx, &base64, &len);
  191. if(result)
  192. return result;
  193. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  194. base64);
  195. if(proxy) {
  196. #ifndef CURL_DISABLE_PROXY
  197. free(data->state.aptr.proxyuserpwd);
  198. data->state.aptr.proxyuserpwd = userp;
  199. #endif
  200. }
  201. else {
  202. free(data->state.aptr.userpwd);
  203. data->state.aptr.userpwd = userp;
  204. }
  205. free(base64);
  206. if(!userp) {
  207. return CURLE_OUT_OF_MEMORY;
  208. }
  209. *state = GSS_AUTHSENT;
  210. #ifdef HAVE_GSSAPI
  211. if(neg_ctx->status == GSS_S_COMPLETE ||
  212. neg_ctx->status == GSS_S_CONTINUE_NEEDED) {
  213. *state = GSS_AUTHDONE;
  214. }
  215. #else
  216. #ifdef USE_WINDOWS_SSPI
  217. if(neg_ctx->status == SEC_E_OK ||
  218. neg_ctx->status == SEC_I_CONTINUE_NEEDED) {
  219. *state = GSS_AUTHDONE;
  220. }
  221. #endif
  222. #endif
  223. }
  224. if(*state == GSS_AUTHDONE || *state == GSS_AUTHSUCC) {
  225. /* connection is already authenticated,
  226. * do not send a header in future requests */
  227. authp->done = TRUE;
  228. }
  229. neg_ctx->havenegdata = FALSE;
  230. return CURLE_OK;
  231. }
  232. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */