spnego_gssapi.c 8.2 KB

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