onion.c 44 KB

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