krb5_gssapi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014 - 2019, Steve Holme, <steve_holme@hotmail.com>.
  9. * Copyright (C) 2015 - 2022, 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. /*
  40. * Curl_auth_is_gssapi_supported()
  41. *
  42. * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
  43. *
  44. * Parameters: None
  45. *
  46. * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
  47. */
  48. bool Curl_auth_is_gssapi_supported(void)
  49. {
  50. return TRUE;
  51. }
  52. /*
  53. * Curl_auth_create_gssapi_user_message()
  54. *
  55. * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
  56. * message ready for sending to the recipient.
  57. *
  58. * Parameters:
  59. *
  60. * data [in] - The session handle.
  61. * userp [in] - The user name.
  62. * passwdp [in] - The user's password.
  63. * service [in] - The service type such as http, smtp, pop or imap.
  64. * host [in[ - The host name.
  65. * mutual_auth [in] - Flag specifying whether or not mutual authentication
  66. * is enabled.
  67. * chlg [in] - Optional challenge message.
  68. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  69. * out [out] - The result storage.
  70. *
  71. * Returns CURLE_OK on success.
  72. */
  73. CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
  74. const char *userp,
  75. const char *passwdp,
  76. const char *service,
  77. const char *host,
  78. const bool mutual_auth,
  79. const struct bufref *chlg,
  80. struct kerberos5data *krb5,
  81. struct bufref *out)
  82. {
  83. CURLcode result = CURLE_OK;
  84. OM_uint32 major_status;
  85. OM_uint32 minor_status;
  86. OM_uint32 unused_status;
  87. gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
  88. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  89. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  90. (void) userp;
  91. (void) passwdp;
  92. if(!krb5->spn) {
  93. /* Generate our SPN */
  94. char *spn = Curl_auth_build_spn(service, NULL, host);
  95. if(!spn)
  96. return CURLE_OUT_OF_MEMORY;
  97. /* Populate the SPN structure */
  98. spn_token.value = spn;
  99. spn_token.length = strlen(spn);
  100. /* Import the SPN */
  101. major_status = gss_import_name(&minor_status, &spn_token,
  102. GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
  103. if(GSS_ERROR(major_status)) {
  104. Curl_gss_log_error(data, "gss_import_name() failed: ",
  105. major_status, minor_status);
  106. free(spn);
  107. return CURLE_AUTH_ERROR;
  108. }
  109. free(spn);
  110. }
  111. if(chlg) {
  112. if(!Curl_bufref_len(chlg)) {
  113. infof(data, "GSSAPI handshake failure (empty challenge message)");
  114. return CURLE_BAD_CONTENT_ENCODING;
  115. }
  116. input_token.value = (void *) Curl_bufref_ptr(chlg);
  117. input_token.length = Curl_bufref_len(chlg);
  118. }
  119. major_status = Curl_gss_init_sec_context(data,
  120. &minor_status,
  121. &krb5->context,
  122. krb5->spn,
  123. &Curl_krb5_mech_oid,
  124. GSS_C_NO_CHANNEL_BINDINGS,
  125. &input_token,
  126. &output_token,
  127. mutual_auth,
  128. NULL);
  129. if(GSS_ERROR(major_status)) {
  130. if(output_token.value)
  131. gss_release_buffer(&unused_status, &output_token);
  132. Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
  133. major_status, minor_status);
  134. return CURLE_AUTH_ERROR;
  135. }
  136. if(output_token.value && output_token.length) {
  137. result = Curl_bufref_memdup(out, output_token.value, output_token.length);
  138. gss_release_buffer(&unused_status, &output_token);
  139. }
  140. else
  141. Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
  142. return result;
  143. }
  144. /*
  145. * Curl_auth_create_gssapi_security_message()
  146. *
  147. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  148. * token message ready for sending to the recipient.
  149. *
  150. * Parameters:
  151. *
  152. * data [in] - The session handle.
  153. * authzid [in] - The authorization identity if some.
  154. * chlg [in] - Optional challenge message.
  155. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
  156. * out [out] - The result storage.
  157. *
  158. * Returns CURLE_OK on success.
  159. */
  160. CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
  161. const char *authzid,
  162. const struct bufref *chlg,
  163. struct kerberos5data *krb5,
  164. struct bufref *out)
  165. {
  166. CURLcode result = CURLE_OK;
  167. size_t messagelen = 0;
  168. unsigned char *message = NULL;
  169. OM_uint32 major_status;
  170. OM_uint32 minor_status;
  171. OM_uint32 unused_status;
  172. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  173. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  174. unsigned char *indata;
  175. gss_qop_t qop = GSS_C_QOP_DEFAULT;
  176. unsigned int sec_layer = 0;
  177. unsigned int max_size = 0;
  178. /* Ensure we have a valid challenge message */
  179. if(!Curl_bufref_len(chlg)) {
  180. infof(data, "GSSAPI handshake failure (empty security message)");
  181. return CURLE_BAD_CONTENT_ENCODING;
  182. }
  183. /* Setup the challenge "input" security buffer */
  184. input_token.value = (void *) Curl_bufref_ptr(chlg);
  185. input_token.length = Curl_bufref_len(chlg);
  186. /* Decrypt the inbound challenge and obtain the qop */
  187. major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
  188. &output_token, NULL, &qop);
  189. if(GSS_ERROR(major_status)) {
  190. Curl_gss_log_error(data, "gss_unwrap() failed: ",
  191. major_status, minor_status);
  192. return CURLE_BAD_CONTENT_ENCODING;
  193. }
  194. /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
  195. if(output_token.length != 4) {
  196. infof(data, "GSSAPI handshake failure (invalid security data)");
  197. return CURLE_BAD_CONTENT_ENCODING;
  198. }
  199. /* Extract the security layer and the maximum message size */
  200. indata = output_token.value;
  201. sec_layer = indata[0];
  202. max_size = (indata[1] << 16) | (indata[2] << 8) | indata[3];
  203. /* Free the challenge as it is not required anymore */
  204. gss_release_buffer(&unused_status, &output_token);
  205. /* Process the security layer */
  206. if(!(sec_layer & GSSAUTH_P_NONE)) {
  207. infof(data, "GSSAPI handshake failure (invalid security layer)");
  208. return CURLE_BAD_CONTENT_ENCODING;
  209. }
  210. sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
  211. /* Process the maximum message size the server can receive */
  212. if(max_size > 0) {
  213. /* The server has told us it supports a maximum receive buffer, however, as
  214. we don't require one unless we are encrypting data, we tell the server
  215. our receive buffer is zero. */
  216. max_size = 0;
  217. }
  218. /* Allocate our message */
  219. messagelen = 4;
  220. if(authzid)
  221. messagelen += strlen(authzid);
  222. message = malloc(messagelen);
  223. if(!message)
  224. return CURLE_OUT_OF_MEMORY;
  225. /* Populate the message with the security layer and client supported receive
  226. message size. */
  227. message[0] = sec_layer & 0xFF;
  228. message[1] = (max_size >> 16) & 0xFF;
  229. message[2] = (max_size >> 8) & 0xFF;
  230. message[3] = max_size & 0xFF;
  231. /* If given, append the authorization identity. */
  232. if(authzid && *authzid)
  233. memcpy(message + 4, authzid, messagelen - 4);
  234. /* Setup the "authentication data" security buffer */
  235. input_token.value = message;
  236. input_token.length = messagelen;
  237. /* Encrypt the data */
  238. major_status = gss_wrap(&minor_status, krb5->context, 0,
  239. GSS_C_QOP_DEFAULT, &input_token, NULL,
  240. &output_token);
  241. if(GSS_ERROR(major_status)) {
  242. Curl_gss_log_error(data, "gss_wrap() failed: ",
  243. major_status, minor_status);
  244. free(message);
  245. return CURLE_AUTH_ERROR;
  246. }
  247. /* Return the response. */
  248. result = Curl_bufref_memdup(out, output_token.value, output_token.length);
  249. /* Free the output buffer */
  250. gss_release_buffer(&unused_status, &output_token);
  251. /* Free the message buffer */
  252. free(message);
  253. return result;
  254. }
  255. /*
  256. * Curl_auth_cleanup_gssapi()
  257. *
  258. * This is used to clean up the GSSAPI (Kerberos V5) specific data.
  259. *
  260. * Parameters:
  261. *
  262. * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
  263. *
  264. */
  265. void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
  266. {
  267. OM_uint32 minor_status;
  268. /* Free our security context */
  269. if(krb5->context != GSS_C_NO_CONTEXT) {
  270. gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
  271. krb5->context = GSS_C_NO_CONTEXT;
  272. }
  273. /* Free the SPN */
  274. if(krb5->spn != GSS_C_NO_NAME) {
  275. gss_release_name(&minor_status, &krb5->spn);
  276. krb5->spn = GSS_C_NO_NAME;
  277. }
  278. }
  279. #endif /* HAVE_GSSAPI && USE_KERBEROS5 */