onion.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. char *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, char *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(char **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. int
  272. onion_skin_server_handshake(int type,
  273. const uint8_t *onion_skin,
  274. const server_onion_keys_t *keys,
  275. uint8_t *reply_out,
  276. uint8_t *keys_out, size_t keys_out_len)
  277. {
  278. int r = -1;
  279. switch (type) {
  280. case ONION_HANDSHAKE_TYPE_TAP:
  281. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  282. keys->onion_key, keys->last_onion_key,
  283. (char*)reply_out,
  284. (char*)keys_out, keys_out_len)<0)
  285. return -1;
  286. r = TAP_ONIONSKIN_REPLY_LEN;
  287. break;
  288. case ONION_HANDSHAKE_TYPE_FAST:
  289. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  290. return -1;
  291. r = CREATED_FAST_LEN;
  292. break;
  293. case ONION_HANDSHAKE_TYPE_NTOR:
  294. #ifdef CURVE25519_ENABLED
  295. if (onion_skin_ntor_server_handshake(onion_skin, keys->curve25519_key_map,
  296. keys->my_identity,
  297. reply_out, keys_out, keys_out_len)<0)
  298. return -1;
  299. r = NTOR_REPLY_LEN;
  300. #else
  301. return -1;
  302. #endif
  303. break;
  304. default:
  305. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  306. tor_fragile_assert();
  307. return -1;
  308. }
  309. /* XXXX we should generate the rendezvous nonce stuff too. Some notes
  310. * below */
  311. // memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
  312. //memcpy(hop->handshake_digest, reply+DIGEST_LEN, DIGEST_LEN);
  313. return r;
  314. }
  315. /** Perform the final (client-side) step of a circuit-creation handshake of
  316. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  317. * server's response in <b>reply</b> On success, generate <b>keys_out_len</b>
  318. * bytes worth of key material in <b>keys_out_len</b>, set
  319. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  320. * establish introduction points at this hop, and return 0. On failure,
  321. * return -1. */
  322. int
  323. onion_skin_client_handshake(int type,
  324. const onion_handshake_state_t *handshake_state,
  325. const uint8_t *reply,
  326. uint8_t *keys_out, size_t keys_out_len,
  327. uint8_t *rend_authenticator_out)
  328. {
  329. if (handshake_state->tag != type)
  330. return -1;
  331. switch (type) {
  332. case ONION_HANDSHAKE_TYPE_TAP:
  333. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  334. (const char*)reply,
  335. (char *)keys_out, keys_out_len) < 0)
  336. return -1;
  337. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  338. return 0;
  339. case ONION_HANDSHAKE_TYPE_FAST:
  340. if (fast_client_handshake(handshake_state->u.fast, reply,
  341. keys_out, keys_out_len) < 0)
  342. return -1;
  343. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  344. return 0;
  345. #ifdef CURVE25519_ENABLED
  346. case ONION_HANDSHAKE_TYPE_NTOR:
  347. {
  348. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  349. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  350. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  351. reply,
  352. keys_tmp, keys_tmp_len) < 0) {
  353. tor_free(keys_tmp);
  354. return -1;
  355. }
  356. memcpy(keys_out, keys_tmp, keys_out_len);
  357. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  358. memwipe(keys_tmp, 0, keys_tmp_len);
  359. tor_free(keys_tmp);
  360. }
  361. return 0;
  362. #endif
  363. default:
  364. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  365. tor_fragile_assert();
  366. return -1;
  367. }
  368. }
  369. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  370. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  371. * recognize. */
  372. static int
  373. check_create_cell(const create_cell_t *cell, int unknown_ok)
  374. {
  375. switch (cell->cell_type) {
  376. case CELL_CREATE:
  377. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP)
  378. return -1;
  379. break;
  380. case CELL_CREATE_FAST:
  381. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  382. return -1;
  383. break;
  384. case CELL_CREATE2:
  385. break;
  386. default:
  387. return -1;
  388. }
  389. switch (cell->handshake_type) {
  390. case ONION_HANDSHAKE_TYPE_TAP:
  391. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  392. return -1;
  393. break;
  394. case ONION_HANDSHAKE_TYPE_FAST:
  395. if (cell->handshake_len != CREATE_FAST_LEN)
  396. return -1;
  397. break;
  398. #ifdef CURVE25519_ENABLED
  399. case ONION_HANDSHAKE_TYPE_NTOR:
  400. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  401. return -1;
  402. break;
  403. #endif
  404. default:
  405. if (! unknown_ok)
  406. return -1;
  407. }
  408. return 0;
  409. }
  410. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  411. * <b>p_len</b> bytes long, and use it to fill the fields of
  412. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  413. *
  414. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  415. * this function is also used for parsing those.
  416. */
  417. static int
  418. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  419. {
  420. if (p_len < 4)
  421. return -1;
  422. cell_out->cell_type = CELL_CREATE2;
  423. cell_out->handshake_type = ntohs(get_uint16(p));
  424. cell_out->handshake_len = ntohs(get_uint16(p+2));
  425. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 4 ||
  426. cell_out->handshake_len > p_len - 4)
  427. return -1;
  428. memcpy(cell_out->onionskin, p+4, cell_out->handshake_len);
  429. return 0;
  430. }
  431. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  432. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  433. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  434. int
  435. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  436. {
  437. memset(cell_out, 0, sizeof(*cell_out));
  438. switch (cell_in->command) {
  439. case CELL_CREATE:
  440. cell_out->cell_type = CELL_CREATE;
  441. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  442. cell_out->handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  443. memcpy(cell_out->onionskin, cell_in->payload, TAP_ONIONSKIN_CHALLENGE_LEN);
  444. break;
  445. case CELL_CREATE_FAST:
  446. cell_out->cell_type = CELL_CREATE_FAST;
  447. cell_out->handshake_type = ONION_HANDSHAKE_TYPE_FAST;
  448. cell_out->handshake_len = CREATE_FAST_LEN;
  449. memcpy(cell_out->onionskin, cell_in->payload, CREATE_FAST_LEN);
  450. break;
  451. case CELL_CREATE2:
  452. if (parse_create2_payload(cell_out, cell_in->payload,
  453. CELL_PAYLOAD_SIZE) < 0)
  454. return -1;
  455. break;
  456. default:
  457. return -1;
  458. }
  459. return check_create_cell(cell_out, 0);
  460. }
  461. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  462. static int
  463. check_created_cell(const created_cell_t *cell)
  464. {
  465. switch (cell->cell_type) {
  466. case CELL_CREATED:
  467. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN)
  468. return -1;
  469. break;
  470. case CELL_CREATED_FAST:
  471. if (cell->handshake_len != CREATED_FAST_LEN)
  472. return -1;
  473. break;
  474. case CELL_CREATED2:
  475. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  476. return -1;
  477. break;
  478. }
  479. return 0;
  480. }
  481. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  482. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  483. int
  484. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  485. {
  486. memset(cell_out, 0, sizeof(*cell_out));
  487. switch (cell_in->command) {
  488. case CELL_CREATED:
  489. cell_out->cell_type = CELL_CREATED;
  490. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  491. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  492. break;
  493. case CELL_CREATED_FAST:
  494. cell_out->cell_type = CELL_CREATED_FAST;
  495. cell_out->handshake_len = CREATED_FAST_LEN;
  496. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  497. break;
  498. case CELL_CREATED2:
  499. {
  500. const uint8_t *p = cell_in->payload;
  501. cell_out->cell_type = CELL_CREATED2;
  502. cell_out->handshake_len = ntohs(get_uint16(p));
  503. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  504. return -1;
  505. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  506. break;
  507. }
  508. }
  509. return check_created_cell(cell_out);
  510. }
  511. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  512. static int
  513. check_extend_cell(const extend_cell_t *cell)
  514. {
  515. if (tor_digest_is_zero((const char*)cell->node_id))
  516. return -1;
  517. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  518. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  519. return -1;
  520. if (cell->create_cell.cell_type == CELL_CREATE) {
  521. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  522. return -1;
  523. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  524. if (cell->cell_type != RELAY_COMMAND_EXTEND2)
  525. return -1;
  526. } else {
  527. /* In particular, no CREATE_FAST cells are allowed */
  528. return -1;
  529. }
  530. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  531. return -1;
  532. return check_create_cell(&cell->create_cell, 1);
  533. }
  534. /** Protocol constants for specifier types in EXTEND2
  535. * @{
  536. */
  537. #define SPECTYPE_IPV4 0
  538. #define SPECTYPE_IPV6 1
  539. #define SPECTYPE_LEGACY_ID 2
  540. /** @} */
  541. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  542. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  543. * 0 on success, -1 on failure. */
  544. int
  545. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  546. const uint8_t *payload, size_t payload_length)
  547. {
  548. const uint8_t *eop;
  549. memset(cell_out, 0, sizeof(*cell_out));
  550. if (payload_length > RELAY_PAYLOAD_SIZE)
  551. return -1;
  552. eop = payload + payload_length;
  553. switch (command) {
  554. case RELAY_COMMAND_EXTEND:
  555. {
  556. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  557. return -1;
  558. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  559. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  560. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  561. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  562. cell_out->create_cell.cell_type = CELL_CREATE;
  563. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  564. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  565. memcpy(cell_out->create_cell.onionskin, payload + 6,
  566. TAP_ONIONSKIN_CHALLENGE_LEN);
  567. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  568. DIGEST_LEN);
  569. break;
  570. }
  571. case RELAY_COMMAND_EXTEND2:
  572. {
  573. uint8_t n_specs = *payload, spectype, speclen;
  574. int i;
  575. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  576. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  577. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  578. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  579. ++payload;
  580. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  581. * addres, and the node ID, and ignore everything else */
  582. for (i = 0; i < n_specs; ++i) {
  583. if (eop - payload < 2)
  584. return -1;
  585. spectype = payload[0];
  586. speclen = payload[1];
  587. payload += 2;
  588. if (eop - payload < speclen)
  589. return -1;
  590. switch (spectype) {
  591. case SPECTYPE_IPV4:
  592. if (speclen != 6)
  593. return -1;
  594. if (!found_ipv4) {
  595. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  596. get_uint32(payload));
  597. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  598. found_ipv4 = 1;
  599. }
  600. break;
  601. case SPECTYPE_IPV6:
  602. if (speclen != 18)
  603. return -1;
  604. if (!found_ipv6) {
  605. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  606. (const char*)payload);
  607. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  608. found_ipv6 = 1;
  609. }
  610. break;
  611. case SPECTYPE_LEGACY_ID:
  612. if (speclen != 20)
  613. return -1;
  614. if (found_id)
  615. return -1;
  616. memcpy(cell_out->node_id, payload, 20);
  617. found_id = 1;
  618. break;
  619. }
  620. payload += speclen;
  621. }
  622. if (!found_id || !found_ipv4)
  623. return -1;
  624. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  625. return -1;
  626. break;
  627. }
  628. default:
  629. return -1;
  630. }
  631. return check_extend_cell(cell_out);
  632. }
  633. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  634. static int
  635. check_extended_cell(const extended_cell_t *cell)
  636. {
  637. if (cell->created_cell.cell_type == CELL_CREATED) {
  638. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  639. return -1;
  640. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  641. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  642. return -1;
  643. } else {
  644. return -1;
  645. }
  646. return check_created_cell(&cell->created_cell);
  647. }
  648. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  649. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  650. * 0 on success, -1 on failure. */
  651. int
  652. extended_cell_parse(extended_cell_t *cell_out,
  653. const uint8_t command, const uint8_t *payload,
  654. size_t payload_len)
  655. {
  656. const uint8_t *eop;
  657. memset(cell_out, 0, sizeof(*cell_out));
  658. if (payload_len > RELAY_PAYLOAD_SIZE)
  659. return -1;
  660. eop = payload + payload_len;
  661. switch (command) {
  662. case RELAY_COMMAND_EXTENDED:
  663. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  664. return -1;
  665. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  666. cell_out->created_cell.cell_type = CELL_CREATED;
  667. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  668. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  669. break;
  670. case RELAY_COMMAND_EXTENDED2:
  671. {
  672. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  673. cell_out->created_cell.cell_type = CELL_CREATED2;
  674. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  675. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  676. cell_out->created_cell.handshake_len > payload_len - 2)
  677. return -1;
  678. memcpy(cell_out->created_cell.reply, payload+2,
  679. cell_out->created_cell.handshake_len);
  680. }
  681. break;
  682. default:
  683. return -1;
  684. }
  685. return check_extended_cell(cell_out);
  686. }
  687. /** Fill <b>cell_out</b> with a correctly formatted version of the
  688. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  689. * failure. */
  690. int
  691. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  692. {
  693. if (check_create_cell(cell_in, 0) < 0)
  694. return -1;
  695. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  696. cell_out->command = cell_in->cell_type;
  697. switch (cell_in->cell_type) {
  698. case CELL_CREATE:
  699. case CELL_CREATE_FAST:
  700. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  701. memcpy(cell_out->payload, cell_in->onionskin, cell_in->handshake_len);
  702. break;
  703. case CELL_CREATE2:
  704. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  705. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  706. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  707. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  708. break;
  709. default:
  710. return -1;
  711. }
  712. return 0;
  713. }
  714. /** Fill <b>cell_out</b> with a correctly formatted version of the
  715. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  716. * failure. */
  717. int
  718. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  719. {
  720. if (check_created_cell(cell_in) < 0)
  721. return -1;
  722. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  723. cell_out->command = cell_in->cell_type;
  724. switch (cell_in->cell_type) {
  725. case CELL_CREATED:
  726. case CELL_CREATED_FAST:
  727. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  728. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  729. break;
  730. case CELL_CREATED2:
  731. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  732. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  733. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  734. break;
  735. default:
  736. return -1;
  737. }
  738. return 0;
  739. }
  740. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  741. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  742. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  743. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  744. int
  745. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  746. uint8_t *payload_out, const extend_cell_t *cell_in)
  747. {
  748. uint8_t *p, *eop;
  749. if (check_extend_cell(cell_in) < 0)
  750. return -1;
  751. p = payload_out;
  752. eop = payload_out + RELAY_PAYLOAD_SIZE;
  753. memset(p, 0, RELAY_PAYLOAD_SIZE);
  754. switch (cell_in->cell_type) {
  755. case RELAY_COMMAND_EXTEND:
  756. {
  757. *command_out = RELAY_COMMAND_EXTEND;
  758. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  759. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  760. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  761. memcpy(p+6, cell_in->create_cell.onionskin,
  762. TAP_ONIONSKIN_CHALLENGE_LEN);
  763. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  764. }
  765. break;
  766. case RELAY_COMMAND_EXTEND2:
  767. {
  768. uint8_t n = 2;
  769. *command_out = RELAY_COMMAND_EXTEND2;
  770. *p++ = n; /* 2 identifiers */
  771. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  772. *p++ = 6; /* It's 6 bytes long. */
  773. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  774. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  775. p += 6;
  776. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  777. *p++ = 20; /* It's 20 bytes long */
  778. memcpy(p, cell_in->node_id, DIGEST_LEN);
  779. p += 20;
  780. /* Now we can send the handshake */
  781. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  782. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  783. p += 4;
  784. if (cell_in->create_cell.handshake_len > eop - p)
  785. return -1;
  786. memcpy(p, cell_in->create_cell.onionskin,
  787. cell_in->create_cell.handshake_len);
  788. p += cell_in->create_cell.handshake_len;
  789. *len_out = p - payload_out;
  790. }
  791. break;
  792. default:
  793. return -1;
  794. }
  795. return 0;
  796. }
  797. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  798. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  799. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  800. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  801. int
  802. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  803. uint8_t *payload_out, const extended_cell_t *cell_in)
  804. {
  805. uint8_t *p, *eop;
  806. if (check_extended_cell(cell_in) < 0)
  807. return -1;
  808. p = payload_out;
  809. eop = payload_out + RELAY_PAYLOAD_SIZE;
  810. memset(p, 0, RELAY_PAYLOAD_SIZE);
  811. switch (cell_in->cell_type) {
  812. case RELAY_COMMAND_EXTENDED:
  813. {
  814. *command_out = RELAY_COMMAND_EXTENDED;
  815. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  816. memcpy(payload_out, cell_in->created_cell.reply,
  817. TAP_ONIONSKIN_REPLY_LEN);
  818. }
  819. break;
  820. case RELAY_COMMAND_EXTENDED2:
  821. {
  822. *command_out = RELAY_COMMAND_EXTENDED2;
  823. *len_out = 2 + cell_in->created_cell.handshake_len;
  824. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  825. memcpy(payload_out+2, cell_in->created_cell.reply,
  826. cell_in->created_cell.handshake_len);
  827. }
  828. break;
  829. default:
  830. return -1;
  831. }
  832. return 0;
  833. }