spnego_gssapi.c 8.7 KB

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