onion.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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, and set *msg_out to an error message if this is worth
  478. * complaining to the usre about. */
  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. const char **msg_out)
  486. {
  487. if (handshake_state->tag != type)
  488. return -1;
  489. switch (type) {
  490. case ONION_HANDSHAKE_TYPE_TAP:
  491. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  492. if (msg_out)
  493. *msg_out = "TAP reply was not of the correct length.";
  494. return -1;
  495. }
  496. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  497. (const char*)reply,
  498. (char *)keys_out, keys_out_len,
  499. msg_out) < 0)
  500. return -1;
  501. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  502. return 0;
  503. case ONION_HANDSHAKE_TYPE_FAST:
  504. if (reply_len != CREATED_FAST_LEN) {
  505. if (msg_out)
  506. *msg_out = "TAP reply was not of the correct length.";
  507. return -1;
  508. }
  509. if (fast_client_handshake(handshake_state->u.fast, reply,
  510. keys_out, keys_out_len, msg_out) < 0)
  511. return -1;
  512. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  513. return 0;
  514. #ifdef CURVE25519_ENABLED
  515. case ONION_HANDSHAKE_TYPE_NTOR:
  516. if (reply_len < NTOR_REPLY_LEN) {
  517. if (msg_out)
  518. *msg_out = "ntor reply was not of the correct length.";
  519. return -1;
  520. }
  521. {
  522. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  523. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  524. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  525. reply,
  526. keys_tmp, keys_tmp_len, msg_out) < 0) {
  527. tor_free(keys_tmp);
  528. return -1;
  529. }
  530. memcpy(keys_out, keys_tmp, keys_out_len);
  531. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  532. memwipe(keys_tmp, 0, keys_tmp_len);
  533. tor_free(keys_tmp);
  534. }
  535. return 0;
  536. #endif
  537. default:
  538. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  539. tor_fragile_assert();
  540. return -1;
  541. }
  542. }
  543. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  544. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  545. * recognize. */
  546. static int
  547. check_create_cell(const create_cell_t *cell, int unknown_ok)
  548. {
  549. switch (cell->cell_type) {
  550. case CELL_CREATE:
  551. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  552. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  553. return -1;
  554. break;
  555. case CELL_CREATE_FAST:
  556. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  557. return -1;
  558. break;
  559. case CELL_CREATE2:
  560. break;
  561. default:
  562. return -1;
  563. }
  564. switch (cell->handshake_type) {
  565. case ONION_HANDSHAKE_TYPE_TAP:
  566. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  567. return -1;
  568. break;
  569. case ONION_HANDSHAKE_TYPE_FAST:
  570. if (cell->handshake_len != CREATE_FAST_LEN)
  571. return -1;
  572. break;
  573. #ifdef CURVE25519_ENABLED
  574. case ONION_HANDSHAKE_TYPE_NTOR:
  575. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  576. return -1;
  577. break;
  578. #endif
  579. default:
  580. if (! unknown_ok)
  581. return -1;
  582. }
  583. return 0;
  584. }
  585. /** Write the various parameters into the create cell. Separate from
  586. * create_cell_parse() to make unit testing easier.
  587. */
  588. void
  589. create_cell_init(create_cell_t *cell_out, uint8_t cell_type,
  590. uint16_t handshake_type, uint16_t handshake_len,
  591. const uint8_t *onionskin)
  592. {
  593. memset(cell_out, 0, sizeof(*cell_out));
  594. cell_out->cell_type = cell_type;
  595. cell_out->handshake_type = handshake_type;
  596. cell_out->handshake_len = handshake_len;
  597. memcpy(cell_out->onionskin, onionskin, handshake_len);
  598. }
  599. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  600. * <b>p_len</b> bytes long, and use it to fill the fields of
  601. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  602. *
  603. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  604. * this function is also used for parsing those.
  605. */
  606. static int
  607. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  608. {
  609. uint16_t handshake_type, handshake_len;
  610. if (p_len < 4)
  611. return -1;
  612. handshake_type = ntohs(get_uint16(p));
  613. handshake_len = ntohs(get_uint16(p+2));
  614. if (handshake_len > CELL_PAYLOAD_SIZE - 4 || handshake_len > p_len - 4)
  615. return -1;
  616. if (handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  617. return -1;
  618. create_cell_init(cell_out, CELL_CREATE2, handshake_type, handshake_len,
  619. p+4);
  620. return 0;
  621. }
  622. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  623. * TAP payload is really an ntor payload. We'd do away with this if every
  624. * relay supported EXTEND2, but we want to be able to extend from A to B with
  625. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  626. * CREATE2 cell.
  627. **/
  628. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  629. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  630. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  631. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  632. int
  633. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  634. {
  635. switch (cell_in->command) {
  636. case CELL_CREATE:
  637. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  638. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  639. NTOR_ONIONSKIN_LEN, cell_in->payload+16);
  640. } else {
  641. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  642. TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->payload);
  643. }
  644. break;
  645. case CELL_CREATE_FAST:
  646. create_cell_init(cell_out, CELL_CREATE_FAST, ONION_HANDSHAKE_TYPE_FAST,
  647. CREATE_FAST_LEN, cell_in->payload);
  648. break;
  649. case CELL_CREATE2:
  650. if (parse_create2_payload(cell_out, cell_in->payload,
  651. CELL_PAYLOAD_SIZE) < 0)
  652. return -1;
  653. break;
  654. default:
  655. return -1;
  656. }
  657. return check_create_cell(cell_out, 0);
  658. }
  659. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  660. static int
  661. check_created_cell(const created_cell_t *cell)
  662. {
  663. switch (cell->cell_type) {
  664. case CELL_CREATED:
  665. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN &&
  666. cell->handshake_len != NTOR_REPLY_LEN)
  667. return -1;
  668. break;
  669. case CELL_CREATED_FAST:
  670. if (cell->handshake_len != CREATED_FAST_LEN)
  671. return -1;
  672. break;
  673. case CELL_CREATED2:
  674. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  675. return -1;
  676. break;
  677. }
  678. return 0;
  679. }
  680. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  681. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  682. int
  683. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  684. {
  685. memset(cell_out, 0, sizeof(*cell_out));
  686. switch (cell_in->command) {
  687. case CELL_CREATED:
  688. cell_out->cell_type = CELL_CREATED;
  689. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  690. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  691. break;
  692. case CELL_CREATED_FAST:
  693. cell_out->cell_type = CELL_CREATED_FAST;
  694. cell_out->handshake_len = CREATED_FAST_LEN;
  695. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  696. break;
  697. case CELL_CREATED2:
  698. {
  699. const uint8_t *p = cell_in->payload;
  700. cell_out->cell_type = CELL_CREATED2;
  701. cell_out->handshake_len = ntohs(get_uint16(p));
  702. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  703. return -1;
  704. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  705. break;
  706. }
  707. }
  708. return check_created_cell(cell_out);
  709. }
  710. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  711. static int
  712. check_extend_cell(const extend_cell_t *cell)
  713. {
  714. if (tor_digest_is_zero((const char*)cell->node_id))
  715. return -1;
  716. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  717. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  718. return -1;
  719. if (cell->create_cell.cell_type == CELL_CREATE) {
  720. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  721. return -1;
  722. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  723. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  724. cell->cell_type != RELAY_COMMAND_EXTEND)
  725. return -1;
  726. } else {
  727. /* In particular, no CREATE_FAST cells are allowed */
  728. return -1;
  729. }
  730. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  731. return -1;
  732. return check_create_cell(&cell->create_cell, 1);
  733. }
  734. /** Protocol constants for specifier types in EXTEND2
  735. * @{
  736. */
  737. #define SPECTYPE_IPV4 0
  738. #define SPECTYPE_IPV6 1
  739. #define SPECTYPE_LEGACY_ID 2
  740. /** @} */
  741. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  742. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  743. * 0 on success, -1 on failure. */
  744. int
  745. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  746. const uint8_t *payload, size_t payload_length)
  747. {
  748. const uint8_t *eop;
  749. memset(cell_out, 0, sizeof(*cell_out));
  750. if (payload_length > RELAY_PAYLOAD_SIZE)
  751. return -1;
  752. eop = payload + payload_length;
  753. switch (command) {
  754. case RELAY_COMMAND_EXTEND:
  755. {
  756. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  757. return -1;
  758. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  759. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  760. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  761. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  762. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  763. cell_out->create_cell.cell_type = CELL_CREATE2;
  764. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  765. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  766. memcpy(cell_out->create_cell.onionskin, payload + 22,
  767. NTOR_ONIONSKIN_LEN);
  768. } else {
  769. cell_out->create_cell.cell_type = CELL_CREATE;
  770. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  771. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  772. memcpy(cell_out->create_cell.onionskin, payload + 6,
  773. TAP_ONIONSKIN_CHALLENGE_LEN);
  774. }
  775. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  776. DIGEST_LEN);
  777. break;
  778. }
  779. case RELAY_COMMAND_EXTEND2:
  780. {
  781. uint8_t n_specs, spectype, speclen;
  782. int i;
  783. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  784. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  785. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  786. if (payload_length == 0)
  787. return -1;
  788. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  789. n_specs = *payload++;
  790. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  791. * address, and the node ID, and ignore everything else */
  792. for (i = 0; i < n_specs; ++i) {
  793. if (eop - payload < 2)
  794. return -1;
  795. spectype = payload[0];
  796. speclen = payload[1];
  797. payload += 2;
  798. if (eop - payload < speclen)
  799. return -1;
  800. switch (spectype) {
  801. case SPECTYPE_IPV4:
  802. if (speclen != 6)
  803. return -1;
  804. if (!found_ipv4) {
  805. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  806. get_uint32(payload));
  807. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  808. found_ipv4 = 1;
  809. }
  810. break;
  811. case SPECTYPE_IPV6:
  812. if (speclen != 18)
  813. return -1;
  814. if (!found_ipv6) {
  815. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  816. (const char*)payload);
  817. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  818. found_ipv6 = 1;
  819. }
  820. break;
  821. case SPECTYPE_LEGACY_ID:
  822. if (speclen != 20)
  823. return -1;
  824. if (found_id)
  825. return -1;
  826. memcpy(cell_out->node_id, payload, 20);
  827. found_id = 1;
  828. break;
  829. }
  830. payload += speclen;
  831. }
  832. if (!found_id || !found_ipv4)
  833. return -1;
  834. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  835. return -1;
  836. break;
  837. }
  838. default:
  839. return -1;
  840. }
  841. return check_extend_cell(cell_out);
  842. }
  843. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  844. static int
  845. check_extended_cell(const extended_cell_t *cell)
  846. {
  847. if (cell->created_cell.cell_type == CELL_CREATED) {
  848. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  849. return -1;
  850. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  851. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  852. return -1;
  853. } else {
  854. return -1;
  855. }
  856. return check_created_cell(&cell->created_cell);
  857. }
  858. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  859. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  860. * 0 on success, -1 on failure. */
  861. int
  862. extended_cell_parse(extended_cell_t *cell_out,
  863. const uint8_t command, const uint8_t *payload,
  864. size_t payload_len)
  865. {
  866. memset(cell_out, 0, sizeof(*cell_out));
  867. if (payload_len > RELAY_PAYLOAD_SIZE)
  868. return -1;
  869. switch (command) {
  870. case RELAY_COMMAND_EXTENDED:
  871. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  872. return -1;
  873. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  874. cell_out->created_cell.cell_type = CELL_CREATED;
  875. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  876. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  877. break;
  878. case RELAY_COMMAND_EXTENDED2:
  879. {
  880. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  881. cell_out->created_cell.cell_type = CELL_CREATED2;
  882. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  883. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  884. cell_out->created_cell.handshake_len > payload_len - 2)
  885. return -1;
  886. memcpy(cell_out->created_cell.reply, payload+2,
  887. cell_out->created_cell.handshake_len);
  888. }
  889. break;
  890. default:
  891. return -1;
  892. }
  893. return check_extended_cell(cell_out);
  894. }
  895. /** Fill <b>cell_out</b> with a correctly formatted version of the
  896. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  897. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  898. static int
  899. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  900. int relayed)
  901. {
  902. uint8_t *p;
  903. size_t space;
  904. if (check_create_cell(cell_in, relayed) < 0)
  905. return -1;
  906. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  907. cell_out->command = cell_in->cell_type;
  908. p = cell_out->payload;
  909. space = sizeof(cell_out->payload);
  910. switch (cell_in->cell_type) {
  911. case CELL_CREATE:
  912. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  913. memcpy(p, NTOR_CREATE_MAGIC, 16);
  914. p += 16;
  915. space -= 16;
  916. }
  917. /* Fall through */
  918. case CELL_CREATE_FAST:
  919. tor_assert(cell_in->handshake_len <= space);
  920. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  921. break;
  922. case CELL_CREATE2:
  923. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  924. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  925. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  926. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  927. break;
  928. default:
  929. return -1;
  930. }
  931. return 0;
  932. }
  933. int
  934. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  935. {
  936. return create_cell_format_impl(cell_out, cell_in, 0);
  937. }
  938. int
  939. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  940. {
  941. return create_cell_format_impl(cell_out, cell_in, 1);
  942. }
  943. /** Fill <b>cell_out</b> with a correctly formatted version of the
  944. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  945. * failure. */
  946. int
  947. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  948. {
  949. if (check_created_cell(cell_in) < 0)
  950. return -1;
  951. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  952. cell_out->command = cell_in->cell_type;
  953. switch (cell_in->cell_type) {
  954. case CELL_CREATED:
  955. case CELL_CREATED_FAST:
  956. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  957. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  958. break;
  959. case CELL_CREATED2:
  960. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  961. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  962. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  963. break;
  964. default:
  965. return -1;
  966. }
  967. return 0;
  968. }
  969. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  970. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  971. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  972. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  973. int
  974. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  975. uint8_t *payload_out, const extend_cell_t *cell_in)
  976. {
  977. uint8_t *p, *eop;
  978. if (check_extend_cell(cell_in) < 0)
  979. return -1;
  980. p = payload_out;
  981. eop = payload_out + RELAY_PAYLOAD_SIZE;
  982. memset(p, 0, RELAY_PAYLOAD_SIZE);
  983. switch (cell_in->cell_type) {
  984. case RELAY_COMMAND_EXTEND:
  985. {
  986. *command_out = RELAY_COMMAND_EXTEND;
  987. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  988. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  989. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  990. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  991. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  992. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  993. } else {
  994. memcpy(p+6, cell_in->create_cell.onionskin,
  995. TAP_ONIONSKIN_CHALLENGE_LEN);
  996. }
  997. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  998. }
  999. break;
  1000. case RELAY_COMMAND_EXTEND2:
  1001. {
  1002. uint8_t n = 2;
  1003. *command_out = RELAY_COMMAND_EXTEND2;
  1004. *p++ = n; /* 2 identifiers */
  1005. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  1006. *p++ = 6; /* It's 6 bytes long. */
  1007. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  1008. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  1009. p += 6;
  1010. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  1011. *p++ = 20; /* It's 20 bytes long */
  1012. memcpy(p, cell_in->node_id, DIGEST_LEN);
  1013. p += 20;
  1014. /* Now we can send the handshake */
  1015. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  1016. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  1017. p += 4;
  1018. if (cell_in->create_cell.handshake_len > eop - p)
  1019. return -1;
  1020. memcpy(p, cell_in->create_cell.onionskin,
  1021. cell_in->create_cell.handshake_len);
  1022. p += cell_in->create_cell.handshake_len;
  1023. *len_out = p - payload_out;
  1024. }
  1025. break;
  1026. default:
  1027. return -1;
  1028. }
  1029. return 0;
  1030. }
  1031. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  1032. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1033. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1034. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1035. int
  1036. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  1037. uint8_t *payload_out, const extended_cell_t *cell_in)
  1038. {
  1039. uint8_t *p;
  1040. if (check_extended_cell(cell_in) < 0)
  1041. return -1;
  1042. p = payload_out;
  1043. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1044. switch (cell_in->cell_type) {
  1045. case RELAY_COMMAND_EXTENDED:
  1046. {
  1047. *command_out = RELAY_COMMAND_EXTENDED;
  1048. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  1049. memcpy(payload_out, cell_in->created_cell.reply,
  1050. TAP_ONIONSKIN_REPLY_LEN);
  1051. }
  1052. break;
  1053. case RELAY_COMMAND_EXTENDED2:
  1054. {
  1055. *command_out = RELAY_COMMAND_EXTENDED2;
  1056. *len_out = 2 + cell_in->created_cell.handshake_len;
  1057. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  1058. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  1059. return -1;
  1060. memcpy(payload_out+2, cell_in->created_cell.reply,
  1061. cell_in->created_cell.handshake_len);
  1062. }
  1063. break;
  1064. default:
  1065. return -1;
  1066. }
  1067. return 0;
  1068. }