onion.c 32 KB

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