onion.c 31 KB

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