onion.c 34 KB

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