socks_gssapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2009, 2011, Markus Moeller, <markus_moeller@compuserve.com>
  9. * Copyright (C) 2012 - 2016, 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 + strlen(conn->socks_proxy.host.name)+1;
  141. snprintf(service.value, service.length+1, "%s@%s",
  142. serviceptr, conn->socks_proxy.host.name);
  143. gss_major_status = gss_import_name(&gss_minor_status, &service,
  144. GSS_C_NT_HOSTBASED_SERVICE, &server);
  145. }
  146. gss_release_buffer(&gss_status, &service); /* clear allocated memory */
  147. if(check_gss_err(data, gss_major_status,
  148. gss_minor_status, "gss_import_name()")) {
  149. failf(data, "Failed to create service name.");
  150. gss_release_name(&gss_status, &server);
  151. return CURLE_COULDNT_CONNECT;
  152. }
  153. /* As long as we need to keep sending some context info, and there's no */
  154. /* errors, keep sending it... */
  155. for(;;) {
  156. gss_major_status = Curl_gss_init_sec_context(data,
  157. &gss_minor_status,
  158. &gss_context,
  159. server,
  160. &Curl_krb5_mech_oid,
  161. NULL,
  162. gss_token,
  163. &gss_send_token,
  164. TRUE,
  165. &gss_ret_flags);
  166. if(gss_token != GSS_C_NO_BUFFER)
  167. gss_release_buffer(&gss_status, &gss_recv_token);
  168. if(check_gss_err(data, gss_major_status,
  169. gss_minor_status, "gss_init_sec_context")) {
  170. gss_release_name(&gss_status, &server);
  171. gss_release_buffer(&gss_status, &gss_recv_token);
  172. gss_release_buffer(&gss_status, &gss_send_token);
  173. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  174. failf(data, "Failed to initial GSS-API token.");
  175. return CURLE_COULDNT_CONNECT;
  176. }
  177. if(gss_send_token.length != 0) {
  178. socksreq[0] = 1; /* GSS-API subnegotiation version */
  179. socksreq[1] = 1; /* authentication message type */
  180. us_length = htons((short)gss_send_token.length);
  181. memcpy(socksreq+2, &us_length, sizeof(short));
  182. code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
  183. if(code || (4 != written)) {
  184. failf(data, "Failed to send GSS-API authentication request.");
  185. gss_release_name(&gss_status, &server);
  186. gss_release_buffer(&gss_status, &gss_recv_token);
  187. gss_release_buffer(&gss_status, &gss_send_token);
  188. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  189. return CURLE_COULDNT_CONNECT;
  190. }
  191. code = Curl_write_plain(conn, sock, (char *)gss_send_token.value,
  192. gss_send_token.length, &written);
  193. if(code || ((ssize_t)gss_send_token.length != written)) {
  194. failf(data, "Failed to send GSS-API authentication token.");
  195. gss_release_name(&gss_status, &server);
  196. gss_release_buffer(&gss_status, &gss_recv_token);
  197. gss_release_buffer(&gss_status, &gss_send_token);
  198. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  199. return CURLE_COULDNT_CONNECT;
  200. }
  201. }
  202. gss_release_buffer(&gss_status, &gss_send_token);
  203. gss_release_buffer(&gss_status, &gss_recv_token);
  204. if(gss_major_status != GSS_S_CONTINUE_NEEDED) break;
  205. /* analyse response */
  206. /* GSS-API response looks like
  207. * +----+------+-----+----------------+
  208. * |VER | MTYP | LEN | TOKEN |
  209. * +----+------+----------------------+
  210. * | 1 | 1 | 2 | up to 2^16 - 1 |
  211. * +----+------+-----+----------------+
  212. */
  213. result=Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
  214. if(result || (actualread != 4)) {
  215. failf(data, "Failed to receive GSS-API authentication response.");
  216. gss_release_name(&gss_status, &server);
  217. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  218. return CURLE_COULDNT_CONNECT;
  219. }
  220. /* ignore the first (VER) byte */
  221. if(socksreq[1] == 255) { /* status / message type */
  222. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  223. socksreq[0], socksreq[1]);
  224. gss_release_name(&gss_status, &server);
  225. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  226. return CURLE_COULDNT_CONNECT;
  227. }
  228. if(socksreq[1] != 1) { /* status / messgae type */
  229. failf(data, "Invalid GSS-API authentication response type (%d %d).",
  230. socksreq[0], socksreq[1]);
  231. gss_release_name(&gss_status, &server);
  232. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  233. return CURLE_COULDNT_CONNECT;
  234. }
  235. memcpy(&us_length, socksreq+2, sizeof(short));
  236. us_length = ntohs(us_length);
  237. gss_recv_token.length=us_length;
  238. gss_recv_token.value=malloc(us_length);
  239. if(!gss_recv_token.value) {
  240. failf(data,
  241. "Could not allocate memory for GSS-API authentication "
  242. "response token.");
  243. gss_release_name(&gss_status, &server);
  244. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  245. return CURLE_OUT_OF_MEMORY;
  246. }
  247. result=Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
  248. gss_recv_token.length, &actualread);
  249. if(result || (actualread != us_length)) {
  250. failf(data, "Failed to receive GSS-API authentication token.");
  251. gss_release_name(&gss_status, &server);
  252. gss_release_buffer(&gss_status, &gss_recv_token);
  253. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  254. return CURLE_COULDNT_CONNECT;
  255. }
  256. gss_token = &gss_recv_token;
  257. }
  258. gss_release_name(&gss_status, &server);
  259. /* Everything is good so far, user was authenticated! */
  260. gss_major_status = gss_inquire_context(&gss_minor_status, gss_context,
  261. &gss_client_name, NULL, NULL, NULL,
  262. NULL, NULL, NULL);
  263. if(check_gss_err(data, gss_major_status,
  264. gss_minor_status, "gss_inquire_context")) {
  265. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  266. gss_release_name(&gss_status, &gss_client_name);
  267. failf(data, "Failed to determine user name.");
  268. return CURLE_COULDNT_CONNECT;
  269. }
  270. gss_major_status = gss_display_name(&gss_minor_status, gss_client_name,
  271. &gss_send_token, NULL);
  272. if(check_gss_err(data, gss_major_status,
  273. gss_minor_status, "gss_display_name")) {
  274. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  275. gss_release_name(&gss_status, &gss_client_name);
  276. gss_release_buffer(&gss_status, &gss_send_token);
  277. failf(data, "Failed to determine user name.");
  278. return CURLE_COULDNT_CONNECT;
  279. }
  280. user=malloc(gss_send_token.length+1);
  281. if(!user) {
  282. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  283. gss_release_name(&gss_status, &gss_client_name);
  284. gss_release_buffer(&gss_status, &gss_send_token);
  285. return CURLE_OUT_OF_MEMORY;
  286. }
  287. memcpy(user, gss_send_token.value, gss_send_token.length);
  288. user[gss_send_token.length] = '\0';
  289. gss_release_name(&gss_status, &gss_client_name);
  290. gss_release_buffer(&gss_status, &gss_send_token);
  291. infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",user);
  292. free(user);
  293. user=NULL;
  294. /* Do encryption */
  295. socksreq[0] = 1; /* GSS-API subnegotiation version */
  296. socksreq[1] = 2; /* encryption message type */
  297. gss_enc = 0; /* no data protection */
  298. /* do confidentiality protection if supported */
  299. if(gss_ret_flags & GSS_C_CONF_FLAG)
  300. gss_enc = 2;
  301. /* else do integrity protection */
  302. else if(gss_ret_flags & GSS_C_INTEG_FLAG)
  303. gss_enc = 1;
  304. infof(data, "SOCKS5 server supports GSS-API %s data protection.\n",
  305. (gss_enc==0)?"no":((gss_enc==1)?"integrity":"confidentiality"));
  306. /* force for the moment to no data protection */
  307. gss_enc = 0;
  308. /*
  309. * Sending the encryption type in clear seems wrong. It should be
  310. * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
  311. * The NEC reference implementations on which this is based is
  312. * therefore at fault
  313. *
  314. * +------+------+------+.......................+
  315. * + ver | mtyp | len | token |
  316. * +------+------+------+.......................+
  317. * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
  318. * +------+------+------+.......................+
  319. *
  320. * Where:
  321. *
  322. * - "ver" is the protocol version number, here 1 to represent the
  323. * first version of the SOCKS/GSS-API protocol
  324. *
  325. * - "mtyp" is the message type, here 2 to represent a protection
  326. * -level negotiation message
  327. *
  328. * - "len" is the length of the "token" field in octets
  329. *
  330. * - "token" is the GSS-API encapsulated protection level
  331. *
  332. * The token is produced by encapsulating an octet containing the
  333. * required protection level using gss_seal()/gss_wrap() with conf_req
  334. * set to FALSE. The token is verified using gss_unseal()/
  335. * gss_unwrap().
  336. *
  337. */
  338. if(data->set.socks5_gssapi_nec) {
  339. us_length = htons((short)1);
  340. memcpy(socksreq+2, &us_length, sizeof(short));
  341. }
  342. else {
  343. gss_send_token.length = 1;
  344. gss_send_token.value = malloc(1);
  345. if(!gss_send_token.value) {
  346. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  347. return CURLE_OUT_OF_MEMORY;
  348. }
  349. memcpy(gss_send_token.value, &gss_enc, 1);
  350. gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0,
  351. GSS_C_QOP_DEFAULT, &gss_send_token,
  352. &gss_conf_state, &gss_w_token);
  353. if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) {
  354. gss_release_buffer(&gss_status, &gss_send_token);
  355. gss_release_buffer(&gss_status, &gss_w_token);
  356. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  357. failf(data, "Failed to wrap GSS-API encryption value into token.");
  358. return CURLE_COULDNT_CONNECT;
  359. }
  360. gss_release_buffer(&gss_status, &gss_send_token);
  361. us_length = htons((short)gss_w_token.length);
  362. memcpy(socksreq+2, &us_length, sizeof(short));
  363. }
  364. code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
  365. if(code || (4 != written)) {
  366. failf(data, "Failed to send GSS-API encryption request.");
  367. gss_release_buffer(&gss_status, &gss_w_token);
  368. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  369. return CURLE_COULDNT_CONNECT;
  370. }
  371. if(data->set.socks5_gssapi_nec) {
  372. memcpy(socksreq, &gss_enc, 1);
  373. code = Curl_write_plain(conn, sock, socksreq, 1, &written);
  374. if(code || ( 1 != written)) {
  375. failf(data, "Failed to send GSS-API encryption type.");
  376. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  377. return CURLE_COULDNT_CONNECT;
  378. }
  379. }
  380. else {
  381. code = Curl_write_plain(conn, sock, (char *)gss_w_token.value,
  382. gss_w_token.length, &written);
  383. if(code || ((ssize_t)gss_w_token.length != written)) {
  384. failf(data, "Failed to send GSS-API encryption type.");
  385. gss_release_buffer(&gss_status, &gss_w_token);
  386. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  387. return CURLE_COULDNT_CONNECT;
  388. }
  389. gss_release_buffer(&gss_status, &gss_w_token);
  390. }
  391. result=Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
  392. if(result || (actualread != 4)) {
  393. failf(data, "Failed to receive GSS-API encryption response.");
  394. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  395. return CURLE_COULDNT_CONNECT;
  396. }
  397. /* ignore the first (VER) byte */
  398. if(socksreq[1] == 255) { /* status / message type */
  399. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  400. socksreq[0], socksreq[1]);
  401. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  402. return CURLE_COULDNT_CONNECT;
  403. }
  404. if(socksreq[1] != 2) { /* status / messgae type */
  405. failf(data, "Invalid GSS-API encryption response type (%d %d).",
  406. socksreq[0], socksreq[1]);
  407. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  408. return CURLE_COULDNT_CONNECT;
  409. }
  410. memcpy(&us_length, socksreq+2, sizeof(short));
  411. us_length = ntohs(us_length);
  412. gss_recv_token.length= us_length;
  413. gss_recv_token.value=malloc(gss_recv_token.length);
  414. if(!gss_recv_token.value) {
  415. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  416. return CURLE_OUT_OF_MEMORY;
  417. }
  418. result=Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
  419. gss_recv_token.length, &actualread);
  420. if(result || (actualread != us_length)) {
  421. failf(data, "Failed to receive GSS-API encryptrion type.");
  422. gss_release_buffer(&gss_status, &gss_recv_token);
  423. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  424. return CURLE_COULDNT_CONNECT;
  425. }
  426. if(!data->set.socks5_gssapi_nec) {
  427. gss_major_status = gss_unwrap(&gss_minor_status, gss_context,
  428. &gss_recv_token, &gss_w_token,
  429. 0, GSS_C_QOP_DEFAULT);
  430. if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) {
  431. gss_release_buffer(&gss_status, &gss_recv_token);
  432. gss_release_buffer(&gss_status, &gss_w_token);
  433. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  434. failf(data, "Failed to unwrap GSS-API encryption value into token.");
  435. return CURLE_COULDNT_CONNECT;
  436. }
  437. gss_release_buffer(&gss_status, &gss_recv_token);
  438. if(gss_w_token.length != 1) {
  439. failf(data, "Invalid GSS-API encryption response length (%d).",
  440. gss_w_token.length);
  441. gss_release_buffer(&gss_status, &gss_w_token);
  442. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  443. return CURLE_COULDNT_CONNECT;
  444. }
  445. memcpy(socksreq, gss_w_token.value, gss_w_token.length);
  446. gss_release_buffer(&gss_status, &gss_w_token);
  447. }
  448. else {
  449. if(gss_recv_token.length != 1) {
  450. failf(data, "Invalid GSS-API encryption response length (%d).",
  451. gss_recv_token.length);
  452. gss_release_buffer(&gss_status, &gss_recv_token);
  453. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  454. return CURLE_COULDNT_CONNECT;
  455. }
  456. memcpy(socksreq, gss_recv_token.value, gss_recv_token.length);
  457. gss_release_buffer(&gss_status, &gss_recv_token);
  458. }
  459. infof(data, "SOCKS5 access with%s protection granted.\n",
  460. (socksreq[0]==0)?"out GSS-API data":
  461. ((socksreq[0]==1)?" GSS-API integrity":" GSS-API confidentiality"));
  462. conn->socks5_gssapi_enctype = socksreq[0];
  463. if(socksreq[0] == 0)
  464. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  465. return CURLE_OK;
  466. }
  467. #endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */