socks_gssapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2009, 2011, Markus Moeller, <markus_moeller@compuserve.com>
  9. * Copyright (C) 2012 - 2014, 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 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. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #ifndef CURL_DISABLE_PROXY
  25. #ifdef HAVE_GSSAPI
  26. #ifdef HAVE_OLD_GSSMIT
  27. #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
  28. #define NCOMPAT 1
  29. #endif
  30. #ifndef gss_nt_service_name
  31. #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
  32. #endif
  33. #include "curl_gssapi.h"
  34. #include "urldata.h"
  35. #include "sendf.h"
  36. #include "connect.h"
  37. #include "timeval.h"
  38. #include "socks.h"
  39. #include "warnless.h"
  40. #define _MPRINTF_REPLACE /* use our functions only */
  41. #include <curl/mprintf.h>
  42. #include "curl_memory.h"
  43. /* The last #include file should be: */
  44. #include "memdebug.h"
  45. static gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
  46. /*
  47. * Helper GSS-API error functions.
  48. */
  49. static int check_gss_err(struct SessionHandle *data,
  50. OM_uint32 major_status,
  51. OM_uint32 minor_status,
  52. const char* function)
  53. {
  54. if(GSS_ERROR(major_status)) {
  55. OM_uint32 maj_stat,min_stat;
  56. OM_uint32 msg_ctx = 0;
  57. gss_buffer_desc status_string;
  58. char buf[1024];
  59. size_t len;
  60. len = 0;
  61. msg_ctx = 0;
  62. while(!msg_ctx) {
  63. /* convert major status code (GSS-API error) to text */
  64. maj_stat = gss_display_status(&min_stat, major_status,
  65. GSS_C_GSS_CODE,
  66. GSS_C_NULL_OID,
  67. &msg_ctx, &status_string);
  68. if(maj_stat == GSS_S_COMPLETE) {
  69. if(sizeof(buf) > len + status_string.length + 1) {
  70. strcpy(buf+len, (char*) status_string.value);
  71. len += status_string.length;
  72. }
  73. gss_release_buffer(&min_stat, &status_string);
  74. break;
  75. }
  76. gss_release_buffer(&min_stat, &status_string);
  77. }
  78. if(sizeof(buf) > len + 3) {
  79. strcpy(buf+len, ".\n");
  80. len += 2;
  81. }
  82. msg_ctx = 0;
  83. while(!msg_ctx) {
  84. /* convert minor status code (underlying routine error) to text */
  85. maj_stat = gss_display_status(&min_stat, minor_status,
  86. GSS_C_MECH_CODE,
  87. GSS_C_NULL_OID,
  88. &msg_ctx, &status_string);
  89. if(maj_stat == GSS_S_COMPLETE) {
  90. if(sizeof(buf) > len + status_string.length)
  91. strcpy(buf+len, (char*) status_string.value);
  92. gss_release_buffer(&min_stat, &status_string);
  93. break;
  94. }
  95. gss_release_buffer(&min_stat, &status_string);
  96. }
  97. failf(data, "GSS-API error: %s failed:\n%s", function, buf);
  98. return(1);
  99. }
  100. return(0);
  101. }
  102. CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
  103. struct connectdata *conn)
  104. {
  105. struct SessionHandle *data = conn->data;
  106. curl_socket_t sock = conn->sock[sockindex];
  107. CURLcode code;
  108. ssize_t actualread;
  109. ssize_t written;
  110. int result;
  111. OM_uint32 gss_major_status, gss_minor_status, gss_status;
  112. OM_uint32 gss_ret_flags;
  113. int gss_conf_state, gss_enc;
  114. gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
  115. gss_buffer_desc gss_send_token = GSS_C_EMPTY_BUFFER;
  116. gss_buffer_desc gss_recv_token = GSS_C_EMPTY_BUFFER;
  117. gss_buffer_desc gss_w_token = GSS_C_EMPTY_BUFFER;
  118. gss_buffer_desc* gss_token = GSS_C_NO_BUFFER;
  119. gss_name_t server = GSS_C_NO_NAME;
  120. gss_name_t gss_client_name = GSS_C_NO_NAME;
  121. unsigned short us_length;
  122. char *user=NULL;
  123. unsigned char socksreq[4]; /* room for GSS-API exchange header only */
  124. char *serviceptr = data->set.str[STRING_SOCKS5_GSSAPI_SERVICE];
  125. /* GSS-API request looks like
  126. * +----+------+-----+----------------+
  127. * |VER | MTYP | LEN | TOKEN |
  128. * +----+------+----------------------+
  129. * | 1 | 1 | 2 | up to 2^16 - 1 |
  130. * +----+------+-----+----------------+
  131. */
  132. /* prepare service name */
  133. if(strchr(serviceptr,'/')) {
  134. service.value = malloc(strlen(serviceptr));
  135. if(!service.value)
  136. return CURLE_OUT_OF_MEMORY;
  137. service.length = strlen(serviceptr);
  138. memcpy(service.value, serviceptr, service.length);
  139. gss_major_status = gss_import_name(&gss_minor_status, &service,
  140. (gss_OID) GSS_C_NULL_OID, &server);
  141. }
  142. else {
  143. service.value = malloc(strlen(serviceptr) +strlen(conn->proxy.name)+2);
  144. if(!service.value)
  145. return CURLE_OUT_OF_MEMORY;
  146. service.length = strlen(serviceptr) +strlen(conn->proxy.name)+1;
  147. snprintf(service.value, service.length+1, "%s@%s",
  148. serviceptr, conn->proxy.name);
  149. gss_major_status = gss_import_name(&gss_minor_status, &service,
  150. gss_nt_service_name, &server);
  151. }
  152. gss_release_buffer(&gss_status, &service); /* clear allocated memory */
  153. if(check_gss_err(data,gss_major_status,
  154. gss_minor_status,"gss_import_name()")) {
  155. failf(data, "Failed to create service name.");
  156. gss_release_name(&gss_status, &server);
  157. return CURLE_COULDNT_CONNECT;
  158. }
  159. /* As long as we need to keep sending some context info, and there's no */
  160. /* errors, keep sending it... */
  161. for(;;) {
  162. gss_major_status = Curl_gss_init_sec_context(data,
  163. &gss_minor_status,
  164. &gss_context,
  165. server,
  166. &Curl_krb5_mech_oid,
  167. NULL,
  168. gss_token,
  169. &gss_send_token,
  170. &gss_ret_flags);
  171. if(gss_token != GSS_C_NO_BUFFER)
  172. gss_release_buffer(&gss_status, &gss_recv_token);
  173. if(check_gss_err(data,gss_major_status,
  174. gss_minor_status,"gss_init_sec_context")) {
  175. gss_release_name(&gss_status, &server);
  176. gss_release_buffer(&gss_status, &gss_recv_token);
  177. gss_release_buffer(&gss_status, &gss_send_token);
  178. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  179. failf(data, "Failed to initial GSS-API token.");
  180. return CURLE_COULDNT_CONNECT;
  181. }
  182. if(gss_send_token.length != 0) {
  183. socksreq[0] = 1; /* GSS-API subnegotiation version */
  184. socksreq[1] = 1; /* authentication message type */
  185. us_length = htons((short)gss_send_token.length);
  186. memcpy(socksreq+2,&us_length,sizeof(short));
  187. code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
  188. if(code || (4 != written)) {
  189. failf(data, "Failed to send GSS-API authentication request.");
  190. gss_release_name(&gss_status, &server);
  191. gss_release_buffer(&gss_status, &gss_recv_token);
  192. gss_release_buffer(&gss_status, &gss_send_token);
  193. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  194. return CURLE_COULDNT_CONNECT;
  195. }
  196. code = Curl_write_plain(conn, sock, (char *)gss_send_token.value,
  197. gss_send_token.length, &written);
  198. if(code || ((ssize_t)gss_send_token.length != written)) {
  199. failf(data, "Failed to send GSS-API authentication token.");
  200. gss_release_name(&gss_status, &server);
  201. gss_release_buffer(&gss_status, &gss_recv_token);
  202. gss_release_buffer(&gss_status, &gss_send_token);
  203. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  204. return CURLE_COULDNT_CONNECT;
  205. }
  206. }
  207. gss_release_buffer(&gss_status, &gss_send_token);
  208. gss_release_buffer(&gss_status, &gss_recv_token);
  209. if(gss_major_status != GSS_S_CONTINUE_NEEDED) 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(conn, 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 / messgae 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(conn, 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 authencticated user %s with GSS-API.\n",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.\n",
  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(conn, 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(conn, 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(conn, 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(conn, 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 / messgae 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(conn, 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 (%d).",
  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 (%d).",
  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. infof(data, "SOCKS5 access with%s protection granted.\n",
  465. (socksreq[0]==0)?"out GSS-API data":
  466. ((socksreq[0]==1)?" GSS-API integrity":" GSS-API confidentiality"));
  467. conn->socks5_gssapi_enctype = socksreq[0];
  468. if(socksreq[0] == 0)
  469. gss_delete_sec_context(&gss_status, &gss_context, NULL);
  470. return CURLE_OK;
  471. }
  472. #endif
  473. #endif /* CURL_DISABLE_PROXY */