onion.c 44 KB

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