onion.c 37 KB

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