onion.c 38 KB

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