curl_sasl_gssapi.c 13 KB

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