onion.c 33 KB

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