socks_gssapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) 2012, Markus Moeller, <markus_moeller@compuserve.com>
  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. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_PROXY)
  27. #include "curl_gssapi.h"
  28. #include "urldata.h"
  29. #include "sendf.h"
  30. #include "connect.h"
  31. #include "timeval.h"
  32. #include "socks.h"
  33. #include "warnless.h"
  34. /* The last 3 #include files should be in this order */
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. static gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
  39. /*
  40. * Helper GSS-API error functions.
  41. */
  42. static int check_gss_err(struct Curl_easy *data,
  43. OM_uint32 major_status,
  44. OM_uint32 minor_status,
  45. const char *function)
  46. {
  47. if(GSS_ERROR(major_status)) {
  48. OM_uint32 maj_stat, min_stat;
  49. OM_uint32 msg_ctx = 0;
  50. gss_buffer_desc status_string = GSS_C_EMPTY_BUFFER;
  51. char buf[1024];
  52. size_t len;
  53. len = 0;
  54. msg_ctx = 0;
  55. while(!msg_ctx) {
  56. /* convert major status code (GSS-API error) to text */
  57. maj_stat = gss_display_status(&min_stat, major_status,
  58. GSS_C_GSS_CODE,
  59. GSS_C_NULL_OID,
  60. &msg_ctx, &status_string);
  61. if(maj_stat == GSS_S_COMPLETE) {
  62. if(sizeof(buf) > len + status_string.length + 1) {
  63. strcpy(buf + len, (char *) status_string.value);
  64. len += status_string.length;
  65. }
  66. gss_release_buffer(&min_stat, &status_string);
  67. break;
  68. }
  69. gss_release_buffer(&min_stat, &status_string);
  70. }
  71. if(sizeof(buf) > len + 3) {
  72. strcpy(buf + len, ".\n");
  73. len += 2;
  74. }
  75. msg_ctx = 0;
  76. while(!msg_ctx) {
  77. /* convert minor status code (underlying routine error) to text */
  78. maj_stat = gss_display_status(&min_stat, minor_status,
  79. GSS_C_MECH_CODE,
  80. GSS_C_NULL_OID,
  81. &msg_ctx, &status_string);
  82. if(maj_stat == GSS_S_COMPLETE) {
  83. if(sizeof(buf) > len + status_string.length)
  84. strcpy(buf + len, (char *) status_string.value);
  85. gss_release_buffer(&min_stat, &status_string);
  86. break;
  87. }
  88. gss_release_buffer(&min_stat, &status_string);
  89. }
  90. failf(data, "GSS-API error: %s failed: %s", function, buf);
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
  96. struct Curl_easy *data)
  97. {
  98. struct connectdata *conn = data->conn;
  99. curl_socket_t sock = conn->sock[sockindex];
  100. CURLcode code;
  101. ssize_t actualread;
  102. ssize_t written;
  103. int result;
  104. OM_uint32 gss_major_status, gss_minor_status, gss_status;
  105. OM_uint32 gss_ret_flags;
  106. int gss_conf_state, gss_enc;
  107. gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
  108. gss_buffer_desc gss_send_token = GSS_C_EMPTY_BUFFER;
  109. gss_buffer_desc gss_recv_token = GSS_C_EMPTY_BUFFER;
  110. gss_buffer_desc gss_w_token = GSS_C_EMPTY_BUFFER;
  111. gss_buffer_desc *gss_token = GSS_C_NO_BUFFER;
  112. gss_name_t server = GSS_C_NO_NAME;
  113. gss_name_t gss_client_name = GSS_C_NO_NAME;
  114. unsigned short us_length;
  115. char *user = NULL;
  116. unsigned char socksreq[4]; /* room for GSS-API exchange header only */
  117. const char *serviceptr = data->set.str[STRING_PROXY_SERVICE_NAME] ?
  118. data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
  119. const size_t serviceptr_length = strlen(serviceptr);
  120. /* GSS-API request looks like
  121. * +----+------+-----+----------------+
  122. * |VER | MTYP | LEN | TOKEN |
  123. * +----+------+----------------------+
  124. * | 1 | 1 | 2 | up to 2^16 - 1 |
  125. * +----+------+-----+----------------+
  126. */
  127. /* prepare service name */
  128. if(strchr(serviceptr, '/')) {
  129. service.length = serviceptr_length;
  130. service.value = malloc(service.length);
  131. if(!service.value)
  132. return CURLE_OUT_OF_MEMORY;
  133. memcpy(service.value, serviceptr, service.length);
  134. gss_major_status = gss_import_name(&gss_minor_status, &service,
  135. (gss_OID) GSS_C_NULL_OID, &server);
  136. }
  137. else {
  138. service.value = malloc(serviceptr_length +
  139. strlen(conn->socks_proxy.host.name) + 2);
  140. if(!service.value)
  141. return CURLE_OUT_OF_MEMORY;
  142. service.length = serviceptr_length +
  143. strlen(conn->socks_proxy.host.name) + 1;
  144. msnprintf(service.value, service.length + 1, "%s@%s",
  145. serviceptr, conn->socks_proxy.host.name);
  146. gss_major_status = gss_import_name(&gss_minor_status, &service,
  147. GSS_C_NT_HOSTBASED_SERVICE, &server);
  148. }
  149. gss_release_buffer(&gss_status, &service); /* clear allocated memory */
  150. if(check_gss_err(data, gss_major_status,
  151. gss_minor_status, "gss_import_name()")) {
  152. failf(data, "Failed to create service name.");
  153. gss_release_name(&gss_status, &server);
  154. return CURLE_COULDNT_CONNECT;
  155. }
  156. (void)curlx_nonblock(sock, FALSE);
  157. /* As long as we need to keep sending some context info, and there's no */
  158. /* errors, keep sending it... */
  159. for(;;) {
  160. gss_major_status = Curl_gss_init_sec_context(data,
  161. &gss_minor_status,
  162. &gss_context,
  163. server,
  164. &Curl_krb5_mech_oid,
  165. NULL,
  166. gss_token,
  167. &gss_send_token,
  168. TRUE,
  169. &gss_ret_flags);
  170. if(gss_token != GSS_C_NO_BUFFER)
  171. gss_release_buffer(&gss_status, &gss_recv_token);
  172. if(check_gss_err(data, gss_major_status,
  173. gss_minor_status, "gss_init_sec_context")) {
  174. gss_release_name(&gss_status, &server);
  175. gss_release_buffer(&gss_status, &gss_recv_token);
  176. gss_release_buffer(&gss_status, &gss_send_token);
  177. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  178. failf(data, "Failed to initial GSS-API token.");
  179. return CURLE_COULDNT_CONNECT;
  180. }
  181. if(gss_send_token.length) {
  182. socksreq[0] = 1; /* GSS-API subnegotiation version */
  183. socksreq[1] = 1; /* authentication message type */
  184. us_length = htons((short)gss_send_token.length);
  185. memcpy(socksreq + 2, &us_length, sizeof(short));
  186. code = Curl_write_plain(data, sock, (char *)socksreq, 4, &written);
  187. if(code || (4 != written)) {
  188. failf(data, "Failed to send GSS-API authentication request.");
  189. gss_release_name(&gss_status, &server);
  190. gss_release_buffer(&gss_status, &gss_recv_token);
  191. gss_release_buffer(&gss_status, &gss_send_token);
  192. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  193. return CURLE_COULDNT_CONNECT;
  194. }
  195. code = Curl_write_plain(data, sock, (char *)gss_send_token.value,
  196. gss_send_token.length, &written);
  197. if(code || ((ssize_t)gss_send_token.length != written)) {
  198. failf(data, "Failed to send GSS-API authentication token.");
  199. gss_release_name(&gss_status, &server);
  200. gss_release_buffer(&gss_status, &gss_recv_token);
  201. gss_release_buffer(&gss_status, &gss_send_token);
  202. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  203. return CURLE_COULDNT_CONNECT;
  204. }
  205. }
  206. gss_release_buffer(&gss_status, &gss_send_token);
  207. gss_release_buffer(&gss_status, &gss_recv_token);
  208. if(gss_major_status != GSS_S_CONTINUE_NEEDED)
  209. break;
  210. /* analyse response */
  211. /* GSS-API response looks like
  212. * +----+------+-----+----------------+
  213. * |VER | MTYP | LEN | TOKEN |
  214. * +----+------+----------------------+
  215. * | 1 | 1 | 2 | up to 2^16 - 1 |
  216. * +----+------+-----+----------------+
  217. */
  218. result = Curl_blockread_all(data, sock, (char *)socksreq, 4, &actualread);
  219. if(result || (actualread != 4)) {
  220. failf(data, "Failed to receive GSS-API authentication response.");
  221. gss_release_name(&gss_status, &server);
  222. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  223. return CURLE_COULDNT_CONNECT;
  224. }
  225. /* ignore the first (VER) byte */
  226. if(socksreq[1] == 255) { /* status / message type */
  227. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  228. socksreq[0], socksreq[1]);
  229. gss_release_name(&gss_status, &server);
  230. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  231. return CURLE_COULDNT_CONNECT;
  232. }
  233. if(socksreq[1] != 1) { /* status / message type */
  234. failf(data, "Invalid GSS-API authentication response type (%d %d).",
  235. socksreq[0], socksreq[1]);
  236. gss_release_name(&gss_status, &server);
  237. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  238. return CURLE_COULDNT_CONNECT;
  239. }
  240. memcpy(&us_length, socksreq + 2, sizeof(short));
  241. us_length = ntohs(us_length);
  242. gss_recv_token.length = us_length;
  243. gss_recv_token.value = malloc(us_length);
  244. if(!gss_recv_token.value) {
  245. failf(data,
  246. "Could not allocate memory for GSS-API authentication "
  247. "response token.");
  248. gss_release_name(&gss_status, &server);
  249. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  250. return CURLE_OUT_OF_MEMORY;
  251. }
  252. result = Curl_blockread_all(data, sock, (char *)gss_recv_token.value,
  253. gss_recv_token.length, &actualread);
  254. if(result || (actualread != us_length)) {
  255. failf(data, "Failed to receive GSS-API authentication token.");
  256. gss_release_name(&gss_status, &server);
  257. gss_release_buffer(&gss_status, &gss_recv_token);
  258. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  259. return CURLE_COULDNT_CONNECT;
  260. }
  261. gss_token = &gss_recv_token;
  262. }
  263. gss_release_name(&gss_status, &server);
  264. /* Everything is good so far, user was authenticated! */
  265. gss_major_status = gss_inquire_context(&gss_minor_status, gss_context,
  266. &gss_client_name, NULL, NULL, NULL,
  267. NULL, NULL, NULL);
  268. if(check_gss_err(data, gss_major_status,
  269. gss_minor_status, "gss_inquire_context")) {
  270. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  271. gss_release_name(&gss_status, &gss_client_name);
  272. failf(data, "Failed to determine user name.");
  273. return CURLE_COULDNT_CONNECT;
  274. }
  275. gss_major_status = gss_display_name(&gss_minor_status, gss_client_name,
  276. &gss_send_token, NULL);
  277. if(check_gss_err(data, gss_major_status,
  278. gss_minor_status, "gss_display_name")) {
  279. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  280. gss_release_name(&gss_status, &gss_client_name);
  281. gss_release_buffer(&gss_status, &gss_send_token);
  282. failf(data, "Failed to determine user name.");
  283. return CURLE_COULDNT_CONNECT;
  284. }
  285. user = malloc(gss_send_token.length + 1);
  286. if(!user) {
  287. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  288. gss_release_name(&gss_status, &gss_client_name);
  289. gss_release_buffer(&gss_status, &gss_send_token);
  290. return CURLE_OUT_OF_MEMORY;
  291. }
  292. memcpy(user, gss_send_token.value, gss_send_token.length);
  293. user[gss_send_token.length] = '\0';
  294. gss_release_name(&gss_status, &gss_client_name);
  295. gss_release_buffer(&gss_status, &gss_send_token);
  296. infof(data, "SOCKS5 server authenticated user %s with GSS-API.",user);
  297. free(user);
  298. user = NULL;
  299. /* Do encryption */
  300. socksreq[0] = 1; /* GSS-API subnegotiation version */
  301. socksreq[1] = 2; /* encryption message type */
  302. gss_enc = 0; /* no data protection */
  303. /* do confidentiality protection if supported */
  304. if(gss_ret_flags & GSS_C_CONF_FLAG)
  305. gss_enc = 2;
  306. /* else do integrity protection */
  307. else if(gss_ret_flags & GSS_C_INTEG_FLAG)
  308. gss_enc = 1;
  309. infof(data, "SOCKS5 server supports GSS-API %s data protection.",
  310. (gss_enc == 0)?"no":((gss_enc==1)?"integrity":"confidentiality"));
  311. /* force for the moment to no data protection */
  312. gss_enc = 0;
  313. /*
  314. * Sending the encryption type in clear seems wrong. It should be
  315. * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
  316. * The NEC reference implementations on which this is based is
  317. * therefore at fault
  318. *
  319. * +------+------+------+.......................+
  320. * + ver | mtyp | len | token |
  321. * +------+------+------+.......................+
  322. * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
  323. * +------+------+------+.......................+
  324. *
  325. * Where:
  326. *
  327. * - "ver" is the protocol version number, here 1 to represent the
  328. * first version of the SOCKS/GSS-API protocol
  329. *
  330. * - "mtyp" is the message type, here 2 to represent a protection
  331. * -level negotiation message
  332. *
  333. * - "len" is the length of the "token" field in octets
  334. *
  335. * - "token" is the GSS-API encapsulated protection level
  336. *
  337. * The token is produced by encapsulating an octet containing the
  338. * required protection level using gss_seal()/gss_wrap() with conf_req
  339. * set to FALSE. The token is verified using gss_unseal()/
  340. * gss_unwrap().
  341. *
  342. */
  343. if(data->set.socks5_gssapi_nec) {
  344. us_length = htons((short)1);
  345. memcpy(socksreq + 2, &us_length, sizeof(short));
  346. }
  347. else {
  348. gss_send_token.length = 1;
  349. gss_send_token.value = malloc(1);
  350. if(!gss_send_token.value) {
  351. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  352. return CURLE_OUT_OF_MEMORY;
  353. }
  354. memcpy(gss_send_token.value, &gss_enc, 1);
  355. gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0,
  356. GSS_C_QOP_DEFAULT, &gss_send_token,
  357. &gss_conf_state, &gss_w_token);
  358. if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) {
  359. gss_release_buffer(&gss_status, &gss_send_token);
  360. gss_release_buffer(&gss_status, &gss_w_token);
  361. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  362. failf(data, "Failed to wrap GSS-API encryption value into token.");
  363. return CURLE_COULDNT_CONNECT;
  364. }
  365. gss_release_buffer(&gss_status, &gss_send_token);
  366. us_length = htons((short)gss_w_token.length);
  367. memcpy(socksreq + 2, &us_length, sizeof(short));
  368. }
  369. code = Curl_write_plain(data, sock, (char *)socksreq, 4, &written);
  370. if(code || (4 != written)) {
  371. failf(data, "Failed to send GSS-API encryption request.");
  372. gss_release_buffer(&gss_status, &gss_w_token);
  373. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  374. return CURLE_COULDNT_CONNECT;
  375. }
  376. if(data->set.socks5_gssapi_nec) {
  377. memcpy(socksreq, &gss_enc, 1);
  378. code = Curl_write_plain(data, sock, socksreq, 1, &written);
  379. if(code || ( 1 != written)) {
  380. failf(data, "Failed to send GSS-API encryption type.");
  381. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  382. return CURLE_COULDNT_CONNECT;
  383. }
  384. }
  385. else {
  386. code = Curl_write_plain(data, sock, (char *)gss_w_token.value,
  387. gss_w_token.length, &written);
  388. if(code || ((ssize_t)gss_w_token.length != written)) {
  389. failf(data, "Failed to send GSS-API encryption type.");
  390. gss_release_buffer(&gss_status, &gss_w_token);
  391. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  392. return CURLE_COULDNT_CONNECT;
  393. }
  394. gss_release_buffer(&gss_status, &gss_w_token);
  395. }
  396. result = Curl_blockread_all(data, sock, (char *)socksreq, 4, &actualread);
  397. if(result || (actualread != 4)) {
  398. failf(data, "Failed to receive GSS-API encryption response.");
  399. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  400. return CURLE_COULDNT_CONNECT;
  401. }
  402. /* ignore the first (VER) byte */
  403. if(socksreq[1] == 255) { /* status / message type */
  404. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  405. socksreq[0], socksreq[1]);
  406. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  407. return CURLE_COULDNT_CONNECT;
  408. }
  409. if(socksreq[1] != 2) { /* status / message type */
  410. failf(data, "Invalid GSS-API encryption response type (%d %d).",
  411. socksreq[0], socksreq[1]);
  412. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  413. return CURLE_COULDNT_CONNECT;
  414. }
  415. memcpy(&us_length, socksreq + 2, sizeof(short));
  416. us_length = ntohs(us_length);
  417. gss_recv_token.length = us_length;
  418. gss_recv_token.value = malloc(gss_recv_token.length);
  419. if(!gss_recv_token.value) {
  420. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  421. return CURLE_OUT_OF_MEMORY;
  422. }
  423. result = Curl_blockread_all(data, sock, (char *)gss_recv_token.value,
  424. gss_recv_token.length, &actualread);
  425. if(result || (actualread != us_length)) {
  426. failf(data, "Failed to receive GSS-API encryptrion type.");
  427. gss_release_buffer(&gss_status, &gss_recv_token);
  428. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  429. return CURLE_COULDNT_CONNECT;
  430. }
  431. if(!data->set.socks5_gssapi_nec) {
  432. gss_major_status = gss_unwrap(&gss_minor_status, gss_context,
  433. &gss_recv_token, &gss_w_token,
  434. 0, GSS_C_QOP_DEFAULT);
  435. if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) {
  436. gss_release_buffer(&gss_status, &gss_recv_token);
  437. gss_release_buffer(&gss_status, &gss_w_token);
  438. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  439. failf(data, "Failed to unwrap GSS-API encryption value into token.");
  440. return CURLE_COULDNT_CONNECT;
  441. }
  442. gss_release_buffer(&gss_status, &gss_recv_token);
  443. if(gss_w_token.length != 1) {
  444. failf(data, "Invalid GSS-API encryption response length (%zu).",
  445. gss_w_token.length);
  446. gss_release_buffer(&gss_status, &gss_w_token);
  447. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  448. return CURLE_COULDNT_CONNECT;
  449. }
  450. memcpy(socksreq, gss_w_token.value, gss_w_token.length);
  451. gss_release_buffer(&gss_status, &gss_w_token);
  452. }
  453. else {
  454. if(gss_recv_token.length != 1) {
  455. failf(data, "Invalid GSS-API encryption response length (%zu).",
  456. gss_recv_token.length);
  457. gss_release_buffer(&gss_status, &gss_recv_token);
  458. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  459. return CURLE_COULDNT_CONNECT;
  460. }
  461. memcpy(socksreq, gss_recv_token.value, gss_recv_token.length);
  462. gss_release_buffer(&gss_status, &gss_recv_token);
  463. }
  464. (void)curlx_nonblock(sock, TRUE);
  465. infof(data, "SOCKS5 access with%s protection granted.",
  466. (socksreq[0] == 0)?"out GSS-API data":
  467. ((socksreq[0] == 1)?" GSS-API integrity":" GSS-API confidentiality"));
  468. conn->socks5_gssapi_enctype = socksreq[0];
  469. if(socksreq[0] == 0)
  470. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  471. return CURLE_OK;
  472. }
  473. #endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */