onion.c 37 KB

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