onion.c 34 KB

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