krb5_gssapi.c 10 KB

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