krb5_gssapi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /*
  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 = ((unsigned int)indata[1] << 16) |
  203. ((unsigned int)indata[2] << 8) | indata[3];
  204. /* Free the challenge as it is not required anymore */
  205. gss_release_buffer(&unused_status, &output_token);
  206. /* Process the security layer */
  207. if(!(sec_layer & GSSAUTH_P_NONE)) {
  208. infof(data, "GSSAPI handshake failure (invalid security layer)");
  209. return CURLE_BAD_CONTENT_ENCODING;
  210. }
  211. sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
  212. /* Process the maximum message size the server can receive */
  213. if(max_size > 0) {
  214. /* The server has told us it supports a maximum receive buffer, however, as
  215. we don't require one unless we are encrypting data, we tell the server
  216. our receive buffer is zero. */
  217. max_size = 0;
  218. }
  219. /* Allocate our message */
  220. messagelen = 4;
  221. if(authzid)
  222. messagelen += strlen(authzid);
  223. message = malloc(messagelen);
  224. if(!message)
  225. return CURLE_OUT_OF_MEMORY;
  226. /* Populate the message with the security layer and client supported receive
  227. message size. */
  228. message[0] = sec_layer & 0xFF;
  229. message[1] = (max_size >> 16) & 0xFF;
  230. message[2] = (max_size >> 8) & 0xFF;
  231. message[3] = max_size & 0xFF;
  232. /* If given, append the authorization identity. */
  233. if(authzid && *authzid)
  234. memcpy(message + 4, authzid, messagelen - 4);
  235. /* Setup the "authentication data" security buffer */
  236. input_token.value = message;
  237. input_token.length = messagelen;
  238. /* Encrypt the data */
  239. major_status = gss_wrap(&minor_status, krb5->context, 0,
  240. GSS_C_QOP_DEFAULT, &input_token, NULL,
  241. &output_token);
  242. if(GSS_ERROR(major_status)) {
  243. Curl_gss_log_error(data, "gss_wrap() failed: ",
  244. major_status, minor_status);
  245. free(message);
  246. return CURLE_AUTH_ERROR;
  247. }
  248. /* Return the response. */
  249. result = Curl_bufref_memdup(out, output_token.value, output_token.length);
  250. /* Free the output buffer */
  251. gss_release_buffer(&unused_status, &output_token);
  252. /* Free the message buffer */
  253. free(message);
  254. return result;
  255. }
  256. /*
  257. * Curl_auth_cleanup_gssapi()
  258. *
  259. * This is used to clean up the GSSAPI (Kerberos V5) specific data.
  260. *
  261. * Parameters:
  262. *
  263. * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
  264. *
  265. */
  266. void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
  267. {
  268. OM_uint32 minor_status;
  269. /* Free our security context */
  270. if(krb5->context != GSS_C_NO_CONTEXT) {
  271. gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
  272. krb5->context = GSS_C_NO_CONTEXT;
  273. }
  274. /* Free the SPN */
  275. if(krb5->spn != GSS_C_NO_NAME) {
  276. gss_release_name(&minor_status, &krb5->spn);
  277. krb5->spn = GSS_C_NO_NAME;
  278. }
  279. }
  280. #endif /* HAVE_GSSAPI && USE_KERBEROS5 */