onion.c 33 KB

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