onion.c 43 KB

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