onion.c 37 KB

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