onion.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. #endif
  173. }
  174. /** Release all storage held in <b>keys</b>, but do not free <b>keys</b>
  175. * itself (as it's likely to be stack-allocated.) */
  176. void
  177. release_server_onion_keys(server_onion_keys_t *keys)
  178. {
  179. if (! keys)
  180. return;
  181. crypto_pk_free(keys->onion_key);
  182. crypto_pk_free(keys->last_onion_key);
  183. #ifdef CURVE25519_ENABLED
  184. ntor_key_map_free(keys->curve25519_key_map);
  185. #endif
  186. memset(keys, 0, sizeof(server_onion_keys_t));
  187. }
  188. /** Release whatever storage is held in <b>state</b>, depending on its
  189. * type, and clear its pointer. */
  190. void
  191. onion_handshake_state_release(onion_handshake_state_t *state)
  192. {
  193. switch (state->tag) {
  194. case ONION_HANDSHAKE_TYPE_TAP:
  195. crypto_dh_free(state->u.tap);
  196. state->u.tap = NULL;
  197. break;
  198. case ONION_HANDSHAKE_TYPE_FAST:
  199. fast_handshake_state_free(state->u.fast);
  200. state->u.fast = NULL;
  201. break;
  202. #ifdef CURVE25519_ENABLED
  203. case ONION_HANDSHAKE_TYPE_NTOR:
  204. ntor_handshake_state_free(state->u.ntor);
  205. state->u.ntor = NULL;
  206. break;
  207. #endif
  208. default:
  209. log_warn(LD_BUG, "called with unknown handshake state type %d",
  210. (int)state->tag);
  211. tor_fragile_assert();
  212. }
  213. }
  214. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  215. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  216. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  217. * Return -1 on failure, and the length of the onionskin on acceptance.
  218. */
  219. int
  220. onion_skin_create(int type,
  221. const extend_info_t *node,
  222. onion_handshake_state_t *state_out,
  223. uint8_t *onion_skin_out)
  224. {
  225. int r = -1;
  226. switch (type) {
  227. case ONION_HANDSHAKE_TYPE_TAP:
  228. if (!node->onion_key)
  229. return -1;
  230. if (onion_skin_TAP_create(node->onion_key,
  231. &state_out->u.tap,
  232. (char*)onion_skin_out) < 0)
  233. return -1;
  234. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  235. break;
  236. case ONION_HANDSHAKE_TYPE_FAST:
  237. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  238. return -1;
  239. r = CREATE_FAST_LEN;
  240. break;
  241. case ONION_HANDSHAKE_TYPE_NTOR:
  242. #ifdef CURVE25519_ENABLED
  243. if (tor_mem_is_zero((const char*)node->curve25519_onion_key.public_key,
  244. CURVE25519_PUBKEY_LEN))
  245. return -1;
  246. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  247. &node->curve25519_onion_key,
  248. &state_out->u.ntor,
  249. onion_skin_out) < 0)
  250. return -1;
  251. r = NTOR_ONIONSKIN_LEN;
  252. #else
  253. return -1;
  254. #endif
  255. break;
  256. default:
  257. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  258. tor_fragile_assert();
  259. r = -1;
  260. }
  261. if (r > 0)
  262. state_out->tag = (uint16_t) type;
  263. return r;
  264. }
  265. /** Perform the second (server-side) step of a circuit-creation handshake of
  266. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  267. * using the keys in <b>keys</b>. On success, write our response into
  268. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  269. * in <b>keys_out_len</b>, and return the length of the reply. On failure,
  270. * return -1.
  271. * DOCDOC rend_nonce_out
  272. */
  273. int
  274. onion_skin_server_handshake(int type,
  275. const uint8_t *onion_skin, size_t onionskin_len,
  276. const server_onion_keys_t *keys,
  277. uint8_t *reply_out,
  278. uint8_t *keys_out, size_t keys_out_len,
  279. uint8_t *rend_nonce_out)
  280. {
  281. int r = -1;
  282. switch (type) {
  283. case ONION_HANDSHAKE_TYPE_TAP:
  284. if (onionskin_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  285. return -1;
  286. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  287. keys->onion_key, keys->last_onion_key,
  288. (char*)reply_out,
  289. (char*)keys_out, keys_out_len)<0)
  290. return -1;
  291. r = TAP_ONIONSKIN_REPLY_LEN;
  292. memcpy(rend_nonce_out, reply_out+DH_KEY_LEN, DIGEST_LEN);
  293. break;
  294. case ONION_HANDSHAKE_TYPE_FAST:
  295. if (onionskin_len != CREATE_FAST_LEN)
  296. return -1;
  297. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  298. return -1;
  299. r = CREATED_FAST_LEN;
  300. memcpy(rend_nonce_out, reply_out+DIGEST_LEN, DIGEST_LEN);
  301. break;
  302. case ONION_HANDSHAKE_TYPE_NTOR:
  303. #ifdef CURVE25519_ENABLED
  304. if (onionskin_len < NTOR_ONIONSKIN_LEN)
  305. return -1;
  306. {
  307. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  308. uint8_t *keys_tmp = tor_malloc(keys_out_len + DIGEST_LEN);
  309. if (onion_skin_ntor_server_handshake(
  310. onion_skin, keys->curve25519_key_map,
  311. keys->my_identity,
  312. reply_out, keys_tmp, keys_tmp_len)<0) {
  313. tor_free(keys_tmp);
  314. return -1;
  315. }
  316. memcpy(keys_out, keys_tmp, keys_out_len);
  317. memcpy(rend_nonce_out, keys_tmp+keys_out_len, DIGEST_LEN);
  318. memwipe(keys_tmp, 0, keys_tmp_len);
  319. tor_free(keys_tmp);
  320. r = NTOR_REPLY_LEN;
  321. }
  322. #else
  323. return -1;
  324. #endif
  325. break;
  326. default:
  327. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  328. tor_fragile_assert();
  329. return -1;
  330. }
  331. return r;
  332. }
  333. /** Perform the final (client-side) step of a circuit-creation handshake of
  334. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  335. * server's response in <b>reply</b> On success, generate <b>keys_out_len</b>
  336. * bytes worth of key material in <b>keys_out_len</b>, set
  337. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  338. * establish introduction points at this hop, and return 0. On failure,
  339. * return -1. */
  340. int
  341. onion_skin_client_handshake(int type,
  342. const onion_handshake_state_t *handshake_state,
  343. const uint8_t *reply, size_t reply_len,
  344. uint8_t *keys_out, size_t keys_out_len,
  345. uint8_t *rend_authenticator_out)
  346. {
  347. if (handshake_state->tag != type)
  348. return -1;
  349. switch (type) {
  350. case ONION_HANDSHAKE_TYPE_TAP:
  351. if (reply_len != TAP_ONIONSKIN_REPLY_LEN)
  352. return -1;
  353. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  354. (const char*)reply,
  355. (char *)keys_out, keys_out_len) < 0)
  356. return -1;
  357. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  358. return 0;
  359. case ONION_HANDSHAKE_TYPE_FAST:
  360. if (reply_len != CREATED_FAST_LEN)
  361. return -1;
  362. if (fast_client_handshake(handshake_state->u.fast, reply,
  363. keys_out, keys_out_len) < 0)
  364. return -1;
  365. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  366. return 0;
  367. #ifdef CURVE25519_ENABLED
  368. case ONION_HANDSHAKE_TYPE_NTOR:
  369. if (reply_len < NTOR_REPLY_LEN)
  370. return -1;
  371. {
  372. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  373. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  374. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  375. reply,
  376. keys_tmp, keys_tmp_len) < 0) {
  377. tor_free(keys_tmp);
  378. return -1;
  379. }
  380. memcpy(keys_out, keys_tmp, keys_out_len);
  381. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  382. memwipe(keys_tmp, 0, keys_tmp_len);
  383. tor_free(keys_tmp);
  384. }
  385. return 0;
  386. #endif
  387. default:
  388. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  389. tor_fragile_assert();
  390. return -1;
  391. }
  392. }
  393. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  394. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  395. * recognize. */
  396. static int
  397. check_create_cell(const create_cell_t *cell, int unknown_ok)
  398. {
  399. switch (cell->cell_type) {
  400. case CELL_CREATE:
  401. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  402. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  403. return -1;
  404. break;
  405. case CELL_CREATE_FAST:
  406. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  407. return -1;
  408. break;
  409. case CELL_CREATE2:
  410. break;
  411. default:
  412. return -1;
  413. }
  414. switch (cell->handshake_type) {
  415. case ONION_HANDSHAKE_TYPE_TAP:
  416. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  417. return -1;
  418. break;
  419. case ONION_HANDSHAKE_TYPE_FAST:
  420. if (cell->handshake_len != CREATE_FAST_LEN)
  421. return -1;
  422. break;
  423. #ifdef CURVE25519_ENABLED
  424. case ONION_HANDSHAKE_TYPE_NTOR:
  425. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  426. return -1;
  427. break;
  428. #endif
  429. default:
  430. if (! unknown_ok)
  431. return -1;
  432. }
  433. return 0;
  434. }
  435. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  436. * <b>p_len</b> bytes long, and use it to fill the fields of
  437. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  438. *
  439. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  440. * this function is also used for parsing those.
  441. */
  442. static int
  443. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  444. {
  445. if (p_len < 4)
  446. return -1;
  447. cell_out->cell_type = CELL_CREATE2;
  448. cell_out->handshake_type = ntohs(get_uint16(p));
  449. cell_out->handshake_len = ntohs(get_uint16(p+2));
  450. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 4 ||
  451. cell_out->handshake_len > p_len - 4)
  452. return -1;
  453. if (cell_out->handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  454. return -1;
  455. memcpy(cell_out->onionskin, p+4, cell_out->handshake_len);
  456. return 0;
  457. }
  458. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  459. * TAP payload is really an ntor payload. We'd do away with this if every
  460. * relay supported EXTEND2, but we want to be able to extend from A to B with
  461. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  462. * CREATE2 cell.
  463. **/
  464. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  465. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  466. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  467. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  468. int
  469. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  470. {
  471. memset(cell_out, 0, sizeof(*cell_out));
  472. switch (cell_in->command) {
  473. case CELL_CREATE:
  474. cell_out->cell_type = CELL_CREATE;
  475. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  476. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  477. cell_out->handshake_len = NTOR_ONIONSKIN_LEN;
  478. memcpy(cell_out->onionskin, cell_in->payload+16, NTOR_ONIONSKIN_LEN);
  479. } else {
  480. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  481. cell_out->handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  482. memcpy(cell_out->onionskin, cell_in->payload,
  483. TAP_ONIONSKIN_CHALLENGE_LEN);
  484. }
  485. break;
  486. case CELL_CREATE_FAST:
  487. cell_out->cell_type = CELL_CREATE_FAST;
  488. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_FAST;
  489. cell_out->handshake_len = CREATE_FAST_LEN;
  490. memcpy(cell_out->onionskin, cell_in->payload, CREATE_FAST_LEN);
  491. break;
  492. case CELL_CREATE2:
  493. if (parse_create2_payload(cell_out, cell_in->payload,
  494. CELL_PAYLOAD_SIZE) < 0)
  495. return -1;
  496. break;
  497. default:
  498. return -1;
  499. }
  500. return check_create_cell(cell_out, 0);
  501. }
  502. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  503. static int
  504. check_created_cell(const created_cell_t *cell)
  505. {
  506. switch (cell->cell_type) {
  507. case CELL_CREATED:
  508. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN)
  509. return -1;
  510. break;
  511. case CELL_CREATED_FAST:
  512. if (cell->handshake_len != CREATED_FAST_LEN)
  513. return -1;
  514. break;
  515. case CELL_CREATED2:
  516. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  517. return -1;
  518. break;
  519. }
  520. return 0;
  521. }
  522. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  523. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  524. int
  525. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  526. {
  527. memset(cell_out, 0, sizeof(*cell_out));
  528. switch (cell_in->command) {
  529. case CELL_CREATED:
  530. cell_out->cell_type = CELL_CREATED;
  531. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  532. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  533. break;
  534. case CELL_CREATED_FAST:
  535. cell_out->cell_type = CELL_CREATED_FAST;
  536. cell_out->handshake_len = CREATED_FAST_LEN;
  537. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  538. break;
  539. case CELL_CREATED2:
  540. {
  541. const uint8_t *p = cell_in->payload;
  542. cell_out->cell_type = CELL_CREATED2;
  543. cell_out->handshake_len = ntohs(get_uint16(p));
  544. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  545. return -1;
  546. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  547. break;
  548. }
  549. }
  550. return check_created_cell(cell_out);
  551. }
  552. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  553. static int
  554. check_extend_cell(const extend_cell_t *cell)
  555. {
  556. if (tor_digest_is_zero((const char*)cell->node_id))
  557. return -1;
  558. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  559. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  560. return -1;
  561. if (cell->create_cell.cell_type == CELL_CREATE) {
  562. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  563. return -1;
  564. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  565. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  566. cell->cell_type != RELAY_COMMAND_EXTEND)
  567. return -1;
  568. } else {
  569. /* In particular, no CREATE_FAST cells are allowed */
  570. return -1;
  571. }
  572. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  573. return -1;
  574. return check_create_cell(&cell->create_cell, 1);
  575. }
  576. /** Protocol constants for specifier types in EXTEND2
  577. * @{
  578. */
  579. #define SPECTYPE_IPV4 0
  580. #define SPECTYPE_IPV6 1
  581. #define SPECTYPE_LEGACY_ID 2
  582. /** @} */
  583. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  584. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  585. * 0 on success, -1 on failure. */
  586. int
  587. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  588. const uint8_t *payload, size_t payload_length)
  589. {
  590. const uint8_t *eop;
  591. memset(cell_out, 0, sizeof(*cell_out));
  592. if (payload_length > RELAY_PAYLOAD_SIZE)
  593. return -1;
  594. eop = payload + payload_length;
  595. switch (command) {
  596. case RELAY_COMMAND_EXTEND:
  597. {
  598. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  599. return -1;
  600. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  601. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  602. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  603. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  604. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  605. cell_out->create_cell.cell_type = CELL_CREATE2;
  606. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  607. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  608. memcpy(cell_out->create_cell.onionskin, payload + 22,
  609. NTOR_ONIONSKIN_LEN);
  610. } else {
  611. cell_out->create_cell.cell_type = CELL_CREATE;
  612. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  613. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  614. memcpy(cell_out->create_cell.onionskin, payload + 6,
  615. TAP_ONIONSKIN_CHALLENGE_LEN);
  616. }
  617. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  618. DIGEST_LEN);
  619. break;
  620. }
  621. case RELAY_COMMAND_EXTEND2:
  622. {
  623. uint8_t n_specs = *payload, spectype, speclen;
  624. int i;
  625. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  626. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  627. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  628. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  629. ++payload;
  630. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  631. * addres, and the node ID, and ignore everything else */
  632. for (i = 0; i < n_specs; ++i) {
  633. if (eop - payload < 2)
  634. return -1;
  635. spectype = payload[0];
  636. speclen = payload[1];
  637. payload += 2;
  638. if (eop - payload < speclen)
  639. return -1;
  640. switch (spectype) {
  641. case SPECTYPE_IPV4:
  642. if (speclen != 6)
  643. return -1;
  644. if (!found_ipv4) {
  645. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  646. get_uint32(payload));
  647. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  648. found_ipv4 = 1;
  649. }
  650. break;
  651. case SPECTYPE_IPV6:
  652. if (speclen != 18)
  653. return -1;
  654. if (!found_ipv6) {
  655. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  656. (const char*)payload);
  657. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  658. found_ipv6 = 1;
  659. }
  660. break;
  661. case SPECTYPE_LEGACY_ID:
  662. if (speclen != 20)
  663. return -1;
  664. if (found_id)
  665. return -1;
  666. memcpy(cell_out->node_id, payload, 20);
  667. found_id = 1;
  668. break;
  669. }
  670. payload += speclen;
  671. }
  672. if (!found_id || !found_ipv4)
  673. return -1;
  674. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  675. return -1;
  676. break;
  677. }
  678. default:
  679. return -1;
  680. }
  681. return check_extend_cell(cell_out);
  682. }
  683. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  684. static int
  685. check_extended_cell(const extended_cell_t *cell)
  686. {
  687. if (cell->created_cell.cell_type == CELL_CREATED) {
  688. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  689. return -1;
  690. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  691. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  692. return -1;
  693. } else {
  694. return -1;
  695. }
  696. return check_created_cell(&cell->created_cell);
  697. }
  698. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  699. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  700. * 0 on success, -1 on failure. */
  701. int
  702. extended_cell_parse(extended_cell_t *cell_out,
  703. const uint8_t command, const uint8_t *payload,
  704. size_t payload_len)
  705. {
  706. memset(cell_out, 0, sizeof(*cell_out));
  707. if (payload_len > RELAY_PAYLOAD_SIZE)
  708. return -1;
  709. switch (command) {
  710. case RELAY_COMMAND_EXTENDED:
  711. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  712. return -1;
  713. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  714. cell_out->created_cell.cell_type = CELL_CREATED;
  715. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  716. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  717. break;
  718. case RELAY_COMMAND_EXTENDED2:
  719. {
  720. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  721. cell_out->created_cell.cell_type = CELL_CREATED2;
  722. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  723. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  724. cell_out->created_cell.handshake_len > payload_len - 2)
  725. return -1;
  726. memcpy(cell_out->created_cell.reply, payload+2,
  727. cell_out->created_cell.handshake_len);
  728. }
  729. break;
  730. default:
  731. return -1;
  732. }
  733. return check_extended_cell(cell_out);
  734. }
  735. /** Fill <b>cell_out</b> with a correctly formatted version of the
  736. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  737. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  738. static int
  739. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  740. int relayed)
  741. {
  742. uint8_t *p;
  743. size_t space;
  744. if (check_create_cell(cell_in, relayed) < 0)
  745. return -1;
  746. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  747. cell_out->command = cell_in->cell_type;
  748. p = cell_out->payload;
  749. space = sizeof(cell_out->payload);
  750. switch (cell_in->cell_type) {
  751. case CELL_CREATE:
  752. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  753. memcpy(p, NTOR_CREATE_MAGIC, 16);
  754. p += 16;
  755. space -= 16;
  756. }
  757. /* Fall through */
  758. case CELL_CREATE_FAST:
  759. tor_assert(cell_in->handshake_len <= space);
  760. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  761. break;
  762. case CELL_CREATE2:
  763. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  764. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  765. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  766. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  767. break;
  768. default:
  769. return -1;
  770. }
  771. return 0;
  772. }
  773. int
  774. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  775. {
  776. return create_cell_format_impl(cell_out, cell_in, 0);
  777. }
  778. int
  779. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  780. {
  781. return create_cell_format_impl(cell_out, cell_in, 1);
  782. }
  783. /** Fill <b>cell_out</b> with a correctly formatted version of the
  784. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  785. * failure. */
  786. int
  787. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  788. {
  789. if (check_created_cell(cell_in) < 0)
  790. return -1;
  791. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  792. cell_out->command = cell_in->cell_type;
  793. switch (cell_in->cell_type) {
  794. case CELL_CREATED:
  795. case CELL_CREATED_FAST:
  796. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  797. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  798. break;
  799. case CELL_CREATED2:
  800. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  801. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  802. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  803. break;
  804. default:
  805. return -1;
  806. }
  807. return 0;
  808. }
  809. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  810. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  811. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  812. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  813. int
  814. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  815. uint8_t *payload_out, const extend_cell_t *cell_in)
  816. {
  817. uint8_t *p, *eop;
  818. if (check_extend_cell(cell_in) < 0)
  819. return -1;
  820. p = payload_out;
  821. eop = payload_out + RELAY_PAYLOAD_SIZE;
  822. memset(p, 0, RELAY_PAYLOAD_SIZE);
  823. switch (cell_in->cell_type) {
  824. case RELAY_COMMAND_EXTEND:
  825. {
  826. *command_out = RELAY_COMMAND_EXTEND;
  827. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  828. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  829. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  830. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  831. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  832. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  833. } else {
  834. memcpy(p+6, cell_in->create_cell.onionskin,
  835. TAP_ONIONSKIN_CHALLENGE_LEN);
  836. }
  837. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  838. }
  839. break;
  840. case RELAY_COMMAND_EXTEND2:
  841. {
  842. uint8_t n = 2;
  843. *command_out = RELAY_COMMAND_EXTEND2;
  844. *p++ = n; /* 2 identifiers */
  845. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  846. *p++ = 6; /* It's 6 bytes long. */
  847. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  848. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  849. p += 6;
  850. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  851. *p++ = 20; /* It's 20 bytes long */
  852. memcpy(p, cell_in->node_id, DIGEST_LEN);
  853. p += 20;
  854. /* Now we can send the handshake */
  855. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  856. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  857. p += 4;
  858. if (cell_in->create_cell.handshake_len > eop - p)
  859. return -1;
  860. memcpy(p, cell_in->create_cell.onionskin,
  861. cell_in->create_cell.handshake_len);
  862. p += cell_in->create_cell.handshake_len;
  863. *len_out = p - payload_out;
  864. }
  865. break;
  866. default:
  867. return -1;
  868. }
  869. return 0;
  870. }
  871. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  872. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  873. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  874. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  875. int
  876. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  877. uint8_t *payload_out, const extended_cell_t *cell_in)
  878. {
  879. uint8_t *p;
  880. if (check_extended_cell(cell_in) < 0)
  881. return -1;
  882. p = payload_out;
  883. memset(p, 0, RELAY_PAYLOAD_SIZE);
  884. switch (cell_in->cell_type) {
  885. case RELAY_COMMAND_EXTENDED:
  886. {
  887. *command_out = RELAY_COMMAND_EXTENDED;
  888. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  889. memcpy(payload_out, cell_in->created_cell.reply,
  890. TAP_ONIONSKIN_REPLY_LEN);
  891. }
  892. break;
  893. case RELAY_COMMAND_EXTENDED2:
  894. {
  895. *command_out = RELAY_COMMAND_EXTENDED2;
  896. *len_out = 2 + cell_in->created_cell.handshake_len;
  897. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  898. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  899. return -1;
  900. memcpy(payload_out+2, cell_in->created_cell.reply,
  901. cell_in->created_cell.handshake_len);
  902. }
  903. break;
  904. default:
  905. return -1;
  906. }
  907. return 0;
  908. }