onion.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion.c
  8. * \brief Functions to queue create cells, wrap the various onionskin types,
  9. * and parse and create the CREATE cell and its allies.
  10. *
  11. * This module has a few functions, all related to the CREATE/CREATED
  12. * handshake that we use on links in order to create a circuit, and the
  13. * related EXTEND/EXTENDED handshake that we use over circuits in order to
  14. * extend them an additional hop.
  15. *
  16. * In this module, we provide a set of abstractions to create a uniform
  17. * interface over the three circuit extension handshakes that Tor has used
  18. * over the years (TAP, CREATE_FAST, and ntor). These handshakes are
  19. * implemented in onion_tap.c, onion_fast.c, and onion_ntor.c respectively.
  20. *
  21. * All[*] of these handshakes follow a similar pattern: a client, knowing
  22. * some key from the relay it wants to extend through, generates the
  23. * first part of a handshake. A relay receives that handshake, and sends
  24. * a reply. Once the client handles the reply, it knows that it is
  25. * talking to the right relay, and it shares some freshly negotiated key
  26. * material with that relay.
  27. *
  28. * We sometimes call the client's part of the handshake an "onionskin".
  29. * We do this because historically, Onion Routing used a multi-layer
  30. * structure called an "onion" to construct circuits. Each layer of the
  31. * onion contained key material chosen by the client, the identity of
  32. * the next relay in the circuit, and a smaller onion, encrypted with
  33. * the key of the next relay. When we changed Tor to use a telescoping
  34. * circuit extension design, it corresponded to sending each layer of the
  35. * onion separately -- as a series of onionskins.
  36. *
  37. * Clients invoke these functions when creating or extending a circuit,
  38. * from circuitbuild.c.
  39. *
  40. * Relays invoke these functions when they receive a CREATE or EXTEND
  41. * cell in command.c or relay.c, in order to queue the pending request.
  42. * They also invoke them from cpuworker.c, which handles dispatching
  43. * onionskin requests to different worker threads.
  44. *
  45. * <br>
  46. *
  47. * This module also handles:
  48. * <ul>
  49. * <li> Queueing incoming onionskins on the relay side before passing
  50. * them to worker threads.
  51. * <li>Expiring onionskins on the relay side if they have waited for
  52. * too long.
  53. * <li>Packaging private keys on the server side in order to pass
  54. * them to worker threads.
  55. * <li>Encoding and decoding CREATE, CREATED, CREATE2, and CREATED2 cells.
  56. * <li>Encoding and decodign EXTEND, EXTENDED, EXTEND2, and EXTENDED2
  57. * relay cells.
  58. * </ul>
  59. *
  60. * [*] The CREATE_FAST handshake is weaker than described here; see
  61. * onion_fast.c for more information.
  62. **/
  63. #include "or.h"
  64. #include "circuitbuild.h"
  65. #include "circuitlist.h"
  66. #include "config.h"
  67. #include "cpuworker.h"
  68. #include "networkstatus.h"
  69. #include "onion.h"
  70. #include "onion_fast.h"
  71. #include "onion_ntor.h"
  72. #include "onion_tap.h"
  73. #include "relay.h"
  74. #include "rephist.h"
  75. #include "router.h"
  76. /** Type for a linked list of circuits that are waiting for a free CPU worker
  77. * to process a waiting onion handshake. */
  78. typedef struct onion_queue_t {
  79. TOR_TAILQ_ENTRY(onion_queue_t) next;
  80. or_circuit_t *circ;
  81. uint16_t handshake_type;
  82. create_cell_t *onionskin;
  83. time_t when_added;
  84. } onion_queue_t;
  85. /** 5 seconds on the onion queue til we just send back a destroy */
  86. #define ONIONQUEUE_WAIT_CUTOFF 5
  87. /** Array of queues of circuits waiting for CPU workers. An element is NULL
  88. * if that queue is empty.*/
  89. static TOR_TAILQ_HEAD(onion_queue_head_t, onion_queue_t)
  90. ol_list[MAX_ONION_HANDSHAKE_TYPE+1] =
  91. { TOR_TAILQ_HEAD_INITIALIZER(ol_list[0]), /* tap */
  92. TOR_TAILQ_HEAD_INITIALIZER(ol_list[1]), /* fast */
  93. TOR_TAILQ_HEAD_INITIALIZER(ol_list[2]), /* ntor */
  94. };
  95. /** Number of entries of each type currently in each element of ol_list[]. */
  96. static int ol_entries[MAX_ONION_HANDSHAKE_TYPE+1];
  97. static int num_ntors_per_tap(void);
  98. static void onion_queue_entry_remove(onion_queue_t *victim);
  99. /* XXXX Check lengths vs MAX_ONIONSKIN_{CHALLENGE,REPLY}_LEN.
  100. *
  101. * (By which I think I meant, "make sure that no
  102. * X_ONIONSKIN_CHALLENGE/REPLY_LEN is greater than
  103. * MAX_ONIONSKIN_CHALLENGE/REPLY_LEN." Also, make sure that we can pass
  104. * over-large values via EXTEND2/EXTENDED2, for future-compatibility.*/
  105. /** Return true iff we have room to queue another onionskin of type
  106. * <b>type</b>. */
  107. static int
  108. have_room_for_onionskin(uint16_t type)
  109. {
  110. const or_options_t *options = get_options();
  111. int num_cpus;
  112. uint64_t tap_usec, ntor_usec;
  113. uint64_t ntor_during_tap_usec, tap_during_ntor_usec;
  114. /* If we've got fewer than 50 entries, we always have room for one more. */
  115. if (ol_entries[type] < 50)
  116. return 1;
  117. num_cpus = get_num_cpus(options);
  118. /* Compute how many microseconds we'd expect to need to clear all
  119. * onionskins in various combinations of the queues. */
  120. /* How long would it take to process all the TAP cells in the queue? */
  121. tap_usec = estimated_usec_for_onionskins(
  122. ol_entries[ONION_HANDSHAKE_TYPE_TAP],
  123. ONION_HANDSHAKE_TYPE_TAP) / num_cpus;
  124. /* How long would it take to process all the NTor cells in the queue? */
  125. ntor_usec = estimated_usec_for_onionskins(
  126. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  127. ONION_HANDSHAKE_TYPE_NTOR) / num_cpus;
  128. /* How long would it take to process the tap cells that we expect to
  129. * process while draining the ntor queue? */
  130. tap_during_ntor_usec = estimated_usec_for_onionskins(
  131. MIN(ol_entries[ONION_HANDSHAKE_TYPE_TAP],
  132. ol_entries[ONION_HANDSHAKE_TYPE_NTOR] / num_ntors_per_tap()),
  133. ONION_HANDSHAKE_TYPE_TAP) / num_cpus;
  134. /* How long would it take to process the ntor cells that we expect to
  135. * process while draining the tap queue? */
  136. ntor_during_tap_usec = estimated_usec_for_onionskins(
  137. MIN(ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  138. ol_entries[ONION_HANDSHAKE_TYPE_TAP] * num_ntors_per_tap()),
  139. ONION_HANDSHAKE_TYPE_NTOR) / num_cpus;
  140. /* See whether that exceeds MaxOnionQueueDelay. If so, we can't queue
  141. * this. */
  142. if (type == ONION_HANDSHAKE_TYPE_NTOR &&
  143. (ntor_usec + tap_during_ntor_usec) / 1000 >
  144. (uint64_t)options->MaxOnionQueueDelay)
  145. return 0;
  146. if (type == ONION_HANDSHAKE_TYPE_TAP &&
  147. (tap_usec + ntor_during_tap_usec) / 1000 >
  148. (uint64_t)options->MaxOnionQueueDelay)
  149. return 0;
  150. /* If we support the ntor handshake, then don't let TAP handshakes use
  151. * more than 2/3 of the space on the queue. */
  152. if (type == ONION_HANDSHAKE_TYPE_TAP &&
  153. tap_usec / 1000 > (uint64_t)options->MaxOnionQueueDelay * 2 / 3)
  154. return 0;
  155. return 1;
  156. }
  157. /** Add <b>circ</b> to the end of ol_list and return 0, except
  158. * if ol_list is too long, in which case do nothing and return -1.
  159. */
  160. int
  161. onion_pending_add(or_circuit_t *circ, create_cell_t *onionskin)
  162. {
  163. onion_queue_t *tmp;
  164. time_t now = time(NULL);
  165. if (onionskin->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  166. /* LCOV_EXCL_START
  167. * We should have rejected this far before this point */
  168. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  169. onionskin->handshake_type);
  170. return -1;
  171. /* LCOV_EXCL_STOP */
  172. }
  173. tmp = tor_malloc_zero(sizeof(onion_queue_t));
  174. tmp->circ = circ;
  175. tmp->handshake_type = onionskin->handshake_type;
  176. tmp->onionskin = onionskin;
  177. tmp->when_added = now;
  178. if (!have_room_for_onionskin(onionskin->handshake_type)) {
  179. #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
  180. static ratelim_t last_warned =
  181. RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
  182. char *m;
  183. if (onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR &&
  184. (m = rate_limit_log(&last_warned, approx_time()))) {
  185. log_warn(LD_GENERAL,
  186. "Your computer is too slow to handle this many circuit "
  187. "creation requests! Please consider using the "
  188. "MaxAdvertisedBandwidth config option or choosing a more "
  189. "restricted exit policy.%s",m);
  190. tor_free(m);
  191. }
  192. tor_free(tmp);
  193. return -1;
  194. }
  195. ++ol_entries[onionskin->handshake_type];
  196. log_info(LD_OR, "New create (%s). Queues now ntor=%d and tap=%d.",
  197. onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR ? "ntor" : "tap",
  198. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  199. ol_entries[ONION_HANDSHAKE_TYPE_TAP]);
  200. circ->onionqueue_entry = tmp;
  201. TOR_TAILQ_INSERT_TAIL(&ol_list[onionskin->handshake_type], tmp, next);
  202. /* cull elderly requests. */
  203. while (1) {
  204. onion_queue_t *head = TOR_TAILQ_FIRST(&ol_list[onionskin->handshake_type]);
  205. if (now - head->when_added < (time_t)ONIONQUEUE_WAIT_CUTOFF)
  206. break;
  207. circ = head->circ;
  208. circ->onionqueue_entry = NULL;
  209. onion_queue_entry_remove(head);
  210. log_info(LD_CIRC,
  211. "Circuit create request is too old; canceling due to overload.");
  212. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  213. }
  214. return 0;
  215. }
  216. /** Return a fairness parameter, to prefer processing NTOR style
  217. * handshakes but still slowly drain the TAP queue so we don't starve
  218. * it entirely. */
  219. static int
  220. num_ntors_per_tap(void)
  221. {
  222. #define DEFAULT_NUM_NTORS_PER_TAP 10
  223. #define MIN_NUM_NTORS_PER_TAP 1
  224. #define MAX_NUM_NTORS_PER_TAP 100000
  225. return networkstatus_get_param(NULL, "NumNTorsPerTAP",
  226. DEFAULT_NUM_NTORS_PER_TAP,
  227. MIN_NUM_NTORS_PER_TAP,
  228. MAX_NUM_NTORS_PER_TAP);
  229. }
  230. /** Choose which onion queue we'll pull from next. If one is empty choose
  231. * the other; if they both have elements, load balance across them but
  232. * favoring NTOR. */
  233. static uint16_t
  234. decide_next_handshake_type(void)
  235. {
  236. /* The number of times we've chosen ntor lately when both were available. */
  237. static int recently_chosen_ntors = 0;
  238. if (!ol_entries[ONION_HANDSHAKE_TYPE_NTOR])
  239. return ONION_HANDSHAKE_TYPE_TAP; /* no ntors? try tap */
  240. if (!ol_entries[ONION_HANDSHAKE_TYPE_TAP]) {
  241. /* Nick wants us to prioritize new tap requests when there aren't
  242. * any in the queue and we've processed k ntor cells since the last
  243. * tap cell. This strategy is maybe a good idea, since it starves tap
  244. * less in the case where tap is rare, or maybe a poor idea, since it
  245. * makes the new tap cell unfairly jump in front of ntor cells that
  246. * got here first. In any case this edge case will only become relevant
  247. * once tap is rare. We should reevaluate whether we like this decision
  248. * once tap gets more rare. */
  249. if (ol_entries[ONION_HANDSHAKE_TYPE_NTOR] &&
  250. recently_chosen_ntors <= num_ntors_per_tap())
  251. ++recently_chosen_ntors;
  252. return ONION_HANDSHAKE_TYPE_NTOR; /* no taps? try ntor */
  253. }
  254. /* They both have something queued. Pick ntor if we haven't done that
  255. * too much lately. */
  256. if (++recently_chosen_ntors <= num_ntors_per_tap()) {
  257. return ONION_HANDSHAKE_TYPE_NTOR;
  258. }
  259. /* Else, it's time to let tap have its turn. */
  260. recently_chosen_ntors = 0;
  261. return ONION_HANDSHAKE_TYPE_TAP;
  262. }
  263. /** Remove the highest priority item from ol_list[] and return it, or
  264. * return NULL if the lists are empty.
  265. */
  266. or_circuit_t *
  267. onion_next_task(create_cell_t **onionskin_out)
  268. {
  269. or_circuit_t *circ;
  270. uint16_t handshake_to_choose = decide_next_handshake_type();
  271. onion_queue_t *head = TOR_TAILQ_FIRST(&ol_list[handshake_to_choose]);
  272. if (!head)
  273. return NULL; /* no onions pending, we're done */
  274. tor_assert(head->circ);
  275. tor_assert(head->handshake_type <= MAX_ONION_HANDSHAKE_TYPE);
  276. // tor_assert(head->circ->p_chan); /* make sure it's still valid */
  277. /* XXX I only commented out the above line to make the unit tests
  278. * more manageable. That's probably not good long-term. -RD */
  279. circ = head->circ;
  280. if (head->onionskin)
  281. --ol_entries[head->handshake_type];
  282. log_info(LD_OR, "Processing create (%s). Queues now ntor=%d and tap=%d.",
  283. head->handshake_type == ONION_HANDSHAKE_TYPE_NTOR ? "ntor" : "tap",
  284. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  285. ol_entries[ONION_HANDSHAKE_TYPE_TAP]);
  286. *onionskin_out = head->onionskin;
  287. head->onionskin = NULL; /* prevent free. */
  288. circ->onionqueue_entry = NULL;
  289. onion_queue_entry_remove(head);
  290. return circ;
  291. }
  292. /** Return the number of <b>handshake_type</b>-style create requests pending.
  293. */
  294. int
  295. onion_num_pending(uint16_t handshake_type)
  296. {
  297. return ol_entries[handshake_type];
  298. }
  299. /** Go through ol_list, find the onion_queue_t element which points to
  300. * circ, remove and free that element. Leave circ itself alone.
  301. */
  302. void
  303. onion_pending_remove(or_circuit_t *circ)
  304. {
  305. onion_queue_t *victim;
  306. if (!circ)
  307. return;
  308. victim = circ->onionqueue_entry;
  309. if (victim)
  310. onion_queue_entry_remove(victim);
  311. cpuworker_cancel_circ_handshake(circ);
  312. }
  313. /** Remove a queue entry <b>victim</b> from the queue, unlinking it from
  314. * its circuit and freeing it and any structures it owns.*/
  315. static void
  316. onion_queue_entry_remove(onion_queue_t *victim)
  317. {
  318. if (victim->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  319. /* LCOV_EXCL_START
  320. * We should have rejected this far before this point */
  321. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  322. victim->handshake_type);
  323. /* XXX leaks */
  324. return;
  325. /* LCOV_EXCL_STOP */
  326. }
  327. TOR_TAILQ_REMOVE(&ol_list[victim->handshake_type], victim, next);
  328. if (victim->circ)
  329. victim->circ->onionqueue_entry = NULL;
  330. if (victim->onionskin)
  331. --ol_entries[victim->handshake_type];
  332. tor_free(victim->onionskin);
  333. tor_free(victim);
  334. }
  335. /** Remove all circuits from the pending list. Called from tor_free_all. */
  336. void
  337. clear_pending_onions(void)
  338. {
  339. onion_queue_t *victim, *next;
  340. int i;
  341. for (i=0; i<=MAX_ONION_HANDSHAKE_TYPE; i++) {
  342. for (victim = TOR_TAILQ_FIRST(&ol_list[i]); victim; victim = next) {
  343. next = TOR_TAILQ_NEXT(victim,next);
  344. onion_queue_entry_remove(victim);
  345. }
  346. tor_assert(TOR_TAILQ_EMPTY(&ol_list[i]));
  347. }
  348. memset(ol_entries, 0, sizeof(ol_entries));
  349. }
  350. /* ============================================================ */
  351. /** Return a new server_onion_keys_t object with all of the keys
  352. * and other info we might need to do onion handshakes. (We make a copy of
  353. * our keys for each cpuworker to avoid race conditions with the main thread,
  354. * and to avoid locking) */
  355. server_onion_keys_t *
  356. server_onion_keys_new(void)
  357. {
  358. server_onion_keys_t *keys = tor_malloc_zero(sizeof(server_onion_keys_t));
  359. memcpy(keys->my_identity, router_get_my_id_digest(), DIGEST_LEN);
  360. dup_onion_keys(&keys->onion_key, &keys->last_onion_key);
  361. keys->curve25519_key_map = construct_ntor_key_map();
  362. keys->junk_keypair = tor_malloc_zero(sizeof(curve25519_keypair_t));
  363. curve25519_keypair_generate(keys->junk_keypair, 0);
  364. return keys;
  365. }
  366. /** Release all storage held in <b>keys</b>. */
  367. void
  368. server_onion_keys_free(server_onion_keys_t *keys)
  369. {
  370. if (! keys)
  371. return;
  372. crypto_pk_free(keys->onion_key);
  373. crypto_pk_free(keys->last_onion_key);
  374. ntor_key_map_free(keys->curve25519_key_map);
  375. tor_free(keys->junk_keypair);
  376. memwipe(keys, 0, sizeof(server_onion_keys_t));
  377. tor_free(keys);
  378. }
  379. /** Release whatever storage is held in <b>state</b>, depending on its
  380. * type, and clear its pointer. */
  381. void
  382. onion_handshake_state_release(onion_handshake_state_t *state)
  383. {
  384. switch (state->tag) {
  385. case ONION_HANDSHAKE_TYPE_TAP:
  386. crypto_dh_free(state->u.tap);
  387. state->u.tap = NULL;
  388. break;
  389. case ONION_HANDSHAKE_TYPE_FAST:
  390. fast_handshake_state_free(state->u.fast);
  391. state->u.fast = NULL;
  392. break;
  393. case ONION_HANDSHAKE_TYPE_NTOR:
  394. ntor_handshake_state_free(state->u.ntor);
  395. state->u.ntor = NULL;
  396. break;
  397. default:
  398. /* LCOV_EXCL_START
  399. * This state should not even exist. */
  400. log_warn(LD_BUG, "called with unknown handshake state type %d",
  401. (int)state->tag);
  402. tor_fragile_assert();
  403. /* LCOV_EXCL_STOP */
  404. }
  405. }
  406. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  407. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  408. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  409. * Return -1 on failure, and the length of the onionskin on acceptance.
  410. */
  411. int
  412. onion_skin_create(int type,
  413. const extend_info_t *node,
  414. onion_handshake_state_t *state_out,
  415. uint8_t *onion_skin_out)
  416. {
  417. int r = -1;
  418. switch (type) {
  419. case ONION_HANDSHAKE_TYPE_TAP:
  420. if (!node->onion_key)
  421. return -1;
  422. if (onion_skin_TAP_create(node->onion_key,
  423. &state_out->u.tap,
  424. (char*)onion_skin_out) < 0)
  425. return -1;
  426. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  427. break;
  428. case ONION_HANDSHAKE_TYPE_FAST:
  429. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  430. return -1;
  431. r = CREATE_FAST_LEN;
  432. break;
  433. case ONION_HANDSHAKE_TYPE_NTOR:
  434. if (!extend_info_supports_ntor(node))
  435. return -1;
  436. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  437. &node->curve25519_onion_key,
  438. &state_out->u.ntor,
  439. onion_skin_out) < 0)
  440. return -1;
  441. r = NTOR_ONIONSKIN_LEN;
  442. break;
  443. default:
  444. /* LCOV_EXCL_START
  445. * We should never try to create an impossible handshake type. */
  446. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  447. tor_fragile_assert();
  448. r = -1;
  449. /* LCOV_EXCL_STOP */
  450. }
  451. if (r > 0)
  452. state_out->tag = (uint16_t) type;
  453. return r;
  454. }
  455. /** Perform the second (server-side) step of a circuit-creation handshake of
  456. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  457. * using the keys in <b>keys</b>. On success, write our response into
  458. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  459. * in <b>keys_out_len</b>, a hidden service nonce to <b>rend_nonce_out</b>,
  460. * and return the length of the reply. On failure, return -1.
  461. */
  462. int
  463. onion_skin_server_handshake(int type,
  464. const uint8_t *onion_skin, size_t onionskin_len,
  465. const server_onion_keys_t *keys,
  466. uint8_t *reply_out,
  467. uint8_t *keys_out, size_t keys_out_len,
  468. uint8_t *rend_nonce_out)
  469. {
  470. int r = -1;
  471. switch (type) {
  472. case ONION_HANDSHAKE_TYPE_TAP:
  473. if (onionskin_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  474. return -1;
  475. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  476. keys->onion_key, keys->last_onion_key,
  477. (char*)reply_out,
  478. (char*)keys_out, keys_out_len)<0)
  479. return -1;
  480. r = TAP_ONIONSKIN_REPLY_LEN;
  481. memcpy(rend_nonce_out, reply_out+DH_KEY_LEN, DIGEST_LEN);
  482. break;
  483. case ONION_HANDSHAKE_TYPE_FAST:
  484. if (onionskin_len != CREATE_FAST_LEN)
  485. return -1;
  486. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  487. return -1;
  488. r = CREATED_FAST_LEN;
  489. memcpy(rend_nonce_out, reply_out+DIGEST_LEN, DIGEST_LEN);
  490. break;
  491. case ONION_HANDSHAKE_TYPE_NTOR:
  492. if (onionskin_len < NTOR_ONIONSKIN_LEN)
  493. return -1;
  494. {
  495. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  496. uint8_t *keys_tmp = tor_malloc(keys_out_len + DIGEST_LEN);
  497. if (onion_skin_ntor_server_handshake(
  498. onion_skin, keys->curve25519_key_map,
  499. keys->junk_keypair,
  500. keys->my_identity,
  501. reply_out, keys_tmp, keys_tmp_len)<0) {
  502. tor_free(keys_tmp);
  503. return -1;
  504. }
  505. memcpy(keys_out, keys_tmp, keys_out_len);
  506. memcpy(rend_nonce_out, keys_tmp+keys_out_len, DIGEST_LEN);
  507. memwipe(keys_tmp, 0, keys_tmp_len);
  508. tor_free(keys_tmp);
  509. r = NTOR_REPLY_LEN;
  510. }
  511. break;
  512. default:
  513. /* LCOV_EXCL_START
  514. * We should have rejected this far before this point */
  515. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  516. tor_fragile_assert();
  517. return -1;
  518. /* LCOV_EXCL_STOP */
  519. }
  520. return r;
  521. }
  522. /** Perform the final (client-side) step of a circuit-creation handshake of
  523. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  524. * server's response in <b>reply</b>. On success, generate <b>keys_out_len</b>
  525. * bytes worth of key material in <b>keys_out_len</b>, set
  526. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  527. * establish introduction points at this hop, and return 0. On failure,
  528. * return -1, and set *msg_out to an error message if this is worth
  529. * complaining to the user about. */
  530. int
  531. onion_skin_client_handshake(int type,
  532. const onion_handshake_state_t *handshake_state,
  533. const uint8_t *reply, size_t reply_len,
  534. uint8_t *keys_out, size_t keys_out_len,
  535. uint8_t *rend_authenticator_out,
  536. const char **msg_out)
  537. {
  538. if (handshake_state->tag != type)
  539. return -1;
  540. switch (type) {
  541. case ONION_HANDSHAKE_TYPE_TAP:
  542. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  543. if (msg_out)
  544. *msg_out = "TAP reply was not of the correct length.";
  545. return -1;
  546. }
  547. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  548. (const char*)reply,
  549. (char *)keys_out, keys_out_len,
  550. msg_out) < 0)
  551. return -1;
  552. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  553. return 0;
  554. case ONION_HANDSHAKE_TYPE_FAST:
  555. if (reply_len != CREATED_FAST_LEN) {
  556. if (msg_out)
  557. *msg_out = "TAP reply was not of the correct length.";
  558. return -1;
  559. }
  560. if (fast_client_handshake(handshake_state->u.fast, reply,
  561. keys_out, keys_out_len, msg_out) < 0)
  562. return -1;
  563. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  564. return 0;
  565. case ONION_HANDSHAKE_TYPE_NTOR:
  566. if (reply_len < NTOR_REPLY_LEN) {
  567. if (msg_out)
  568. *msg_out = "ntor reply was not of the correct length.";
  569. return -1;
  570. }
  571. {
  572. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  573. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  574. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  575. reply,
  576. keys_tmp, keys_tmp_len, msg_out) < 0) {
  577. tor_free(keys_tmp);
  578. return -1;
  579. }
  580. memcpy(keys_out, keys_tmp, keys_out_len);
  581. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  582. memwipe(keys_tmp, 0, keys_tmp_len);
  583. tor_free(keys_tmp);
  584. }
  585. return 0;
  586. default:
  587. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  588. tor_fragile_assert();
  589. return -1;
  590. }
  591. }
  592. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  593. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  594. * recognize. */
  595. static int
  596. check_create_cell(const create_cell_t *cell, int unknown_ok)
  597. {
  598. switch (cell->cell_type) {
  599. case CELL_CREATE:
  600. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  601. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  602. return -1;
  603. break;
  604. case CELL_CREATE_FAST:
  605. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  606. return -1;
  607. break;
  608. case CELL_CREATE2:
  609. break;
  610. default:
  611. return -1;
  612. }
  613. switch (cell->handshake_type) {
  614. case ONION_HANDSHAKE_TYPE_TAP:
  615. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  616. return -1;
  617. break;
  618. case ONION_HANDSHAKE_TYPE_FAST:
  619. if (cell->handshake_len != CREATE_FAST_LEN)
  620. return -1;
  621. break;
  622. case ONION_HANDSHAKE_TYPE_NTOR:
  623. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  624. return -1;
  625. break;
  626. default:
  627. if (! unknown_ok)
  628. return -1;
  629. }
  630. return 0;
  631. }
  632. /** Write the various parameters into the create cell. Separate from
  633. * create_cell_parse() to make unit testing easier.
  634. */
  635. void
  636. create_cell_init(create_cell_t *cell_out, uint8_t cell_type,
  637. uint16_t handshake_type, uint16_t handshake_len,
  638. const uint8_t *onionskin)
  639. {
  640. memset(cell_out, 0, sizeof(*cell_out));
  641. cell_out->cell_type = cell_type;
  642. cell_out->handshake_type = handshake_type;
  643. cell_out->handshake_len = handshake_len;
  644. memcpy(cell_out->onionskin, onionskin, handshake_len);
  645. }
  646. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  647. * <b>p_len</b> bytes long, and use it to fill the fields of
  648. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  649. *
  650. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  651. * this function is also used for parsing those.
  652. */
  653. static int
  654. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  655. {
  656. uint16_t handshake_type, handshake_len;
  657. if (p_len < 4)
  658. return -1;
  659. handshake_type = ntohs(get_uint16(p));
  660. handshake_len = ntohs(get_uint16(p+2));
  661. if (handshake_len > CELL_PAYLOAD_SIZE - 4 || handshake_len > p_len - 4)
  662. return -1;
  663. if (handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  664. return -1;
  665. create_cell_init(cell_out, CELL_CREATE2, handshake_type, handshake_len,
  666. p+4);
  667. return 0;
  668. }
  669. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  670. * TAP payload is really an ntor payload. We'd do away with this if every
  671. * relay supported EXTEND2, but we want to be able to extend from A to B with
  672. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  673. * CREATE2 cell.
  674. **/
  675. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  676. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  677. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  678. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  679. int
  680. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  681. {
  682. switch (cell_in->command) {
  683. case CELL_CREATE:
  684. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  685. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  686. NTOR_ONIONSKIN_LEN, cell_in->payload+16);
  687. } else {
  688. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  689. TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->payload);
  690. }
  691. break;
  692. case CELL_CREATE_FAST:
  693. create_cell_init(cell_out, CELL_CREATE_FAST, ONION_HANDSHAKE_TYPE_FAST,
  694. CREATE_FAST_LEN, cell_in->payload);
  695. break;
  696. case CELL_CREATE2:
  697. if (parse_create2_payload(cell_out, cell_in->payload,
  698. CELL_PAYLOAD_SIZE) < 0)
  699. return -1;
  700. break;
  701. default:
  702. return -1;
  703. }
  704. return check_create_cell(cell_out, 0);
  705. }
  706. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  707. static int
  708. check_created_cell(const created_cell_t *cell)
  709. {
  710. switch (cell->cell_type) {
  711. case CELL_CREATED:
  712. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN &&
  713. cell->handshake_len != NTOR_REPLY_LEN)
  714. return -1;
  715. break;
  716. case CELL_CREATED_FAST:
  717. if (cell->handshake_len != CREATED_FAST_LEN)
  718. return -1;
  719. break;
  720. case CELL_CREATED2:
  721. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  722. return -1;
  723. break;
  724. }
  725. return 0;
  726. }
  727. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  728. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  729. int
  730. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  731. {
  732. memset(cell_out, 0, sizeof(*cell_out));
  733. switch (cell_in->command) {
  734. case CELL_CREATED:
  735. cell_out->cell_type = CELL_CREATED;
  736. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  737. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  738. break;
  739. case CELL_CREATED_FAST:
  740. cell_out->cell_type = CELL_CREATED_FAST;
  741. cell_out->handshake_len = CREATED_FAST_LEN;
  742. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  743. break;
  744. case CELL_CREATED2:
  745. {
  746. const uint8_t *p = cell_in->payload;
  747. cell_out->cell_type = CELL_CREATED2;
  748. cell_out->handshake_len = ntohs(get_uint16(p));
  749. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  750. return -1;
  751. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  752. break;
  753. }
  754. }
  755. return check_created_cell(cell_out);
  756. }
  757. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  758. static int
  759. check_extend_cell(const extend_cell_t *cell)
  760. {
  761. if (tor_digest_is_zero((const char*)cell->node_id))
  762. return -1;
  763. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  764. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  765. return -1;
  766. if (cell->create_cell.cell_type == CELL_CREATE) {
  767. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  768. return -1;
  769. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  770. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  771. cell->cell_type != RELAY_COMMAND_EXTEND)
  772. return -1;
  773. } else {
  774. /* In particular, no CREATE_FAST cells are allowed */
  775. return -1;
  776. }
  777. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  778. return -1;
  779. return check_create_cell(&cell->create_cell, 1);
  780. }
  781. /** Protocol constants for specifier types in EXTEND2
  782. * @{
  783. */
  784. #define SPECTYPE_IPV4 0
  785. #define SPECTYPE_IPV6 1
  786. #define SPECTYPE_LEGACY_ID 2
  787. /** @} */
  788. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  789. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  790. * 0 on success, -1 on failure. */
  791. int
  792. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  793. const uint8_t *payload, size_t payload_length)
  794. {
  795. const uint8_t *eop;
  796. memset(cell_out, 0, sizeof(*cell_out));
  797. if (payload_length > RELAY_PAYLOAD_SIZE)
  798. return -1;
  799. eop = payload + payload_length;
  800. switch (command) {
  801. case RELAY_COMMAND_EXTEND:
  802. {
  803. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  804. return -1;
  805. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  806. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  807. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  808. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  809. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  810. cell_out->create_cell.cell_type = CELL_CREATE2;
  811. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  812. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  813. memcpy(cell_out->create_cell.onionskin, payload + 22,
  814. NTOR_ONIONSKIN_LEN);
  815. } else {
  816. cell_out->create_cell.cell_type = CELL_CREATE;
  817. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  818. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  819. memcpy(cell_out->create_cell.onionskin, payload + 6,
  820. TAP_ONIONSKIN_CHALLENGE_LEN);
  821. }
  822. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  823. DIGEST_LEN);
  824. break;
  825. }
  826. case RELAY_COMMAND_EXTEND2:
  827. {
  828. uint8_t n_specs, spectype, speclen;
  829. int i;
  830. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  831. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  832. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  833. if (payload_length == 0)
  834. return -1;
  835. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  836. n_specs = *payload++;
  837. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  838. * address, and the node ID, and ignore everything else */
  839. for (i = 0; i < n_specs; ++i) {
  840. if (eop - payload < 2)
  841. return -1;
  842. spectype = payload[0];
  843. speclen = payload[1];
  844. payload += 2;
  845. if (eop - payload < speclen)
  846. return -1;
  847. switch (spectype) {
  848. case SPECTYPE_IPV4:
  849. if (speclen != 6)
  850. return -1;
  851. if (!found_ipv4) {
  852. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  853. get_uint32(payload));
  854. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  855. found_ipv4 = 1;
  856. }
  857. break;
  858. case SPECTYPE_IPV6:
  859. if (speclen != 18)
  860. return -1;
  861. if (!found_ipv6) {
  862. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  863. (const char*)payload);
  864. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  865. found_ipv6 = 1;
  866. }
  867. break;
  868. case SPECTYPE_LEGACY_ID:
  869. if (speclen != 20)
  870. return -1;
  871. if (found_id)
  872. return -1;
  873. memcpy(cell_out->node_id, payload, 20);
  874. found_id = 1;
  875. break;
  876. }
  877. payload += speclen;
  878. }
  879. if (!found_id || !found_ipv4)
  880. return -1;
  881. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  882. return -1;
  883. break;
  884. }
  885. default:
  886. return -1;
  887. }
  888. return check_extend_cell(cell_out);
  889. }
  890. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  891. static int
  892. check_extended_cell(const extended_cell_t *cell)
  893. {
  894. if (cell->created_cell.cell_type == CELL_CREATED) {
  895. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  896. return -1;
  897. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  898. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  899. return -1;
  900. } else {
  901. return -1;
  902. }
  903. return check_created_cell(&cell->created_cell);
  904. }
  905. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  906. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  907. * 0 on success, -1 on failure. */
  908. int
  909. extended_cell_parse(extended_cell_t *cell_out,
  910. const uint8_t command, const uint8_t *payload,
  911. size_t payload_len)
  912. {
  913. memset(cell_out, 0, sizeof(*cell_out));
  914. if (payload_len > RELAY_PAYLOAD_SIZE)
  915. return -1;
  916. switch (command) {
  917. case RELAY_COMMAND_EXTENDED:
  918. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  919. return -1;
  920. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  921. cell_out->created_cell.cell_type = CELL_CREATED;
  922. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  923. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  924. break;
  925. case RELAY_COMMAND_EXTENDED2:
  926. {
  927. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  928. cell_out->created_cell.cell_type = CELL_CREATED2;
  929. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  930. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  931. cell_out->created_cell.handshake_len > payload_len - 2)
  932. return -1;
  933. memcpy(cell_out->created_cell.reply, payload+2,
  934. cell_out->created_cell.handshake_len);
  935. }
  936. break;
  937. default:
  938. return -1;
  939. }
  940. return check_extended_cell(cell_out);
  941. }
  942. /** Fill <b>cell_out</b> with a correctly formatted version of the
  943. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  944. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  945. static int
  946. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  947. int relayed)
  948. {
  949. uint8_t *p;
  950. size_t space;
  951. if (check_create_cell(cell_in, relayed) < 0)
  952. return -1;
  953. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  954. cell_out->command = cell_in->cell_type;
  955. p = cell_out->payload;
  956. space = sizeof(cell_out->payload);
  957. switch (cell_in->cell_type) {
  958. case CELL_CREATE:
  959. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  960. memcpy(p, NTOR_CREATE_MAGIC, 16);
  961. p += 16;
  962. space -= 16;
  963. }
  964. /* Fall through */
  965. case CELL_CREATE_FAST:
  966. tor_assert(cell_in->handshake_len <= space);
  967. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  968. break;
  969. case CELL_CREATE2:
  970. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  971. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  972. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  973. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  974. break;
  975. default:
  976. return -1;
  977. }
  978. return 0;
  979. }
  980. int
  981. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  982. {
  983. return create_cell_format_impl(cell_out, cell_in, 0);
  984. }
  985. int
  986. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  987. {
  988. return create_cell_format_impl(cell_out, cell_in, 1);
  989. }
  990. /** Fill <b>cell_out</b> with a correctly formatted version of the
  991. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  992. * failure. */
  993. int
  994. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  995. {
  996. if (check_created_cell(cell_in) < 0)
  997. return -1;
  998. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  999. cell_out->command = cell_in->cell_type;
  1000. switch (cell_in->cell_type) {
  1001. case CELL_CREATED:
  1002. case CELL_CREATED_FAST:
  1003. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  1004. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  1005. break;
  1006. case CELL_CREATED2:
  1007. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  1008. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  1009. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  1010. break;
  1011. default:
  1012. return -1;
  1013. }
  1014. return 0;
  1015. }
  1016. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  1017. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1018. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1019. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1020. int
  1021. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  1022. uint8_t *payload_out, const extend_cell_t *cell_in)
  1023. {
  1024. uint8_t *p, *eop;
  1025. if (check_extend_cell(cell_in) < 0)
  1026. return -1;
  1027. p = payload_out;
  1028. eop = payload_out + RELAY_PAYLOAD_SIZE;
  1029. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1030. switch (cell_in->cell_type) {
  1031. case RELAY_COMMAND_EXTEND:
  1032. {
  1033. *command_out = RELAY_COMMAND_EXTEND;
  1034. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  1035. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  1036. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  1037. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  1038. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  1039. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  1040. } else {
  1041. memcpy(p+6, cell_in->create_cell.onionskin,
  1042. TAP_ONIONSKIN_CHALLENGE_LEN);
  1043. }
  1044. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  1045. }
  1046. break;
  1047. case RELAY_COMMAND_EXTEND2:
  1048. {
  1049. uint8_t n = 2;
  1050. *command_out = RELAY_COMMAND_EXTEND2;
  1051. *p++ = n; /* 2 identifiers */
  1052. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  1053. *p++ = 6; /* It's 6 bytes long. */
  1054. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  1055. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  1056. p += 6;
  1057. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  1058. *p++ = 20; /* It's 20 bytes long */
  1059. memcpy(p, cell_in->node_id, DIGEST_LEN);
  1060. p += 20;
  1061. /* Now we can send the handshake */
  1062. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  1063. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  1064. p += 4;
  1065. if (cell_in->create_cell.handshake_len > eop - p)
  1066. return -1;
  1067. memcpy(p, cell_in->create_cell.onionskin,
  1068. cell_in->create_cell.handshake_len);
  1069. p += cell_in->create_cell.handshake_len;
  1070. *len_out = p - payload_out;
  1071. }
  1072. break;
  1073. default:
  1074. return -1;
  1075. }
  1076. return 0;
  1077. }
  1078. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  1079. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1080. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1081. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1082. int
  1083. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  1084. uint8_t *payload_out, const extended_cell_t *cell_in)
  1085. {
  1086. uint8_t *p;
  1087. if (check_extended_cell(cell_in) < 0)
  1088. return -1;
  1089. p = payload_out;
  1090. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1091. switch (cell_in->cell_type) {
  1092. case RELAY_COMMAND_EXTENDED:
  1093. {
  1094. *command_out = RELAY_COMMAND_EXTENDED;
  1095. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  1096. memcpy(payload_out, cell_in->created_cell.reply,
  1097. TAP_ONIONSKIN_REPLY_LEN);
  1098. }
  1099. break;
  1100. case RELAY_COMMAND_EXTENDED2:
  1101. {
  1102. *command_out = RELAY_COMMAND_EXTENDED2;
  1103. *len_out = 2 + cell_in->created_cell.handshake_len;
  1104. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  1105. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  1106. return -1;
  1107. memcpy(payload_out+2, cell_in->created_cell.reply,
  1108. cell_in->created_cell.handshake_len);
  1109. }
  1110. break;
  1111. default:
  1112. return -1;
  1113. }
  1114. return 0;
  1115. }