onion.c 44 KB

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