test_cell_formats.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONNECTION_EDGE_PRIVATE
  7. #define RELAY_PRIVATE
  8. #include "or.h"
  9. #include "connection_edge.h"
  10. #include "onion.h"
  11. #include "onion_tap.h"
  12. #include "onion_fast.h"
  13. #include "onion_ntor.h"
  14. #include "relay.h"
  15. #include "test.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. static void
  19. test_cfmt_relay_header(void *arg)
  20. {
  21. relay_header_t rh;
  22. const uint8_t hdr_1[RELAY_HEADER_SIZE] =
  23. "\x03" "\x00\x00" "\x21\x22" "ABCD" "\x01\x03";
  24. uint8_t hdr_out[RELAY_HEADER_SIZE];
  25. (void)arg;
  26. tt_int_op(sizeof(hdr_1), ==, RELAY_HEADER_SIZE);
  27. relay_header_unpack(&rh, hdr_1);
  28. tt_int_op(rh.command, ==, 3);
  29. tt_int_op(rh.recognized, ==, 0);
  30. tt_int_op(rh.stream_id, ==, 0x2122);
  31. test_mem_op(rh.integrity, ==, "ABCD", 4);
  32. tt_int_op(rh.length, ==, 0x103);
  33. relay_header_pack(hdr_out, &rh);
  34. test_mem_op(hdr_out, ==, hdr_1, RELAY_HEADER_SIZE);
  35. done:
  36. ;
  37. }
  38. static void
  39. make_relay_cell(cell_t *out, uint8_t command,
  40. const void *body, size_t bodylen)
  41. {
  42. relay_header_t rh;
  43. memset(&rh, 0, sizeof(rh));
  44. rh.stream_id = 5;
  45. rh.command = command;
  46. rh.length = bodylen;
  47. out->command = CELL_RELAY;
  48. out->circ_id = 10;
  49. relay_header_pack(out->payload, &rh);
  50. memcpy(out->payload + RELAY_HEADER_SIZE, body, bodylen);
  51. }
  52. static void
  53. test_cfmt_begin_cells(void *arg)
  54. {
  55. cell_t cell;
  56. begin_cell_t bcell;
  57. uint8_t end_reason;
  58. (void)arg;
  59. /* Try begindir. */
  60. memset(&bcell, 0x7f, sizeof(bcell));
  61. make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "", 0);
  62. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  63. tt_ptr_op(NULL, ==, bcell.address);
  64. tt_int_op(0, ==, bcell.flags);
  65. tt_int_op(0, ==, bcell.port);
  66. tt_int_op(5, ==, bcell.stream_id);
  67. tt_int_op(1, ==, bcell.is_begindir);
  68. /* A Begindir with extra stuff. */
  69. memset(&bcell, 0x7f, sizeof(bcell));
  70. make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "12345", 5);
  71. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  72. tt_ptr_op(NULL, ==, bcell.address);
  73. tt_int_op(0, ==, bcell.flags);
  74. tt_int_op(0, ==, bcell.port);
  75. tt_int_op(5, ==, bcell.stream_id);
  76. tt_int_op(1, ==, bcell.is_begindir);
  77. /* A short but valid begin cell */
  78. memset(&bcell, 0x7f, sizeof(bcell));
  79. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:9", 6);
  80. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  81. tt_str_op("a.b", ==, bcell.address);
  82. tt_int_op(0, ==, bcell.flags);
  83. tt_int_op(9, ==, bcell.port);
  84. tt_int_op(5, ==, bcell.stream_id);
  85. tt_int_op(0, ==, bcell.is_begindir);
  86. tor_free(bcell.address);
  87. /* A significantly loner begin cell */
  88. memset(&bcell, 0x7f, sizeof(bcell));
  89. {
  90. const char c[] = "here-is-a-nice-long.hostname.com:65535";
  91. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, strlen(c)+1);
  92. }
  93. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  94. tt_str_op("here-is-a-nice-long.hostname.com", ==, bcell.address);
  95. tt_int_op(0, ==, bcell.flags);
  96. tt_int_op(65535, ==, bcell.port);
  97. tt_int_op(5, ==, bcell.stream_id);
  98. tt_int_op(0, ==, bcell.is_begindir);
  99. tor_free(bcell.address);
  100. /* An IPv4 begin cell. */
  101. memset(&bcell, 0x7f, sizeof(bcell));
  102. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "18.9.22.169:80", 15);
  103. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  104. tt_str_op("18.9.22.169", ==, bcell.address);
  105. tt_int_op(0, ==, bcell.flags);
  106. tt_int_op(80, ==, bcell.port);
  107. tt_int_op(5, ==, bcell.stream_id);
  108. tt_int_op(0, ==, bcell.is_begindir);
  109. tor_free(bcell.address);
  110. /* An IPv6 begin cell. Let's make sure we handle colons*/
  111. memset(&bcell, 0x7f, sizeof(bcell));
  112. make_relay_cell(&cell, RELAY_COMMAND_BEGIN,
  113. "[2620::6b0:b:1a1a:0:26e5:480e]:80", 34);
  114. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  115. tt_str_op("[2620::6b0:b:1a1a:0:26e5:480e]", ==, bcell.address);
  116. tt_int_op(0, ==, bcell.flags);
  117. tt_int_op(80, ==, bcell.port);
  118. tt_int_op(5, ==, bcell.stream_id);
  119. tt_int_op(0, ==, bcell.is_begindir);
  120. tor_free(bcell.address);
  121. /* a begin cell with extra junk but not enough for flags. */
  122. memset(&bcell, 0x7f, sizeof(bcell));
  123. {
  124. const char c[] = "another.example.com:80\x00\x01\x02";
  125. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  126. }
  127. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  128. tt_str_op("another.example.com", ==, bcell.address);
  129. tt_int_op(0, ==, bcell.flags);
  130. tt_int_op(80, ==, bcell.port);
  131. tt_int_op(5, ==, bcell.stream_id);
  132. tt_int_op(0, ==, bcell.is_begindir);
  133. tor_free(bcell.address);
  134. /* a begin cell with flags. */
  135. memset(&bcell, 0x7f, sizeof(bcell));
  136. {
  137. const char c[] = "another.example.com:443\x00\x01\x02\x03\x04";
  138. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  139. }
  140. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  141. tt_str_op("another.example.com", ==, bcell.address);
  142. tt_int_op(0x1020304, ==, bcell.flags);
  143. tt_int_op(443, ==, bcell.port);
  144. tt_int_op(5, ==, bcell.stream_id);
  145. tt_int_op(0, ==, bcell.is_begindir);
  146. tor_free(bcell.address);
  147. /* a begin cell with flags and even more cruft after that. */
  148. memset(&bcell, 0x7f, sizeof(bcell));
  149. {
  150. const char c[] = "a-further.example.com:22\x00\xee\xaa\x00\xffHi mom";
  151. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  152. }
  153. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  154. tt_str_op("a-further.example.com", ==, bcell.address);
  155. tt_int_op(0xeeaa00ff, ==, bcell.flags);
  156. tt_int_op(22, ==, bcell.port);
  157. tt_int_op(5, ==, bcell.stream_id);
  158. tt_int_op(0, ==, bcell.is_begindir);
  159. tor_free(bcell.address);
  160. /* bad begin cell: impossible length. */
  161. memset(&bcell, 0x7f, sizeof(bcell));
  162. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 7);
  163. cell.payload[9] = 0x01; /* Set length to 510 */
  164. cell.payload[10] = 0xfe;
  165. {
  166. relay_header_t rh;
  167. relay_header_unpack(&rh, cell.payload);
  168. tt_int_op(rh.length, ==, 510);
  169. }
  170. tt_int_op(-2, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  171. /* Bad begin cell: no body. */
  172. memset(&bcell, 0x7f, sizeof(bcell));
  173. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
  174. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  175. /* bad begin cell: no body. */
  176. memset(&bcell, 0x7f, sizeof(bcell));
  177. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
  178. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  179. /* bad begin cell: no colon */
  180. memset(&bcell, 0x7f, sizeof(bcell));
  181. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b", 4);
  182. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  183. /* bad begin cell: no ports */
  184. memset(&bcell, 0x7f, sizeof(bcell));
  185. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:", 5);
  186. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  187. /* bad begin cell: bad port */
  188. memset(&bcell, 0x7f, sizeof(bcell));
  189. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:xyz", 8);
  190. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  191. memset(&bcell, 0x7f, sizeof(bcell));
  192. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:100000", 11);
  193. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  194. /* bad begin cell: no nul */
  195. memset(&bcell, 0x7f, sizeof(bcell));
  196. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 6);
  197. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  198. done:
  199. tor_free(bcell.address);
  200. }
  201. static void
  202. test_cfmt_connected_cells(void *arg)
  203. {
  204. relay_header_t rh;
  205. cell_t cell;
  206. tor_addr_t addr;
  207. int ttl, r;
  208. char *mem_op_hex_tmp = NULL;
  209. (void)arg;
  210. /* Let's try an oldschool one with nothing in it. */
  211. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "", 0);
  212. relay_header_unpack(&rh, cell.payload);
  213. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  214. tt_int_op(r, ==, 0);
  215. tt_int_op(tor_addr_family(&addr), ==, AF_UNSPEC);
  216. tt_int_op(ttl, ==, -1);
  217. /* A slightly less oldschool one: only an IPv4 address */
  218. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x20\x30\x40\x50", 4);
  219. relay_header_unpack(&rh, cell.payload);
  220. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  221. tt_int_op(r, ==, 0);
  222. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  223. tt_str_op(fmt_addr(&addr), ==, "32.48.64.80");
  224. tt_int_op(ttl, ==, -1);
  225. /* Bogus but understandable: truncated TTL */
  226. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x11\x12\x13\x14\x15", 5);
  227. relay_header_unpack(&rh, cell.payload);
  228. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  229. tt_int_op(r, ==, 0);
  230. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  231. tt_str_op(fmt_addr(&addr), ==, "17.18.19.20");
  232. tt_int_op(ttl, ==, -1);
  233. /* Regular IPv4 one: address and TTL */
  234. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  235. "\x02\x03\x04\x05\x00\x00\x0e\x10", 8);
  236. relay_header_unpack(&rh, cell.payload);
  237. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  238. tt_int_op(r, ==, 0);
  239. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  240. tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
  241. tt_int_op(ttl, ==, 3600);
  242. /* IPv4 with too-big TTL */
  243. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  244. "\x02\x03\x04\x05\xf0\x00\x00\x00", 8);
  245. relay_header_unpack(&rh, cell.payload);
  246. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  247. tt_int_op(r, ==, 0);
  248. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  249. tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
  250. tt_int_op(ttl, ==, -1);
  251. /* IPv6 (ttl is mandatory) */
  252. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  253. "\x00\x00\x00\x00\x06"
  254. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  255. "\x00\x00\x00\x00\x00\x00\x00\x68"
  256. "\x00\x00\x02\x58", 25);
  257. relay_header_unpack(&rh, cell.payload);
  258. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  259. tt_int_op(r, ==, 0);
  260. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  261. tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
  262. tt_int_op(ttl, ==, 600);
  263. /* IPv6 (ttl too big) */
  264. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  265. "\x00\x00\x00\x00\x06"
  266. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  267. "\x00\x00\x00\x00\x00\x00\x00\x68"
  268. "\x90\x00\x02\x58", 25);
  269. relay_header_unpack(&rh, cell.payload);
  270. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  271. tt_int_op(r, ==, 0);
  272. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  273. tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
  274. tt_int_op(ttl, ==, -1);
  275. /* Bogus size: 3. */
  276. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  277. "\x00\x01\x02", 3);
  278. relay_header_unpack(&rh, cell.payload);
  279. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  280. tt_int_op(r, ==, -1);
  281. /* Bogus family: 7. */
  282. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  283. "\x00\x00\x00\x00\x07"
  284. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  285. "\x00\x00\x00\x00\x00\x00\x00\x68"
  286. "\x90\x00\x02\x58", 25);
  287. relay_header_unpack(&rh, cell.payload);
  288. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  289. tt_int_op(r, ==, -1);
  290. /* Truncated IPv6. */
  291. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  292. "\x00\x00\x00\x00\x06"
  293. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  294. "\x00\x00\x00\x00\x00\x00\x00\x68"
  295. "\x00\x00\x02", 24);
  296. relay_header_unpack(&rh, cell.payload);
  297. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  298. tt_int_op(r, ==, -1);
  299. /* Now make sure we can generate connected cells correctly. */
  300. /* Try an IPv4 address */
  301. memset(&rh, 0, sizeof(rh));
  302. memset(&cell, 0, sizeof(cell));
  303. tor_addr_parse(&addr, "30.40.50.60");
  304. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  305. &addr, 128);
  306. tt_int_op(rh.length, ==, 8);
  307. test_memeq_hex(cell.payload+RELAY_HEADER_SIZE, "1e28323c" "00000080");
  308. /* Try parsing it. */
  309. tor_addr_make_unspec(&addr);
  310. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  311. tt_int_op(r, ==, 0);
  312. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  313. tt_str_op(fmt_addr(&addr), ==, "30.40.50.60");
  314. tt_int_op(ttl, ==, 128);
  315. /* Try an IPv6 address */
  316. memset(&rh, 0, sizeof(rh));
  317. memset(&cell, 0, sizeof(cell));
  318. tor_addr_parse(&addr, "2620::6b0:b:1a1a:0:26e5:480e");
  319. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  320. &addr, 3600);
  321. tt_int_op(rh.length, ==, 25);
  322. test_memeq_hex(cell.payload + RELAY_HEADER_SIZE,
  323. "00000000" "06"
  324. "2620000006b0000b1a1a000026e5480e" "00000e10");
  325. /* Try parsing it. */
  326. tor_addr_make_unspec(&addr);
  327. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  328. tt_int_op(r, ==, 0);
  329. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  330. tt_str_op(fmt_addr(&addr), ==, "2620:0:6b0:b:1a1a:0:26e5:480e");
  331. tt_int_op(ttl, ==, 3600);
  332. done:
  333. tor_free(mem_op_hex_tmp);
  334. }
  335. static void
  336. test_cfmt_create_cells(void *arg)
  337. {
  338. uint8_t b[MAX_ONIONSKIN_CHALLENGE_LEN];
  339. create_cell_t cc;
  340. cell_t cell;
  341. cell_t cell2;
  342. (void)arg;
  343. /* === Let's try parsing some good cells! */
  344. /* A valid create cell. */
  345. memset(&cell, 0, sizeof(cell));
  346. memset(b, 0, sizeof(b));
  347. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  348. cell.command = CELL_CREATE;
  349. memcpy(cell.payload, b, TAP_ONIONSKIN_CHALLENGE_LEN);
  350. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  351. tt_int_op(CELL_CREATE, ==, cc.cell_type);
  352. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
  353. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
  354. test_memeq(cc.onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  355. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  356. tt_int_op(cell.command, ==, cell2.command);
  357. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  358. /* A valid create_fast cell. */
  359. memset(&cell, 0, sizeof(cell));
  360. memset(b, 0, sizeof(b));
  361. crypto_rand((char*)b, CREATE_FAST_LEN);
  362. cell.command = CELL_CREATE_FAST;
  363. memcpy(cell.payload, b, CREATE_FAST_LEN);
  364. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  365. tt_int_op(CELL_CREATE_FAST, ==, cc.cell_type);
  366. tt_int_op(ONION_HANDSHAKE_TYPE_FAST, ==, cc.handshake_type);
  367. tt_int_op(CREATE_FAST_LEN, ==, cc.handshake_len);
  368. test_memeq(cc.onionskin, b, CREATE_FAST_LEN + 10);
  369. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  370. tt_int_op(cell.command, ==, cell2.command);
  371. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  372. /* A valid create2 cell with a TAP payload */
  373. memset(&cell, 0, sizeof(cell));
  374. memset(b, 0, sizeof(b));
  375. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  376. cell.command = CELL_CREATE2;
  377. memcpy(cell.payload, "\x00\x00\x00\xBA", 4); /* TAP, 186 bytes long */
  378. memcpy(cell.payload+4, b, TAP_ONIONSKIN_CHALLENGE_LEN);
  379. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  380. tt_int_op(CELL_CREATE2, ==, cc.cell_type);
  381. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
  382. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
  383. test_memeq(cc.onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  384. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  385. tt_int_op(cell.command, ==, cell2.command);
  386. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  387. /* A valid create2 cell with an ntor payload */
  388. memset(&cell, 0, sizeof(cell));
  389. memset(b, 0, sizeof(b));
  390. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  391. cell.command = CELL_CREATE2;
  392. memcpy(cell.payload, "\x00\x02\x00\x54", 4); /* ntor, 84 bytes long */
  393. memcpy(cell.payload+4, b, NTOR_ONIONSKIN_LEN);
  394. #ifdef CURVE25519_ENABLED
  395. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  396. tt_int_op(CELL_CREATE2, ==, cc.cell_type);
  397. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
  398. tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
  399. test_memeq(cc.onionskin, b, NTOR_ONIONSKIN_LEN + 10);
  400. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  401. tt_int_op(cell.command, ==, cell2.command);
  402. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  403. #else
  404. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  405. #endif
  406. /* A valid create cell with an ntor payload, in legacy format. */
  407. memset(&cell, 0, sizeof(cell));
  408. memset(b, 0, sizeof(b));
  409. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  410. cell.command = CELL_CREATE;
  411. memcpy(cell.payload, "ntorNTORntorNTOR", 16);
  412. memcpy(cell.payload+16, b, NTOR_ONIONSKIN_LEN);
  413. #ifdef CURVE25519_ENABLED
  414. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  415. tt_int_op(CELL_CREATE, ==, cc.cell_type);
  416. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
  417. tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
  418. test_memeq(cc.onionskin, b, NTOR_ONIONSKIN_LEN + 10);
  419. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  420. tt_int_op(cell.command, ==, cell2.command);
  421. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  422. #else
  423. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  424. #endif
  425. /* == Okay, now let's try to parse some impossible stuff. */
  426. /* It has to be some kind of a create cell! */
  427. cell.command = CELL_CREATED;
  428. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  429. /* You can't acutally make an unparseable CREATE or CREATE_FAST cell. */
  430. /* Try some CREATE2 cells. First with a bad type. */
  431. cell.command = CELL_CREATE2;
  432. memcpy(cell.payload, "\x00\x50\x00\x99", 4); /* Type 0x50???? */
  433. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  434. /* Now a good type with an incorrect length. */
  435. memcpy(cell.payload, "\x00\x00\x00\xBC", 4); /* TAP, 187 bytes.*/
  436. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  437. /* Now a good type with a ridiculous length. */
  438. memcpy(cell.payload, "\x00\x00\x02\x00", 4); /* TAP, 512 bytes.*/
  439. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  440. /* == Time to try formatting bad cells. The important thing is that
  441. we reject big lengths, so just check that for now. */
  442. cc.handshake_len = 512;
  443. tt_int_op(-1, ==, create_cell_format(&cell2, &cc));
  444. /* == Try formatting a create2 cell we don't understand. XXXX */
  445. done:
  446. ;
  447. }
  448. static void
  449. test_cfmt_created_cells(void *arg)
  450. {
  451. uint8_t b[512];
  452. created_cell_t cc;
  453. cell_t cell;
  454. cell_t cell2;
  455. (void)arg;
  456. /* A good CREATED cell */
  457. memset(&cell, 0, sizeof(cell));
  458. memset(b, 0, sizeof(b));
  459. crypto_rand((char*)b, TAP_ONIONSKIN_REPLY_LEN);
  460. cell.command = CELL_CREATED;
  461. memcpy(cell.payload, b, TAP_ONIONSKIN_REPLY_LEN);
  462. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  463. tt_int_op(CELL_CREATED, ==, cc.cell_type);
  464. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, cc.handshake_len);
  465. test_memeq(cc.reply, b, TAP_ONIONSKIN_REPLY_LEN + 10);
  466. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  467. tt_int_op(cell.command, ==, cell2.command);
  468. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  469. /* A good CREATED_FAST cell */
  470. memset(&cell, 0, sizeof(cell));
  471. memset(b, 0, sizeof(b));
  472. crypto_rand((char*)b, CREATED_FAST_LEN);
  473. cell.command = CELL_CREATED_FAST;
  474. memcpy(cell.payload, b, CREATED_FAST_LEN);
  475. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  476. tt_int_op(CELL_CREATED_FAST, ==, cc.cell_type);
  477. tt_int_op(CREATED_FAST_LEN, ==, cc.handshake_len);
  478. test_memeq(cc.reply, b, CREATED_FAST_LEN + 10);
  479. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  480. tt_int_op(cell.command, ==, cell2.command);
  481. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  482. /* A good CREATED2 cell with short reply */
  483. memset(&cell, 0, sizeof(cell));
  484. memset(b, 0, sizeof(b));
  485. crypto_rand((char*)b, 64);
  486. cell.command = CELL_CREATED2;
  487. memcpy(cell.payload, "\x00\x40", 2);
  488. memcpy(cell.payload+2, b, 64);
  489. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  490. tt_int_op(CELL_CREATED2, ==, cc.cell_type);
  491. tt_int_op(64, ==, cc.handshake_len);
  492. test_memeq(cc.reply, b, 80);
  493. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  494. tt_int_op(cell.command, ==, cell2.command);
  495. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  496. /* A good CREATED2 cell with maximal reply */
  497. memset(&cell, 0, sizeof(cell));
  498. memset(b, 0, sizeof(b));
  499. crypto_rand((char*)b, 496);
  500. cell.command = CELL_CREATED2;
  501. memcpy(cell.payload, "\x01\xF0", 2);
  502. memcpy(cell.payload+2, b, 496);
  503. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  504. tt_int_op(CELL_CREATED2, ==, cc.cell_type);
  505. tt_int_op(496, ==, cc.handshake_len);
  506. test_memeq(cc.reply, b, 496);
  507. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  508. tt_int_op(cell.command, ==, cell2.command);
  509. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  510. /* Bogus CREATED2 cell: too long! */
  511. memset(&cell, 0, sizeof(cell));
  512. memset(b, 0, sizeof(b));
  513. crypto_rand((char*)b, 496);
  514. cell.command = CELL_CREATED2;
  515. memcpy(cell.payload, "\x01\xF1", 2);
  516. tt_int_op(-1, ==, created_cell_parse(&cc, &cell));
  517. /* Unformattable CREATED2 cell: too long! */
  518. cc.handshake_len = 497;
  519. tt_int_op(-1, ==, created_cell_format(&cell2, &cc));
  520. done:
  521. ;
  522. }
  523. static void
  524. test_cfmt_extend_cells(void *arg)
  525. {
  526. cell_t cell;
  527. uint8_t b[512];
  528. extend_cell_t ec;
  529. create_cell_t *cc = &ec.create_cell;
  530. uint8_t p[RELAY_PAYLOAD_SIZE];
  531. uint8_t p2[RELAY_PAYLOAD_SIZE];
  532. uint8_t p2_cmd;
  533. uint16_t p2_len;
  534. char *mem_op_hex_tmp = NULL;
  535. (void) arg;
  536. /* Let's start with a simple EXTEND cell. */
  537. memset(p, 0, sizeof(p));
  538. memset(b, 0, sizeof(b));
  539. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  540. memcpy(p, "\x12\xf4\x00\x01\x01\x02", 6); /* 18 244 0 1 : 258 */
  541. memcpy(p+6,b,TAP_ONIONSKIN_CHALLENGE_LEN);
  542. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, "electroencephalogram", 20);
  543. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  544. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  545. tt_int_op(RELAY_COMMAND_EXTEND, ==, ec.cell_type);
  546. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  547. tt_int_op(258, ==, ec.orport_ipv4.port);
  548. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  549. test_memeq(ec.node_id, "electroencephalogram", 20);
  550. tt_int_op(cc->cell_type, ==, CELL_CREATE);
  551. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_TAP);
  552. tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_CHALLENGE_LEN);
  553. test_memeq(cc->onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN+20);
  554. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  555. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
  556. tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  557. test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
  558. /* Let's do an ntor stuffed in a legacy EXTEND cell */
  559. memset(p, 0, sizeof(p));
  560. memset(b, 0, sizeof(b));
  561. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  562. memcpy(p, "\x12\xf4\x00\x01\x01\x02", 6); /* 18 244 0 1 : 258 */
  563. memcpy(p+6,"ntorNTORntorNTOR", 16);
  564. memcpy(p+22, b, NTOR_ONIONSKIN_LEN);
  565. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, "electroencephalogram", 20);
  566. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  567. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  568. tt_int_op(RELAY_COMMAND_EXTEND, ==, ec.cell_type);
  569. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  570. tt_int_op(258, ==, ec.orport_ipv4.port);
  571. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  572. test_memeq(ec.node_id, "electroencephalogram", 20);
  573. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  574. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
  575. tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
  576. test_memeq(cc->onionskin, b, NTOR_ONIONSKIN_LEN+20);
  577. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  578. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
  579. tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  580. test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
  581. tt_int_op(0, ==, create_cell_format_relayed(&cell, cc));
  582. /* Now let's do a minimal ntor EXTEND2 cell. */
  583. memset(&ec, 0xff, sizeof(ec));
  584. memset(p, 0, sizeof(p));
  585. memset(b, 0, sizeof(b));
  586. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  587. /* 2 items; one 18.244.0.1:61681 */
  588. memcpy(p, "\x02\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  589. /* The other is a digest. */
  590. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  591. /* Prep for the handshake: type and length */
  592. memcpy(p+31, "\x00\x02\x00\x54", 4);
  593. memcpy(p+35, b, NTOR_ONIONSKIN_LEN);
  594. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  595. p, 35+NTOR_ONIONSKIN_LEN));
  596. tt_int_op(RELAY_COMMAND_EXTEND2, ==, ec.cell_type);
  597. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  598. tt_int_op(61681, ==, ec.orport_ipv4.port);
  599. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  600. test_memeq(ec.node_id, "anarchoindividualist", 20);
  601. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  602. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
  603. tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
  604. test_memeq(cc->onionskin, b, NTOR_ONIONSKIN_LEN+20);
  605. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  606. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
  607. tt_int_op(p2_len, ==, 35+NTOR_ONIONSKIN_LEN);
  608. test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
  609. /* Now let's do a fanciful EXTEND2 cell. */
  610. memset(&ec, 0xff, sizeof(ec));
  611. memset(p, 0, sizeof(p));
  612. memset(b, 0, sizeof(b));
  613. crypto_rand((char*)b, 99);
  614. /* 4 items; one 18 244 0 1 61681 */
  615. memcpy(p, "\x04\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  616. /* One is a digest. */
  617. memcpy(p+9, "\x02\x14" "anthropomorphization", 22);
  618. /* One is an ipv6 address */
  619. memcpy(p+31, "\x01\x12\x20\x02\x00\x00\x00\x00\x00\x00"
  620. "\x00\x00\x00\x00\x00\xf0\xc5\x1e\x11\x12", 20);
  621. /* One is the Konami code. */
  622. memcpy(p+51, "\xf0\x20upupdowndownleftrightleftrightba", 34);
  623. /* Prep for the handshake: weird type and length */
  624. memcpy(p+85, "\x01\x05\x00\x63", 4);
  625. memcpy(p+89, b, 99);
  626. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, 89+99));
  627. tt_int_op(RELAY_COMMAND_EXTEND2, ==, ec.cell_type);
  628. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  629. tt_int_op(61681, ==, ec.orport_ipv4.port);
  630. tt_str_op("2002::f0:c51e", ==, fmt_addr(&ec.orport_ipv6.addr));
  631. tt_int_op(4370, ==, ec.orport_ipv6.port);
  632. test_memeq(ec.node_id, "anthropomorphization", 20);
  633. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  634. tt_int_op(cc->handshake_type, ==, 0x105);
  635. tt_int_op(cc->handshake_len, ==, 99);
  636. test_memeq(cc->onionskin, b, 99+20);
  637. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  638. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
  639. /* We'll generate it minus the IPv6 address and minus the konami code */
  640. tt_int_op(p2_len, ==, 89+99-34-20);
  641. test_memeq_hex(p2,
  642. /* Two items: one that same darn IP address. */
  643. "02000612F40001F0F1"
  644. /* The next is a digest : anthropomorphization */
  645. "0214616e7468726f706f6d6f727068697a6174696f6e"
  646. /* Now the handshake prologue */
  647. "01050063");
  648. test_memeq(p2+1+8+22+4, b, 99+20);
  649. tt_int_op(0, ==, create_cell_format_relayed(&cell, cc));
  650. /* == Now try parsing some junk */
  651. /* Try a too-long handshake */
  652. memset(p, 0, sizeof(p));
  653. memcpy(p, "\x02\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  654. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  655. memcpy(p+31, "\xff\xff\x01\xd0", 4);
  656. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  657. p, sizeof(p)));
  658. /* Try two identities. */
  659. memset(p, 0, sizeof(p));
  660. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  661. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  662. memcpy(p+31, "\x02\x14" "autodepolymerization", 22);
  663. memcpy(p+53, "\xff\xff\x00\x10", 4);
  664. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  665. p, sizeof(p)));
  666. /* No identities. */
  667. memset(p, 0, sizeof(p));
  668. memcpy(p, "\x01\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  669. memcpy(p+53, "\xff\xff\x00\x10", 4);
  670. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  671. p, sizeof(p)));
  672. /* Try a bad IPv4 address (too long, too short)*/
  673. memset(p, 0, sizeof(p));
  674. memcpy(p, "\x02\x00\x07\x12\xf4\x00\x01\xf0\xf1\xff", 10);
  675. memcpy(p+10, "\x02\x14" "anarchoindividualist", 22);
  676. memcpy(p+32, "\xff\xff\x00\x10", 4);
  677. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  678. p, sizeof(p)));
  679. memset(p, 0, sizeof(p));
  680. memcpy(p, "\x02\x00\x05\x12\xf4\x00\x01\xf0", 8);
  681. memcpy(p+8, "\x02\x14" "anarchoindividualist", 22);
  682. memcpy(p+30, "\xff\xff\x00\x10", 4);
  683. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  684. p, sizeof(p)));
  685. /* IPv6 address (too long, too short, no IPv4)*/
  686. memset(p, 0, sizeof(p));
  687. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  688. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  689. memcpy(p+31, "\x01\x13" "xxxxxxxxxxxxxxxxYYZ", 19);
  690. memcpy(p+50, "\xff\xff\x00\x20", 4);
  691. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  692. p, sizeof(p)));
  693. memset(p, 0, sizeof(p));
  694. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  695. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  696. memcpy(p+31, "\x01\x11" "xxxxxxxxxxxxxxxxY", 17);
  697. memcpy(p+48, "\xff\xff\x00\x20", 4);
  698. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  699. p, sizeof(p)));
  700. memset(p, 0, sizeof(p));
  701. memcpy(p, "\x02", 1);
  702. memcpy(p+1, "\x02\x14" "anarchoindividualist", 22);
  703. memcpy(p+23, "\x01\x12" "xxxxxxxxxxxxxxxxYY", 18);
  704. memcpy(p+41, "\xff\xff\x00\x20", 4);
  705. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  706. p, sizeof(p)));
  707. /* Running out of space in specifiers */
  708. memset(p,0,sizeof(p));
  709. memcpy(p, "\x05\x0a\xff", 3);
  710. memcpy(p+3+255, "\x0a\xff", 2);
  711. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  712. p, sizeof(p)));
  713. /* Fuzz, because why not. */
  714. memset(&ec, 0xff, sizeof(ec));
  715. {
  716. int i;
  717. memset(p, 0, sizeof(p));
  718. for (i = 0; i < 10000; ++i) {
  719. int n = crypto_rand_int(sizeof(p));
  720. crypto_rand((char *)p, n);
  721. extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, n);
  722. }
  723. }
  724. done:
  725. tor_free(mem_op_hex_tmp);
  726. }
  727. static void
  728. test_cfmt_extended_cells(void *arg)
  729. {
  730. uint8_t b[512];
  731. extended_cell_t ec;
  732. created_cell_t *cc = &ec.created_cell;
  733. uint8_t p[RELAY_PAYLOAD_SIZE];
  734. uint8_t p2[RELAY_PAYLOAD_SIZE];
  735. uint8_t p2_cmd;
  736. uint16_t p2_len;
  737. char *mem_op_hex_tmp = NULL;
  738. (void) arg;
  739. /* Try a regular EXTENDED cell. */
  740. memset(&ec, 0xff, sizeof(ec));
  741. memset(p, 0, sizeof(p));
  742. memset(b, 0, sizeof(b));
  743. crypto_rand((char*)b, TAP_ONIONSKIN_REPLY_LEN);
  744. memcpy(p,b,TAP_ONIONSKIN_REPLY_LEN);
  745. tt_int_op(0, ==, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED, p,
  746. TAP_ONIONSKIN_REPLY_LEN));
  747. tt_int_op(RELAY_COMMAND_EXTENDED, ==, ec.cell_type);
  748. tt_int_op(cc->cell_type, ==, CELL_CREATED);
  749. tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_REPLY_LEN);
  750. test_memeq(cc->reply, b, TAP_ONIONSKIN_REPLY_LEN);
  751. tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  752. tt_int_op(RELAY_COMMAND_EXTENDED, ==, p2_cmd);
  753. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, p2_len);
  754. test_memeq(p2, p, sizeof(p2));
  755. /* Try an EXTENDED2 cell */
  756. memset(&ec, 0xff, sizeof(ec));
  757. memset(p, 0, sizeof(p));
  758. memset(b, 0, sizeof(b));
  759. crypto_rand((char*)b, 42);
  760. memcpy(p,"\x00\x2a",2);
  761. memcpy(p+2,b,42);
  762. tt_int_op(0, ==, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, 2+42));
  763. tt_int_op(RELAY_COMMAND_EXTENDED2, ==, ec.cell_type);
  764. tt_int_op(cc->cell_type, ==, CELL_CREATED2);
  765. tt_int_op(cc->handshake_len, ==, 42);
  766. test_memeq(cc->reply, b, 42+10);
  767. tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  768. tt_int_op(RELAY_COMMAND_EXTENDED2, ==, p2_cmd);
  769. tt_int_op(2+42, ==, p2_len);
  770. test_memeq(p2, p, sizeof(p2));
  771. /* Try an almost-too-long EXTENDED2 cell */
  772. memcpy(p, "\x01\xf0", 2);
  773. tt_int_op(0, ==,
  774. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  775. /* Now try a too-long extended2 cell. That's the only misparse I can think
  776. * of. */
  777. memcpy(p, "\x01\xf1", 2);
  778. tt_int_op(-1, ==,
  779. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  780. done:
  781. tor_free(mem_op_hex_tmp);
  782. }
  783. static void
  784. test_cfmt_resolved_cells(void *arg)
  785. {
  786. smartlist_t *addrs = smartlist_new();
  787. relay_header_t rh;
  788. cell_t cell;
  789. int r, errcode;
  790. address_ttl_t *a;
  791. (void)arg;
  792. #define CLEAR_CELL() do { \
  793. memset(&cell, 0, sizeof(cell)); \
  794. memset(&rh, 0, sizeof(rh)); \
  795. } while (0)
  796. #define CLEAR_ADDRS() do { \
  797. SMARTLIST_FOREACH(addrs, address_ttl_t *, a, \
  798. address_ttl_free(a); ); \
  799. smartlist_clear(addrs); \
  800. } while (0)
  801. #define SET_CELL(s) do { \
  802. CLEAR_CELL(); \
  803. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  804. rh.length = sizeof((s))-1; \
  805. rh.command = RELAY_COMMAND_RESOLVED; \
  806. errcode = -1; \
  807. } while (0)
  808. /* The cell format is one or more answers; each of the form
  809. * type [1 byte---0:hostname, 4:ipv4, 6:ipv6, f0:err-transient, f1:err]
  810. * length [1 byte]
  811. * body [length bytes]
  812. * ttl [4 bytes]
  813. */
  814. /* Let's try an empty cell */
  815. SET_CELL("");
  816. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  817. tt_int_op(errcode, ==, 0);
  818. tt_int_op(r, ==, 0);
  819. tt_int_op(smartlist_len(addrs), ==, 0);
  820. CLEAR_ADDRS(); /* redundant but let's be consistent */
  821. /* Cell with one ipv4 addr */
  822. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  823. tt_int_op(rh.length, ==, 10);
  824. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  825. tt_int_op(errcode, ==, 0);
  826. tt_int_op(r, ==, 0);
  827. tt_int_op(smartlist_len(addrs), ==, 1);
  828. a = smartlist_get(addrs, 0);
  829. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  830. tt_ptr_op(a->hostname, ==, NULL);
  831. tt_int_op(a->ttl, ==, 256);
  832. CLEAR_ADDRS();
  833. /* Cell with one ipv6 addr */
  834. SET_CELL("\x06\x10"
  835. "\x20\x02\x90\x90\x00\x00\x00\x00"
  836. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  837. "\x02\00\x00\x01");
  838. tt_int_op(rh.length, ==, 22);
  839. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  840. tt_int_op(errcode, ==, 0);
  841. tt_int_op(r, ==, 0);
  842. tt_int_op(smartlist_len(addrs), ==, 1);
  843. a = smartlist_get(addrs, 0);
  844. tt_str_op(fmt_addr(&a->addr), ==, "2002:9090::f0f0:abcd");
  845. tt_ptr_op(a->hostname, ==, NULL);
  846. tt_int_op(a->ttl, ==, 0x2000001);
  847. CLEAR_ADDRS();
  848. /* Cell with one hostname */
  849. SET_CELL("\x00\x11"
  850. "motherbrain.zebes"
  851. "\x00\00\x00\x00");
  852. tt_int_op(rh.length, ==, 23);
  853. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  854. tt_int_op(errcode, ==, 0);
  855. tt_int_op(r, ==, 0);
  856. tt_int_op(smartlist_len(addrs), ==, 1);
  857. a = smartlist_get(addrs, 0);
  858. tt_assert(tor_addr_is_null(&a->addr));
  859. tt_str_op(a->hostname, ==, "motherbrain.zebes");
  860. tt_int_op(a->ttl, ==, 0);
  861. CLEAR_ADDRS();
  862. #define LONG_NAME \
  863. "this-hostname-has-255-characters.in-order-to-test-whether-very-long.ho" \
  864. "stnames-are-accepted.i-am-putting-it-in-a-macro-because-although.this-" \
  865. "function-is-already-very-full.of-copy-and-pasted-stuff.having-this-app" \
  866. "ear-more-than-once-would-bother-me-somehow.is"
  867. tt_int_op(strlen(LONG_NAME), ==, 255);
  868. SET_CELL("\x00\xff"
  869. LONG_NAME
  870. "\x00\01\x00\x00");
  871. tt_int_op(rh.length, ==, 261);
  872. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  873. tt_int_op(errcode, ==, 0);
  874. tt_int_op(r, ==, 0);
  875. tt_int_op(smartlist_len(addrs), ==, 1);
  876. a = smartlist_get(addrs, 0);
  877. tt_assert(tor_addr_is_null(&a->addr));
  878. tt_str_op(a->hostname, ==, LONG_NAME);
  879. tt_int_op(a->ttl, ==, 65536);
  880. CLEAR_ADDRS();
  881. /* Cells with an error */
  882. SET_CELL("\xf0\x2b"
  883. "I'm sorry, Dave. I'm afraid I can't do that"
  884. "\x00\x11\x22\x33");
  885. tt_int_op(rh.length, ==, 49);
  886. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  887. tt_int_op(errcode, ==, RESOLVED_TYPE_ERROR_TRANSIENT);
  888. tt_int_op(r, ==, 0);
  889. tt_int_op(smartlist_len(addrs), ==, 0);
  890. CLEAR_ADDRS();
  891. SET_CELL("\xf1\x40"
  892. "This hostname is too important for me to allow you to resolve it"
  893. "\x00\x00\x00\x00");
  894. tt_int_op(rh.length, ==, 70);
  895. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  896. tt_int_op(errcode, ==, RESOLVED_TYPE_ERROR);
  897. tt_int_op(r, ==, 0);
  898. tt_int_op(smartlist_len(addrs), ==, 0);
  899. CLEAR_ADDRS();
  900. /* Cell with an unrecognized type */
  901. SET_CELL("\xee\x16"
  902. "fault in the AE35 unit"
  903. "\x09\x09\x01\x01");
  904. tt_int_op(rh.length, ==, 28);
  905. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  906. tt_int_op(errcode, ==, 0);
  907. tt_int_op(r, ==, 0);
  908. tt_int_op(smartlist_len(addrs), ==, 0);
  909. CLEAR_ADDRS();
  910. /* Cell with one of each */
  911. SET_CELL(/* unrecognized: */
  912. "\xee\x16"
  913. "fault in the AE35 unit"
  914. "\x09\x09\x01\x01"
  915. /* error: */
  916. "\xf0\x2b"
  917. "I'm sorry, Dave. I'm afraid I can't do that"
  918. "\x00\x11\x22\x33"
  919. /* IPv6: */
  920. "\x06\x10"
  921. "\x20\x02\x90\x90\x00\x00\x00\x00"
  922. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  923. "\x02\00\x00\x01"
  924. /* IPv4: */
  925. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  926. /* Hostname: */
  927. "\x00\x11"
  928. "motherbrain.zebes"
  929. "\x00\00\x00\x00"
  930. );
  931. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  932. tt_int_op(errcode, ==, 0); /* no error reported; we got answers */
  933. tt_int_op(r, ==, 0);
  934. tt_int_op(smartlist_len(addrs), ==, 3);
  935. a = smartlist_get(addrs, 0);
  936. tt_str_op(fmt_addr(&a->addr), ==, "2002:9090::f0f0:abcd");
  937. tt_ptr_op(a->hostname, ==, NULL);
  938. tt_int_op(a->ttl, ==, 0x2000001);
  939. a = smartlist_get(addrs, 1);
  940. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  941. tt_ptr_op(a->hostname, ==, NULL);
  942. tt_int_op(a->ttl, ==, 256);
  943. a = smartlist_get(addrs, 2);
  944. tt_assert(tor_addr_is_null(&a->addr));
  945. tt_str_op(a->hostname, ==, "motherbrain.zebes");
  946. tt_int_op(a->ttl, ==, 0);
  947. CLEAR_ADDRS();
  948. /* Cell with several of similar type */
  949. SET_CELL(/* IPv4 */
  950. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  951. "\x04\x04" "\x08\x08\x08\x08" "\x00\00\x01\x05"
  952. "\x04\x04" "\x7f\xb0\x02\xb0" "\x00\01\xff\xff"
  953. /* IPv6 */
  954. "\x06\x10"
  955. "\x20\x02\x90\x00\x00\x00\x00\x00"
  956. "\x00\x00\x00\x00\xca\xfe\xf0\x0d"
  957. "\x00\00\x00\x01"
  958. "\x06\x10"
  959. "\x20\x02\x90\x01\x00\x00\x00\x00"
  960. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  961. "\x00\00\x00\x03");
  962. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  963. tt_int_op(errcode, ==, 0);
  964. tt_int_op(r, ==, 0);
  965. tt_int_op(smartlist_len(addrs), ==, 5);
  966. a = smartlist_get(addrs, 0);
  967. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  968. tt_ptr_op(a->hostname, ==, NULL);
  969. tt_int_op(a->ttl, ==, 256);
  970. a = smartlist_get(addrs, 1);
  971. tt_str_op(fmt_addr(&a->addr), ==, "8.8.8.8");
  972. tt_ptr_op(a->hostname, ==, NULL);
  973. tt_int_op(a->ttl, ==, 261);
  974. a = smartlist_get(addrs, 2);
  975. tt_str_op(fmt_addr(&a->addr), ==, "127.176.2.176");
  976. tt_ptr_op(a->hostname, ==, NULL);
  977. tt_int_op(a->ttl, ==, 131071);
  978. a = smartlist_get(addrs, 3);
  979. tt_str_op(fmt_addr(&a->addr), ==, "2002:9000::cafe:f00d");
  980. tt_ptr_op(a->hostname, ==, NULL);
  981. tt_int_op(a->ttl, ==, 1);
  982. a = smartlist_get(addrs, 4);
  983. tt_str_op(fmt_addr(&a->addr), ==, "2002:9001::fa:cade");
  984. tt_ptr_op(a->hostname, ==, NULL);
  985. tt_int_op(a->ttl, ==, 3);
  986. CLEAR_ADDRS();
  987. /* Full cell */
  988. #define LONG_NAME2 \
  989. "this-name-has-231-characters.so-that-it-plus-LONG_NAME-can-completely-" \
  990. "fill-up-the-payload-of-a-cell.its-important-to-check-for-the-full-thin" \
  991. "g-case.to-avoid-off-by-one-errors.where-full-things-are-misreported-as" \
  992. ".overflowing-by-one.z"
  993. tt_int_op(strlen(LONG_NAME2), ==, 231);
  994. SET_CELL("\x00\xff"
  995. LONG_NAME
  996. "\x00\01\x00\x00"
  997. "\x00\xe7"
  998. LONG_NAME2
  999. "\x00\01\x00\x00");
  1000. tt_int_op(rh.length, ==, RELAY_PAYLOAD_SIZE);
  1001. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1002. tt_int_op(errcode, ==, 0);
  1003. tt_int_op(r, ==, 0);
  1004. tt_int_op(smartlist_len(addrs), ==, 2);
  1005. a = smartlist_get(addrs, 0);
  1006. tt_str_op(a->hostname, ==, LONG_NAME);
  1007. a = smartlist_get(addrs, 1);
  1008. tt_str_op(a->hostname, ==, LONG_NAME2);
  1009. CLEAR_ADDRS();
  1010. /* BAD CELLS */
  1011. /* Invalid length on an IPv4 */
  1012. SET_CELL("\x04\x03zzz1234");
  1013. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1014. tt_int_op(errcode, ==, 0);
  1015. tt_int_op(r, ==, -1);
  1016. tt_int_op(smartlist_len(addrs), ==, 0);
  1017. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1018. "\x04\x05zzzzz1234");
  1019. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1020. tt_int_op(errcode, ==, 0);
  1021. tt_int_op(r, ==, -1);
  1022. tt_int_op(smartlist_len(addrs), ==, 0);
  1023. /* Invalid length on an IPv6 */
  1024. SET_CELL("\x06\x03zzz1234");
  1025. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1026. tt_int_op(errcode, ==, 0);
  1027. tt_int_op(r, ==, -1);
  1028. tt_int_op(smartlist_len(addrs), ==, 0);
  1029. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1030. "\x06\x17wwwwwwwwwwwwwwwww1234");
  1031. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1032. tt_int_op(errcode, ==, 0);
  1033. tt_int_op(r, ==, -1);
  1034. tt_int_op(smartlist_len(addrs), ==, 0);
  1035. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1036. "\x06\x10xxxx");
  1037. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1038. tt_int_op(errcode, ==, 0);
  1039. tt_int_op(r, ==, -1);
  1040. tt_int_op(smartlist_len(addrs), ==, 0);
  1041. /* Empty hostname */
  1042. SET_CELL("\x00\x00xxxx");
  1043. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1044. tt_int_op(errcode, ==, 0);
  1045. tt_int_op(r, ==, -1);
  1046. tt_int_op(smartlist_len(addrs), ==, 0);
  1047. /* rh.length out of range */
  1048. CLEAR_CELL();
  1049. rh.length = 499;
  1050. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1051. tt_int_op(errcode, ==, 0);
  1052. tt_int_op(r, ==, -1);
  1053. tt_int_op(smartlist_len(addrs), ==, 0);
  1054. /* Item length extends beyond rh.length */
  1055. CLEAR_CELL();
  1056. SET_CELL("\x00\xff"
  1057. LONG_NAME
  1058. "\x00\01\x00\x00");
  1059. rh.length -= 1;
  1060. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1061. tt_int_op(r, ==, -1);
  1062. tt_int_op(smartlist_len(addrs), ==, 0);
  1063. rh.length -= 5;
  1064. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1065. tt_int_op(r, ==, -1);
  1066. tt_int_op(smartlist_len(addrs), ==, 0);
  1067. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  1068. rh.length -= 1;
  1069. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1070. tt_int_op(r, ==, -1);
  1071. tt_int_op(smartlist_len(addrs), ==, 0);
  1072. SET_CELL("\xee\x10"
  1073. "\x20\x02\x90\x01\x00\x00\x00\x00"
  1074. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  1075. "\x00\00\x00\x03");
  1076. rh.length -= 1;
  1077. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1078. tt_int_op(r, ==, -1);
  1079. tt_int_op(smartlist_len(addrs), ==, 0);
  1080. /* Truncated item after first character */
  1081. SET_CELL("\x04");
  1082. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1083. tt_int_op(r, ==, -1);
  1084. tt_int_op(smartlist_len(addrs), ==, 0);
  1085. SET_CELL("\xee");
  1086. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1087. tt_int_op(r, ==, -1);
  1088. tt_int_op(smartlist_len(addrs), ==, 0);
  1089. done:
  1090. CLEAR_ADDRS();
  1091. CLEAR_CELL();
  1092. smartlist_free(addrs);
  1093. #undef CLEAR_ADDRS
  1094. #undef CLEAR_CELL
  1095. }
  1096. #define TEST(name, flags) \
  1097. { #name, test_cfmt_ ## name, flags, 0, NULL }
  1098. struct testcase_t cell_format_tests[] = {
  1099. TEST(relay_header, 0),
  1100. TEST(begin_cells, 0),
  1101. TEST(connected_cells, 0),
  1102. TEST(create_cells, 0),
  1103. TEST(created_cells, 0),
  1104. TEST(extend_cells, 0),
  1105. TEST(extended_cells, 0),
  1106. TEST(resolved_cells, 0),
  1107. END_OF_TESTCASES
  1108. };