onion.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. const uint8_t *eop;
  707. memset(cell_out, 0, sizeof(*cell_out));
  708. if (payload_len > RELAY_PAYLOAD_SIZE)
  709. return -1;
  710. eop = payload + payload_len;
  711. switch (command) {
  712. case RELAY_COMMAND_EXTENDED:
  713. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  714. return -1;
  715. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  716. cell_out->created_cell.cell_type = CELL_CREATED;
  717. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  718. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  719. break;
  720. case RELAY_COMMAND_EXTENDED2:
  721. {
  722. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  723. cell_out->created_cell.cell_type = CELL_CREATED2;
  724. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  725. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  726. cell_out->created_cell.handshake_len > payload_len - 2)
  727. return -1;
  728. memcpy(cell_out->created_cell.reply, payload+2,
  729. cell_out->created_cell.handshake_len);
  730. }
  731. break;
  732. default:
  733. return -1;
  734. }
  735. return check_extended_cell(cell_out);
  736. }
  737. /** Fill <b>cell_out</b> with a correctly formatted version of the
  738. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  739. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  740. static int
  741. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  742. int relayed)
  743. {
  744. uint8_t *p;
  745. size_t space;
  746. if (check_create_cell(cell_in, relayed) < 0)
  747. return -1;
  748. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  749. cell_out->command = cell_in->cell_type;
  750. p = cell_out->payload;
  751. space = sizeof(cell_out->payload);
  752. switch (cell_in->cell_type) {
  753. case CELL_CREATE:
  754. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  755. memcpy(p, NTOR_CREATE_MAGIC, 16);
  756. p += 16;
  757. space -= 16;
  758. }
  759. /* Fall through */
  760. case CELL_CREATE_FAST:
  761. tor_assert(cell_in->handshake_len <= space);
  762. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  763. break;
  764. case CELL_CREATE2:
  765. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  766. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  767. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  768. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  769. break;
  770. default:
  771. return -1;
  772. }
  773. return 0;
  774. }
  775. int
  776. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  777. {
  778. return create_cell_format_impl(cell_out, cell_in, 0);
  779. }
  780. int
  781. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  782. {
  783. return create_cell_format_impl(cell_out, cell_in, 1);
  784. }
  785. /** Fill <b>cell_out</b> with a correctly formatted version of the
  786. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  787. * failure. */
  788. int
  789. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  790. {
  791. if (check_created_cell(cell_in) < 0)
  792. return -1;
  793. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  794. cell_out->command = cell_in->cell_type;
  795. switch (cell_in->cell_type) {
  796. case CELL_CREATED:
  797. case CELL_CREATED_FAST:
  798. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  799. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  800. break;
  801. case CELL_CREATED2:
  802. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  803. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  804. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  805. break;
  806. default:
  807. return -1;
  808. }
  809. return 0;
  810. }
  811. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  812. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  813. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  814. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  815. int
  816. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  817. uint8_t *payload_out, const extend_cell_t *cell_in)
  818. {
  819. uint8_t *p, *eop;
  820. if (check_extend_cell(cell_in) < 0)
  821. return -1;
  822. p = payload_out;
  823. eop = payload_out + RELAY_PAYLOAD_SIZE;
  824. memset(p, 0, RELAY_PAYLOAD_SIZE);
  825. switch (cell_in->cell_type) {
  826. case RELAY_COMMAND_EXTEND:
  827. {
  828. *command_out = RELAY_COMMAND_EXTEND;
  829. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  830. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  831. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  832. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  833. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  834. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  835. } else {
  836. memcpy(p+6, cell_in->create_cell.onionskin,
  837. TAP_ONIONSKIN_CHALLENGE_LEN);
  838. }
  839. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  840. }
  841. break;
  842. case RELAY_COMMAND_EXTEND2:
  843. {
  844. uint8_t n = 2;
  845. *command_out = RELAY_COMMAND_EXTEND2;
  846. *p++ = n; /* 2 identifiers */
  847. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  848. *p++ = 6; /* It's 6 bytes long. */
  849. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  850. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  851. p += 6;
  852. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  853. *p++ = 20; /* It's 20 bytes long */
  854. memcpy(p, cell_in->node_id, DIGEST_LEN);
  855. p += 20;
  856. /* Now we can send the handshake */
  857. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  858. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  859. p += 4;
  860. if (cell_in->create_cell.handshake_len > eop - p)
  861. return -1;
  862. memcpy(p, cell_in->create_cell.onionskin,
  863. cell_in->create_cell.handshake_len);
  864. p += cell_in->create_cell.handshake_len;
  865. *len_out = p - payload_out;
  866. }
  867. break;
  868. default:
  869. return -1;
  870. }
  871. return 0;
  872. }
  873. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  874. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  875. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  876. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  877. int
  878. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  879. uint8_t *payload_out, const extended_cell_t *cell_in)
  880. {
  881. uint8_t *p, *eop;
  882. if (check_extended_cell(cell_in) < 0)
  883. return -1;
  884. p = payload_out;
  885. eop = payload_out + RELAY_PAYLOAD_SIZE;
  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. memcpy(payload_out+2, cell_in->created_cell.reply,
  902. cell_in->created_cell.handshake_len);
  903. }
  904. break;
  905. default:
  906. return -1;
  907. }
  908. return 0;
  909. }