onion.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. return -1;
  403. break;
  404. case CELL_CREATE_FAST:
  405. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  406. return -1;
  407. break;
  408. case CELL_CREATE2:
  409. break;
  410. default:
  411. return -1;
  412. }
  413. switch (cell->handshake_type) {
  414. case ONION_HANDSHAKE_TYPE_TAP:
  415. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  416. return -1;
  417. break;
  418. case ONION_HANDSHAKE_TYPE_FAST:
  419. if (cell->handshake_len != CREATE_FAST_LEN)
  420. return -1;
  421. break;
  422. #ifdef CURVE25519_ENABLED
  423. case ONION_HANDSHAKE_TYPE_NTOR:
  424. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  425. return -1;
  426. break;
  427. #endif
  428. default:
  429. if (! unknown_ok)
  430. return -1;
  431. }
  432. return 0;
  433. }
  434. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  435. * <b>p_len</b> bytes long, and use it to fill the fields of
  436. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  437. *
  438. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  439. * this function is also used for parsing those.
  440. */
  441. static int
  442. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  443. {
  444. if (p_len < 4)
  445. return -1;
  446. cell_out->cell_type = CELL_CREATE2;
  447. cell_out->handshake_type = ntohs(get_uint16(p));
  448. cell_out->handshake_len = ntohs(get_uint16(p+2));
  449. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 4 ||
  450. cell_out->handshake_len > p_len - 4)
  451. return -1;
  452. memcpy(cell_out->onionskin, p+4, cell_out->handshake_len);
  453. return 0;
  454. }
  455. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  456. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  457. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  458. int
  459. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  460. {
  461. memset(cell_out, 0, sizeof(*cell_out));
  462. switch (cell_in->command) {
  463. case CELL_CREATE:
  464. cell_out->cell_type = CELL_CREATE;
  465. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  466. cell_out->handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  467. memcpy(cell_out->onionskin, cell_in->payload, TAP_ONIONSKIN_CHALLENGE_LEN);
  468. break;
  469. case CELL_CREATE_FAST:
  470. cell_out->cell_type = CELL_CREATE_FAST;
  471. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_FAST;
  472. cell_out->handshake_len = CREATE_FAST_LEN;
  473. memcpy(cell_out->onionskin, cell_in->payload, CREATE_FAST_LEN);
  474. break;
  475. case CELL_CREATE2:
  476. if (parse_create2_payload(cell_out, cell_in->payload,
  477. CELL_PAYLOAD_SIZE) < 0)
  478. return -1;
  479. break;
  480. default:
  481. return -1;
  482. }
  483. return check_create_cell(cell_out, 0);
  484. }
  485. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  486. static int
  487. check_created_cell(const created_cell_t *cell)
  488. {
  489. switch (cell->cell_type) {
  490. case CELL_CREATED:
  491. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN)
  492. return -1;
  493. break;
  494. case CELL_CREATED_FAST:
  495. if (cell->handshake_len != CREATED_FAST_LEN)
  496. return -1;
  497. break;
  498. case CELL_CREATED2:
  499. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  500. return -1;
  501. break;
  502. }
  503. return 0;
  504. }
  505. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  506. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  507. int
  508. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  509. {
  510. memset(cell_out, 0, sizeof(*cell_out));
  511. switch (cell_in->command) {
  512. case CELL_CREATED:
  513. cell_out->cell_type = CELL_CREATED;
  514. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  515. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  516. break;
  517. case CELL_CREATED_FAST:
  518. cell_out->cell_type = CELL_CREATED_FAST;
  519. cell_out->handshake_len = CREATED_FAST_LEN;
  520. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  521. break;
  522. case CELL_CREATED2:
  523. {
  524. const uint8_t *p = cell_in->payload;
  525. cell_out->cell_type = CELL_CREATED2;
  526. cell_out->handshake_len = ntohs(get_uint16(p));
  527. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  528. return -1;
  529. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  530. break;
  531. }
  532. }
  533. return check_created_cell(cell_out);
  534. }
  535. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  536. static int
  537. check_extend_cell(const extend_cell_t *cell)
  538. {
  539. if (tor_digest_is_zero((const char*)cell->node_id))
  540. return -1;
  541. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  542. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  543. return -1;
  544. if (cell->create_cell.cell_type == CELL_CREATE) {
  545. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  546. return -1;
  547. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  548. if (cell->cell_type != RELAY_COMMAND_EXTEND2)
  549. return -1;
  550. } else {
  551. /* In particular, no CREATE_FAST cells are allowed */
  552. return -1;
  553. }
  554. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  555. return -1;
  556. return check_create_cell(&cell->create_cell, 1);
  557. }
  558. /** Protocol constants for specifier types in EXTEND2
  559. * @{
  560. */
  561. #define SPECTYPE_IPV4 0
  562. #define SPECTYPE_IPV6 1
  563. #define SPECTYPE_LEGACY_ID 2
  564. /** @} */
  565. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  566. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  567. * 0 on success, -1 on failure. */
  568. int
  569. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  570. const uint8_t *payload, size_t payload_length)
  571. {
  572. const uint8_t *eop;
  573. memset(cell_out, 0, sizeof(*cell_out));
  574. if (payload_length > RELAY_PAYLOAD_SIZE)
  575. return -1;
  576. eop = payload + payload_length;
  577. switch (command) {
  578. case RELAY_COMMAND_EXTEND:
  579. {
  580. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  581. return -1;
  582. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  583. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  584. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  585. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  586. cell_out->create_cell.cell_type = CELL_CREATE;
  587. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  588. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  589. memcpy(cell_out->create_cell.onionskin, payload + 6,
  590. TAP_ONIONSKIN_CHALLENGE_LEN);
  591. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  592. DIGEST_LEN);
  593. break;
  594. }
  595. case RELAY_COMMAND_EXTEND2:
  596. {
  597. uint8_t n_specs = *payload, spectype, speclen;
  598. int i;
  599. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  600. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  601. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  602. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  603. ++payload;
  604. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  605. * addres, and the node ID, and ignore everything else */
  606. for (i = 0; i < n_specs; ++i) {
  607. if (eop - payload < 2)
  608. return -1;
  609. spectype = payload[0];
  610. speclen = payload[1];
  611. payload += 2;
  612. if (eop - payload < speclen)
  613. return -1;
  614. switch (spectype) {
  615. case SPECTYPE_IPV4:
  616. if (speclen != 6)
  617. return -1;
  618. if (!found_ipv4) {
  619. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  620. get_uint32(payload));
  621. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  622. found_ipv4 = 1;
  623. }
  624. break;
  625. case SPECTYPE_IPV6:
  626. if (speclen != 18)
  627. return -1;
  628. if (!found_ipv6) {
  629. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  630. (const char*)payload);
  631. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  632. found_ipv6 = 1;
  633. }
  634. break;
  635. case SPECTYPE_LEGACY_ID:
  636. if (speclen != 20)
  637. return -1;
  638. if (found_id)
  639. return -1;
  640. memcpy(cell_out->node_id, payload, 20);
  641. found_id = 1;
  642. break;
  643. }
  644. payload += speclen;
  645. }
  646. if (!found_id || !found_ipv4)
  647. return -1;
  648. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  649. return -1;
  650. break;
  651. }
  652. default:
  653. return -1;
  654. }
  655. return check_extend_cell(cell_out);
  656. }
  657. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  658. static int
  659. check_extended_cell(const extended_cell_t *cell)
  660. {
  661. if (cell->created_cell.cell_type == CELL_CREATED) {
  662. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  663. return -1;
  664. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  665. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  666. return -1;
  667. } else {
  668. return -1;
  669. }
  670. return check_created_cell(&cell->created_cell);
  671. }
  672. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  673. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  674. * 0 on success, -1 on failure. */
  675. int
  676. extended_cell_parse(extended_cell_t *cell_out,
  677. const uint8_t command, const uint8_t *payload,
  678. size_t payload_len)
  679. {
  680. const uint8_t *eop;
  681. memset(cell_out, 0, sizeof(*cell_out));
  682. if (payload_len > RELAY_PAYLOAD_SIZE)
  683. return -1;
  684. eop = payload + payload_len;
  685. switch (command) {
  686. case RELAY_COMMAND_EXTENDED:
  687. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  688. return -1;
  689. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  690. cell_out->created_cell.cell_type = CELL_CREATED;
  691. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  692. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  693. break;
  694. case RELAY_COMMAND_EXTENDED2:
  695. {
  696. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  697. cell_out->created_cell.cell_type = CELL_CREATED2;
  698. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  699. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  700. cell_out->created_cell.handshake_len > payload_len - 2)
  701. return -1;
  702. memcpy(cell_out->created_cell.reply, payload+2,
  703. cell_out->created_cell.handshake_len);
  704. }
  705. break;
  706. default:
  707. return -1;
  708. }
  709. return check_extended_cell(cell_out);
  710. }
  711. /** Fill <b>cell_out</b> with a correctly formatted version of the
  712. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  713. * failure. */
  714. int
  715. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  716. {
  717. if (check_create_cell(cell_in, 0) < 0)
  718. return -1;
  719. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  720. cell_out->command = cell_in->cell_type;
  721. switch (cell_in->cell_type) {
  722. case CELL_CREATE:
  723. case CELL_CREATE_FAST:
  724. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  725. memcpy(cell_out->payload, cell_in->onionskin, cell_in->handshake_len);
  726. break;
  727. case CELL_CREATE2:
  728. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  729. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  730. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  731. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  732. break;
  733. default:
  734. return -1;
  735. }
  736. return 0;
  737. }
  738. /** Fill <b>cell_out</b> with a correctly formatted version of the
  739. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  740. * failure. */
  741. int
  742. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  743. {
  744. if (check_created_cell(cell_in) < 0)
  745. return -1;
  746. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  747. cell_out->command = cell_in->cell_type;
  748. switch (cell_in->cell_type) {
  749. case CELL_CREATED:
  750. case CELL_CREATED_FAST:
  751. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  752. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  753. break;
  754. case CELL_CREATED2:
  755. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  756. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  757. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  758. break;
  759. default:
  760. return -1;
  761. }
  762. return 0;
  763. }
  764. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  765. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  766. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  767. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  768. int
  769. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  770. uint8_t *payload_out, const extend_cell_t *cell_in)
  771. {
  772. uint8_t *p, *eop;
  773. if (check_extend_cell(cell_in) < 0)
  774. return -1;
  775. p = payload_out;
  776. eop = payload_out + RELAY_PAYLOAD_SIZE;
  777. memset(p, 0, RELAY_PAYLOAD_SIZE);
  778. switch (cell_in->cell_type) {
  779. case RELAY_COMMAND_EXTEND:
  780. {
  781. *command_out = RELAY_COMMAND_EXTEND;
  782. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  783. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  784. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  785. memcpy(p+6, cell_in->create_cell.onionskin,
  786. TAP_ONIONSKIN_CHALLENGE_LEN);
  787. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  788. }
  789. break;
  790. case RELAY_COMMAND_EXTEND2:
  791. {
  792. uint8_t n = 2;
  793. *command_out = RELAY_COMMAND_EXTEND2;
  794. *p++ = n; /* 2 identifiers */
  795. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  796. *p++ = 6; /* It's 6 bytes long. */
  797. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  798. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  799. p += 6;
  800. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  801. *p++ = 20; /* It's 20 bytes long */
  802. memcpy(p, cell_in->node_id, DIGEST_LEN);
  803. p += 20;
  804. /* Now we can send the handshake */
  805. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  806. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  807. p += 4;
  808. if (cell_in->create_cell.handshake_len > eop - p)
  809. return -1;
  810. memcpy(p, cell_in->create_cell.onionskin,
  811. cell_in->create_cell.handshake_len);
  812. p += cell_in->create_cell.handshake_len;
  813. *len_out = p - payload_out;
  814. }
  815. break;
  816. default:
  817. return -1;
  818. }
  819. return 0;
  820. }
  821. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  822. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  823. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  824. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  825. int
  826. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  827. uint8_t *payload_out, const extended_cell_t *cell_in)
  828. {
  829. uint8_t *p, *eop;
  830. if (check_extended_cell(cell_in) < 0)
  831. return -1;
  832. p = payload_out;
  833. eop = payload_out + RELAY_PAYLOAD_SIZE;
  834. memset(p, 0, RELAY_PAYLOAD_SIZE);
  835. switch (cell_in->cell_type) {
  836. case RELAY_COMMAND_EXTENDED:
  837. {
  838. *command_out = RELAY_COMMAND_EXTENDED;
  839. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  840. memcpy(payload_out, cell_in->created_cell.reply,
  841. TAP_ONIONSKIN_REPLY_LEN);
  842. }
  843. break;
  844. case RELAY_COMMAND_EXTENDED2:
  845. {
  846. *command_out = RELAY_COMMAND_EXTENDED2;
  847. *len_out = 2 + cell_in->created_cell.handshake_len;
  848. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  849. memcpy(payload_out+2, cell_in->created_cell.reply,
  850. cell_in->created_cell.handshake_len);
  851. }
  852. break;
  853. default:
  854. return -1;
  855. }
  856. return 0;
  857. }