onion.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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-2016, 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. * This module has a few functions, all related to the CREATE/CREATED
  12. * handshake that we use on links in order to create a circuit, and the
  13. * related EXTEND/EXTENDED handshake that we use over circuits in order to
  14. * extend them an additional hop.
  15. *
  16. * In this module, we provide a set of abstractions to create a uniform
  17. * interface over the three circuit extension handshakes that Tor has used
  18. * over the years (TAP, CREATE_FAST, and ntor). These handshakes are
  19. * implemented in onion_tap.c, onion_fast.c, and onion_ntor.c respectively.
  20. *
  21. * All[*] of these handshakes follow a similar pattern: a client, knowing
  22. * some key from the relay it wants to extend through, generates the
  23. * first part of a handshake. A relay receives that handshake, and sends
  24. * a reply. Once the client handles the reply, it knows that it is
  25. * talking to the right relay, and it shares some freshly negotiated key
  26. * material with that relay.
  27. *
  28. * We sometimes call the client's part of the handshake an "onionskin".
  29. * We do this because historically, Onion Routing used a multi-layer
  30. * structure called an "onion" to construct circuits. Each layer of the
  31. * onion contained key material chosen by the client, the identity of
  32. * the next relay in the circuit, and a smaller onion, encrypted with
  33. * the key of the next relay. When we changed Tor to use a telescoping
  34. * circuit extension design, it corresponded to sending each layer of the
  35. * onion separately -- as a series of onionskins.
  36. *
  37. * Clients invoke these functions when creating or extending a circuit,
  38. * from circuitbuild.c.
  39. *
  40. * Relays invoke these functions when they receive a CREATE or EXTEND
  41. * cell in command.c or relay.c, in order to queue the pending request.
  42. * They also invoke them from cpuworker.c, which handles dispatching
  43. * onionskin requests to different worker threads.
  44. *
  45. * <br>
  46. *
  47. * This module also handles:
  48. * <ul>
  49. * <li> Queueing incoming onionskins on the relay side before passing
  50. * them to worker threads.
  51. * <li>Expiring onionskins on the relay side if they have waited for
  52. * too long.
  53. * <li>Packaging private keys on the server side in order to pass
  54. * them to worker threads.
  55. * <li>Encoding and decoding CREATE, CREATED, CREATE2, and CREATED2 cells.
  56. * <li>Encoding and decodign EXTEND, EXTENDED, EXTEND2, and EXTENDED2
  57. * relay cells.
  58. * </ul>
  59. *
  60. * [*] The CREATE_FAST handshake is weaker than described here; see
  61. * onion_fast.c for more information.
  62. **/
  63. #include "or.h"
  64. #include "circuitbuild.h"
  65. #include "circuitlist.h"
  66. #include "config.h"
  67. #include "cpuworker.h"
  68. #include "networkstatus.h"
  69. #include "onion.h"
  70. #include "onion_fast.h"
  71. #include "onion_ntor.h"
  72. #include "onion_tap.h"
  73. #include "relay.h"
  74. #include "rephist.h"
  75. #include "router.h"
  76. /** Type for a linked list of circuits that are waiting for a free CPU worker
  77. * to process a waiting onion handshake. */
  78. typedef struct onion_queue_t {
  79. TOR_TAILQ_ENTRY(onion_queue_t) next;
  80. or_circuit_t *circ;
  81. uint16_t handshake_type;
  82. create_cell_t *onionskin;
  83. time_t when_added;
  84. } onion_queue_t;
  85. /** 5 seconds on the onion queue til we just send back a destroy */
  86. #define ONIONQUEUE_WAIT_CUTOFF 5
  87. /** Array of queues of circuits waiting for CPU workers. An element is NULL
  88. * if that queue is empty.*/
  89. static TOR_TAILQ_HEAD(onion_queue_head_t, onion_queue_t)
  90. ol_list[MAX_ONION_HANDSHAKE_TYPE+1] =
  91. { TOR_TAILQ_HEAD_INITIALIZER(ol_list[0]), /* tap */
  92. TOR_TAILQ_HEAD_INITIALIZER(ol_list[1]), /* fast */
  93. TOR_TAILQ_HEAD_INITIALIZER(ol_list[2]), /* ntor */
  94. };
  95. /** Number of entries of each type currently in each element of ol_list[]. */
  96. static int ol_entries[MAX_ONION_HANDSHAKE_TYPE+1];
  97. static int num_ntors_per_tap(void);
  98. static void onion_queue_entry_remove(onion_queue_t *victim);
  99. /* XXXX Check lengths vs MAX_ONIONSKIN_{CHALLENGE,REPLY}_LEN.
  100. *
  101. * (By which I think I meant, "make sure that no
  102. * X_ONIONSKIN_CHALLENGE/REPLY_LEN is greater than
  103. * MAX_ONIONSKIN_CHALLENGE/REPLY_LEN." Also, make sure that we can pass
  104. * over-large values via EXTEND2/EXTENDED2, for future-compatibility.*/
  105. /** Return true iff we have room to queue another onionskin of type
  106. * <b>type</b>. */
  107. static int
  108. have_room_for_onionskin(uint16_t type)
  109. {
  110. const or_options_t *options = get_options();
  111. int num_cpus;
  112. uint64_t tap_usec, ntor_usec;
  113. uint64_t ntor_during_tap_usec, tap_during_ntor_usec;
  114. /* If we've got fewer than 50 entries, we always have room for one more. */
  115. if (ol_entries[type] < 50)
  116. return 1;
  117. num_cpus = get_num_cpus(options);
  118. /* Compute how many microseconds we'd expect to need to clear all
  119. * onionskins in various combinations of the queues. */
  120. /* How long would it take to process all the TAP cells in the queue? */
  121. tap_usec = estimated_usec_for_onionskins(
  122. ol_entries[ONION_HANDSHAKE_TYPE_TAP],
  123. ONION_HANDSHAKE_TYPE_TAP) / num_cpus;
  124. /* How long would it take to process all the NTor cells in the queue? */
  125. ntor_usec = estimated_usec_for_onionskins(
  126. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  127. ONION_HANDSHAKE_TYPE_NTOR) / num_cpus;
  128. /* How long would it take to process the tap cells that we expect to
  129. * process while draining the ntor queue? */
  130. tap_during_ntor_usec = estimated_usec_for_onionskins(
  131. MIN(ol_entries[ONION_HANDSHAKE_TYPE_TAP],
  132. ol_entries[ONION_HANDSHAKE_TYPE_NTOR] / num_ntors_per_tap()),
  133. ONION_HANDSHAKE_TYPE_TAP) / num_cpus;
  134. /* How long would it take to process the ntor cells that we expect to
  135. * process while draining the tap queue? */
  136. ntor_during_tap_usec = estimated_usec_for_onionskins(
  137. MIN(ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  138. ol_entries[ONION_HANDSHAKE_TYPE_TAP] * num_ntors_per_tap()),
  139. ONION_HANDSHAKE_TYPE_NTOR) / num_cpus;
  140. /* See whether that exceeds MaxOnionQueueDelay. If so, we can't queue
  141. * this. */
  142. if (type == ONION_HANDSHAKE_TYPE_NTOR &&
  143. (ntor_usec + tap_during_ntor_usec) / 1000 >
  144. (uint64_t)options->MaxOnionQueueDelay)
  145. return 0;
  146. if (type == ONION_HANDSHAKE_TYPE_TAP &&
  147. (tap_usec + ntor_during_tap_usec) / 1000 >
  148. (uint64_t)options->MaxOnionQueueDelay)
  149. return 0;
  150. /* If we support the ntor handshake, then don't let TAP handshakes use
  151. * more than 2/3 of the space on the queue. */
  152. if (type == ONION_HANDSHAKE_TYPE_TAP &&
  153. tap_usec / 1000 > (uint64_t)options->MaxOnionQueueDelay * 2 / 3)
  154. return 0;
  155. return 1;
  156. }
  157. /** Add <b>circ</b> to the end of ol_list and return 0, except
  158. * if ol_list is too long, in which case do nothing and return -1.
  159. */
  160. int
  161. onion_pending_add(or_circuit_t *circ, create_cell_t *onionskin)
  162. {
  163. onion_queue_t *tmp;
  164. time_t now = time(NULL);
  165. if (onionskin->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  166. /* LCOV_EXCL_START
  167. * We should have rejected this far before this point */
  168. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  169. onionskin->handshake_type);
  170. return -1;
  171. /* LCOV_EXCL_STOP */
  172. }
  173. tmp = tor_malloc_zero(sizeof(onion_queue_t));
  174. tmp->circ = circ;
  175. tmp->handshake_type = onionskin->handshake_type;
  176. tmp->onionskin = onionskin;
  177. tmp->when_added = now;
  178. if (!have_room_for_onionskin(onionskin->handshake_type)) {
  179. #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
  180. static ratelim_t last_warned =
  181. RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
  182. char *m;
  183. if (onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR &&
  184. (m = rate_limit_log(&last_warned, approx_time()))) {
  185. log_warn(LD_GENERAL,
  186. "Your computer is too slow to handle this many circuit "
  187. "creation requests! Please consider using the "
  188. "MaxAdvertisedBandwidth config option or choosing a more "
  189. "restricted exit policy.%s",m);
  190. tor_free(m);
  191. }
  192. tor_free(tmp);
  193. return -1;
  194. }
  195. ++ol_entries[onionskin->handshake_type];
  196. log_info(LD_OR, "New create (%s). Queues now ntor=%d and tap=%d.",
  197. onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR ? "ntor" : "tap",
  198. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  199. ol_entries[ONION_HANDSHAKE_TYPE_TAP]);
  200. circ->onionqueue_entry = tmp;
  201. TOR_TAILQ_INSERT_TAIL(&ol_list[onionskin->handshake_type], tmp, next);
  202. /* cull elderly requests. */
  203. while (1) {
  204. onion_queue_t *head = TOR_TAILQ_FIRST(&ol_list[onionskin->handshake_type]);
  205. if (now - head->when_added < (time_t)ONIONQUEUE_WAIT_CUTOFF)
  206. break;
  207. circ = head->circ;
  208. circ->onionqueue_entry = NULL;
  209. onion_queue_entry_remove(head);
  210. log_info(LD_CIRC,
  211. "Circuit create request is too old; canceling due to overload.");
  212. if (! TO_CIRCUIT(circ)->marked_for_close) {
  213. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  214. }
  215. }
  216. return 0;
  217. }
  218. /** Return a fairness parameter, to prefer processing NTOR style
  219. * handshakes but still slowly drain the TAP queue so we don't starve
  220. * it entirely. */
  221. static int
  222. num_ntors_per_tap(void)
  223. {
  224. #define DEFAULT_NUM_NTORS_PER_TAP 10
  225. #define MIN_NUM_NTORS_PER_TAP 1
  226. #define MAX_NUM_NTORS_PER_TAP 100000
  227. return networkstatus_get_param(NULL, "NumNTorsPerTAP",
  228. DEFAULT_NUM_NTORS_PER_TAP,
  229. MIN_NUM_NTORS_PER_TAP,
  230. MAX_NUM_NTORS_PER_TAP);
  231. }
  232. /** Choose which onion queue we'll pull from next. If one is empty choose
  233. * the other; if they both have elements, load balance across them but
  234. * favoring NTOR. */
  235. static uint16_t
  236. decide_next_handshake_type(void)
  237. {
  238. /* The number of times we've chosen ntor lately when both were available. */
  239. static int recently_chosen_ntors = 0;
  240. if (!ol_entries[ONION_HANDSHAKE_TYPE_NTOR])
  241. return ONION_HANDSHAKE_TYPE_TAP; /* no ntors? try tap */
  242. if (!ol_entries[ONION_HANDSHAKE_TYPE_TAP]) {
  243. /* Nick wants us to prioritize new tap requests when there aren't
  244. * any in the queue and we've processed k ntor cells since the last
  245. * tap cell. This strategy is maybe a good idea, since it starves tap
  246. * less in the case where tap is rare, or maybe a poor idea, since it
  247. * makes the new tap cell unfairly jump in front of ntor cells that
  248. * got here first. In any case this edge case will only become relevant
  249. * once tap is rare. We should reevaluate whether we like this decision
  250. * once tap gets more rare. */
  251. if (ol_entries[ONION_HANDSHAKE_TYPE_NTOR] &&
  252. recently_chosen_ntors <= num_ntors_per_tap())
  253. ++recently_chosen_ntors;
  254. return ONION_HANDSHAKE_TYPE_NTOR; /* no taps? try ntor */
  255. }
  256. /* They both have something queued. Pick ntor if we haven't done that
  257. * too much lately. */
  258. if (++recently_chosen_ntors <= num_ntors_per_tap()) {
  259. return ONION_HANDSHAKE_TYPE_NTOR;
  260. }
  261. /* Else, it's time to let tap have its turn. */
  262. recently_chosen_ntors = 0;
  263. return ONION_HANDSHAKE_TYPE_TAP;
  264. }
  265. /** Remove the highest priority item from ol_list[] and return it, or
  266. * return NULL if the lists are empty.
  267. */
  268. or_circuit_t *
  269. onion_next_task(create_cell_t **onionskin_out)
  270. {
  271. or_circuit_t *circ;
  272. uint16_t handshake_to_choose = decide_next_handshake_type();
  273. onion_queue_t *head = TOR_TAILQ_FIRST(&ol_list[handshake_to_choose]);
  274. if (!head)
  275. return NULL; /* no onions pending, we're done */
  276. tor_assert(head->circ);
  277. tor_assert(head->handshake_type <= MAX_ONION_HANDSHAKE_TYPE);
  278. // tor_assert(head->circ->p_chan); /* make sure it's still valid */
  279. /* XXX I only commented out the above line to make the unit tests
  280. * more manageable. That's probably not good long-term. -RD */
  281. circ = head->circ;
  282. if (head->onionskin)
  283. --ol_entries[head->handshake_type];
  284. log_info(LD_OR, "Processing create (%s). Queues now ntor=%d and tap=%d.",
  285. head->handshake_type == ONION_HANDSHAKE_TYPE_NTOR ? "ntor" : "tap",
  286. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  287. ol_entries[ONION_HANDSHAKE_TYPE_TAP]);
  288. *onionskin_out = head->onionskin;
  289. head->onionskin = NULL; /* prevent free. */
  290. circ->onionqueue_entry = NULL;
  291. onion_queue_entry_remove(head);
  292. return circ;
  293. }
  294. /** Return the number of <b>handshake_type</b>-style create requests pending.
  295. */
  296. int
  297. onion_num_pending(uint16_t handshake_type)
  298. {
  299. return ol_entries[handshake_type];
  300. }
  301. /** Go through ol_list, find the onion_queue_t element which points to
  302. * circ, remove and free that element. Leave circ itself alone.
  303. */
  304. void
  305. onion_pending_remove(or_circuit_t *circ)
  306. {
  307. onion_queue_t *victim;
  308. if (!circ)
  309. return;
  310. victim = circ->onionqueue_entry;
  311. if (victim)
  312. onion_queue_entry_remove(victim);
  313. cpuworker_cancel_circ_handshake(circ);
  314. }
  315. /** Remove a queue entry <b>victim</b> from the queue, unlinking it from
  316. * its circuit and freeing it and any structures it owns.*/
  317. static void
  318. onion_queue_entry_remove(onion_queue_t *victim)
  319. {
  320. if (victim->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  321. /* LCOV_EXCL_START
  322. * We should have rejected this far before this point */
  323. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  324. victim->handshake_type);
  325. /* XXX leaks */
  326. return;
  327. /* LCOV_EXCL_STOP */
  328. }
  329. TOR_TAILQ_REMOVE(&ol_list[victim->handshake_type], victim, next);
  330. if (victim->circ)
  331. victim->circ->onionqueue_entry = NULL;
  332. if (victim->onionskin)
  333. --ol_entries[victim->handshake_type];
  334. tor_free(victim->onionskin);
  335. tor_free(victim);
  336. }
  337. /** Remove all circuits from the pending list. Called from tor_free_all. */
  338. void
  339. clear_pending_onions(void)
  340. {
  341. onion_queue_t *victim, *next;
  342. int i;
  343. for (i=0; i<=MAX_ONION_HANDSHAKE_TYPE; i++) {
  344. for (victim = TOR_TAILQ_FIRST(&ol_list[i]); victim; victim = next) {
  345. next = TOR_TAILQ_NEXT(victim,next);
  346. onion_queue_entry_remove(victim);
  347. }
  348. tor_assert(TOR_TAILQ_EMPTY(&ol_list[i]));
  349. }
  350. memset(ol_entries, 0, sizeof(ol_entries));
  351. }
  352. /* ============================================================ */
  353. /** Return a new server_onion_keys_t object with all of the keys
  354. * and other info we might need to do onion handshakes. (We make a copy of
  355. * our keys for each cpuworker to avoid race conditions with the main thread,
  356. * and to avoid locking) */
  357. server_onion_keys_t *
  358. server_onion_keys_new(void)
  359. {
  360. server_onion_keys_t *keys = tor_malloc_zero(sizeof(server_onion_keys_t));
  361. memcpy(keys->my_identity, router_get_my_id_digest(), DIGEST_LEN);
  362. dup_onion_keys(&keys->onion_key, &keys->last_onion_key);
  363. keys->curve25519_key_map = construct_ntor_key_map();
  364. keys->junk_keypair = tor_malloc_zero(sizeof(curve25519_keypair_t));
  365. curve25519_keypair_generate(keys->junk_keypair, 0);
  366. return keys;
  367. }
  368. /** Release all storage held in <b>keys</b>. */
  369. void
  370. server_onion_keys_free(server_onion_keys_t *keys)
  371. {
  372. if (! keys)
  373. return;
  374. crypto_pk_free(keys->onion_key);
  375. crypto_pk_free(keys->last_onion_key);
  376. ntor_key_map_free(keys->curve25519_key_map);
  377. tor_free(keys->junk_keypair);
  378. memwipe(keys, 0, sizeof(server_onion_keys_t));
  379. tor_free(keys);
  380. }
  381. /** Release whatever storage is held in <b>state</b>, depending on its
  382. * type, and clear its pointer. */
  383. void
  384. onion_handshake_state_release(onion_handshake_state_t *state)
  385. {
  386. switch (state->tag) {
  387. case ONION_HANDSHAKE_TYPE_TAP:
  388. crypto_dh_free(state->u.tap);
  389. state->u.tap = NULL;
  390. break;
  391. case ONION_HANDSHAKE_TYPE_FAST:
  392. fast_handshake_state_free(state->u.fast);
  393. state->u.fast = NULL;
  394. break;
  395. case ONION_HANDSHAKE_TYPE_NTOR:
  396. ntor_handshake_state_free(state->u.ntor);
  397. state->u.ntor = NULL;
  398. break;
  399. default:
  400. /* LCOV_EXCL_START
  401. * This state should not even exist. */
  402. log_warn(LD_BUG, "called with unknown handshake state type %d",
  403. (int)state->tag);
  404. tor_fragile_assert();
  405. /* LCOV_EXCL_STOP */
  406. }
  407. }
  408. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  409. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  410. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  411. * Return -1 on failure, and the length of the onionskin on acceptance.
  412. */
  413. int
  414. onion_skin_create(int type,
  415. const extend_info_t *node,
  416. onion_handshake_state_t *state_out,
  417. uint8_t *onion_skin_out)
  418. {
  419. int r = -1;
  420. switch (type) {
  421. case ONION_HANDSHAKE_TYPE_TAP:
  422. if (!node->onion_key)
  423. return -1;
  424. if (onion_skin_TAP_create(node->onion_key,
  425. &state_out->u.tap,
  426. (char*)onion_skin_out) < 0)
  427. return -1;
  428. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  429. break;
  430. case ONION_HANDSHAKE_TYPE_FAST:
  431. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  432. return -1;
  433. r = CREATE_FAST_LEN;
  434. break;
  435. case ONION_HANDSHAKE_TYPE_NTOR:
  436. if (!extend_info_supports_ntor(node))
  437. return -1;
  438. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  439. &node->curve25519_onion_key,
  440. &state_out->u.ntor,
  441. onion_skin_out) < 0)
  442. return -1;
  443. r = NTOR_ONIONSKIN_LEN;
  444. break;
  445. default:
  446. /* LCOV_EXCL_START
  447. * We should never try to create an impossible handshake type. */
  448. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  449. tor_fragile_assert();
  450. r = -1;
  451. /* LCOV_EXCL_STOP */
  452. }
  453. if (r > 0)
  454. state_out->tag = (uint16_t) type;
  455. return r;
  456. }
  457. /** Perform the second (server-side) step of a circuit-creation handshake of
  458. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  459. * using the keys in <b>keys</b>. On success, write our response into
  460. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  461. * in <b>keys_out_len</b>, a hidden service nonce to <b>rend_nonce_out</b>,
  462. * and return the length of the reply. On failure, return -1.
  463. */
  464. int
  465. onion_skin_server_handshake(int type,
  466. const uint8_t *onion_skin, size_t onionskin_len,
  467. const server_onion_keys_t *keys,
  468. uint8_t *reply_out,
  469. uint8_t *keys_out, size_t keys_out_len,
  470. uint8_t *rend_nonce_out)
  471. {
  472. int r = -1;
  473. switch (type) {
  474. case ONION_HANDSHAKE_TYPE_TAP:
  475. if (onionskin_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  476. return -1;
  477. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  478. keys->onion_key, keys->last_onion_key,
  479. (char*)reply_out,
  480. (char*)keys_out, keys_out_len)<0)
  481. return -1;
  482. r = TAP_ONIONSKIN_REPLY_LEN;
  483. memcpy(rend_nonce_out, reply_out+DH_KEY_LEN, DIGEST_LEN);
  484. break;
  485. case ONION_HANDSHAKE_TYPE_FAST:
  486. if (onionskin_len != CREATE_FAST_LEN)
  487. return -1;
  488. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  489. return -1;
  490. r = CREATED_FAST_LEN;
  491. memcpy(rend_nonce_out, reply_out+DIGEST_LEN, DIGEST_LEN);
  492. break;
  493. case ONION_HANDSHAKE_TYPE_NTOR:
  494. if (onionskin_len < NTOR_ONIONSKIN_LEN)
  495. return -1;
  496. {
  497. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  498. uint8_t *keys_tmp = tor_malloc(keys_out_len + DIGEST_LEN);
  499. if (onion_skin_ntor_server_handshake(
  500. onion_skin, keys->curve25519_key_map,
  501. keys->junk_keypair,
  502. keys->my_identity,
  503. reply_out, keys_tmp, keys_tmp_len)<0) {
  504. tor_free(keys_tmp);
  505. return -1;
  506. }
  507. memcpy(keys_out, keys_tmp, keys_out_len);
  508. memcpy(rend_nonce_out, keys_tmp+keys_out_len, DIGEST_LEN);
  509. memwipe(keys_tmp, 0, keys_tmp_len);
  510. tor_free(keys_tmp);
  511. r = NTOR_REPLY_LEN;
  512. }
  513. break;
  514. default:
  515. /* LCOV_EXCL_START
  516. * We should have rejected this far before this point */
  517. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  518. tor_fragile_assert();
  519. return -1;
  520. /* LCOV_EXCL_STOP */
  521. }
  522. return r;
  523. }
  524. /** Perform the final (client-side) step of a circuit-creation handshake of
  525. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  526. * server's response in <b>reply</b>. On success, generate <b>keys_out_len</b>
  527. * bytes worth of key material in <b>keys_out_len</b>, set
  528. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  529. * establish introduction points at this hop, and return 0. On failure,
  530. * return -1, and set *msg_out to an error message if this is worth
  531. * complaining to the user about. */
  532. int
  533. onion_skin_client_handshake(int type,
  534. const onion_handshake_state_t *handshake_state,
  535. const uint8_t *reply, size_t reply_len,
  536. uint8_t *keys_out, size_t keys_out_len,
  537. uint8_t *rend_authenticator_out,
  538. const char **msg_out)
  539. {
  540. if (handshake_state->tag != type)
  541. return -1;
  542. switch (type) {
  543. case ONION_HANDSHAKE_TYPE_TAP:
  544. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  545. if (msg_out)
  546. *msg_out = "TAP reply was not of the correct length.";
  547. return -1;
  548. }
  549. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  550. (const char*)reply,
  551. (char *)keys_out, keys_out_len,
  552. msg_out) < 0)
  553. return -1;
  554. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  555. return 0;
  556. case ONION_HANDSHAKE_TYPE_FAST:
  557. if (reply_len != CREATED_FAST_LEN) {
  558. if (msg_out)
  559. *msg_out = "TAP reply was not of the correct length.";
  560. return -1;
  561. }
  562. if (fast_client_handshake(handshake_state->u.fast, reply,
  563. keys_out, keys_out_len, msg_out) < 0)
  564. return -1;
  565. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  566. return 0;
  567. case ONION_HANDSHAKE_TYPE_NTOR:
  568. if (reply_len < NTOR_REPLY_LEN) {
  569. if (msg_out)
  570. *msg_out = "ntor reply was not of the correct length.";
  571. return -1;
  572. }
  573. {
  574. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  575. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  576. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  577. reply,
  578. keys_tmp, keys_tmp_len, msg_out) < 0) {
  579. tor_free(keys_tmp);
  580. return -1;
  581. }
  582. memcpy(keys_out, keys_tmp, keys_out_len);
  583. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  584. memwipe(keys_tmp, 0, keys_tmp_len);
  585. tor_free(keys_tmp);
  586. }
  587. return 0;
  588. default:
  589. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  590. tor_fragile_assert();
  591. return -1;
  592. }
  593. }
  594. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  595. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  596. * recognize. */
  597. static int
  598. check_create_cell(const create_cell_t *cell, int unknown_ok)
  599. {
  600. switch (cell->cell_type) {
  601. case CELL_CREATE:
  602. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  603. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  604. return -1;
  605. break;
  606. case CELL_CREATE_FAST:
  607. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  608. return -1;
  609. break;
  610. case CELL_CREATE2:
  611. break;
  612. default:
  613. return -1;
  614. }
  615. switch (cell->handshake_type) {
  616. case ONION_HANDSHAKE_TYPE_TAP:
  617. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  618. return -1;
  619. break;
  620. case ONION_HANDSHAKE_TYPE_FAST:
  621. if (cell->handshake_len != CREATE_FAST_LEN)
  622. return -1;
  623. break;
  624. case ONION_HANDSHAKE_TYPE_NTOR:
  625. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  626. return -1;
  627. break;
  628. default:
  629. if (! unknown_ok)
  630. return -1;
  631. }
  632. return 0;
  633. }
  634. /** Write the various parameters into the create cell. Separate from
  635. * create_cell_parse() to make unit testing easier.
  636. */
  637. void
  638. create_cell_init(create_cell_t *cell_out, uint8_t cell_type,
  639. uint16_t handshake_type, uint16_t handshake_len,
  640. const uint8_t *onionskin)
  641. {
  642. memset(cell_out, 0, sizeof(*cell_out));
  643. cell_out->cell_type = cell_type;
  644. cell_out->handshake_type = handshake_type;
  645. cell_out->handshake_len = handshake_len;
  646. memcpy(cell_out->onionskin, onionskin, handshake_len);
  647. }
  648. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  649. * <b>p_len</b> bytes long, and use it to fill the fields of
  650. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  651. *
  652. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  653. * this function is also used for parsing those.
  654. */
  655. static int
  656. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  657. {
  658. uint16_t handshake_type, handshake_len;
  659. if (p_len < 4)
  660. return -1;
  661. handshake_type = ntohs(get_uint16(p));
  662. handshake_len = ntohs(get_uint16(p+2));
  663. if (handshake_len > CELL_PAYLOAD_SIZE - 4 || handshake_len > p_len - 4)
  664. return -1;
  665. if (handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  666. return -1;
  667. create_cell_init(cell_out, CELL_CREATE2, handshake_type, handshake_len,
  668. p+4);
  669. return 0;
  670. }
  671. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  672. * TAP payload is really an ntor payload. We'd do away with this if every
  673. * relay supported EXTEND2, but we want to be able to extend from A to B with
  674. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  675. * CREATE2 cell.
  676. **/
  677. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  678. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  679. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  680. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  681. int
  682. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  683. {
  684. switch (cell_in->command) {
  685. case CELL_CREATE:
  686. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  687. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  688. NTOR_ONIONSKIN_LEN, cell_in->payload+16);
  689. } else {
  690. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  691. TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->payload);
  692. }
  693. break;
  694. case CELL_CREATE_FAST:
  695. create_cell_init(cell_out, CELL_CREATE_FAST, ONION_HANDSHAKE_TYPE_FAST,
  696. CREATE_FAST_LEN, cell_in->payload);
  697. break;
  698. case CELL_CREATE2:
  699. if (parse_create2_payload(cell_out, cell_in->payload,
  700. CELL_PAYLOAD_SIZE) < 0)
  701. return -1;
  702. break;
  703. default:
  704. return -1;
  705. }
  706. return check_create_cell(cell_out, 0);
  707. }
  708. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  709. static int
  710. check_created_cell(const created_cell_t *cell)
  711. {
  712. switch (cell->cell_type) {
  713. case CELL_CREATED:
  714. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN &&
  715. cell->handshake_len != NTOR_REPLY_LEN)
  716. return -1;
  717. break;
  718. case CELL_CREATED_FAST:
  719. if (cell->handshake_len != CREATED_FAST_LEN)
  720. return -1;
  721. break;
  722. case CELL_CREATED2:
  723. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  724. return -1;
  725. break;
  726. }
  727. return 0;
  728. }
  729. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  730. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  731. int
  732. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  733. {
  734. memset(cell_out, 0, sizeof(*cell_out));
  735. switch (cell_in->command) {
  736. case CELL_CREATED:
  737. cell_out->cell_type = CELL_CREATED;
  738. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  739. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  740. break;
  741. case CELL_CREATED_FAST:
  742. cell_out->cell_type = CELL_CREATED_FAST;
  743. cell_out->handshake_len = CREATED_FAST_LEN;
  744. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  745. break;
  746. case CELL_CREATED2:
  747. {
  748. const uint8_t *p = cell_in->payload;
  749. cell_out->cell_type = CELL_CREATED2;
  750. cell_out->handshake_len = ntohs(get_uint16(p));
  751. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  752. return -1;
  753. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  754. break;
  755. }
  756. }
  757. return check_created_cell(cell_out);
  758. }
  759. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  760. static int
  761. check_extend_cell(const extend_cell_t *cell)
  762. {
  763. if (tor_digest_is_zero((const char*)cell->node_id))
  764. return -1;
  765. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  766. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  767. return -1;
  768. if (cell->create_cell.cell_type == CELL_CREATE) {
  769. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  770. return -1;
  771. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  772. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  773. cell->cell_type != RELAY_COMMAND_EXTEND)
  774. return -1;
  775. } else {
  776. /* In particular, no CREATE_FAST cells are allowed */
  777. return -1;
  778. }
  779. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  780. return -1;
  781. return check_create_cell(&cell->create_cell, 1);
  782. }
  783. /** Protocol constants for specifier types in EXTEND2
  784. * @{
  785. */
  786. #define SPECTYPE_IPV4 0
  787. #define SPECTYPE_IPV6 1
  788. #define SPECTYPE_LEGACY_ID 2
  789. /** @} */
  790. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  791. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  792. * 0 on success, -1 on failure. */
  793. int
  794. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  795. const uint8_t *payload, size_t payload_length)
  796. {
  797. const uint8_t *eop;
  798. memset(cell_out, 0, sizeof(*cell_out));
  799. if (payload_length > RELAY_PAYLOAD_SIZE)
  800. return -1;
  801. eop = payload + payload_length;
  802. switch (command) {
  803. case RELAY_COMMAND_EXTEND:
  804. {
  805. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  806. return -1;
  807. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  808. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  809. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  810. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  811. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  812. cell_out->create_cell.cell_type = CELL_CREATE2;
  813. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  814. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  815. memcpy(cell_out->create_cell.onionskin, payload + 22,
  816. NTOR_ONIONSKIN_LEN);
  817. } else {
  818. cell_out->create_cell.cell_type = CELL_CREATE;
  819. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  820. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  821. memcpy(cell_out->create_cell.onionskin, payload + 6,
  822. TAP_ONIONSKIN_CHALLENGE_LEN);
  823. }
  824. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  825. DIGEST_LEN);
  826. break;
  827. }
  828. case RELAY_COMMAND_EXTEND2:
  829. {
  830. uint8_t n_specs, spectype, speclen;
  831. int i;
  832. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  833. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  834. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  835. if (payload_length == 0)
  836. return -1;
  837. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  838. n_specs = *payload++;
  839. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  840. * address, and the node ID, and ignore everything else */
  841. for (i = 0; i < n_specs; ++i) {
  842. if (eop - payload < 2)
  843. return -1;
  844. spectype = payload[0];
  845. speclen = payload[1];
  846. payload += 2;
  847. if (eop - payload < speclen)
  848. return -1;
  849. switch (spectype) {
  850. case SPECTYPE_IPV4:
  851. if (speclen != 6)
  852. return -1;
  853. if (!found_ipv4) {
  854. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  855. get_uint32(payload));
  856. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  857. found_ipv4 = 1;
  858. }
  859. break;
  860. case SPECTYPE_IPV6:
  861. if (speclen != 18)
  862. return -1;
  863. if (!found_ipv6) {
  864. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  865. (const char*)payload);
  866. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  867. found_ipv6 = 1;
  868. }
  869. break;
  870. case SPECTYPE_LEGACY_ID:
  871. if (speclen != 20)
  872. return -1;
  873. if (found_id)
  874. return -1;
  875. memcpy(cell_out->node_id, payload, 20);
  876. found_id = 1;
  877. break;
  878. }
  879. payload += speclen;
  880. }
  881. if (!found_id || !found_ipv4)
  882. return -1;
  883. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  884. return -1;
  885. break;
  886. }
  887. default:
  888. return -1;
  889. }
  890. return check_extend_cell(cell_out);
  891. }
  892. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  893. static int
  894. check_extended_cell(const extended_cell_t *cell)
  895. {
  896. if (cell->created_cell.cell_type == CELL_CREATED) {
  897. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  898. return -1;
  899. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  900. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  901. return -1;
  902. } else {
  903. return -1;
  904. }
  905. return check_created_cell(&cell->created_cell);
  906. }
  907. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  908. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  909. * 0 on success, -1 on failure. */
  910. int
  911. extended_cell_parse(extended_cell_t *cell_out,
  912. const uint8_t command, const uint8_t *payload,
  913. size_t payload_len)
  914. {
  915. memset(cell_out, 0, sizeof(*cell_out));
  916. if (payload_len > RELAY_PAYLOAD_SIZE)
  917. return -1;
  918. switch (command) {
  919. case RELAY_COMMAND_EXTENDED:
  920. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  921. return -1;
  922. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  923. cell_out->created_cell.cell_type = CELL_CREATED;
  924. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  925. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  926. break;
  927. case RELAY_COMMAND_EXTENDED2:
  928. {
  929. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  930. cell_out->created_cell.cell_type = CELL_CREATED2;
  931. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  932. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  933. cell_out->created_cell.handshake_len > payload_len - 2)
  934. return -1;
  935. memcpy(cell_out->created_cell.reply, payload+2,
  936. cell_out->created_cell.handshake_len);
  937. }
  938. break;
  939. default:
  940. return -1;
  941. }
  942. return check_extended_cell(cell_out);
  943. }
  944. /** Fill <b>cell_out</b> with a correctly formatted version of the
  945. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  946. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  947. static int
  948. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  949. int relayed)
  950. {
  951. uint8_t *p;
  952. size_t space;
  953. if (check_create_cell(cell_in, relayed) < 0)
  954. return -1;
  955. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  956. cell_out->command = cell_in->cell_type;
  957. p = cell_out->payload;
  958. space = sizeof(cell_out->payload);
  959. switch (cell_in->cell_type) {
  960. case CELL_CREATE:
  961. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  962. memcpy(p, NTOR_CREATE_MAGIC, 16);
  963. p += 16;
  964. space -= 16;
  965. }
  966. /* Fall through */
  967. case CELL_CREATE_FAST:
  968. tor_assert(cell_in->handshake_len <= space);
  969. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  970. break;
  971. case CELL_CREATE2:
  972. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  973. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  974. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  975. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  976. break;
  977. default:
  978. return -1;
  979. }
  980. return 0;
  981. }
  982. int
  983. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  984. {
  985. return create_cell_format_impl(cell_out, cell_in, 0);
  986. }
  987. int
  988. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  989. {
  990. return create_cell_format_impl(cell_out, cell_in, 1);
  991. }
  992. /** Fill <b>cell_out</b> with a correctly formatted version of the
  993. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  994. * failure. */
  995. int
  996. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  997. {
  998. if (check_created_cell(cell_in) < 0)
  999. return -1;
  1000. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  1001. cell_out->command = cell_in->cell_type;
  1002. switch (cell_in->cell_type) {
  1003. case CELL_CREATED:
  1004. case CELL_CREATED_FAST:
  1005. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  1006. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  1007. break;
  1008. case CELL_CREATED2:
  1009. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  1010. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  1011. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  1012. break;
  1013. default:
  1014. return -1;
  1015. }
  1016. return 0;
  1017. }
  1018. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  1019. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1020. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1021. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1022. int
  1023. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  1024. uint8_t *payload_out, const extend_cell_t *cell_in)
  1025. {
  1026. uint8_t *p, *eop;
  1027. if (check_extend_cell(cell_in) < 0)
  1028. return -1;
  1029. p = payload_out;
  1030. eop = payload_out + RELAY_PAYLOAD_SIZE;
  1031. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1032. switch (cell_in->cell_type) {
  1033. case RELAY_COMMAND_EXTEND:
  1034. {
  1035. *command_out = RELAY_COMMAND_EXTEND;
  1036. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  1037. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  1038. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  1039. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  1040. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  1041. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  1042. } else {
  1043. memcpy(p+6, cell_in->create_cell.onionskin,
  1044. TAP_ONIONSKIN_CHALLENGE_LEN);
  1045. }
  1046. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  1047. }
  1048. break;
  1049. case RELAY_COMMAND_EXTEND2:
  1050. {
  1051. uint8_t n = 2;
  1052. *command_out = RELAY_COMMAND_EXTEND2;
  1053. *p++ = n; /* 2 identifiers */
  1054. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  1055. *p++ = 6; /* It's 6 bytes long. */
  1056. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  1057. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  1058. p += 6;
  1059. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  1060. *p++ = 20; /* It's 20 bytes long */
  1061. memcpy(p, cell_in->node_id, DIGEST_LEN);
  1062. p += 20;
  1063. /* Now we can send the handshake */
  1064. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  1065. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  1066. p += 4;
  1067. if (cell_in->create_cell.handshake_len > eop - p)
  1068. return -1;
  1069. memcpy(p, cell_in->create_cell.onionskin,
  1070. cell_in->create_cell.handshake_len);
  1071. p += cell_in->create_cell.handshake_len;
  1072. *len_out = p - payload_out;
  1073. }
  1074. break;
  1075. default:
  1076. return -1;
  1077. }
  1078. return 0;
  1079. }
  1080. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  1081. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1082. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1083. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1084. int
  1085. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  1086. uint8_t *payload_out, const extended_cell_t *cell_in)
  1087. {
  1088. uint8_t *p;
  1089. if (check_extended_cell(cell_in) < 0)
  1090. return -1;
  1091. p = payload_out;
  1092. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1093. switch (cell_in->cell_type) {
  1094. case RELAY_COMMAND_EXTENDED:
  1095. {
  1096. *command_out = RELAY_COMMAND_EXTENDED;
  1097. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  1098. memcpy(payload_out, cell_in->created_cell.reply,
  1099. TAP_ONIONSKIN_REPLY_LEN);
  1100. }
  1101. break;
  1102. case RELAY_COMMAND_EXTENDED2:
  1103. {
  1104. *command_out = RELAY_COMMAND_EXTENDED2;
  1105. *len_out = 2 + cell_in->created_cell.handshake_len;
  1106. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  1107. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  1108. return -1;
  1109. memcpy(payload_out+2, cell_in->created_cell.reply,
  1110. cell_in->created_cell.handshake_len);
  1111. }
  1112. break;
  1113. default:
  1114. return -1;
  1115. }
  1116. return 0;
  1117. }