onion.c 31 KB

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