test_socks.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "or.h"
  6. #include "buffers.h"
  7. #include "config.h"
  8. #include "proto_socks.h"
  9. #include "test.h"
  10. typedef struct socks_test_data_t {
  11. socks_request_t *req;
  12. buf_t *buf;
  13. } socks_test_data_t;
  14. static void *
  15. socks_test_setup(const struct testcase_t *testcase)
  16. {
  17. socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
  18. (void)testcase;
  19. data->buf = buf_new_with_capacity(256);
  20. data->req = socks_request_new();
  21. config_register_addressmaps(get_options());
  22. return data;
  23. }
  24. static int
  25. socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
  26. {
  27. socks_test_data_t *data = ptr;
  28. (void)testcase;
  29. buf_free(data->buf);
  30. socks_request_free(data->req);
  31. tor_free(data);
  32. return 1;
  33. }
  34. static const struct testcase_setup_t socks_setup = {
  35. socks_test_setup, socks_test_cleanup
  36. };
  37. #define SOCKS_TEST_INIT() \
  38. socks_test_data_t *testdata = ptr; \
  39. buf_t *buf = testdata->buf; \
  40. socks_request_t *socks = testdata->req;
  41. #define ADD_DATA(buf, s) \
  42. buf_add(buf, s, sizeof(s)-1)
  43. static void
  44. socks_request_clear(socks_request_t *socks)
  45. {
  46. tor_free(socks->username);
  47. tor_free(socks->password);
  48. memset(socks, 0, sizeof(socks_request_t));
  49. }
  50. /** Perform unsupported SOCKS 4 commands */
  51. static void
  52. test_socks_4_unsupported_commands(void *ptr)
  53. {
  54. SOCKS_TEST_INIT();
  55. /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
  56. ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
  57. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  58. get_options()->SafeSocks),
  59. OP_EQ, -1);
  60. tt_int_op(4,OP_EQ, socks->socks_version);
  61. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  62. done:
  63. ;
  64. }
  65. /** Perform supported SOCKS 4 commands */
  66. static void
  67. test_socks_4_supported_commands(void *ptr)
  68. {
  69. SOCKS_TEST_INIT();
  70. tt_int_op(0,OP_EQ, buf_datalen(buf));
  71. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
  72. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
  73. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  74. get_options()->SafeSocks),
  75. OP_EQ, 1);
  76. tt_int_op(4,OP_EQ, socks->socks_version);
  77. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  78. tt_int_op(SOCKS_COMMAND_CONNECT,OP_EQ, socks->command);
  79. tt_str_op("2.2.2.3",OP_EQ, socks->address);
  80. tt_int_op(4370,OP_EQ, socks->port);
  81. tt_assert(socks->got_auth == 0);
  82. tt_assert(! socks->username);
  83. tt_int_op(0,OP_EQ, buf_datalen(buf));
  84. socks_request_clear(socks);
  85. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
  86. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
  87. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  88. get_options()->SafeSocks),
  89. OP_EQ, 1);
  90. tt_int_op(4,OP_EQ, socks->socks_version);
  91. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  92. tt_int_op(SOCKS_COMMAND_CONNECT,OP_EQ, socks->command);
  93. tt_str_op("2.2.2.4",OP_EQ, socks->address);
  94. tt_int_op(4370,OP_EQ, socks->port);
  95. tt_assert(socks->got_auth == 1);
  96. tt_assert(socks->username);
  97. tt_int_op(2,OP_EQ, socks->usernamelen);
  98. tt_mem_op("me",OP_EQ, socks->username, 2);
  99. tt_int_op(0,OP_EQ, buf_datalen(buf));
  100. socks_request_clear(socks);
  101. /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
  102. ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
  103. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  104. get_options()->SafeSocks),
  105. OP_EQ, 1);
  106. tt_int_op(4,OP_EQ, socks->socks_version);
  107. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  108. tt_str_op("torproject.org",OP_EQ, socks->address);
  109. tt_int_op(0,OP_EQ, buf_datalen(buf));
  110. done:
  111. ;
  112. }
  113. /** Perform unsupported SOCKS 5 commands */
  114. static void
  115. test_socks_5_unsupported_commands(void *ptr)
  116. {
  117. SOCKS_TEST_INIT();
  118. /* SOCKS 5 Send unsupported BIND [02] command */
  119. ADD_DATA(buf, "\x05\x02\x00\x01");
  120. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  121. get_options()->SafeSocks),OP_EQ, 0);
  122. tt_int_op(0,OP_EQ, buf_datalen(buf));
  123. tt_int_op(5,OP_EQ, socks->socks_version);
  124. tt_int_op(2,OP_EQ, socks->replylen);
  125. tt_int_op(5,OP_EQ, socks->reply[0]);
  126. tt_int_op(0,OP_EQ, socks->reply[1]);
  127. ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
  128. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  129. get_options()->SafeSocks),OP_EQ, -1);
  130. tt_int_op(5,OP_EQ,socks->socks_version);
  131. tt_int_op(10,OP_EQ,socks->replylen);
  132. tt_int_op(5,OP_EQ,socks->reply[0]);
  133. tt_int_op(SOCKS5_COMMAND_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  134. tt_int_op(1,OP_EQ,socks->reply[3]);
  135. buf_clear(buf);
  136. socks_request_clear(socks);
  137. /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
  138. ADD_DATA(buf, "\x05\x02\x00\x01");
  139. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  140. get_options()->SafeSocks),OP_EQ, 0);
  141. tt_int_op(5,OP_EQ, socks->socks_version);
  142. tt_int_op(2,OP_EQ, socks->replylen);
  143. tt_int_op(5,OP_EQ, socks->reply[0]);
  144. tt_int_op(0,OP_EQ, socks->reply[1]);
  145. ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
  146. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  147. get_options()->SafeSocks),OP_EQ, -1);
  148. tt_int_op(5,OP_EQ,socks->socks_version);
  149. tt_int_op(10,OP_EQ,socks->replylen);
  150. tt_int_op(5,OP_EQ,socks->reply[0]);
  151. tt_int_op(SOCKS5_COMMAND_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  152. tt_int_op(1,OP_EQ,socks->reply[3]);
  153. done:
  154. ;
  155. }
  156. /** Perform supported SOCKS 5 commands */
  157. static void
  158. test_socks_5_supported_commands(void *ptr)
  159. {
  160. SOCKS_TEST_INIT();
  161. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  162. ADD_DATA(buf, "\x05\x01\x00");
  163. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  164. get_options()->SafeSocks),OP_EQ, 0);
  165. tt_int_op(5,OP_EQ, socks->socks_version);
  166. tt_int_op(2,OP_EQ, socks->replylen);
  167. tt_int_op(5,OP_EQ, socks->reply[0]);
  168. tt_int_op(0,OP_EQ, socks->reply[1]);
  169. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  170. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  171. get_options()->SafeSocks),OP_EQ, 1);
  172. tt_str_op("2.2.2.2",OP_EQ, socks->address);
  173. tt_int_op(4369,OP_EQ, socks->port);
  174. tt_int_op(0,OP_EQ, buf_datalen(buf));
  175. socks_request_clear(socks);
  176. /* SOCKS 5 Send CONNECT [01] to one of the ipv6 addresses for
  177. torproject.org:80 */
  178. ADD_DATA(buf, "\x05\x01\x00");
  179. ADD_DATA(buf, "\x05\x01\x00\x04"
  180. "\x20\x02\x41\xb8\x02\x02\x0d\xeb\x02\x13\x21\xff\xfe\x20\x14\x26"
  181. "\x00\x50");
  182. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  183. get_options()->SafeSocks),OP_EQ, 1);
  184. tt_int_op(5,OP_EQ, socks->socks_version);
  185. tt_int_op(2,OP_EQ, socks->replylen);
  186. tt_int_op(5,OP_EQ, socks->reply[0]);
  187. tt_int_op(0,OP_EQ, socks->reply[1]);
  188. tt_str_op("[2002:41b8:202:deb:213:21ff:fe20:1426]",OP_EQ, socks->address);
  189. tt_int_op(80,OP_EQ, socks->port);
  190. tt_int_op(0,OP_EQ, buf_datalen(buf));
  191. socks_request_clear(socks);
  192. /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
  193. ADD_DATA(buf, "\x05\x01\x00");
  194. ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
  195. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  196. get_options()->SafeSocks),OP_EQ, 1);
  197. tt_int_op(5,OP_EQ, socks->socks_version);
  198. tt_int_op(2,OP_EQ, socks->replylen);
  199. tt_int_op(5,OP_EQ, socks->reply[0]);
  200. tt_int_op(0,OP_EQ, socks->reply[1]);
  201. tt_str_op("torproject.org",OP_EQ, socks->address);
  202. tt_int_op(4369,OP_EQ, socks->port);
  203. tt_int_op(0,OP_EQ, buf_datalen(buf));
  204. socks_request_clear(socks);
  205. /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
  206. ADD_DATA(buf, "\x05\x01\x00");
  207. ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
  208. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  209. get_options()->SafeSocks),
  210. OP_EQ, 1);
  211. tt_int_op(5,OP_EQ, socks->socks_version);
  212. tt_int_op(2,OP_EQ, socks->replylen);
  213. tt_int_op(5,OP_EQ, socks->reply[0]);
  214. tt_int_op(0,OP_EQ, socks->reply[1]);
  215. tt_str_op("torproject.org",OP_EQ, socks->address);
  216. tt_int_op(0,OP_EQ, buf_datalen(buf));
  217. socks_request_clear(socks);
  218. /* SOCKS 5 Should NOT reject RESOLVE [F0] request for IPv4 address
  219. * string if SafeSocks is enabled. */
  220. ADD_DATA(buf, "\x05\x01\x00");
  221. ADD_DATA(buf, "\x05\xF0\x00\x03\x07");
  222. ADD_DATA(buf, "8.8.8.8");
  223. ADD_DATA(buf, "\x11\x11");
  224. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  225. OP_EQ, 1);
  226. tt_str_op("8.8.8.8", OP_EQ, socks->address);
  227. tt_int_op(4369, OP_EQ, socks->port);
  228. tt_int_op(0, OP_EQ, buf_datalen(buf));
  229. socks_request_clear(socks);
  230. /* SOCKS 5 should NOT reject RESOLVE [F0] reject for IPv6 address
  231. * string if SafeSocks is enabled. */
  232. ADD_DATA(buf, "\x05\x01\x00");
  233. ADD_DATA(buf, "\x05\xF0\x00\x03\x27");
  234. ADD_DATA(buf, "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  235. ADD_DATA(buf, "\x01\x02");
  236. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  237. OP_EQ, -1);
  238. tt_str_op("2001:0db8:85a3:0000:0000:8a2e:0370:7334", OP_EQ, socks->address);
  239. tt_int_op(258, OP_EQ, socks->port);
  240. tt_int_op(0, OP_EQ, buf_datalen(buf));
  241. socks_request_clear(socks);
  242. /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
  243. ADD_DATA(buf, "\x05\x01\x00");
  244. ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
  245. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  246. get_options()->SafeSocks),
  247. OP_EQ, 1);
  248. tt_int_op(5,OP_EQ, socks->socks_version);
  249. tt_int_op(2,OP_EQ, socks->replylen);
  250. tt_int_op(5,OP_EQ, socks->reply[0]);
  251. tt_int_op(0,OP_EQ, socks->reply[1]);
  252. tt_str_op("2.2.2.5",OP_EQ, socks->address);
  253. tt_int_op(0,OP_EQ, buf_datalen(buf));
  254. done:
  255. ;
  256. }
  257. /** Perform SOCKS 5 authentication */
  258. static void
  259. test_socks_5_no_authenticate(void *ptr)
  260. {
  261. SOCKS_TEST_INIT();
  262. /*SOCKS 5 No Authentication */
  263. ADD_DATA(buf,"\x05\x01\x00");
  264. tt_assert(!fetch_from_buf_socks(buf, socks,
  265. get_options()->TestSocks,
  266. get_options()->SafeSocks));
  267. tt_int_op(2,OP_EQ, socks->replylen);
  268. tt_int_op(5,OP_EQ, socks->reply[0]);
  269. tt_int_op(SOCKS_NO_AUTH,OP_EQ, socks->reply[1]);
  270. tt_int_op(0,OP_EQ, buf_datalen(buf));
  271. /*SOCKS 5 Send username/password anyway - pretend to be broken */
  272. ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
  273. tt_assert(!fetch_from_buf_socks(buf, socks,
  274. get_options()->TestSocks,
  275. get_options()->SafeSocks));
  276. tt_int_op(5,OP_EQ, socks->socks_version);
  277. tt_int_op(2,OP_EQ, socks->replylen);
  278. tt_int_op(1,OP_EQ, socks->reply[0]);
  279. tt_int_op(0,OP_EQ, socks->reply[1]);
  280. tt_int_op(2,OP_EQ, socks->usernamelen);
  281. tt_int_op(2,OP_EQ, socks->passwordlen);
  282. tt_mem_op("\x01\x01",OP_EQ, socks->username, 2);
  283. tt_mem_op("\x01\x01",OP_EQ, socks->password, 2);
  284. done:
  285. ;
  286. }
  287. /** Perform SOCKS 5 authentication */
  288. static void
  289. test_socks_5_authenticate(void *ptr)
  290. {
  291. SOCKS_TEST_INIT();
  292. /* SOCKS 5 Negotiate username/password authentication */
  293. ADD_DATA(buf, "\x05\x01\x02");
  294. tt_assert(!fetch_from_buf_socks(buf, socks,
  295. get_options()->TestSocks,
  296. get_options()->SafeSocks));
  297. tt_int_op(2,OP_EQ, socks->replylen);
  298. tt_int_op(5,OP_EQ, socks->reply[0]);
  299. tt_int_op(SOCKS_USER_PASS,OP_EQ, socks->reply[1]);
  300. tt_int_op(5,OP_EQ, socks->socks_version);
  301. tt_int_op(0,OP_EQ, buf_datalen(buf));
  302. /* SOCKS 5 Send username/password */
  303. ADD_DATA(buf, "\x01\x02me\x08mypasswd");
  304. tt_assert(!fetch_from_buf_socks(buf, socks,
  305. get_options()->TestSocks,
  306. get_options()->SafeSocks));
  307. tt_int_op(5,OP_EQ, socks->socks_version);
  308. tt_int_op(2,OP_EQ, socks->replylen);
  309. tt_int_op(1,OP_EQ, socks->reply[0]);
  310. tt_int_op(0,OP_EQ, socks->reply[1]);
  311. tt_int_op(2,OP_EQ, socks->usernamelen);
  312. tt_int_op(8,OP_EQ, socks->passwordlen);
  313. tt_mem_op("me",OP_EQ, socks->username, 2);
  314. tt_mem_op("mypasswd",OP_EQ, socks->password, 8);
  315. done:
  316. ;
  317. }
  318. /** Perform SOCKS 5 authentication and send data all in one go */
  319. static void
  320. test_socks_5_authenticate_with_data(void *ptr)
  321. {
  322. SOCKS_TEST_INIT();
  323. /* SOCKS 5 Negotiate username/password authentication */
  324. ADD_DATA(buf, "\x05\x01\x02");
  325. tt_assert(!fetch_from_buf_socks(buf, socks,
  326. get_options()->TestSocks,
  327. get_options()->SafeSocks));
  328. tt_int_op(2,OP_EQ, socks->replylen);
  329. tt_int_op(5,OP_EQ, socks->reply[0]);
  330. tt_int_op(SOCKS_USER_PASS,OP_EQ, socks->reply[1]);
  331. tt_int_op(5,OP_EQ, socks->socks_version);
  332. tt_int_op(0,OP_EQ, buf_datalen(buf));
  333. /* SOCKS 5 Send username/password */
  334. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  335. ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  336. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  337. get_options()->SafeSocks),
  338. OP_EQ, 1);
  339. tt_int_op(5,OP_EQ, socks->socks_version);
  340. tt_int_op(2,OP_EQ, socks->replylen);
  341. tt_int_op(1,OP_EQ, socks->reply[0]);
  342. tt_int_op(0,OP_EQ, socks->reply[1]);
  343. tt_str_op("2.2.2.2",OP_EQ, socks->address);
  344. tt_int_op(4369,OP_EQ, socks->port);
  345. tt_int_op(2,OP_EQ, socks->usernamelen);
  346. tt_int_op(3,OP_EQ, socks->passwordlen);
  347. tt_mem_op("me",OP_EQ, socks->username, 2);
  348. tt_mem_op("you",OP_EQ, socks->password, 3);
  349. done:
  350. ;
  351. }
  352. /** Try to negotiate an unsupported authentication type */
  353. static void
  354. test_socks_5_auth_unsupported_type(void *ptr)
  355. {
  356. SOCKS_TEST_INIT();
  357. /* None of these authentication types are recognized. */
  358. ADD_DATA(buf, "\x05\x03\x99\x21\x10");
  359. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  360. get_options()->SafeSocks),
  361. OP_EQ, -1);
  362. tt_int_op(0,OP_EQ, socks->socks_version);
  363. tt_int_op(2,OP_EQ, socks->replylen);
  364. tt_int_op(5,OP_EQ, socks->reply[0]);
  365. tt_int_op(0xff,OP_EQ, socks->reply[1]);
  366. done:
  367. ;
  368. }
  369. /** Try to negotiate an unsupported version of username/password auth. */
  370. static void
  371. test_socks_5_auth_unsupported_version(void *ptr)
  372. {
  373. SOCKS_TEST_INIT();
  374. /* Negotiate username/password */
  375. ADD_DATA(buf, "\x05\x01\x02");
  376. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  377. get_options()->SafeSocks),
  378. OP_EQ, 0);
  379. tt_int_op(0,OP_EQ, buf_datalen(buf)); /* buf should be drained */
  380. /* Now, suggest an unrecognized username/password version */
  381. ADD_DATA(buf, "\x02\x05" "hello" "\x05" "world");
  382. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  383. get_options()->SafeSocks),
  384. OP_EQ, -1);
  385. done:
  386. ;
  387. }
  388. /** Perform SOCKS 5 authentication before method negotiated */
  389. static void
  390. test_socks_5_auth_before_negotiation(void *ptr)
  391. {
  392. SOCKS_TEST_INIT();
  393. /* SOCKS 5 Send username/password */
  394. ADD_DATA(buf, "\x01\x02me\x02me");
  395. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  396. get_options()->SafeSocks),
  397. OP_EQ, -1);
  398. tt_int_op(0,OP_EQ, socks->socks_version);
  399. tt_int_op(0,OP_EQ, socks->replylen);
  400. tt_int_op(0,OP_EQ, socks->reply[0]);
  401. tt_int_op(0,OP_EQ, socks->reply[1]);
  402. done:
  403. ;
  404. }
  405. /** Perform malformed SOCKS 5 commands */
  406. static void
  407. test_socks_5_malformed_commands(void *ptr)
  408. {
  409. SOCKS_TEST_INIT();
  410. /* XXX: Stringified address length > MAX_SOCKS_ADDR_LEN will never happen */
  411. /** SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369, with SafeSocks set
  412. */
  413. ADD_DATA(buf, "\x05\x01\x00");
  414. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  415. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  416. OP_EQ, -1);
  417. tt_int_op(5,OP_EQ,socks->socks_version);
  418. tt_int_op(10,OP_EQ,socks->replylen);
  419. tt_int_op(5,OP_EQ,socks->reply[0]);
  420. tt_int_op(SOCKS5_NOT_ALLOWED,OP_EQ,socks->reply[1]);
  421. tt_int_op(1,OP_EQ,socks->reply[3]);
  422. buf_clear(buf);
  423. socks_request_clear(socks);
  424. /* SOCKS 5 Send RESOLVE_PTR [F1] for FQDN torproject.org */
  425. ADD_DATA(buf, "\x05\x01\x00");
  426. ADD_DATA(buf, "\x05\xF1\x00\x03\x0Etorproject.org\x11\x11");
  427. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  428. get_options()->SafeSocks),OP_EQ, -1);
  429. tt_int_op(5,OP_EQ,socks->socks_version);
  430. tt_int_op(10,OP_EQ,socks->replylen);
  431. tt_int_op(5,OP_EQ,socks->reply[0]);
  432. tt_int_op(SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  433. tt_int_op(1,OP_EQ,socks->reply[3]);
  434. buf_clear(buf);
  435. socks_request_clear(socks);
  436. /* XXX: len + 1 > MAX_SOCKS_ADDR_LEN (FQDN request) will never happen */
  437. /* SOCKS 5 Send CONNECT [01] to FQDN """"".com */
  438. ADD_DATA(buf, "\x05\x01\x00");
  439. ADD_DATA(buf, "\x05\x01\x00\x03\x09\"\"\"\"\".com\x11\x11");
  440. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  441. get_options()->SafeSocks),OP_EQ, -1);
  442. tt_int_op(5,OP_EQ,socks->socks_version);
  443. tt_int_op(10,OP_EQ,socks->replylen);
  444. tt_int_op(5,OP_EQ,socks->reply[0]);
  445. tt_int_op(SOCKS5_GENERAL_ERROR,OP_EQ,socks->reply[1]);
  446. tt_int_op(1,OP_EQ,socks->reply[3]);
  447. buf_clear(buf);
  448. socks_request_clear(socks);
  449. /* SOCKS 5 Send CONNECT [01] to address type 0x23 */
  450. ADD_DATA(buf, "\x05\x01\x00");
  451. ADD_DATA(buf, "\x05\x01\x00\x23\x02\x02\x02\x02\x11\x11");
  452. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  453. get_options()->SafeSocks),OP_EQ, -1);
  454. tt_int_op(5,OP_EQ,socks->socks_version);
  455. tt_int_op(10,OP_EQ,socks->replylen);
  456. tt_int_op(5,OP_EQ,socks->reply[0]);
  457. tt_int_op(SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  458. tt_int_op(1,OP_EQ,socks->reply[3]);
  459. done:
  460. ;
  461. }
  462. /** check for correct behavior when the socks command has not arrived. */
  463. static void
  464. test_socks_truncated(void *ptr)
  465. {
  466. const struct {
  467. enum { NONE, AUTH, ALL } setup;
  468. const char *body;
  469. size_t len;
  470. } commands[] = {
  471. /* SOCKS4 */
  472. /* Connect, to an IP. */
  473. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04\x00", 9},
  474. /* Connect, to an IP, with authentication. */
  475. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04hello\x00", 14},
  476. /* SOCKS4A */
  477. /* Connect, to a hostname */
  478. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01\x00www.example.com\x00", 25},
  479. /* Connect, to a hostname, with authentication */
  480. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01hi\x00www.example.com\x00", 27},
  481. /* SOCKS5 */
  482. /* initial handshake */
  483. { NONE, "\x05\x00", 2 },
  484. /* no-auth handshake */
  485. { NONE, "\x05\x03\x99\x21\x10", 5 },
  486. /* SOCSK5, username-password, all empty. */
  487. { AUTH, "\x01\x00\x00", 3 },
  488. /* SOCSK5, username-password, 1 char each. */
  489. { AUTH, "\x01\x01x\x01y", 5 },
  490. /* SOCSK5, username-password, max length. */
  491. { AUTH, "\x01\xff"
  492. "Ogni tempo ha il suo fascismo: se ne notano i segni premonitori "
  493. "dovunque la concentrazione di potere nega al cittadino la "
  494. "possibilit\xc3\xa0 e la capacit\xc3\xa0 di esprimere ed attuare la "
  495. "sua volont\xc3\xa0. A questo si arriva in molti modi, non "
  496. "necessariamente col terror"
  497. "\xff"
  498. "e dell'intimidazione poliziesca, ma anche negando o distorcendo "
  499. "l'informazione, inquinando la giustizia, paralizzando la scuola, "
  500. "diffondendo in molti modi sottili la nostalgia per un mondo in cui "
  501. "regnava sovrano l'ordine, ed in cui la sicurezza dei pochi "
  502. /* privilegiati riposava sul lavoro forzato e sul silenzio forzato dei
  503. molti. -- Primo Levi */ , 513 },
  504. /* Socks5, IPv4 address */
  505. { ALL, "\x05\x01\x00\x01\x01\x02\x03\x04\x20\x20", 10 },
  506. /* Socks5, IPv6 address */
  507. { ALL, "\x05\x01\x00\x04"
  508. "\x49\x20\x48\x41\x5a\x20\x45\x41\x53\x54\x45\x52\x20\x45\x47\x47"
  509. "\x20\x20", 22 },
  510. /* Socks5, hostname, empty. */
  511. { ALL, "\x05\x01\x00\x03" "\x00" "\x00\x50", 7 },
  512. /* Socks5, hostname, moderate. */
  513. { ALL, "\x05\x01\x00\x03" "\x11" "onion.example.com" "\x00\x50", 24 },
  514. /* Socks5, hostname, maximum. */
  515. { ALL, "\x05\x01\x00\x03" "\xff"
  516. "whatsoever.I.shall.see.or.hear.in.the.course.of.my.profession.as.well."
  517. "as.outside.my.profession.in.my.intercourse.with.men.if.it.be.what."
  518. "should.not.be.published.abroad.I.will.never.divulge.holding.such."
  519. "things.to.be.holy.secrets.x.hippocratic.oath.wikipedia"
  520. "\x00\x50", 262 },
  521. };
  522. unsigned i, j;
  523. SOCKS_TEST_INIT();
  524. for (i = 0; i < ARRAY_LENGTH(commands); ++i) {
  525. for (j = 0; j < commands[i].len; ++j) {
  526. switch (commands[i].setup) {
  527. default: /* Falls through */
  528. case NONE:
  529. /* This test calls for no setup on the socks state. */
  530. break;
  531. case AUTH:
  532. /* This test calls for the socks state to be waiting for
  533. * username/password authentication */
  534. ADD_DATA(buf, "\x05\x01\x02");
  535. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  536. tt_int_op(0, OP_EQ, buf_datalen(buf));
  537. break;
  538. case ALL:
  539. /* This test calls for the socks state to be waiting for
  540. * the connection request */
  541. ADD_DATA(buf, "\x05\x01\x00");
  542. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  543. tt_int_op(0, OP_EQ, buf_datalen(buf));
  544. }
  545. TT_BLATHER(("Checking command %u, length %u, omitting char %u", i, j,
  546. (unsigned)commands[i].body[j]));
  547. buf_add(buf, commands[i].body, j);
  548. /* This should return 0 meaning "not done yet" */
  549. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  550. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  551. buf_clear(buf);
  552. socks_request_free(testdata->req);
  553. socks = testdata->req = socks_request_new();
  554. }
  555. }
  556. done:
  557. ;
  558. }
  559. /* Check our client-side socks4 parsing (that is to say, our parsing of
  560. * server responses).
  561. */
  562. static void
  563. test_socks_client_v4(void *arg)
  564. {
  565. (void)arg;
  566. buf_t *buf = buf_new();
  567. char *reason = NULL;
  568. /* Legit socks4 response, success */
  569. ADD_DATA(buf, "\x04\x5a\x20\x25\x01\x02\x03\x04");
  570. tt_int_op(1, OP_EQ,
  571. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  572. &reason));
  573. tt_ptr_op(reason, OP_EQ, NULL);
  574. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  575. /* Legit socks4 response, failure. */
  576. ADD_DATA(buf, "\x04\x5b\x20\x25\x01\x02\x03\x04");
  577. tt_int_op(-1, OP_EQ,
  578. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  579. &reason));
  580. tt_ptr_op(reason, OP_NE, NULL);
  581. tt_str_op(reason, OP_EQ, "server rejected connection");
  582. done:
  583. buf_free(buf);
  584. tor_free(reason);
  585. }
  586. /* Check our client-side socks5 authentication-negotiation parsing (that is to
  587. * say, our parsing of server responses).
  588. */
  589. static void
  590. test_socks_client_v5_auth(void *arg)
  591. {
  592. (void)arg;
  593. buf_t *buf = buf_new();
  594. char *reason = NULL;
  595. /* Legit socks5 responses, got a method we like. */
  596. ADD_DATA(buf, "\x05\x00");
  597. tt_int_op(1, OP_EQ,
  598. fetch_from_buf_socks_client(buf,
  599. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  600. &reason));
  601. tt_ptr_op(reason, OP_EQ, NULL);
  602. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  603. /* Same, but we wanted something else. */
  604. ADD_DATA(buf, "\x05\x00");
  605. tt_int_op(1, OP_EQ,
  606. fetch_from_buf_socks_client(buf,
  607. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  608. &reason));
  609. tt_ptr_op(reason, OP_EQ, NULL);
  610. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  611. /* Same, and they offered a password. */
  612. ADD_DATA(buf, "\x05\x02");
  613. tt_int_op(2, OP_EQ,
  614. fetch_from_buf_socks_client(buf,
  615. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  616. &reason));
  617. tt_ptr_op(reason, OP_EQ, NULL);
  618. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  619. /* They rejected our method, or selected something we don't know. */
  620. ADD_DATA(buf, "\x05\xff");
  621. tt_int_op(-1, OP_EQ,
  622. fetch_from_buf_socks_client(buf,
  623. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  624. &reason));
  625. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  626. "authentication methods");
  627. buf_clear(buf);
  628. tor_free(reason);
  629. ADD_DATA(buf, "\x05\xff");
  630. tt_int_op(-1, OP_EQ,
  631. fetch_from_buf_socks_client(buf,
  632. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  633. &reason));
  634. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  635. "authentication methods");
  636. tor_free(reason);
  637. buf_clear(buf);
  638. /* Now check for authentication responses: check success and failure. */
  639. ADD_DATA(buf, "\x01\x00");
  640. tt_int_op(1, OP_EQ,
  641. fetch_from_buf_socks_client(buf,
  642. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  643. &reason));
  644. tt_ptr_op(reason, OP_EQ, NULL);
  645. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  646. ADD_DATA(buf, "\x01\xf0");
  647. tt_int_op(-1, OP_EQ,
  648. fetch_from_buf_socks_client(buf,
  649. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  650. &reason));
  651. tt_ptr_op(reason, OP_NE, NULL);
  652. tt_str_op(reason, OP_EQ, "authentication failed");
  653. done:
  654. buf_free(buf);
  655. tor_free(reason);
  656. }
  657. /* Check our client-side socks5 connect parsing (that is to say, our parsing
  658. * of server responses).
  659. */
  660. static void
  661. test_socks_client_v5_connect(void *arg)
  662. {
  663. (void)arg;
  664. buf_t *buf = buf_new();
  665. char *reason = NULL;
  666. /* Legit socks5 responses, success, ipv4. */
  667. ADD_DATA(buf, "\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05");
  668. tt_int_op(1, OP_EQ,
  669. fetch_from_buf_socks_client(buf,
  670. PROXY_SOCKS5_WANT_CONNECT_OK,
  671. &reason));
  672. tt_ptr_op(reason, OP_EQ, NULL);
  673. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  674. /* Legit socks5 responses, success, ipv6. */
  675. ADD_DATA(buf, "\x05\x00\x00\x04"
  676. "abcdefghijklmnop"
  677. "\x00\x05");
  678. tt_int_op(1, OP_EQ,
  679. fetch_from_buf_socks_client(buf,
  680. PROXY_SOCKS5_WANT_CONNECT_OK,
  681. &reason));
  682. tt_ptr_op(reason, OP_EQ, NULL);
  683. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  684. /* Legit socks5 responses, success, hostname. */
  685. ADD_DATA(buf, "\x05\x00\x00\x03\x12"
  686. "gopher.example.com"
  687. "\x00\x05");
  688. tt_int_op(1, OP_EQ,
  689. fetch_from_buf_socks_client(buf,
  690. PROXY_SOCKS5_WANT_CONNECT_OK,
  691. &reason));
  692. tt_ptr_op(reason, OP_EQ, NULL);
  693. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  694. /* Legit socks5 responses, failure, hostname. */
  695. ADD_DATA(buf, "\x05\x03\x00\x03\x12"
  696. "gopher.example.com"
  697. "\x00\x05");
  698. tt_int_op(-1, OP_EQ,
  699. fetch_from_buf_socks_client(buf,
  700. PROXY_SOCKS5_WANT_CONNECT_OK,
  701. &reason));
  702. tt_ptr_op(reason, OP_NE, NULL);
  703. tt_str_op(reason, OP_EQ, "Network unreachable");
  704. tor_free(reason);
  705. buf_clear(buf);
  706. /* Bogus socks5 responses: what is address type 0x17? */
  707. ADD_DATA(buf, "\x05\x03\x00\x17\x12 blah blah");
  708. tt_int_op(-1, OP_EQ,
  709. fetch_from_buf_socks_client(buf,
  710. PROXY_SOCKS5_WANT_CONNECT_OK,
  711. &reason));
  712. tt_ptr_op(reason, OP_NE, NULL);
  713. tt_str_op(reason, OP_EQ, "invalid response to connect request");
  714. buf_clear(buf);
  715. done:
  716. buf_free(buf);
  717. tor_free(reason);
  718. }
  719. static void
  720. test_socks_client_truncated(void *arg)
  721. {
  722. (void)arg;
  723. buf_t *buf = buf_new();
  724. char *reason = NULL;
  725. #define S(str) str, (sizeof(str)-1)
  726. const struct {
  727. int state;
  728. const char *body;
  729. size_t len;
  730. } replies[] = {
  731. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5a\x20\x25\x01\x02\x03\x04") },
  732. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5b\x20\x25\x01\x02\x03\x04") },
  733. { PROXY_SOCKS5_WANT_AUTH_METHOD_NONE, S("\x05\x00") },
  734. { PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929, S("\x05\x00") },
  735. { PROXY_SOCKS5_WANT_AUTH_RFC1929_OK, S("\x01\x00") },
  736. { PROXY_SOCKS5_WANT_CONNECT_OK,
  737. S("\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05") },
  738. { PROXY_SOCKS5_WANT_CONNECT_OK,
  739. S("\x05\x00\x00\x04" "abcdefghijklmnop" "\x00\x05") },
  740. { PROXY_SOCKS5_WANT_CONNECT_OK,
  741. S("\x05\x00\x00\x03\x12" "gopher.example.com" "\x00\x05") },
  742. { PROXY_SOCKS5_WANT_CONNECT_OK,
  743. S("\x05\x03\x00\x03\x12" "gopher.example.com""\x00\x05") },
  744. { PROXY_SOCKS5_WANT_CONNECT_OK,
  745. S("\x05\x03\x00\x17") },
  746. };
  747. unsigned i, j;
  748. for (i = 0; i < ARRAY_LENGTH(replies); ++i) {
  749. for (j = 0; j < replies[i].len; ++j) {
  750. TT_BLATHER(("Checking command %u, length %u", i, j));
  751. buf_add(buf, replies[i].body, j);
  752. /* This should return 0 meaning "not done yet" */
  753. tt_int_op(0, OP_EQ,
  754. fetch_from_buf_socks_client(buf, replies[i].state, &reason));
  755. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  756. buf_clear(buf);
  757. tt_ptr_op(reason, OP_EQ, NULL);
  758. }
  759. }
  760. done:
  761. tor_free(reason);
  762. buf_free(buf);
  763. }
  764. #define SOCKSENT(name) \
  765. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  766. struct testcase_t socks_tests[] = {
  767. SOCKSENT(4_unsupported_commands),
  768. SOCKSENT(4_supported_commands),
  769. SOCKSENT(5_unsupported_commands),
  770. SOCKSENT(5_supported_commands),
  771. SOCKSENT(5_no_authenticate),
  772. SOCKSENT(5_auth_unsupported_type),
  773. SOCKSENT(5_auth_unsupported_version),
  774. SOCKSENT(5_auth_before_negotiation),
  775. SOCKSENT(5_authenticate),
  776. SOCKSENT(5_authenticate_with_data),
  777. SOCKSENT(5_malformed_commands),
  778. SOCKSENT(truncated),
  779. { "client/v4", test_socks_client_v4, TT_FORK, NULL, NULL },
  780. { "client/v5_auth", test_socks_client_v5_auth, TT_FORK, NULL, NULL },
  781. { "client/v5_connect", test_socks_client_v5_connect, TT_FORK, NULL, NULL },
  782. { "client/truncated", test_socks_client_truncated, TT_FORK, NULL, NULL },
  783. END_OF_TESTCASES
  784. };