spnego_gssapi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 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. * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #if defined(HAVE_GSSAPI) && defined(USE_SPNEGO)
  28. #include <curl/curl.h>
  29. #include "vauth/vauth.h"
  30. #include "urldata.h"
  31. #include "curl_base64.h"
  32. #include "curl_gssapi.h"
  33. #include "warnless.h"
  34. #include "curl_multibyte.h"
  35. #include "sendf.h"
  36. /* The last #include files should be: */
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. #if defined(__GNUC__) && defined(__APPLE__)
  40. #pragma GCC diagnostic push
  41. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  42. #endif
  43. /*
  44. * Curl_auth_is_spnego_supported()
  45. *
  46. * This is used to evaluate if SPNEGO (Negotiate) is supported.
  47. *
  48. * Parameters: None
  49. *
  50. * Returns TRUE if Negotiate supported by the GSS-API library.
  51. */
  52. bool Curl_auth_is_spnego_supported(void)
  53. {
  54. return TRUE;
  55. }
  56. /*
  57. * Curl_auth_decode_spnego_message()
  58. *
  59. * This is used to decode an already encoded SPNEGO (Negotiate) challenge
  60. * message.
  61. *
  62. * Parameters:
  63. *
  64. * data [in] - The session handle.
  65. * userp [in] - The username in the format User or Domain\User.
  66. * passwdp [in] - The user's password.
  67. * service [in] - The service type such as http, smtp, pop or imap.
  68. * host [in] - The hostname.
  69. * chlg64 [in] - The optional base64 encoded challenge message.
  70. * nego [in/out] - The Negotiate data struct being used and modified.
  71. *
  72. * Returns CURLE_OK on success.
  73. */
  74. CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
  75. const char *user,
  76. const char *password,
  77. const char *service,
  78. const char *host,
  79. const char *chlg64,
  80. struct negotiatedata *nego)
  81. {
  82. CURLcode result = CURLE_OK;
  83. size_t chlglen = 0;
  84. unsigned char *chlg = NULL;
  85. OM_uint32 major_status;
  86. OM_uint32 minor_status;
  87. OM_uint32 unused_status;
  88. gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
  89. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  90. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  91. gss_channel_bindings_t chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
  92. struct gss_channel_bindings_struct chan;
  93. (void) user;
  94. (void) password;
  95. if(nego->context && nego->status == GSS_S_COMPLETE) {
  96. /* We finished successfully our part of authentication, but server
  97. * rejected it (since we are again here). Exit with an error since we
  98. * cannot invent anything better */
  99. Curl_auth_cleanup_spnego(nego);
  100. return CURLE_LOGIN_DENIED;
  101. }
  102. if(!nego->spn) {
  103. /* Generate our SPN */
  104. char *spn = Curl_auth_build_spn(service, NULL, host);
  105. if(!spn)
  106. return CURLE_OUT_OF_MEMORY;
  107. /* Populate the SPN structure */
  108. spn_token.value = spn;
  109. spn_token.length = strlen(spn);
  110. /* Import the SPN */
  111. major_status = gss_import_name(&minor_status, &spn_token,
  112. GSS_C_NT_HOSTBASED_SERVICE,
  113. &nego->spn);
  114. if(GSS_ERROR(major_status)) {
  115. Curl_gss_log_error(data, "gss_import_name() failed: ",
  116. major_status, minor_status);
  117. free(spn);
  118. return CURLE_AUTH_ERROR;
  119. }
  120. free(spn);
  121. }
  122. if(chlg64 && *chlg64) {
  123. /* Decode the base-64 encoded challenge message */
  124. if(*chlg64 != '=') {
  125. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  126. if(result)
  127. return result;
  128. }
  129. /* Ensure we have a valid challenge message */
  130. if(!chlg) {
  131. infof(data, "SPNEGO handshake failure (empty challenge message)");
  132. return CURLE_BAD_CONTENT_ENCODING;
  133. }
  134. /* Setup the challenge "input" security buffer */
  135. input_token.value = chlg;
  136. input_token.length = chlglen;
  137. }
  138. /* Set channel binding data if available */
  139. if(nego->channel_binding_data.leng > 0) {
  140. memset(&chan, 0, sizeof(struct gss_channel_bindings_struct));
  141. chan.application_data.length = nego->channel_binding_data.leng;
  142. chan.application_data.value = nego->channel_binding_data.bufr;
  143. chan_bindings = &chan;
  144. }
  145. /* Generate our challenge-response message */
  146. major_status = Curl_gss_init_sec_context(data,
  147. &minor_status,
  148. &nego->context,
  149. nego->spn,
  150. &Curl_spnego_mech_oid,
  151. chan_bindings,
  152. &input_token,
  153. &output_token,
  154. TRUE,
  155. NULL);
  156. /* Free the decoded challenge as it is not required anymore */
  157. Curl_safefree(input_token.value);
  158. nego->status = major_status;
  159. if(GSS_ERROR(major_status)) {
  160. if(output_token.value)
  161. gss_release_buffer(&unused_status, &output_token);
  162. Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
  163. major_status, minor_status);
  164. return CURLE_AUTH_ERROR;
  165. }
  166. if(!output_token.value || !output_token.length) {
  167. if(output_token.value)
  168. gss_release_buffer(&unused_status, &output_token);
  169. return CURLE_AUTH_ERROR;
  170. }
  171. /* Free previous token */
  172. if(nego->output_token.length && nego->output_token.value)
  173. gss_release_buffer(&unused_status, &nego->output_token);
  174. nego->output_token = output_token;
  175. return CURLE_OK;
  176. }
  177. /*
  178. * Curl_auth_create_spnego_message()
  179. *
  180. * This is used to generate an already encoded SPNEGO (Negotiate) response
  181. * message ready for sending to the recipient.
  182. *
  183. * Parameters:
  184. *
  185. * data [in] - The session handle.
  186. * nego [in/out] - The Negotiate data struct being used and modified.
  187. * outptr [in/out] - The address where a pointer to newly allocated memory
  188. * holding the result will be stored upon completion.
  189. * outlen [out] - The length of the output message.
  190. *
  191. * Returns CURLE_OK on success.
  192. */
  193. CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
  194. char **outptr, size_t *outlen)
  195. {
  196. CURLcode result;
  197. OM_uint32 minor_status;
  198. /* Base64 encode the already generated response */
  199. result = Curl_base64_encode(nego->output_token.value,
  200. nego->output_token.length,
  201. outptr, outlen);
  202. if(result) {
  203. gss_release_buffer(&minor_status, &nego->output_token);
  204. nego->output_token.value = NULL;
  205. nego->output_token.length = 0;
  206. return result;
  207. }
  208. if(!*outptr || !*outlen) {
  209. gss_release_buffer(&minor_status, &nego->output_token);
  210. nego->output_token.value = NULL;
  211. nego->output_token.length = 0;
  212. return CURLE_REMOTE_ACCESS_DENIED;
  213. }
  214. return CURLE_OK;
  215. }
  216. /*
  217. * Curl_auth_cleanup_spnego()
  218. *
  219. * This is used to clean up the SPNEGO (Negotiate) specific data.
  220. *
  221. * Parameters:
  222. *
  223. * nego [in/out] - The Negotiate data struct being cleaned up.
  224. *
  225. */
  226. void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
  227. {
  228. OM_uint32 minor_status;
  229. /* Free our security context */
  230. if(nego->context != GSS_C_NO_CONTEXT) {
  231. gss_delete_sec_context(&minor_status, &nego->context, GSS_C_NO_BUFFER);
  232. nego->context = GSS_C_NO_CONTEXT;
  233. }
  234. /* Free the output token */
  235. if(nego->output_token.value) {
  236. gss_release_buffer(&minor_status, &nego->output_token);
  237. nego->output_token.value = NULL;
  238. nego->output_token.length = 0;
  239. }
  240. /* Free the SPN */
  241. if(nego->spn != GSS_C_NO_NAME) {
  242. gss_release_name(&minor_status, &nego->spn);
  243. nego->spn = GSS_C_NO_NAME;
  244. }
  245. /* Reset any variables */
  246. nego->status = 0;
  247. nego->noauthpersist = FALSE;
  248. nego->havenoauthpersist = FALSE;
  249. nego->havenegdata = FALSE;
  250. nego->havemultiplerequests = FALSE;
  251. }
  252. #if defined(__GNUC__) && defined(__APPLE__)
  253. #pragma GCC diagnostic pop
  254. #endif
  255. #endif /* HAVE_GSSAPI && USE_SPNEGO */