curl_sasl_gssapi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014, Steve Holme, <[email protected]>.
  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 http://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. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
  26. #ifdef HAVE_OLD_GSSMIT
  27. #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
  28. #define NCOMPAT 1
  29. #endif
  30. #define GSSAUTH_P_NONE 1
  31. #define GSSAUTH_P_INTEGRITY 2
  32. #define GSSAUTH_P_PRIVACY 4
  33. #include <curl/curl.h>
  34. #include "curl_sasl.h"
  35. #include "urldata.h"
  36. #include "curl_base64.h"
  37. #include "curl_gssapi.h"
  38. #include "curl_memory.h"
  39. #include "sendf.h"
  40. #define _MPRINTF_REPLACE /* use our functions only */
  41. #include <curl/mprintf.h>
  42. /* The last #include file should be: */
  43. #include "memdebug.h"
  44. /*
  45. * Curl_sasl_build_gssapi_spn()
  46. *
  47. * This is used to build a SPN string in the format service@host.
  48. *
  49. * Parameters:
  50. *
  51. * serivce [in] - The service type such as www, smtp, pop or imap.
  52. * host [in] - The host name or realm.
  53. *
  54. * Returns a pointer to the newly allocated SPN.
  55. */
  56. static char *Curl_sasl_build_gssapi_spn(const char *service, const char *host)
  57. {
  58. /* Generate and return our SPN */
  59. return aprintf("%s@%s", service, host);
  60. }
  61. /*
  62. * Curl_sasl_create_gssapi_user_message()
  63. *
  64. * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
  65. * message ready for sending to the recipient.
  66. *
  67. * Parameters:
  68. *
  69. * data [in] - The session handle.
  70. * userp [in] - The user name.
  71. * passdwp [in] - The user's password.
  72. * service [in] - The service type such as www, smtp, pop or imap.
  73. * mutual_auth [in] - Flag specifing whether or not mutual authentication
  74. * is enabled.
  75. * chlg64 [in] - Pointer to the optional base64 encoded challenge
  76. * message.
  77. * krb5 [in/out] - The gssapi data struct being used and modified.
  78. * outptr [in/out] - The address where a pointer to newly allocated memory
  79. * holding the result will be stored upon completion.
  80. * outlen [out] - The length of the output message.
  81. *
  82. * Returns CURLE_OK on success.
  83. */
  84. CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
  85. const char *userp,
  86. const char *passwdp,
  87. const char *service,
  88. const bool mutual_auth,
  89. const char *chlg64,
  90. struct kerberos5data *krb5,
  91. char **outptr, size_t *outlen)
  92. {
  93. CURLcode result = CURLE_OK;
  94. size_t chlglen = 0;
  95. unsigned char *chlg = NULL;
  96. OM_uint32 gss_status;
  97. OM_uint32 gss_major_status;
  98. OM_uint32 gss_minor_status;
  99. gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
  100. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  101. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  102. (void) userp;
  103. (void) passwdp;
  104. if(krb5->context == GSS_C_NO_CONTEXT) {
  105. /* Generate our SPN */
  106. char *spn = Curl_sasl_build_gssapi_spn(service,
  107. data->easy_conn->host.name);
  108. if(!spn)
  109. return CURLE_OUT_OF_MEMORY;
  110. /* Populate the SPN structure */
  111. spn_token.value = spn;
  112. spn_token.length = strlen(spn);
  113. /* Import the SPN */
  114. gss_major_status = gss_import_name(&gss_minor_status, &spn_token,
  115. gss_nt_service_name, &krb5->spn);
  116. if(GSS_ERROR(gss_major_status)) {
  117. Curl_gss_log_error(data, gss_minor_status, "gss_import_name() failed: ");
  118. return CURLE_OUT_OF_MEMORY;
  119. }
  120. }
  121. else {
  122. /* Decode the base-64 encoded challenge message */
  123. if(strlen(chlg64) && *chlg64 != '=') {
  124. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  125. if(result)
  126. return result;
  127. }
  128. /* Ensure we have a valid challenge message */
  129. if(!chlg) {
  130. infof(data, "GSSAPI handshake failure (empty challenge message)\n");
  131. return CURLE_BAD_CONTENT_ENCODING;
  132. }
  133. /* Setup the challenge "input" security buffer */
  134. input_token.value = chlg;
  135. input_token.length = chlglen;
  136. }
  137. gss_major_status = Curl_gss_init_sec_context(data,
  138. &gss_minor_status,
  139. &krb5->context,
  140. krb5->spn,
  141. &Curl_krb5_mech_oid,
  142. GSS_C_NO_CHANNEL_BINDINGS,
  143. &input_token,
  144. &output_token,
  145. mutual_auth,
  146. NULL);
  147. Curl_safefree(input_token.value);
  148. if(GSS_ERROR(gss_major_status)) {
  149. if(output_token.value)
  150. gss_release_buffer(&gss_status, &output_token);
  151. Curl_gss_log_error(data, gss_minor_status,
  152. "gss_init_sec_context() failed: ");
  153. return CURLE_RECV_ERROR;
  154. }
  155. if(output_token.value && output_token.length) {
  156. /* Base64 encode the response */
  157. result = Curl_base64_encode(data, (char *) output_token.value,
  158. output_token.length, outptr, outlen);
  159. gss_release_buffer(&gss_status, &output_token);
  160. }
  161. return result;
  162. }
  163. /*
  164. * Curl_sasl_create_gssapi_security_message()
  165. *
  166. * This is used to generate an already encoded GSSAPI (Kerberos V5) security
  167. * token message ready for sending to the recipient.
  168. *
  169. * Parameters:
  170. *
  171. * data [in] - The session handle.
  172. * chlg64 [in] - Pointer to the optional base64 encoded challenge message.
  173. * krb5 [in/out] - The gssapi data struct being used and modified.
  174. * outptr [in/out] - The address where a pointer to newly allocated memory
  175. * holding the result will be stored upon completion.
  176. * outlen [out] - The length of the output message.
  177. *
  178. * Returns CURLE_OK on success.
  179. */
  180. CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
  181. const char *chlg64,
  182. struct kerberos5data *krb5,
  183. char **outptr,
  184. size_t *outlen)
  185. {
  186. CURLcode result = CURLE_OK;
  187. size_t chlglen = 0;
  188. size_t messagelen = 0;
  189. unsigned char *chlg = NULL;
  190. unsigned char *message = NULL;
  191. OM_uint32 gss_status;
  192. OM_uint32 gss_major_status;
  193. OM_uint32 gss_minor_status;
  194. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  195. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  196. unsigned int indata = 0;
  197. unsigned int outdata = 0;
  198. gss_qop_t qop = GSS_C_QOP_DEFAULT;
  199. unsigned int sec_layer = 0;
  200. unsigned int max_size = 0;
  201. gss_name_t username = GSS_C_NO_NAME;
  202. gss_buffer_desc username_token;
  203. /* Decode the base-64 encoded input message */
  204. if(strlen(chlg64) && *chlg64 != '=') {
  205. result = Curl_base64_decode(chlg64, &chlg, &chlglen);
  206. if(result)
  207. return result;
  208. }
  209. /* Ensure we have a valid challenge message */
  210. if(!chlg) {
  211. infof(data, "GSSAPI handshake failure (empty security message)\n");
  212. return CURLE_BAD_CONTENT_ENCODING;
  213. }
  214. /* Get the fully qualified username back from the context */
  215. gss_major_status = gss_inquire_context(&gss_minor_status, krb5->context,
  216. &username, NULL, NULL, NULL, NULL,
  217. NULL, NULL);
  218. if(GSS_ERROR(gss_major_status)) {
  219. Curl_gss_log_error(data, gss_minor_status,
  220. "gss_inquire_context() failed: ");
  221. Curl_safefree(chlg);
  222. return CURLE_OUT_OF_MEMORY;
  223. }
  224. /* Convert the username from internal format to a displayable token */
  225. gss_major_status = gss_display_name(&gss_minor_status, username,
  226. &username_token, NULL);
  227. if(GSS_ERROR(gss_major_status)) {
  228. Curl_gss_log_error(data, gss_minor_status, "gss_display_name() failed: ");
  229. Curl_safefree(chlg);
  230. return CURLE_OUT_OF_MEMORY;
  231. }
  232. /* Setup the challenge "input" security buffer */
  233. input_token.value = chlg;
  234. input_token.length = chlglen;
  235. /* Decrypt the inbound challenge and obtain the qop */
  236. gss_major_status = gss_unwrap(&gss_minor_status, krb5->context, &input_token,
  237. &output_token, NULL, &qop);
  238. if(GSS_ERROR(gss_major_status)) {
  239. Curl_gss_log_error(data, gss_minor_status, "gss_unwrap() failed: ");
  240. gss_release_buffer(&gss_status, &username_token);
  241. Curl_safefree(chlg);
  242. return CURLE_BAD_CONTENT_ENCODING;
  243. }
  244. /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
  245. if(output_token.length != 4) {
  246. infof(data, "GSSAPI handshake failure (invalid security data)\n");
  247. gss_release_buffer(&gss_status, &username_token);
  248. Curl_safefree(chlg);
  249. return CURLE_BAD_CONTENT_ENCODING;
  250. }
  251. /* Copy the data out and free the challenge as it is not required anymore */
  252. memcpy(&indata, output_token.value, 4);
  253. gss_release_buffer(&gss_status, &output_token);
  254. Curl_safefree(chlg);
  255. /* Extract the security layer */
  256. sec_layer = indata & 0x000000FF;
  257. if(!(sec_layer & GSSAUTH_P_NONE)) {
  258. infof(data, "GSSAPI handshake failure (invalid security layer)\n");
  259. gss_release_buffer(&gss_status, &username_token);
  260. return CURLE_BAD_CONTENT_ENCODING;
  261. }
  262. /* Extract the maximum message size the server can receive */
  263. max_size = ntohl(indata & 0xFFFFFF00);
  264. if(max_size > 0) {
  265. /* The server has told us it supports a maximum receive buffer, however, as
  266. we don't require one unless we are encrypting data, we tell the server
  267. our receive buffer is zero. */
  268. max_size = 0;
  269. }
  270. /* Allocate our message */
  271. messagelen = sizeof(outdata) + username_token.length + 1;
  272. message = malloc(messagelen);
  273. if(!message) {
  274. gss_release_buffer(&gss_status, &username_token);
  275. return CURLE_OUT_OF_MEMORY;
  276. }
  277. /* Populate the message with the security layer, client supported receive
  278. message size and authorization identity including the 0x00 based
  279. terminator. Note: Dispite RFC4752 Section 3.1 stating "The authorization
  280. identity is not terminated with the zero-valued (%x00) octet." it seems
  281. necessary to include it. */
  282. outdata = htonl(max_size) | sec_layer;
  283. memcpy(message, &outdata, sizeof(outdata));
  284. memcpy(message + sizeof(outdata), username_token.value,
  285. username_token.length);
  286. message[messagelen - 1] = '\0';
  287. /* Free the username token as it is not required anymore */
  288. gss_release_buffer(&gss_status, &username_token);
  289. /* Setup the "authentication data" security buffer */
  290. input_token.value = message;
  291. input_token.length = messagelen;
  292. /* Encrypt the data */
  293. gss_major_status = gss_wrap(&gss_minor_status, krb5->context, 0,
  294. GSS_C_QOP_DEFAULT, &input_token, NULL,
  295. &output_token);
  296. if(GSS_ERROR(gss_major_status)) {
  297. Curl_gss_log_error(data, gss_minor_status, "gss_wrap() failed: ");
  298. Curl_safefree(message);
  299. return CURLE_OUT_OF_MEMORY;
  300. }
  301. /* Base64 encode the response */
  302. result = Curl_base64_encode(data, (char *) output_token.value,
  303. output_token.length, outptr, outlen);
  304. /* Free the output buffer */
  305. gss_release_buffer(&gss_status, &output_token);
  306. /* Free the message buffer */
  307. Curl_safefree(message);
  308. return result;
  309. }
  310. /*
  311. * Curl_sasl_gssapi_cleanup()
  312. *
  313. * This is used to clean up the gssapi specific data.
  314. *
  315. * Parameters:
  316. *
  317. * krb5 [in/out] - The kerberos 5 data struct being cleaned up.
  318. *
  319. */
  320. void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5)
  321. {
  322. OM_uint32 minor_status;
  323. /* Free our security context */
  324. if(krb5->context != GSS_C_NO_CONTEXT) {
  325. gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
  326. krb5->context = GSS_C_NO_CONTEXT;
  327. }
  328. /* Free the SPN */
  329. if(krb5->spn != GSS_C_NO_NAME) {
  330. gss_release_name(&minor_status, &krb5->spn);
  331. krb5->spn = GSS_C_NO_NAME;
  332. }
  333. }
  334. #endif /* HAVE_GSSAPI && USE_KERBEROS5 */