socks_gssapi.c 20 KB

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