onion.c 34 KB

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