onion.c 44 KB

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