onion.c 36 KB

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