curl_gssapi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_GSSAPI
  26. #include "curl_gssapi.h"
  27. #include "sendf.h"
  28. #ifdef DEBUGBUILD
  29. #if defined(HAVE_GSSGNU) || !defined(_WIN32)
  30. /* To avoid memdebug macro replacement, wrap the name in parentheses to call
  31. the original version. It is freed via the GSS API gss_release_buffer(). */
  32. #define Curl_gss_alloc (malloc)
  33. #define Curl_gss_free (free)
  34. #define CURL_GSS_STUB
  35. /* For correctness this would be required for all platforms, not only Windows,
  36. but, as of v1.22.1, MIT Kerberos uses a special allocator only for Windows,
  37. and the availability of 'gssapi/gssapi_alloc.h' is difficult to detect,
  38. because GSS headers are not versioned, and there is also no other macro to
  39. indicate 1.18+ vs. previous versions. On Windows we can use 'GSS_S_BAD_MIC'.
  40. */
  41. #elif defined(_WIN32) && defined(GSS_S_BAD_MIC) /* MIT Kerberos 1.15+ */
  42. /* MIT Kerberos 1.10+ (Windows), 1.18+ (all platforms), missing from GNU GSS */
  43. #include <gssapi/gssapi_alloc.h>
  44. #define Curl_gss_alloc gssalloc_malloc
  45. #define Curl_gss_free gssalloc_free
  46. #define CURL_GSS_STUB
  47. #endif
  48. #endif /* DEBUGBUILD */
  49. /* The last 2 #include files should be in this order */
  50. #include "curl_memory.h"
  51. #include "memdebug.h"
  52. #ifdef __GNUC__
  53. #define CURL_ALIGN8 __attribute__((aligned(8)))
  54. #else
  55. #define CURL_ALIGN8
  56. #endif
  57. #if defined(__GNUC__) && defined(__APPLE__)
  58. #pragma GCC diagnostic push
  59. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  60. #endif
  61. gss_OID_desc Curl_spnego_mech_oid CURL_ALIGN8 = {
  62. 6, CURL_UNCONST("\x2b\x06\x01\x05\x05\x02")
  63. };
  64. gss_OID_desc Curl_krb5_mech_oid CURL_ALIGN8 = {
  65. 9, CURL_UNCONST("\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")
  66. };
  67. #ifdef CURL_GSS_STUB
  68. enum min_err_code {
  69. STUB_GSS_OK = 0,
  70. STUB_GSS_NO_MEMORY,
  71. STUB_GSS_INVALID_ARGS,
  72. STUB_GSS_INVALID_CREDS,
  73. STUB_GSS_INVALID_CTX,
  74. STUB_GSS_SERVER_ERR,
  75. STUB_GSS_NO_MECH,
  76. STUB_GSS_LAST
  77. };
  78. /* libcurl is also passing this struct to these functions, which are not yet
  79. * stubbed:
  80. * gss_inquire_context()
  81. * gss_unwrap()
  82. * gss_wrap()
  83. */
  84. struct stub_gss_ctx_id_t_desc {
  85. enum { STUB_GSS_NONE, STUB_GSS_KRB5, STUB_GSS_NTLM1, STUB_GSS_NTLM3 } sent;
  86. int have_krb5;
  87. int have_ntlm;
  88. OM_uint32 flags;
  89. char creds[250];
  90. };
  91. static OM_uint32
  92. stub_gss_init_sec_context(OM_uint32 *min,
  93. gss_cred_id_t initiator_cred_handle,
  94. struct stub_gss_ctx_id_t_desc **context,
  95. gss_name_t target_name,
  96. const gss_OID mech_type,
  97. OM_uint32 req_flags,
  98. OM_uint32 time_req,
  99. const gss_channel_bindings_t input_chan_bindings,
  100. gss_buffer_desc *input_token,
  101. gss_OID *actual_mech_type,
  102. gss_buffer_desc *output_token,
  103. OM_uint32 *ret_flags,
  104. OM_uint32 *time_rec)
  105. {
  106. struct stub_gss_ctx_id_t_desc *ctx = NULL;
  107. /* The token will be encoded in base64 */
  108. size_t length = sizeof(ctx->creds) * 3 / 4;
  109. size_t used = 0;
  110. char *token = NULL;
  111. const char *creds = NULL;
  112. (void)initiator_cred_handle;
  113. (void)mech_type;
  114. (void)time_req;
  115. (void)input_chan_bindings;
  116. (void)actual_mech_type;
  117. if(!min)
  118. return GSS_S_FAILURE;
  119. *min = 0;
  120. if(!context || !target_name || !output_token) {
  121. *min = STUB_GSS_INVALID_ARGS;
  122. return GSS_S_FAILURE;
  123. }
  124. creds = getenv("CURL_STUB_GSS_CREDS");
  125. if(!creds || strlen(creds) >= sizeof(ctx->creds)) {
  126. *min = STUB_GSS_INVALID_CREDS;
  127. return GSS_S_FAILURE;
  128. }
  129. ctx = *context;
  130. if(ctx && strcmp(ctx->creds, creds)) {
  131. *min = STUB_GSS_INVALID_CREDS;
  132. return GSS_S_FAILURE;
  133. }
  134. output_token->length = 0;
  135. output_token->value = NULL;
  136. if(input_token && input_token->length) {
  137. if(!ctx) {
  138. *min = STUB_GSS_INVALID_CTX;
  139. return GSS_S_FAILURE;
  140. }
  141. /* Server response, either D (RA==) or C (Qw==) */
  142. if(((char *) input_token->value)[0] == 'D') {
  143. /* Done */
  144. switch(ctx->sent) {
  145. case STUB_GSS_KRB5:
  146. case STUB_GSS_NTLM3:
  147. if(ret_flags)
  148. *ret_flags = ctx->flags;
  149. if(time_rec)
  150. *time_rec = GSS_C_INDEFINITE;
  151. return GSS_S_COMPLETE;
  152. default:
  153. *min = STUB_GSS_SERVER_ERR;
  154. return GSS_S_FAILURE;
  155. }
  156. }
  157. if(((char *) input_token->value)[0] != 'C') {
  158. /* We only support Done or Continue */
  159. *min = STUB_GSS_SERVER_ERR;
  160. return GSS_S_FAILURE;
  161. }
  162. /* Continue */
  163. switch(ctx->sent) {
  164. case STUB_GSS_KRB5:
  165. /* We sent KRB5 and it failed, let's try NTLM */
  166. if(ctx->have_ntlm) {
  167. ctx->sent = STUB_GSS_NTLM1;
  168. break;
  169. }
  170. else {
  171. *min = STUB_GSS_SERVER_ERR;
  172. return GSS_S_FAILURE;
  173. }
  174. case STUB_GSS_NTLM1:
  175. ctx->sent = STUB_GSS_NTLM3;
  176. break;
  177. default:
  178. *min = STUB_GSS_SERVER_ERR;
  179. return GSS_S_FAILURE;
  180. }
  181. }
  182. else {
  183. if(ctx) {
  184. *min = STUB_GSS_INVALID_CTX;
  185. return GSS_S_FAILURE;
  186. }
  187. ctx = calloc(1, sizeof(*ctx));
  188. if(!ctx) {
  189. *min = STUB_GSS_NO_MEMORY;
  190. return GSS_S_FAILURE;
  191. }
  192. if(strstr(creds, "KRB5"))
  193. ctx->have_krb5 = 1;
  194. if(strstr(creds, "NTLM"))
  195. ctx->have_ntlm = 1;
  196. if(ctx->have_krb5)
  197. ctx->sent = STUB_GSS_KRB5;
  198. else if(ctx->have_ntlm)
  199. ctx->sent = STUB_GSS_NTLM1;
  200. else {
  201. free(ctx);
  202. *min = STUB_GSS_NO_MECH;
  203. return GSS_S_FAILURE;
  204. }
  205. strcpy(ctx->creds, creds);
  206. ctx->flags = req_flags;
  207. }
  208. token = Curl_gss_alloc(length);
  209. if(!token) {
  210. free(ctx);
  211. *min = STUB_GSS_NO_MEMORY;
  212. return GSS_S_FAILURE;
  213. }
  214. {
  215. gss_buffer_desc target_desc;
  216. gss_OID name_type = GSS_C_NO_OID;
  217. OM_uint32 minor_status;
  218. OM_uint32 major_status;
  219. major_status = gss_display_name(&minor_status, target_name,
  220. &target_desc, &name_type);
  221. if(GSS_ERROR(major_status)) {
  222. Curl_gss_free(token);
  223. free(ctx);
  224. *min = STUB_GSS_NO_MEMORY;
  225. return GSS_S_FAILURE;
  226. }
  227. if(strlen(creds) + target_desc.length + 5 >= sizeof(ctx->creds)) {
  228. Curl_gss_free(token);
  229. free(ctx);
  230. *min = STUB_GSS_NO_MEMORY;
  231. return GSS_S_FAILURE;
  232. }
  233. /* Token format: creds:target:type:padding */
  234. used = curl_msnprintf(token, length, "%s:%.*s:%d:", creds,
  235. (int)target_desc.length,
  236. (const char *)target_desc.value,
  237. ctx->sent);
  238. gss_release_buffer(&minor_status, &target_desc);
  239. }
  240. if(used >= length) {
  241. Curl_gss_free(token);
  242. free(ctx);
  243. *min = STUB_GSS_NO_MEMORY;
  244. return GSS_S_FAILURE;
  245. }
  246. /* Overwrite null-terminator */
  247. memset(token + used, 'A', length - used);
  248. *context = ctx;
  249. output_token->value = token;
  250. output_token->length = length;
  251. return GSS_S_CONTINUE_NEEDED;
  252. }
  253. static OM_uint32
  254. stub_gss_delete_sec_context(OM_uint32 *min,
  255. struct stub_gss_ctx_id_t_desc **context,
  256. gss_buffer_t output_token)
  257. {
  258. (void)output_token;
  259. if(!min)
  260. return GSS_S_FAILURE;
  261. if(!context) {
  262. *min = STUB_GSS_INVALID_CTX;
  263. return GSS_S_FAILURE;
  264. }
  265. if(!*context) {
  266. *min = STUB_GSS_INVALID_CTX;
  267. return GSS_S_FAILURE;
  268. }
  269. free(*context);
  270. *context = NULL;
  271. *min = 0;
  272. return GSS_S_COMPLETE;
  273. }
  274. #endif /* CURL_GSS_STUB */
  275. OM_uint32 Curl_gss_init_sec_context(struct Curl_easy *data,
  276. OM_uint32 *minor_status,
  277. gss_ctx_id_t *context,
  278. gss_name_t target_name,
  279. gss_OID mech_type,
  280. gss_channel_bindings_t input_chan_bindings,
  281. gss_buffer_t input_token,
  282. gss_buffer_t output_token,
  283. const bool mutual_auth,
  284. OM_uint32 *ret_flags)
  285. {
  286. OM_uint32 req_flags = GSS_C_REPLAY_FLAG;
  287. if(mutual_auth)
  288. req_flags |= GSS_C_MUTUAL_FLAG;
  289. if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_POLICY_FLAG) {
  290. #ifdef GSS_C_DELEG_POLICY_FLAG /* MIT Kerberos 1.8+, missing from GNU GSS */
  291. req_flags |= GSS_C_DELEG_POLICY_FLAG;
  292. #else
  293. infof(data, "WARNING: support for CURLGSSAPI_DELEGATION_POLICY_FLAG not "
  294. "compiled in");
  295. #endif
  296. }
  297. if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_FLAG)
  298. req_flags |= GSS_C_DELEG_FLAG;
  299. #ifdef CURL_GSS_STUB
  300. if(getenv("CURL_STUB_GSS_CREDS"))
  301. return stub_gss_init_sec_context(minor_status,
  302. GSS_C_NO_CREDENTIAL, /* cred_handle */
  303. (struct stub_gss_ctx_id_t_desc **)context,
  304. target_name,
  305. mech_type,
  306. req_flags,
  307. 0, /* time_req */
  308. input_chan_bindings,
  309. input_token,
  310. NULL, /* actual_mech_type */
  311. output_token,
  312. ret_flags,
  313. NULL /* time_rec */);
  314. #endif /* CURL_GSS_STUB */
  315. return gss_init_sec_context(minor_status,
  316. GSS_C_NO_CREDENTIAL, /* cred_handle */
  317. context,
  318. target_name,
  319. mech_type,
  320. req_flags,
  321. 0, /* time_req */
  322. input_chan_bindings,
  323. input_token,
  324. NULL, /* actual_mech_type */
  325. output_token,
  326. ret_flags,
  327. NULL /* time_rec */);
  328. }
  329. OM_uint32 Curl_gss_delete_sec_context(OM_uint32 *min,
  330. gss_ctx_id_t *context,
  331. gss_buffer_t output_token)
  332. {
  333. #ifdef CURL_GSS_STUB
  334. if(getenv("CURL_STUB_GSS_CREDS"))
  335. return stub_gss_delete_sec_context(min,
  336. (struct stub_gss_ctx_id_t_desc **)context,
  337. output_token);
  338. #endif /* CURL_GSS_STUB */
  339. return gss_delete_sec_context(min, context, output_token);
  340. }
  341. #define GSS_LOG_BUFFER_LEN 1024
  342. static size_t display_gss_error(OM_uint32 status, int type,
  343. char *buf, size_t len) {
  344. OM_uint32 maj_stat;
  345. OM_uint32 min_stat;
  346. OM_uint32 msg_ctx = 0;
  347. gss_buffer_desc status_string = GSS_C_EMPTY_BUFFER;
  348. do {
  349. maj_stat = gss_display_status(&min_stat,
  350. status,
  351. type,
  352. GSS_C_NO_OID,
  353. &msg_ctx,
  354. &status_string);
  355. if(maj_stat == GSS_S_COMPLETE && status_string.length > 0) {
  356. if(GSS_LOG_BUFFER_LEN > len + status_string.length + 3) {
  357. len += curl_msnprintf(buf + len, GSS_LOG_BUFFER_LEN - len,
  358. "%.*s. ", (int)status_string.length,
  359. (char *)status_string.value);
  360. }
  361. }
  362. gss_release_buffer(&min_stat, &status_string);
  363. } while(!GSS_ERROR(maj_stat) && msg_ctx);
  364. return len;
  365. }
  366. /*
  367. * Curl_gss_log_error()
  368. *
  369. * This is used to log a GSS-API error status.
  370. *
  371. * Parameters:
  372. *
  373. * data [in] - The session handle.
  374. * prefix [in] - The prefix of the log message.
  375. * major [in] - The major status code.
  376. * minor [in] - The minor status code.
  377. */
  378. void Curl_gss_log_error(struct Curl_easy *data, const char *prefix,
  379. OM_uint32 major, OM_uint32 minor)
  380. {
  381. char buf[GSS_LOG_BUFFER_LEN];
  382. size_t len = 0;
  383. if(major != GSS_S_FAILURE)
  384. len = display_gss_error(major, GSS_C_GSS_CODE, buf, len);
  385. display_gss_error(minor, GSS_C_MECH_CODE, buf, len);
  386. infof(data, "%s%s", prefix, buf);
  387. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  388. (void)data;
  389. (void)prefix;
  390. #endif
  391. }
  392. #if defined(__GNUC__) && defined(__APPLE__)
  393. #pragma GCC diagnostic pop
  394. #endif
  395. #endif /* HAVE_GSSAPI */