onion.c 44 KB

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