onion.c 32 KB

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