onion.c 32 KB

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