onion.c 37 KB

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