onion.c 44 KB

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