onion.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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, *next;
  284. int i;
  285. for (i=0; i<=MAX_ONION_HANDSHAKE_TYPE; i++) {
  286. for (victim = TOR_TAILQ_FIRST(&ol_list[i]); victim; victim = next) {
  287. next = TOR_TAILQ_NEXT(victim,next);
  288. onion_queue_entry_remove(victim);
  289. }
  290. tor_assert(TOR_TAILQ_EMPTY(&ol_list[i]));
  291. }
  292. memset(ol_entries, 0, sizeof(ol_entries));
  293. }
  294. /* ============================================================ */
  295. /** Fill in a server_onion_keys_t object at <b>keys</b> with all of the keys
  296. * and other info we might need to do onion handshakes. (We make a copy of
  297. * our keys for each cpuworker to avoid race conditions with the main thread,
  298. * and to avoid locking) */
  299. void
  300. setup_server_onion_keys(server_onion_keys_t *keys)
  301. {
  302. memset(keys, 0, sizeof(server_onion_keys_t));
  303. memcpy(keys->my_identity, router_get_my_id_digest(), DIGEST_LEN);
  304. dup_onion_keys(&keys->onion_key, &keys->last_onion_key);
  305. #ifdef CURVE25519_ENABLED
  306. keys->curve25519_key_map = construct_ntor_key_map();
  307. keys->junk_keypair = tor_malloc_zero(sizeof(curve25519_keypair_t));
  308. curve25519_keypair_generate(keys->junk_keypair, 0);
  309. #endif
  310. }
  311. /** Release all storage held in <b>keys</b>, but do not free <b>keys</b>
  312. * itself (as it's likely to be stack-allocated.) */
  313. void
  314. release_server_onion_keys(server_onion_keys_t *keys)
  315. {
  316. if (! keys)
  317. return;
  318. crypto_pk_free(keys->onion_key);
  319. crypto_pk_free(keys->last_onion_key);
  320. #ifdef CURVE25519_ENABLED
  321. ntor_key_map_free(keys->curve25519_key_map);
  322. tor_free(keys->junk_keypair);
  323. #endif
  324. memset(keys, 0, sizeof(server_onion_keys_t));
  325. }
  326. /** Release whatever storage is held in <b>state</b>, depending on its
  327. * type, and clear its pointer. */
  328. void
  329. onion_handshake_state_release(onion_handshake_state_t *state)
  330. {
  331. switch (state->tag) {
  332. case ONION_HANDSHAKE_TYPE_TAP:
  333. crypto_dh_free(state->u.tap);
  334. state->u.tap = NULL;
  335. break;
  336. case ONION_HANDSHAKE_TYPE_FAST:
  337. fast_handshake_state_free(state->u.fast);
  338. state->u.fast = NULL;
  339. break;
  340. #ifdef CURVE25519_ENABLED
  341. case ONION_HANDSHAKE_TYPE_NTOR:
  342. ntor_handshake_state_free(state->u.ntor);
  343. state->u.ntor = NULL;
  344. break;
  345. #endif
  346. default:
  347. log_warn(LD_BUG, "called with unknown handshake state type %d",
  348. (int)state->tag);
  349. tor_fragile_assert();
  350. }
  351. }
  352. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  353. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  354. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  355. * Return -1 on failure, and the length of the onionskin on acceptance.
  356. */
  357. int
  358. onion_skin_create(int type,
  359. const extend_info_t *node,
  360. onion_handshake_state_t *state_out,
  361. uint8_t *onion_skin_out)
  362. {
  363. int r = -1;
  364. switch (type) {
  365. case ONION_HANDSHAKE_TYPE_TAP:
  366. if (!node->onion_key)
  367. return -1;
  368. if (onion_skin_TAP_create(node->onion_key,
  369. &state_out->u.tap,
  370. (char*)onion_skin_out) < 0)
  371. return -1;
  372. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  373. break;
  374. case ONION_HANDSHAKE_TYPE_FAST:
  375. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  376. return -1;
  377. r = CREATE_FAST_LEN;
  378. break;
  379. case ONION_HANDSHAKE_TYPE_NTOR:
  380. #ifdef CURVE25519_ENABLED
  381. if (tor_mem_is_zero((const char*)node->curve25519_onion_key.public_key,
  382. CURVE25519_PUBKEY_LEN))
  383. return -1;
  384. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  385. &node->curve25519_onion_key,
  386. &state_out->u.ntor,
  387. onion_skin_out) < 0)
  388. return -1;
  389. r = NTOR_ONIONSKIN_LEN;
  390. #else
  391. return -1;
  392. #endif
  393. break;
  394. default:
  395. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  396. tor_fragile_assert();
  397. r = -1;
  398. }
  399. if (r > 0)
  400. state_out->tag = (uint16_t) type;
  401. return r;
  402. }
  403. /** Perform the second (server-side) step of a circuit-creation handshake of
  404. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  405. * using the keys in <b>keys</b>. On success, write our response into
  406. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  407. * in <b>keys_out_len</b>, a hidden service nonce to <b>rend_nonce_out</b>,
  408. * and return the length of the reply. On failure, return -1.
  409. */
  410. int
  411. onion_skin_server_handshake(int type,
  412. const uint8_t *onion_skin, size_t onionskin_len,
  413. const server_onion_keys_t *keys,
  414. uint8_t *reply_out,
  415. uint8_t *keys_out, size_t keys_out_len,
  416. uint8_t *rend_nonce_out)
  417. {
  418. int r = -1;
  419. switch (type) {
  420. case ONION_HANDSHAKE_TYPE_TAP:
  421. if (onionskin_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  422. return -1;
  423. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  424. keys->onion_key, keys->last_onion_key,
  425. (char*)reply_out,
  426. (char*)keys_out, keys_out_len)<0)
  427. return -1;
  428. r = TAP_ONIONSKIN_REPLY_LEN;
  429. memcpy(rend_nonce_out, reply_out+DH_KEY_LEN, DIGEST_LEN);
  430. break;
  431. case ONION_HANDSHAKE_TYPE_FAST:
  432. if (onionskin_len != CREATE_FAST_LEN)
  433. return -1;
  434. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  435. return -1;
  436. r = CREATED_FAST_LEN;
  437. memcpy(rend_nonce_out, reply_out+DIGEST_LEN, DIGEST_LEN);
  438. break;
  439. case ONION_HANDSHAKE_TYPE_NTOR:
  440. #ifdef CURVE25519_ENABLED
  441. if (onionskin_len < NTOR_ONIONSKIN_LEN)
  442. return -1;
  443. {
  444. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  445. uint8_t *keys_tmp = tor_malloc(keys_out_len + DIGEST_LEN);
  446. if (onion_skin_ntor_server_handshake(
  447. onion_skin, keys->curve25519_key_map,
  448. keys->junk_keypair,
  449. keys->my_identity,
  450. reply_out, keys_tmp, keys_tmp_len)<0) {
  451. tor_free(keys_tmp);
  452. return -1;
  453. }
  454. memcpy(keys_out, keys_tmp, keys_out_len);
  455. memcpy(rend_nonce_out, keys_tmp+keys_out_len, DIGEST_LEN);
  456. memwipe(keys_tmp, 0, keys_tmp_len);
  457. tor_free(keys_tmp);
  458. r = NTOR_REPLY_LEN;
  459. }
  460. #else
  461. return -1;
  462. #endif
  463. break;
  464. default:
  465. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  466. tor_fragile_assert();
  467. return -1;
  468. }
  469. return r;
  470. }
  471. /** Perform the final (client-side) step of a circuit-creation handshake of
  472. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  473. * server's response in <b>reply</b>. On success, generate <b>keys_out_len</b>
  474. * bytes worth of key material in <b>keys_out_len</b>, set
  475. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  476. * establish introduction points at this hop, and return 0. On failure,
  477. * return -1. */
  478. int
  479. onion_skin_client_handshake(int type,
  480. const onion_handshake_state_t *handshake_state,
  481. const uint8_t *reply, size_t reply_len,
  482. uint8_t *keys_out, size_t keys_out_len,
  483. uint8_t *rend_authenticator_out)
  484. {
  485. if (handshake_state->tag != type)
  486. return -1;
  487. switch (type) {
  488. case ONION_HANDSHAKE_TYPE_TAP:
  489. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  490. log_warn(LD_CIRC, "TAP reply was not of the correct length.");
  491. return -1;
  492. }
  493. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  494. (const char*)reply,
  495. (char *)keys_out, keys_out_len) < 0)
  496. return -1;
  497. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  498. return 0;
  499. case ONION_HANDSHAKE_TYPE_FAST:
  500. if (reply_len != CREATED_FAST_LEN) {
  501. log_warn(LD_CIRC, "CREATED_FAST reply was not of the correct length.");
  502. return -1;
  503. }
  504. if (fast_client_handshake(handshake_state->u.fast, reply,
  505. keys_out, keys_out_len) < 0)
  506. return -1;
  507. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  508. return 0;
  509. #ifdef CURVE25519_ENABLED
  510. case ONION_HANDSHAKE_TYPE_NTOR:
  511. if (reply_len < NTOR_REPLY_LEN) {
  512. log_warn(LD_CIRC, "ntor reply was not of the correct length.");
  513. return -1;
  514. }
  515. {
  516. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  517. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  518. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  519. reply,
  520. keys_tmp, keys_tmp_len) < 0) {
  521. tor_free(keys_tmp);
  522. return -1;
  523. }
  524. memcpy(keys_out, keys_tmp, keys_out_len);
  525. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  526. memwipe(keys_tmp, 0, keys_tmp_len);
  527. tor_free(keys_tmp);
  528. }
  529. return 0;
  530. #endif
  531. default:
  532. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  533. tor_fragile_assert();
  534. return -1;
  535. }
  536. }
  537. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  538. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  539. * recognize. */
  540. static int
  541. check_create_cell(const create_cell_t *cell, int unknown_ok)
  542. {
  543. switch (cell->cell_type) {
  544. case CELL_CREATE:
  545. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  546. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  547. return -1;
  548. break;
  549. case CELL_CREATE_FAST:
  550. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  551. return -1;
  552. break;
  553. case CELL_CREATE2:
  554. break;
  555. default:
  556. return -1;
  557. }
  558. switch (cell->handshake_type) {
  559. case ONION_HANDSHAKE_TYPE_TAP:
  560. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  561. return -1;
  562. break;
  563. case ONION_HANDSHAKE_TYPE_FAST:
  564. if (cell->handshake_len != CREATE_FAST_LEN)
  565. return -1;
  566. break;
  567. #ifdef CURVE25519_ENABLED
  568. case ONION_HANDSHAKE_TYPE_NTOR:
  569. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  570. return -1;
  571. break;
  572. #endif
  573. default:
  574. if (! unknown_ok)
  575. return -1;
  576. }
  577. return 0;
  578. }
  579. /** Write the various parameters into the create cell. Separate from
  580. * create_cell_parse() to make unit testing easier.
  581. */
  582. void
  583. create_cell_init(create_cell_t *cell_out, uint8_t cell_type,
  584. uint16_t handshake_type, uint16_t handshake_len,
  585. const uint8_t *onionskin)
  586. {
  587. memset(cell_out, 0, sizeof(*cell_out));
  588. cell_out->cell_type = cell_type;
  589. cell_out->handshake_type = handshake_type;
  590. cell_out->handshake_len = handshake_len;
  591. memcpy(cell_out->onionskin, onionskin, handshake_len);
  592. }
  593. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  594. * <b>p_len</b> bytes long, and use it to fill the fields of
  595. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  596. *
  597. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  598. * this function is also used for parsing those.
  599. */
  600. static int
  601. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  602. {
  603. uint16_t handshake_type, handshake_len;
  604. if (p_len < 4)
  605. return -1;
  606. handshake_type = ntohs(get_uint16(p));
  607. handshake_len = ntohs(get_uint16(p+2));
  608. if (handshake_len > CELL_PAYLOAD_SIZE - 4 || handshake_len > p_len - 4)
  609. return -1;
  610. if (handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  611. return -1;
  612. create_cell_init(cell_out, CELL_CREATE2, handshake_type, handshake_len,
  613. p+4);
  614. return 0;
  615. }
  616. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  617. * TAP payload is really an ntor payload. We'd do away with this if every
  618. * relay supported EXTEND2, but we want to be able to extend from A to B with
  619. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  620. * CREATE2 cell.
  621. **/
  622. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  623. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  624. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  625. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  626. int
  627. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  628. {
  629. switch (cell_in->command) {
  630. case CELL_CREATE:
  631. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  632. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  633. NTOR_ONIONSKIN_LEN, cell_in->payload+16);
  634. } else {
  635. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  636. TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->payload);
  637. }
  638. break;
  639. case CELL_CREATE_FAST:
  640. create_cell_init(cell_out, CELL_CREATE_FAST, ONION_HANDSHAKE_TYPE_FAST,
  641. CREATE_FAST_LEN, cell_in->payload);
  642. break;
  643. case CELL_CREATE2:
  644. if (parse_create2_payload(cell_out, cell_in->payload,
  645. CELL_PAYLOAD_SIZE) < 0)
  646. return -1;
  647. break;
  648. default:
  649. return -1;
  650. }
  651. return check_create_cell(cell_out, 0);
  652. }
  653. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  654. static int
  655. check_created_cell(const created_cell_t *cell)
  656. {
  657. switch (cell->cell_type) {
  658. case CELL_CREATED:
  659. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN &&
  660. cell->handshake_len != NTOR_REPLY_LEN)
  661. return -1;
  662. break;
  663. case CELL_CREATED_FAST:
  664. if (cell->handshake_len != CREATED_FAST_LEN)
  665. return -1;
  666. break;
  667. case CELL_CREATED2:
  668. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  669. return -1;
  670. break;
  671. }
  672. return 0;
  673. }
  674. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  675. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  676. int
  677. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  678. {
  679. memset(cell_out, 0, sizeof(*cell_out));
  680. switch (cell_in->command) {
  681. case CELL_CREATED:
  682. cell_out->cell_type = CELL_CREATED;
  683. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  684. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  685. break;
  686. case CELL_CREATED_FAST:
  687. cell_out->cell_type = CELL_CREATED_FAST;
  688. cell_out->handshake_len = CREATED_FAST_LEN;
  689. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  690. break;
  691. case CELL_CREATED2:
  692. {
  693. const uint8_t *p = cell_in->payload;
  694. cell_out->cell_type = CELL_CREATED2;
  695. cell_out->handshake_len = ntohs(get_uint16(p));
  696. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  697. return -1;
  698. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  699. break;
  700. }
  701. }
  702. return check_created_cell(cell_out);
  703. }
  704. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  705. static int
  706. check_extend_cell(const extend_cell_t *cell)
  707. {
  708. if (tor_digest_is_zero((const char*)cell->node_id))
  709. return -1;
  710. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  711. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  712. return -1;
  713. if (cell->create_cell.cell_type == CELL_CREATE) {
  714. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  715. return -1;
  716. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  717. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  718. cell->cell_type != RELAY_COMMAND_EXTEND)
  719. return -1;
  720. } else {
  721. /* In particular, no CREATE_FAST cells are allowed */
  722. return -1;
  723. }
  724. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  725. return -1;
  726. return check_create_cell(&cell->create_cell, 1);
  727. }
  728. /** Protocol constants for specifier types in EXTEND2
  729. * @{
  730. */
  731. #define SPECTYPE_IPV4 0
  732. #define SPECTYPE_IPV6 1
  733. #define SPECTYPE_LEGACY_ID 2
  734. /** @} */
  735. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  736. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  737. * 0 on success, -1 on failure. */
  738. int
  739. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  740. const uint8_t *payload, size_t payload_length)
  741. {
  742. const uint8_t *eop;
  743. memset(cell_out, 0, sizeof(*cell_out));
  744. if (payload_length > RELAY_PAYLOAD_SIZE)
  745. return -1;
  746. eop = payload + payload_length;
  747. switch (command) {
  748. case RELAY_COMMAND_EXTEND:
  749. {
  750. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  751. return -1;
  752. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  753. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  754. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  755. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  756. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  757. cell_out->create_cell.cell_type = CELL_CREATE2;
  758. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  759. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  760. memcpy(cell_out->create_cell.onionskin, payload + 22,
  761. NTOR_ONIONSKIN_LEN);
  762. } else {
  763. cell_out->create_cell.cell_type = CELL_CREATE;
  764. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  765. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  766. memcpy(cell_out->create_cell.onionskin, payload + 6,
  767. TAP_ONIONSKIN_CHALLENGE_LEN);
  768. }
  769. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  770. DIGEST_LEN);
  771. break;
  772. }
  773. case RELAY_COMMAND_EXTEND2:
  774. {
  775. uint8_t n_specs, spectype, speclen;
  776. int i;
  777. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  778. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  779. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  780. if (payload_length == 0)
  781. return -1;
  782. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  783. n_specs = *payload++;
  784. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  785. * address, and the node ID, and ignore everything else */
  786. for (i = 0; i < n_specs; ++i) {
  787. if (eop - payload < 2)
  788. return -1;
  789. spectype = payload[0];
  790. speclen = payload[1];
  791. payload += 2;
  792. if (eop - payload < speclen)
  793. return -1;
  794. switch (spectype) {
  795. case SPECTYPE_IPV4:
  796. if (speclen != 6)
  797. return -1;
  798. if (!found_ipv4) {
  799. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  800. get_uint32(payload));
  801. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  802. found_ipv4 = 1;
  803. }
  804. break;
  805. case SPECTYPE_IPV6:
  806. if (speclen != 18)
  807. return -1;
  808. if (!found_ipv6) {
  809. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  810. (const char*)payload);
  811. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  812. found_ipv6 = 1;
  813. }
  814. break;
  815. case SPECTYPE_LEGACY_ID:
  816. if (speclen != 20)
  817. return -1;
  818. if (found_id)
  819. return -1;
  820. memcpy(cell_out->node_id, payload, 20);
  821. found_id = 1;
  822. break;
  823. }
  824. payload += speclen;
  825. }
  826. if (!found_id || !found_ipv4)
  827. return -1;
  828. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  829. return -1;
  830. break;
  831. }
  832. default:
  833. return -1;
  834. }
  835. return check_extend_cell(cell_out);
  836. }
  837. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  838. static int
  839. check_extended_cell(const extended_cell_t *cell)
  840. {
  841. if (cell->created_cell.cell_type == CELL_CREATED) {
  842. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  843. return -1;
  844. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  845. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  846. return -1;
  847. } else {
  848. return -1;
  849. }
  850. return check_created_cell(&cell->created_cell);
  851. }
  852. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  853. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  854. * 0 on success, -1 on failure. */
  855. int
  856. extended_cell_parse(extended_cell_t *cell_out,
  857. const uint8_t command, const uint8_t *payload,
  858. size_t payload_len)
  859. {
  860. memset(cell_out, 0, sizeof(*cell_out));
  861. if (payload_len > RELAY_PAYLOAD_SIZE)
  862. return -1;
  863. switch (command) {
  864. case RELAY_COMMAND_EXTENDED:
  865. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  866. return -1;
  867. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  868. cell_out->created_cell.cell_type = CELL_CREATED;
  869. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  870. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  871. break;
  872. case RELAY_COMMAND_EXTENDED2:
  873. {
  874. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  875. cell_out->created_cell.cell_type = CELL_CREATED2;
  876. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  877. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  878. cell_out->created_cell.handshake_len > payload_len - 2)
  879. return -1;
  880. memcpy(cell_out->created_cell.reply, payload+2,
  881. cell_out->created_cell.handshake_len);
  882. }
  883. break;
  884. default:
  885. return -1;
  886. }
  887. return check_extended_cell(cell_out);
  888. }
  889. /** Fill <b>cell_out</b> with a correctly formatted version of the
  890. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  891. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  892. static int
  893. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  894. int relayed)
  895. {
  896. uint8_t *p;
  897. size_t space;
  898. if (check_create_cell(cell_in, relayed) < 0)
  899. return -1;
  900. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  901. cell_out->command = cell_in->cell_type;
  902. p = cell_out->payload;
  903. space = sizeof(cell_out->payload);
  904. switch (cell_in->cell_type) {
  905. case CELL_CREATE:
  906. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  907. memcpy(p, NTOR_CREATE_MAGIC, 16);
  908. p += 16;
  909. space -= 16;
  910. }
  911. /* Fall through */
  912. case CELL_CREATE_FAST:
  913. tor_assert(cell_in->handshake_len <= space);
  914. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  915. break;
  916. case CELL_CREATE2:
  917. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  918. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  919. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  920. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  921. break;
  922. default:
  923. return -1;
  924. }
  925. return 0;
  926. }
  927. int
  928. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  929. {
  930. return create_cell_format_impl(cell_out, cell_in, 0);
  931. }
  932. int
  933. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  934. {
  935. return create_cell_format_impl(cell_out, cell_in, 1);
  936. }
  937. /** Fill <b>cell_out</b> with a correctly formatted version of the
  938. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  939. * failure. */
  940. int
  941. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  942. {
  943. if (check_created_cell(cell_in) < 0)
  944. return -1;
  945. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  946. cell_out->command = cell_in->cell_type;
  947. switch (cell_in->cell_type) {
  948. case CELL_CREATED:
  949. case CELL_CREATED_FAST:
  950. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  951. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  952. break;
  953. case CELL_CREATED2:
  954. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  955. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  956. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  957. break;
  958. default:
  959. return -1;
  960. }
  961. return 0;
  962. }
  963. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  964. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  965. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  966. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  967. int
  968. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  969. uint8_t *payload_out, const extend_cell_t *cell_in)
  970. {
  971. uint8_t *p, *eop;
  972. if (check_extend_cell(cell_in) < 0)
  973. return -1;
  974. p = payload_out;
  975. eop = payload_out + RELAY_PAYLOAD_SIZE;
  976. memset(p, 0, RELAY_PAYLOAD_SIZE);
  977. switch (cell_in->cell_type) {
  978. case RELAY_COMMAND_EXTEND:
  979. {
  980. *command_out = RELAY_COMMAND_EXTEND;
  981. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  982. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  983. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  984. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  985. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  986. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  987. } else {
  988. memcpy(p+6, cell_in->create_cell.onionskin,
  989. TAP_ONIONSKIN_CHALLENGE_LEN);
  990. }
  991. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  992. }
  993. break;
  994. case RELAY_COMMAND_EXTEND2:
  995. {
  996. uint8_t n = 2;
  997. *command_out = RELAY_COMMAND_EXTEND2;
  998. *p++ = n; /* 2 identifiers */
  999. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  1000. *p++ = 6; /* It's 6 bytes long. */
  1001. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  1002. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  1003. p += 6;
  1004. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  1005. *p++ = 20; /* It's 20 bytes long */
  1006. memcpy(p, cell_in->node_id, DIGEST_LEN);
  1007. p += 20;
  1008. /* Now we can send the handshake */
  1009. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  1010. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  1011. p += 4;
  1012. if (cell_in->create_cell.handshake_len > eop - p)
  1013. return -1;
  1014. memcpy(p, cell_in->create_cell.onionskin,
  1015. cell_in->create_cell.handshake_len);
  1016. p += cell_in->create_cell.handshake_len;
  1017. *len_out = p - payload_out;
  1018. }
  1019. break;
  1020. default:
  1021. return -1;
  1022. }
  1023. return 0;
  1024. }
  1025. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  1026. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1027. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1028. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1029. int
  1030. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  1031. uint8_t *payload_out, const extended_cell_t *cell_in)
  1032. {
  1033. uint8_t *p;
  1034. if (check_extended_cell(cell_in) < 0)
  1035. return -1;
  1036. p = payload_out;
  1037. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1038. switch (cell_in->cell_type) {
  1039. case RELAY_COMMAND_EXTENDED:
  1040. {
  1041. *command_out = RELAY_COMMAND_EXTENDED;
  1042. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  1043. memcpy(payload_out, cell_in->created_cell.reply,
  1044. TAP_ONIONSKIN_REPLY_LEN);
  1045. }
  1046. break;
  1047. case RELAY_COMMAND_EXTENDED2:
  1048. {
  1049. *command_out = RELAY_COMMAND_EXTENDED2;
  1050. *len_out = 2 + cell_in->created_cell.handshake_len;
  1051. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  1052. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  1053. return -1;
  1054. memcpy(payload_out+2, cell_in->created_cell.reply,
  1055. cell_in->created_cell.handshake_len);
  1056. }
  1057. break;
  1058. default:
  1059. return -1;
  1060. }
  1061. return 0;
  1062. }