onion.c 37 KB

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