onion.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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-2015, 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. cpuworker_cancel_circ_handshake(circ);
  256. }
  257. /** Remove a queue entry <b>victim</b> from the queue, unlinking it from
  258. * its circuit and freeing it and any structures it owns.*/
  259. static void
  260. onion_queue_entry_remove(onion_queue_t *victim)
  261. {
  262. if (victim->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  263. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  264. victim->handshake_type);
  265. /* XXX leaks */
  266. return;
  267. }
  268. TOR_TAILQ_REMOVE(&ol_list[victim->handshake_type], victim, next);
  269. if (victim->circ)
  270. victim->circ->onionqueue_entry = NULL;
  271. if (victim->onionskin)
  272. --ol_entries[victim->handshake_type];
  273. tor_free(victim->onionskin);
  274. tor_free(victim);
  275. }
  276. /** Remove all circuits from the pending list. Called from tor_free_all. */
  277. void
  278. clear_pending_onions(void)
  279. {
  280. onion_queue_t *victim, *next;
  281. int i;
  282. for (i=0; i<=MAX_ONION_HANDSHAKE_TYPE; i++) {
  283. for (victim = TOR_TAILQ_FIRST(&ol_list[i]); victim; victim = next) {
  284. next = TOR_TAILQ_NEXT(victim,next);
  285. onion_queue_entry_remove(victim);
  286. }
  287. tor_assert(TOR_TAILQ_EMPTY(&ol_list[i]));
  288. }
  289. memset(ol_entries, 0, sizeof(ol_entries));
  290. }
  291. /* ============================================================ */
  292. /** Return a new server_onion_keys_t object with all of the keys
  293. * and other info we might need to do onion handshakes. (We make a copy of
  294. * our keys for each cpuworker to avoid race conditions with the main thread,
  295. * and to avoid locking) */
  296. server_onion_keys_t *
  297. server_onion_keys_new(void)
  298. {
  299. server_onion_keys_t *keys = tor_malloc_zero(sizeof(server_onion_keys_t));
  300. memcpy(keys->my_identity, router_get_my_id_digest(), DIGEST_LEN);
  301. dup_onion_keys(&keys->onion_key, &keys->last_onion_key);
  302. keys->curve25519_key_map = construct_ntor_key_map();
  303. keys->junk_keypair = tor_malloc_zero(sizeof(curve25519_keypair_t));
  304. curve25519_keypair_generate(keys->junk_keypair, 0);
  305. return keys;
  306. }
  307. /** Release all storage held in <b>keys</b>. */
  308. void
  309. server_onion_keys_free(server_onion_keys_t *keys)
  310. {
  311. if (! keys)
  312. return;
  313. crypto_pk_free(keys->onion_key);
  314. crypto_pk_free(keys->last_onion_key);
  315. ntor_key_map_free(keys->curve25519_key_map);
  316. tor_free(keys->junk_keypair);
  317. memwipe(keys, 0, sizeof(server_onion_keys_t));
  318. tor_free(keys);
  319. }
  320. /** Release whatever storage is held in <b>state</b>, depending on its
  321. * type, and clear its pointer. */
  322. void
  323. onion_handshake_state_release(onion_handshake_state_t *state)
  324. {
  325. switch (state->tag) {
  326. case ONION_HANDSHAKE_TYPE_TAP:
  327. crypto_dh_free(state->u.tap);
  328. state->u.tap = NULL;
  329. break;
  330. case ONION_HANDSHAKE_TYPE_FAST:
  331. fast_handshake_state_free(state->u.fast);
  332. state->u.fast = NULL;
  333. break;
  334. case ONION_HANDSHAKE_TYPE_NTOR:
  335. ntor_handshake_state_free(state->u.ntor);
  336. state->u.ntor = NULL;
  337. break;
  338. default:
  339. log_warn(LD_BUG, "called with unknown handshake state type %d",
  340. (int)state->tag);
  341. tor_fragile_assert();
  342. }
  343. }
  344. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  345. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  346. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  347. * Return -1 on failure, and the length of the onionskin on acceptance.
  348. */
  349. int
  350. onion_skin_create(int type,
  351. const extend_info_t *node,
  352. onion_handshake_state_t *state_out,
  353. uint8_t *onion_skin_out)
  354. {
  355. int r = -1;
  356. switch (type) {
  357. case ONION_HANDSHAKE_TYPE_TAP:
  358. if (!node->onion_key)
  359. return -1;
  360. if (onion_skin_TAP_create(node->onion_key,
  361. &state_out->u.tap,
  362. (char*)onion_skin_out) < 0)
  363. return -1;
  364. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  365. break;
  366. case ONION_HANDSHAKE_TYPE_FAST:
  367. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  368. return -1;
  369. r = CREATE_FAST_LEN;
  370. break;
  371. case ONION_HANDSHAKE_TYPE_NTOR:
  372. if (tor_mem_is_zero((const char*)node->curve25519_onion_key.public_key,
  373. CURVE25519_PUBKEY_LEN))
  374. return -1;
  375. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  376. &node->curve25519_onion_key,
  377. &state_out->u.ntor,
  378. onion_skin_out) < 0)
  379. return -1;
  380. r = NTOR_ONIONSKIN_LEN;
  381. break;
  382. default:
  383. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  384. tor_fragile_assert();
  385. r = -1;
  386. }
  387. if (r > 0)
  388. state_out->tag = (uint16_t) type;
  389. return r;
  390. }
  391. /** Perform the second (server-side) step of a circuit-creation handshake of
  392. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  393. * using the keys in <b>keys</b>. On success, write our response into
  394. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  395. * in <b>keys_out_len</b>, a hidden service nonce to <b>rend_nonce_out</b>,
  396. * and return the length of the reply. On failure, return -1.
  397. */
  398. int
  399. onion_skin_server_handshake(int type,
  400. const uint8_t *onion_skin, size_t onionskin_len,
  401. const server_onion_keys_t *keys,
  402. uint8_t *reply_out,
  403. uint8_t *keys_out, size_t keys_out_len,
  404. uint8_t *rend_nonce_out)
  405. {
  406. int r = -1;
  407. switch (type) {
  408. case ONION_HANDSHAKE_TYPE_TAP:
  409. if (onionskin_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  410. return -1;
  411. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  412. keys->onion_key, keys->last_onion_key,
  413. (char*)reply_out,
  414. (char*)keys_out, keys_out_len)<0)
  415. return -1;
  416. r = TAP_ONIONSKIN_REPLY_LEN;
  417. memcpy(rend_nonce_out, reply_out+DH_KEY_LEN, DIGEST_LEN);
  418. break;
  419. case ONION_HANDSHAKE_TYPE_FAST:
  420. if (onionskin_len != CREATE_FAST_LEN)
  421. return -1;
  422. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  423. return -1;
  424. r = CREATED_FAST_LEN;
  425. memcpy(rend_nonce_out, reply_out+DIGEST_LEN, DIGEST_LEN);
  426. break;
  427. case ONION_HANDSHAKE_TYPE_NTOR:
  428. if (onionskin_len < NTOR_ONIONSKIN_LEN)
  429. return -1;
  430. {
  431. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  432. uint8_t *keys_tmp = tor_malloc(keys_out_len + DIGEST_LEN);
  433. if (onion_skin_ntor_server_handshake(
  434. onion_skin, keys->curve25519_key_map,
  435. keys->junk_keypair,
  436. keys->my_identity,
  437. reply_out, keys_tmp, keys_tmp_len)<0) {
  438. tor_free(keys_tmp);
  439. return -1;
  440. }
  441. memcpy(keys_out, keys_tmp, keys_out_len);
  442. memcpy(rend_nonce_out, keys_tmp+keys_out_len, DIGEST_LEN);
  443. memwipe(keys_tmp, 0, keys_tmp_len);
  444. tor_free(keys_tmp);
  445. r = NTOR_REPLY_LEN;
  446. }
  447. break;
  448. default:
  449. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  450. tor_fragile_assert();
  451. return -1;
  452. }
  453. return r;
  454. }
  455. /** Perform the final (client-side) step of a circuit-creation handshake of
  456. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  457. * server's response in <b>reply</b>. On success, generate <b>keys_out_len</b>
  458. * bytes worth of key material in <b>keys_out_len</b>, set
  459. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  460. * establish introduction points at this hop, and return 0. On failure,
  461. * return -1, and set *msg_out to an error message if this is worth
  462. * complaining to the usre about. */
  463. int
  464. onion_skin_client_handshake(int type,
  465. const onion_handshake_state_t *handshake_state,
  466. const uint8_t *reply, size_t reply_len,
  467. uint8_t *keys_out, size_t keys_out_len,
  468. uint8_t *rend_authenticator_out,
  469. const char **msg_out)
  470. {
  471. if (handshake_state->tag != type)
  472. return -1;
  473. switch (type) {
  474. case ONION_HANDSHAKE_TYPE_TAP:
  475. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  476. if (msg_out)
  477. *msg_out = "TAP reply was not of the correct length.";
  478. return -1;
  479. }
  480. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  481. (const char*)reply,
  482. (char *)keys_out, keys_out_len,
  483. msg_out) < 0)
  484. return -1;
  485. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  486. return 0;
  487. case ONION_HANDSHAKE_TYPE_FAST:
  488. if (reply_len != CREATED_FAST_LEN) {
  489. if (msg_out)
  490. *msg_out = "TAP reply was not of the correct length.";
  491. return -1;
  492. }
  493. if (fast_client_handshake(handshake_state->u.fast, reply,
  494. keys_out, keys_out_len, msg_out) < 0)
  495. return -1;
  496. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  497. return 0;
  498. case ONION_HANDSHAKE_TYPE_NTOR:
  499. if (reply_len < NTOR_REPLY_LEN) {
  500. if (msg_out)
  501. *msg_out = "ntor reply was not of the correct length.";
  502. return -1;
  503. }
  504. {
  505. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  506. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  507. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  508. reply,
  509. keys_tmp, keys_tmp_len, msg_out) < 0) {
  510. tor_free(keys_tmp);
  511. return -1;
  512. }
  513. memcpy(keys_out, keys_tmp, keys_out_len);
  514. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  515. memwipe(keys_tmp, 0, keys_tmp_len);
  516. tor_free(keys_tmp);
  517. }
  518. return 0;
  519. default:
  520. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  521. tor_fragile_assert();
  522. return -1;
  523. }
  524. }
  525. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  526. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  527. * recognize. */
  528. static int
  529. check_create_cell(const create_cell_t *cell, int unknown_ok)
  530. {
  531. switch (cell->cell_type) {
  532. case CELL_CREATE:
  533. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  534. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  535. return -1;
  536. break;
  537. case CELL_CREATE_FAST:
  538. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  539. return -1;
  540. break;
  541. case CELL_CREATE2:
  542. break;
  543. default:
  544. return -1;
  545. }
  546. switch (cell->handshake_type) {
  547. case ONION_HANDSHAKE_TYPE_TAP:
  548. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  549. return -1;
  550. break;
  551. case ONION_HANDSHAKE_TYPE_FAST:
  552. if (cell->handshake_len != CREATE_FAST_LEN)
  553. return -1;
  554. break;
  555. case ONION_HANDSHAKE_TYPE_NTOR:
  556. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  557. return -1;
  558. break;
  559. default:
  560. if (! unknown_ok)
  561. return -1;
  562. }
  563. return 0;
  564. }
  565. /** Write the various parameters into the create cell. Separate from
  566. * create_cell_parse() to make unit testing easier.
  567. */
  568. void
  569. create_cell_init(create_cell_t *cell_out, uint8_t cell_type,
  570. uint16_t handshake_type, uint16_t handshake_len,
  571. const uint8_t *onionskin)
  572. {
  573. memset(cell_out, 0, sizeof(*cell_out));
  574. cell_out->cell_type = cell_type;
  575. cell_out->handshake_type = handshake_type;
  576. cell_out->handshake_len = handshake_len;
  577. memcpy(cell_out->onionskin, onionskin, handshake_len);
  578. }
  579. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  580. * <b>p_len</b> bytes long, and use it to fill the fields of
  581. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  582. *
  583. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  584. * this function is also used for parsing those.
  585. */
  586. static int
  587. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  588. {
  589. uint16_t handshake_type, handshake_len;
  590. if (p_len < 4)
  591. return -1;
  592. handshake_type = ntohs(get_uint16(p));
  593. handshake_len = ntohs(get_uint16(p+2));
  594. if (handshake_len > CELL_PAYLOAD_SIZE - 4 || handshake_len > p_len - 4)
  595. return -1;
  596. if (handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  597. return -1;
  598. create_cell_init(cell_out, CELL_CREATE2, handshake_type, handshake_len,
  599. p+4);
  600. return 0;
  601. }
  602. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  603. * TAP payload is really an ntor payload. We'd do away with this if every
  604. * relay supported EXTEND2, but we want to be able to extend from A to B with
  605. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  606. * CREATE2 cell.
  607. **/
  608. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  609. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  610. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  611. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  612. int
  613. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  614. {
  615. switch (cell_in->command) {
  616. case CELL_CREATE:
  617. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  618. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  619. NTOR_ONIONSKIN_LEN, cell_in->payload+16);
  620. } else {
  621. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  622. TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->payload);
  623. }
  624. break;
  625. case CELL_CREATE_FAST:
  626. create_cell_init(cell_out, CELL_CREATE_FAST, ONION_HANDSHAKE_TYPE_FAST,
  627. CREATE_FAST_LEN, cell_in->payload);
  628. break;
  629. case CELL_CREATE2:
  630. if (parse_create2_payload(cell_out, cell_in->payload,
  631. CELL_PAYLOAD_SIZE) < 0)
  632. return -1;
  633. break;
  634. default:
  635. return -1;
  636. }
  637. return check_create_cell(cell_out, 0);
  638. }
  639. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  640. static int
  641. check_created_cell(const created_cell_t *cell)
  642. {
  643. switch (cell->cell_type) {
  644. case CELL_CREATED:
  645. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN &&
  646. cell->handshake_len != NTOR_REPLY_LEN)
  647. return -1;
  648. break;
  649. case CELL_CREATED_FAST:
  650. if (cell->handshake_len != CREATED_FAST_LEN)
  651. return -1;
  652. break;
  653. case CELL_CREATED2:
  654. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  655. return -1;
  656. break;
  657. }
  658. return 0;
  659. }
  660. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  661. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  662. int
  663. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  664. {
  665. memset(cell_out, 0, sizeof(*cell_out));
  666. switch (cell_in->command) {
  667. case CELL_CREATED:
  668. cell_out->cell_type = CELL_CREATED;
  669. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  670. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  671. break;
  672. case CELL_CREATED_FAST:
  673. cell_out->cell_type = CELL_CREATED_FAST;
  674. cell_out->handshake_len = CREATED_FAST_LEN;
  675. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  676. break;
  677. case CELL_CREATED2:
  678. {
  679. const uint8_t *p = cell_in->payload;
  680. cell_out->cell_type = CELL_CREATED2;
  681. cell_out->handshake_len = ntohs(get_uint16(p));
  682. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  683. return -1;
  684. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  685. break;
  686. }
  687. }
  688. return check_created_cell(cell_out);
  689. }
  690. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  691. static int
  692. check_extend_cell(const extend_cell_t *cell)
  693. {
  694. if (tor_digest_is_zero((const char*)cell->node_id))
  695. return -1;
  696. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  697. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  698. return -1;
  699. if (cell->create_cell.cell_type == CELL_CREATE) {
  700. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  701. return -1;
  702. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  703. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  704. cell->cell_type != RELAY_COMMAND_EXTEND)
  705. return -1;
  706. } else {
  707. /* In particular, no CREATE_FAST cells are allowed */
  708. return -1;
  709. }
  710. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  711. return -1;
  712. return check_create_cell(&cell->create_cell, 1);
  713. }
  714. /** Protocol constants for specifier types in EXTEND2
  715. * @{
  716. */
  717. #define SPECTYPE_IPV4 0
  718. #define SPECTYPE_IPV6 1
  719. #define SPECTYPE_LEGACY_ID 2
  720. /** @} */
  721. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  722. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  723. * 0 on success, -1 on failure. */
  724. int
  725. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  726. const uint8_t *payload, size_t payload_length)
  727. {
  728. const uint8_t *eop;
  729. memset(cell_out, 0, sizeof(*cell_out));
  730. if (payload_length > RELAY_PAYLOAD_SIZE)
  731. return -1;
  732. eop = payload + payload_length;
  733. switch (command) {
  734. case RELAY_COMMAND_EXTEND:
  735. {
  736. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  737. return -1;
  738. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  739. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  740. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  741. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  742. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  743. cell_out->create_cell.cell_type = CELL_CREATE2;
  744. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  745. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  746. memcpy(cell_out->create_cell.onionskin, payload + 22,
  747. NTOR_ONIONSKIN_LEN);
  748. } else {
  749. cell_out->create_cell.cell_type = CELL_CREATE;
  750. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  751. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  752. memcpy(cell_out->create_cell.onionskin, payload + 6,
  753. TAP_ONIONSKIN_CHALLENGE_LEN);
  754. }
  755. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  756. DIGEST_LEN);
  757. break;
  758. }
  759. case RELAY_COMMAND_EXTEND2:
  760. {
  761. uint8_t n_specs, spectype, speclen;
  762. int i;
  763. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  764. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  765. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  766. if (payload_length == 0)
  767. return -1;
  768. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  769. n_specs = *payload++;
  770. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  771. * address, and the node ID, and ignore everything else */
  772. for (i = 0; i < n_specs; ++i) {
  773. if (eop - payload < 2)
  774. return -1;
  775. spectype = payload[0];
  776. speclen = payload[1];
  777. payload += 2;
  778. if (eop - payload < speclen)
  779. return -1;
  780. switch (spectype) {
  781. case SPECTYPE_IPV4:
  782. if (speclen != 6)
  783. return -1;
  784. if (!found_ipv4) {
  785. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  786. get_uint32(payload));
  787. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  788. found_ipv4 = 1;
  789. }
  790. break;
  791. case SPECTYPE_IPV6:
  792. if (speclen != 18)
  793. return -1;
  794. if (!found_ipv6) {
  795. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  796. (const char*)payload);
  797. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  798. found_ipv6 = 1;
  799. }
  800. break;
  801. case SPECTYPE_LEGACY_ID:
  802. if (speclen != 20)
  803. return -1;
  804. if (found_id)
  805. return -1;
  806. memcpy(cell_out->node_id, payload, 20);
  807. found_id = 1;
  808. break;
  809. }
  810. payload += speclen;
  811. }
  812. if (!found_id || !found_ipv4)
  813. return -1;
  814. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  815. return -1;
  816. break;
  817. }
  818. default:
  819. return -1;
  820. }
  821. return check_extend_cell(cell_out);
  822. }
  823. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  824. static int
  825. check_extended_cell(const extended_cell_t *cell)
  826. {
  827. if (cell->created_cell.cell_type == CELL_CREATED) {
  828. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  829. return -1;
  830. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  831. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  832. return -1;
  833. } else {
  834. return -1;
  835. }
  836. return check_created_cell(&cell->created_cell);
  837. }
  838. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  839. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  840. * 0 on success, -1 on failure. */
  841. int
  842. extended_cell_parse(extended_cell_t *cell_out,
  843. const uint8_t command, const uint8_t *payload,
  844. size_t payload_len)
  845. {
  846. memset(cell_out, 0, sizeof(*cell_out));
  847. if (payload_len > RELAY_PAYLOAD_SIZE)
  848. return -1;
  849. switch (command) {
  850. case RELAY_COMMAND_EXTENDED:
  851. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  852. return -1;
  853. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  854. cell_out->created_cell.cell_type = CELL_CREATED;
  855. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  856. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  857. break;
  858. case RELAY_COMMAND_EXTENDED2:
  859. {
  860. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  861. cell_out->created_cell.cell_type = CELL_CREATED2;
  862. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  863. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  864. cell_out->created_cell.handshake_len > payload_len - 2)
  865. return -1;
  866. memcpy(cell_out->created_cell.reply, payload+2,
  867. cell_out->created_cell.handshake_len);
  868. }
  869. break;
  870. default:
  871. return -1;
  872. }
  873. return check_extended_cell(cell_out);
  874. }
  875. /** Fill <b>cell_out</b> with a correctly formatted version of the
  876. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  877. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  878. static int
  879. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  880. int relayed)
  881. {
  882. uint8_t *p;
  883. size_t space;
  884. if (check_create_cell(cell_in, relayed) < 0)
  885. return -1;
  886. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  887. cell_out->command = cell_in->cell_type;
  888. p = cell_out->payload;
  889. space = sizeof(cell_out->payload);
  890. switch (cell_in->cell_type) {
  891. case CELL_CREATE:
  892. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  893. memcpy(p, NTOR_CREATE_MAGIC, 16);
  894. p += 16;
  895. space -= 16;
  896. }
  897. /* Fall through */
  898. case CELL_CREATE_FAST:
  899. tor_assert(cell_in->handshake_len <= space);
  900. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  901. break;
  902. case CELL_CREATE2:
  903. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  904. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  905. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  906. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  907. break;
  908. default:
  909. return -1;
  910. }
  911. return 0;
  912. }
  913. int
  914. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  915. {
  916. return create_cell_format_impl(cell_out, cell_in, 0);
  917. }
  918. int
  919. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  920. {
  921. return create_cell_format_impl(cell_out, cell_in, 1);
  922. }
  923. /** Fill <b>cell_out</b> with a correctly formatted version of the
  924. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  925. * failure. */
  926. int
  927. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  928. {
  929. if (check_created_cell(cell_in) < 0)
  930. return -1;
  931. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  932. cell_out->command = cell_in->cell_type;
  933. switch (cell_in->cell_type) {
  934. case CELL_CREATED:
  935. case CELL_CREATED_FAST:
  936. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  937. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  938. break;
  939. case CELL_CREATED2:
  940. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  941. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  942. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  943. break;
  944. default:
  945. return -1;
  946. }
  947. return 0;
  948. }
  949. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  950. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  951. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  952. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  953. int
  954. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  955. uint8_t *payload_out, const extend_cell_t *cell_in)
  956. {
  957. uint8_t *p, *eop;
  958. if (check_extend_cell(cell_in) < 0)
  959. return -1;
  960. p = payload_out;
  961. eop = payload_out + RELAY_PAYLOAD_SIZE;
  962. memset(p, 0, RELAY_PAYLOAD_SIZE);
  963. switch (cell_in->cell_type) {
  964. case RELAY_COMMAND_EXTEND:
  965. {
  966. *command_out = RELAY_COMMAND_EXTEND;
  967. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  968. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  969. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  970. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  971. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  972. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  973. } else {
  974. memcpy(p+6, cell_in->create_cell.onionskin,
  975. TAP_ONIONSKIN_CHALLENGE_LEN);
  976. }
  977. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  978. }
  979. break;
  980. case RELAY_COMMAND_EXTEND2:
  981. {
  982. uint8_t n = 2;
  983. *command_out = RELAY_COMMAND_EXTEND2;
  984. *p++ = n; /* 2 identifiers */
  985. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  986. *p++ = 6; /* It's 6 bytes long. */
  987. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  988. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  989. p += 6;
  990. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  991. *p++ = 20; /* It's 20 bytes long */
  992. memcpy(p, cell_in->node_id, DIGEST_LEN);
  993. p += 20;
  994. /* Now we can send the handshake */
  995. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  996. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  997. p += 4;
  998. if (cell_in->create_cell.handshake_len > eop - p)
  999. return -1;
  1000. memcpy(p, cell_in->create_cell.onionskin,
  1001. cell_in->create_cell.handshake_len);
  1002. p += cell_in->create_cell.handshake_len;
  1003. *len_out = p - payload_out;
  1004. }
  1005. break;
  1006. default:
  1007. return -1;
  1008. }
  1009. return 0;
  1010. }
  1011. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  1012. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1013. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1014. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1015. int
  1016. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  1017. uint8_t *payload_out, const extended_cell_t *cell_in)
  1018. {
  1019. uint8_t *p;
  1020. if (check_extended_cell(cell_in) < 0)
  1021. return -1;
  1022. p = payload_out;
  1023. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1024. switch (cell_in->cell_type) {
  1025. case RELAY_COMMAND_EXTENDED:
  1026. {
  1027. *command_out = RELAY_COMMAND_EXTENDED;
  1028. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  1029. memcpy(payload_out, cell_in->created_cell.reply,
  1030. TAP_ONIONSKIN_REPLY_LEN);
  1031. }
  1032. break;
  1033. case RELAY_COMMAND_EXTENDED2:
  1034. {
  1035. *command_out = RELAY_COMMAND_EXTENDED2;
  1036. *len_out = 2 + cell_in->created_cell.handshake_len;
  1037. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  1038. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  1039. return -1;
  1040. memcpy(payload_out+2, cell_in->created_cell.reply,
  1041. cell_in->created_cell.handshake_len);
  1042. }
  1043. break;
  1044. default:
  1045. return -1;
  1046. }
  1047. return 0;
  1048. }