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