socks_gssapi.c 20 KB

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