onion.c 38 KB

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