krb5_gssapi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
  9. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  25. *
  26. ***************************************************************************/
  27. #include "curl_setup.h"
  28. #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
  29. #include <curl/curl.h>
  30. #include "vauth/vauth.h"
  31. #include "curl_sasl.h"
  32. #include "urldata.h"
  33. #include "curl_gssapi.h"
  34. #include "sendf.h"
  35. #include "curl_printf.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_gssapi_supported()
  45. *
  46. * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
  47. *
  48. * Parameters: None
  49. *
  50. * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
  51. */
  52. bool Curl_auth_is_gssapi_supported(void)
  53. {
  54. return TRUE;
  55. }
  56. /*
  57. * Curl_auth_create_gssapi_user_message()
  58. *
  59. * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
  60. * message ready for sending to the recipient.
  61. *
  62. * Parameters:
  63. *
  64. * data [in] - The session handle.
  65. * userp [in] - The username.
  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. * mutual_auth [in] - Flag specifying whether or not mutual authentication
  70. * is enabled.
  71. * chlg [in] - Optional challenge message.
  72. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  73. * out [out] - The result storage.
  74. *
  75. * Returns CURLE_OK on success.
  76. */
  77. CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
  78. const char *userp,
  79. const char *passwdp,
  80. const char *service,
  81. const char *host,
  82. const bool mutual_auth,
  83. const struct bufref *chlg,
  84. struct kerberos5data *krb5,
  85. struct bufref *out)
  86. {
  87. CURLcode result = CURLE_OK;
  88. OM_uint32 major_status;
  89. OM_uint32 minor_status;
  90. OM_uint32 unused_status;
  91. gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
  92. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  93. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  94. (void) userp;
  95. (void) passwdp;
  96. if(!krb5->spn) {
  97. /* Generate our SPN */
  98. char *spn = Curl_auth_build_spn(service, NULL, host);
  99. if(!spn)
  100. return CURLE_OUT_OF_MEMORY;
  101. /* Populate the SPN structure */
  102. spn_token.value = spn;
  103. spn_token.length = strlen(spn);
  104. /* Import the SPN */
  105. major_status = gss_import_name(&minor_status, &spn_token,
  106. GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
  107. if(GSS_ERROR(major_status)) {
  108. Curl_gss_log_error(data, "gss_import_name() failed: ",
  109. major_status, minor_status);
  110. free(spn);
  111. return CURLE_AUTH_ERROR;
  112. }
  113. free(spn);
  114. }
  115. if(chlg) {
  116. if(!Curl_bufref_len(chlg)) {
  117. infof(data, "GSSAPI handshake failure (empty challenge message)");
  118. return CURLE_BAD_CONTENT_ENCODING;
  119. }
  120. input_token.value = (void *) Curl_bufref_ptr(chlg);
  121. input_token.length = Curl_bufref_len(chlg);
  122. }
  123. major_status = Curl_gss_init_sec_context(data,
  124. &minor_status,
  125. &krb5->context,
  126. krb5->spn,
  127. &Curl_krb5_mech_oid,
  128. GSS_C_NO_CHANNEL_BINDINGS,
  129. &input_token,
  130. &output_token,
  131. mutual_auth,
  132. NULL);
  133. if(GSS_ERROR(major_status)) {
  134. if(output_token.value)
  135. gss_release_buffer(&unused_status, &output_token);
  136. Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
  137. major_status, minor_status);
  138. return CURLE_AUTH_ERROR;
  139. }
  140. if(output_token.value && output_token.length) {
  141. result = Curl_bufref_memdup(out, output_token.value, output_token.length);
  142. gss_release_buffer(&unused_status, &output_token);
  143. }
  144. else
  145. Curl_bufref_set(out, mutual_auth ? "": NULL, 0, NULL);
  146. return result;
  147. }
  148. /*
  149. * Curl_auth_create_gssapi_security_message()
  150. *
  151. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  152. * token message ready for sending to the recipient.
  153. *
  154. * Parameters:
  155. *
  156. * data [in] - The session handle.
  157. * authzid [in] - The authorization identity if some.
  158. * chlg [in] - Optional challenge message.
  159. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  160. * out [out] - The result storage.
  161. *
  162. * Returns CURLE_OK on success.
  163. */
  164. CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
  165. const char *authzid,
  166. const struct bufref *chlg,
  167. struct kerberos5data *krb5,
  168. struct bufref *out)
  169. {
  170. CURLcode result = CURLE_OK;
  171. size_t messagelen = 0;
  172. unsigned char *message = NULL;
  173. OM_uint32 major_status;
  174. OM_uint32 minor_status;
  175. OM_uint32 unused_status;
  176. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  177. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  178. unsigned char *indata;
  179. gss_qop_t qop = GSS_C_QOP_DEFAULT;
  180. unsigned int sec_layer = 0;
  181. unsigned int max_size = 0;
  182. /* Ensure we have a valid challenge message */
  183. if(!Curl_bufref_len(chlg)) {
  184. infof(data, "GSSAPI handshake failure (empty security message)");
  185. return CURLE_BAD_CONTENT_ENCODING;
  186. }
  187. /* Setup the challenge "input" security buffer */
  188. input_token.value = (void *) Curl_bufref_ptr(chlg);
  189. input_token.length = Curl_bufref_len(chlg);
  190. /* Decrypt the inbound challenge and obtain the qop */
  191. major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
  192. &output_token, NULL, &qop);
  193. if(GSS_ERROR(major_status)) {
  194. Curl_gss_log_error(data, "gss_unwrap() failed: ",
  195. major_status, minor_status);
  196. return CURLE_BAD_CONTENT_ENCODING;
  197. }
  198. /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
  199. if(output_token.length != 4) {
  200. infof(data, "GSSAPI handshake failure (invalid security data)");
  201. return CURLE_BAD_CONTENT_ENCODING;
  202. }
  203. /* Extract the security layer and the maximum message size */
  204. indata = output_token.value;
  205. sec_layer = indata[0];
  206. max_size = ((unsigned int)indata[1] << 16) |
  207. ((unsigned int)indata[2] << 8) | indata[3];
  208. /* Free the challenge as it is not required anymore */
  209. gss_release_buffer(&unused_status, &output_token);
  210. /* Process the security layer */
  211. if(!(sec_layer & GSSAUTH_P_NONE)) {
  212. infof(data, "GSSAPI handshake failure (invalid security layer)");
  213. return CURLE_BAD_CONTENT_ENCODING;
  214. }
  215. sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
  216. /* Process the maximum message size the server can receive */
  217. if(max_size > 0) {
  218. /* The server has told us it supports a maximum receive buffer, however, as
  219. we do not require one unless we are encrypting data, we tell the server
  220. our receive buffer is zero. */
  221. max_size = 0;
  222. }
  223. /* Allocate our message */
  224. messagelen = 4;
  225. if(authzid)
  226. messagelen += strlen(authzid);
  227. message = malloc(messagelen);
  228. if(!message)
  229. return CURLE_OUT_OF_MEMORY;
  230. /* Populate the message with the security layer and client supported receive
  231. message size. */
  232. message[0] = sec_layer & 0xFF;
  233. message[1] = (max_size >> 16) & 0xFF;
  234. message[2] = (max_size >> 8) & 0xFF;
  235. message[3] = max_size & 0xFF;
  236. /* If given, append the authorization identity. */
  237. if(authzid && *authzid)
  238. memcpy(message + 4, authzid, messagelen - 4);
  239. /* Setup the "authentication data" security buffer */
  240. input_token.value = message;
  241. input_token.length = messagelen;
  242. /* Encrypt the data */
  243. major_status = gss_wrap(&minor_status, krb5->context, 0,
  244. GSS_C_QOP_DEFAULT, &input_token, NULL,
  245. &output_token);
  246. if(GSS_ERROR(major_status)) {
  247. Curl_gss_log_error(data, "gss_wrap() failed: ",
  248. major_status, minor_status);
  249. free(message);
  250. return CURLE_AUTH_ERROR;
  251. }
  252. /* Return the response. */
  253. result = Curl_bufref_memdup(out, output_token.value, output_token.length);
  254. /* Free the output buffer */
  255. gss_release_buffer(&unused_status, &output_token);
  256. /* Free the message buffer */
  257. free(message);
  258. return result;
  259. }
  260. /*
  261. * Curl_auth_cleanup_gssapi()
  262. *
  263. * This is used to clean up the GSSAPI (Kerberos V5) specific data.
  264. *
  265. * Parameters:
  266. *
  267. * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
  268. *
  269. */
  270. void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
  271. {
  272. OM_uint32 minor_status;
  273. /* Free our security context */
  274. if(krb5->context != GSS_C_NO_CONTEXT) {
  275. gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
  276. krb5->context = GSS_C_NO_CONTEXT;
  277. }
  278. /* Free the SPN */
  279. if(krb5->spn != GSS_C_NO_NAME) {
  280. gss_release_name(&minor_status, &krb5->spn);
  281. krb5->spn = GSS_C_NO_NAME;
  282. }
  283. }
  284. #if defined(__GNUC__) && defined(__APPLE__)
  285. #pragma GCC diagnostic pop
  286. #endif
  287. #endif /* HAVE_GSSAPI && USE_KERBEROS5 */