onion.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. */
  462. int
  463. onion_skin_client_handshake(int type,
  464. const onion_handshake_state_t *handshake_state,
  465. const uint8_t *reply, size_t reply_len,
  466. uint8_t *keys_out, size_t keys_out_len,
  467. uint8_t *rend_authenticator_out)
  468. {
  469. if (handshake_state->tag != type)
  470. return -1;
  471. switch (type) {
  472. case ONION_HANDSHAKE_TYPE_TAP:
  473. if (reply_len != TAP_ONIONSKIN_REPLY_LEN) {
  474. log_warn(LD_CIRC, "TAP reply was not of the correct length.");
  475. return -1;
  476. }
  477. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  478. (const char*)reply,
  479. (char *)keys_out, keys_out_len) < 0)
  480. return -1;
  481. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  482. return 0;
  483. case ONION_HANDSHAKE_TYPE_FAST:
  484. if (reply_len != CREATED_FAST_LEN) {
  485. log_warn(LD_CIRC, "CREATED_FAST reply was not of the correct length.");
  486. return -1;
  487. }
  488. if (fast_client_handshake(handshake_state->u.fast, reply,
  489. keys_out, keys_out_len) < 0)
  490. return -1;
  491. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  492. return 0;
  493. case ONION_HANDSHAKE_TYPE_NTOR:
  494. if (reply_len < NTOR_REPLY_LEN) {
  495. log_warn(LD_CIRC, "ntor reply was not of the correct length.");
  496. return -1;
  497. }
  498. {
  499. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  500. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  501. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  502. reply,
  503. keys_tmp, keys_tmp_len) < 0) {
  504. tor_free(keys_tmp);
  505. return -1;
  506. }
  507. memcpy(keys_out, keys_tmp, keys_out_len);
  508. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  509. memwipe(keys_tmp, 0, keys_tmp_len);
  510. tor_free(keys_tmp);
  511. }
  512. return 0;
  513. default:
  514. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  515. tor_fragile_assert();
  516. return -1;
  517. }
  518. }
  519. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. If
  520. * <b>unknown_ok</b> is true, allow cells with handshake types we don't
  521. * recognize. */
  522. static int
  523. check_create_cell(const create_cell_t *cell, int unknown_ok)
  524. {
  525. switch (cell->cell_type) {
  526. case CELL_CREATE:
  527. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_TAP &&
  528. cell->handshake_type != ONION_HANDSHAKE_TYPE_NTOR)
  529. return -1;
  530. break;
  531. case CELL_CREATE_FAST:
  532. if (cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST)
  533. return -1;
  534. break;
  535. case CELL_CREATE2:
  536. break;
  537. default:
  538. return -1;
  539. }
  540. switch (cell->handshake_type) {
  541. case ONION_HANDSHAKE_TYPE_TAP:
  542. if (cell->handshake_len != TAP_ONIONSKIN_CHALLENGE_LEN)
  543. return -1;
  544. break;
  545. case ONION_HANDSHAKE_TYPE_FAST:
  546. if (cell->handshake_len != CREATE_FAST_LEN)
  547. return -1;
  548. break;
  549. case ONION_HANDSHAKE_TYPE_NTOR:
  550. if (cell->handshake_len != NTOR_ONIONSKIN_LEN)
  551. return -1;
  552. break;
  553. default:
  554. if (! unknown_ok)
  555. return -1;
  556. }
  557. return 0;
  558. }
  559. /** Write the various parameters into the create cell. Separate from
  560. * create_cell_parse() to make unit testing easier.
  561. */
  562. void
  563. create_cell_init(create_cell_t *cell_out, uint8_t cell_type,
  564. uint16_t handshake_type, uint16_t handshake_len,
  565. const uint8_t *onionskin)
  566. {
  567. memset(cell_out, 0, sizeof(*cell_out));
  568. cell_out->cell_type = cell_type;
  569. cell_out->handshake_type = handshake_type;
  570. cell_out->handshake_len = handshake_len;
  571. memcpy(cell_out->onionskin, onionskin, handshake_len);
  572. }
  573. /** Helper: parse the CREATE2 payload at <b>p</b>, which could be up to
  574. * <b>p_len</b> bytes long, and use it to fill the fields of
  575. * <b>cell_out</b>. Return 0 on success and -1 on failure.
  576. *
  577. * Note that part of the body of an EXTEND2 cell is a CREATE2 payload, so
  578. * this function is also used for parsing those.
  579. */
  580. static int
  581. parse_create2_payload(create_cell_t *cell_out, const uint8_t *p, size_t p_len)
  582. {
  583. uint16_t handshake_type, handshake_len;
  584. if (p_len < 4)
  585. return -1;
  586. handshake_type = ntohs(get_uint16(p));
  587. handshake_len = ntohs(get_uint16(p+2));
  588. if (handshake_len > CELL_PAYLOAD_SIZE - 4 || handshake_len > p_len - 4)
  589. return -1;
  590. if (handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  591. return -1;
  592. create_cell_init(cell_out, CELL_CREATE2, handshake_type, handshake_len,
  593. p+4);
  594. return 0;
  595. }
  596. /** Magic string which, in a CREATE or EXTEND cell, indicates that a seeming
  597. * TAP payload is really an ntor payload. We'd do away with this if every
  598. * relay supported EXTEND2, but we want to be able to extend from A to B with
  599. * ntor even when A doesn't understand EXTEND2 and so can't generate a
  600. * CREATE2 cell.
  601. **/
  602. #define NTOR_CREATE_MAGIC "ntorNTORntorNTOR"
  603. /** Parse a CREATE, CREATE_FAST, or CREATE2 cell from <b>cell_in</b> into
  604. * <b>cell_out</b>. Return 0 on success, -1 on failure. (We reject some
  605. * syntactically valid CREATE2 cells that we can't generate or react to.) */
  606. int
  607. create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
  608. {
  609. switch (cell_in->command) {
  610. case CELL_CREATE:
  611. if (tor_memeq(cell_in->payload, NTOR_CREATE_MAGIC, 16)) {
  612. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  613. NTOR_ONIONSKIN_LEN, cell_in->payload+16);
  614. } else {
  615. create_cell_init(cell_out, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  616. TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->payload);
  617. }
  618. break;
  619. case CELL_CREATE_FAST:
  620. create_cell_init(cell_out, CELL_CREATE_FAST, ONION_HANDSHAKE_TYPE_FAST,
  621. CREATE_FAST_LEN, cell_in->payload);
  622. break;
  623. case CELL_CREATE2:
  624. if (parse_create2_payload(cell_out, cell_in->payload,
  625. CELL_PAYLOAD_SIZE) < 0)
  626. return -1;
  627. break;
  628. default:
  629. return -1;
  630. }
  631. return check_create_cell(cell_out, 0);
  632. }
  633. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  634. static int
  635. check_created_cell(const created_cell_t *cell)
  636. {
  637. switch (cell->cell_type) {
  638. case CELL_CREATED:
  639. if (cell->handshake_len != TAP_ONIONSKIN_REPLY_LEN &&
  640. cell->handshake_len != NTOR_REPLY_LEN)
  641. return -1;
  642. break;
  643. case CELL_CREATED_FAST:
  644. if (cell->handshake_len != CREATED_FAST_LEN)
  645. return -1;
  646. break;
  647. case CELL_CREATED2:
  648. if (cell->handshake_len > RELAY_PAYLOAD_SIZE-2)
  649. return -1;
  650. break;
  651. }
  652. return 0;
  653. }
  654. /** Parse a CREATED, CREATED_FAST, or CREATED2 cell from <b>cell_in</b> into
  655. * <b>cell_out</b>. Return 0 on success, -1 on failure. */
  656. int
  657. created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
  658. {
  659. memset(cell_out, 0, sizeof(*cell_out));
  660. switch (cell_in->command) {
  661. case CELL_CREATED:
  662. cell_out->cell_type = CELL_CREATED;
  663. cell_out->handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  664. memcpy(cell_out->reply, cell_in->payload, TAP_ONIONSKIN_REPLY_LEN);
  665. break;
  666. case CELL_CREATED_FAST:
  667. cell_out->cell_type = CELL_CREATED_FAST;
  668. cell_out->handshake_len = CREATED_FAST_LEN;
  669. memcpy(cell_out->reply, cell_in->payload, CREATED_FAST_LEN);
  670. break;
  671. case CELL_CREATED2:
  672. {
  673. const uint8_t *p = cell_in->payload;
  674. cell_out->cell_type = CELL_CREATED2;
  675. cell_out->handshake_len = ntohs(get_uint16(p));
  676. if (cell_out->handshake_len > CELL_PAYLOAD_SIZE - 2)
  677. return -1;
  678. memcpy(cell_out->reply, p+2, cell_out->handshake_len);
  679. break;
  680. }
  681. }
  682. return check_created_cell(cell_out);
  683. }
  684. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  685. static int
  686. check_extend_cell(const extend_cell_t *cell)
  687. {
  688. if (tor_digest_is_zero((const char*)cell->node_id))
  689. return -1;
  690. /* We don't currently allow EXTEND2 cells without an IPv4 address */
  691. if (tor_addr_family(&cell->orport_ipv4.addr) == AF_UNSPEC)
  692. return -1;
  693. if (cell->create_cell.cell_type == CELL_CREATE) {
  694. if (cell->cell_type != RELAY_COMMAND_EXTEND)
  695. return -1;
  696. } else if (cell->create_cell.cell_type == CELL_CREATE2) {
  697. if (cell->cell_type != RELAY_COMMAND_EXTEND2 &&
  698. cell->cell_type != RELAY_COMMAND_EXTEND)
  699. return -1;
  700. } else {
  701. /* In particular, no CREATE_FAST cells are allowed */
  702. return -1;
  703. }
  704. if (cell->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_FAST)
  705. return -1;
  706. return check_create_cell(&cell->create_cell, 1);
  707. }
  708. /** Protocol constants for specifier types in EXTEND2
  709. * @{
  710. */
  711. #define SPECTYPE_IPV4 0
  712. #define SPECTYPE_IPV6 1
  713. #define SPECTYPE_LEGACY_ID 2
  714. /** @} */
  715. /** Parse an EXTEND or EXTEND2 cell (according to <b>command</b>) from the
  716. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  717. * 0 on success, -1 on failure. */
  718. int
  719. extend_cell_parse(extend_cell_t *cell_out, const uint8_t command,
  720. const uint8_t *payload, size_t payload_length)
  721. {
  722. const uint8_t *eop;
  723. memset(cell_out, 0, sizeof(*cell_out));
  724. if (payload_length > RELAY_PAYLOAD_SIZE)
  725. return -1;
  726. eop = payload + payload_length;
  727. switch (command) {
  728. case RELAY_COMMAND_EXTEND:
  729. {
  730. if (payload_length != 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN)
  731. return -1;
  732. cell_out->cell_type = RELAY_COMMAND_EXTEND;
  733. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr, get_uint32(payload));
  734. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  735. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  736. if (tor_memeq(payload + 6, NTOR_CREATE_MAGIC, 16)) {
  737. cell_out->create_cell.cell_type = CELL_CREATE2;
  738. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_NTOR;
  739. cell_out->create_cell.handshake_len = NTOR_ONIONSKIN_LEN;
  740. memcpy(cell_out->create_cell.onionskin, payload + 22,
  741. NTOR_ONIONSKIN_LEN);
  742. } else {
  743. cell_out->create_cell.cell_type = CELL_CREATE;
  744. cell_out->create_cell.handshake_type = ONION_HANDSHAKE_TYPE_TAP;
  745. cell_out->create_cell.handshake_len = TAP_ONIONSKIN_CHALLENGE_LEN;
  746. memcpy(cell_out->create_cell.onionskin, payload + 6,
  747. TAP_ONIONSKIN_CHALLENGE_LEN);
  748. }
  749. memcpy(cell_out->node_id, payload + 6 + TAP_ONIONSKIN_CHALLENGE_LEN,
  750. DIGEST_LEN);
  751. break;
  752. }
  753. case RELAY_COMMAND_EXTEND2:
  754. {
  755. uint8_t n_specs, spectype, speclen;
  756. int i;
  757. int found_ipv4 = 0, found_ipv6 = 0, found_id = 0;
  758. tor_addr_make_unspec(&cell_out->orport_ipv4.addr);
  759. tor_addr_make_unspec(&cell_out->orport_ipv6.addr);
  760. if (payload_length == 0)
  761. return -1;
  762. cell_out->cell_type = RELAY_COMMAND_EXTEND2;
  763. n_specs = *payload++;
  764. /* Parse the specifiers. We'll only take the first IPv4 and first IPv6
  765. * address, and the node ID, and ignore everything else */
  766. for (i = 0; i < n_specs; ++i) {
  767. if (eop - payload < 2)
  768. return -1;
  769. spectype = payload[0];
  770. speclen = payload[1];
  771. payload += 2;
  772. if (eop - payload < speclen)
  773. return -1;
  774. switch (spectype) {
  775. case SPECTYPE_IPV4:
  776. if (speclen != 6)
  777. return -1;
  778. if (!found_ipv4) {
  779. tor_addr_from_ipv4n(&cell_out->orport_ipv4.addr,
  780. get_uint32(payload));
  781. cell_out->orport_ipv4.port = ntohs(get_uint16(payload+4));
  782. found_ipv4 = 1;
  783. }
  784. break;
  785. case SPECTYPE_IPV6:
  786. if (speclen != 18)
  787. return -1;
  788. if (!found_ipv6) {
  789. tor_addr_from_ipv6_bytes(&cell_out->orport_ipv6.addr,
  790. (const char*)payload);
  791. cell_out->orport_ipv6.port = ntohs(get_uint16(payload+16));
  792. found_ipv6 = 1;
  793. }
  794. break;
  795. case SPECTYPE_LEGACY_ID:
  796. if (speclen != 20)
  797. return -1;
  798. if (found_id)
  799. return -1;
  800. memcpy(cell_out->node_id, payload, 20);
  801. found_id = 1;
  802. break;
  803. }
  804. payload += speclen;
  805. }
  806. if (!found_id || !found_ipv4)
  807. return -1;
  808. if (parse_create2_payload(&cell_out->create_cell,payload,eop-payload)<0)
  809. return -1;
  810. break;
  811. }
  812. default:
  813. return -1;
  814. }
  815. return check_extend_cell(cell_out);
  816. }
  817. /** Helper: return 0 if <b>cell</b> appears valid, -1 otherwise. */
  818. static int
  819. check_extended_cell(const extended_cell_t *cell)
  820. {
  821. if (cell->created_cell.cell_type == CELL_CREATED) {
  822. if (cell->cell_type != RELAY_COMMAND_EXTENDED)
  823. return -1;
  824. } else if (cell->created_cell.cell_type == CELL_CREATED2) {
  825. if (cell->cell_type != RELAY_COMMAND_EXTENDED2)
  826. return -1;
  827. } else {
  828. return -1;
  829. }
  830. return check_created_cell(&cell->created_cell);
  831. }
  832. /** Parse an EXTENDED or EXTENDED2 cell (according to <b>command</b>) from the
  833. * <b>payload_length</b> bytes of <b>payload</b> into <b>cell_out</b>. Return
  834. * 0 on success, -1 on failure. */
  835. int
  836. extended_cell_parse(extended_cell_t *cell_out,
  837. const uint8_t command, const uint8_t *payload,
  838. size_t payload_len)
  839. {
  840. memset(cell_out, 0, sizeof(*cell_out));
  841. if (payload_len > RELAY_PAYLOAD_SIZE)
  842. return -1;
  843. switch (command) {
  844. case RELAY_COMMAND_EXTENDED:
  845. if (payload_len != TAP_ONIONSKIN_REPLY_LEN)
  846. return -1;
  847. cell_out->cell_type = RELAY_COMMAND_EXTENDED;
  848. cell_out->created_cell.cell_type = CELL_CREATED;
  849. cell_out->created_cell.handshake_len = TAP_ONIONSKIN_REPLY_LEN;
  850. memcpy(cell_out->created_cell.reply, payload, TAP_ONIONSKIN_REPLY_LEN);
  851. break;
  852. case RELAY_COMMAND_EXTENDED2:
  853. {
  854. cell_out->cell_type = RELAY_COMMAND_EXTENDED2;
  855. cell_out->created_cell.cell_type = CELL_CREATED2;
  856. cell_out->created_cell.handshake_len = ntohs(get_uint16(payload));
  857. if (cell_out->created_cell.handshake_len > RELAY_PAYLOAD_SIZE - 2 ||
  858. cell_out->created_cell.handshake_len > payload_len - 2)
  859. return -1;
  860. memcpy(cell_out->created_cell.reply, payload+2,
  861. cell_out->created_cell.handshake_len);
  862. }
  863. break;
  864. default:
  865. return -1;
  866. }
  867. return check_extended_cell(cell_out);
  868. }
  869. /** Fill <b>cell_out</b> with a correctly formatted version of the
  870. * CREATE{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  871. * failure. This is a cell we didn't originate if <b>relayed</b> is true. */
  872. static int
  873. create_cell_format_impl(cell_t *cell_out, const create_cell_t *cell_in,
  874. int relayed)
  875. {
  876. uint8_t *p;
  877. size_t space;
  878. if (check_create_cell(cell_in, relayed) < 0)
  879. return -1;
  880. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  881. cell_out->command = cell_in->cell_type;
  882. p = cell_out->payload;
  883. space = sizeof(cell_out->payload);
  884. switch (cell_in->cell_type) {
  885. case CELL_CREATE:
  886. if (cell_in->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  887. memcpy(p, NTOR_CREATE_MAGIC, 16);
  888. p += 16;
  889. space -= 16;
  890. }
  891. /* Fall through */
  892. case CELL_CREATE_FAST:
  893. tor_assert(cell_in->handshake_len <= space);
  894. memcpy(p, cell_in->onionskin, cell_in->handshake_len);
  895. break;
  896. case CELL_CREATE2:
  897. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-4);
  898. set_uint16(cell_out->payload, htons(cell_in->handshake_type));
  899. set_uint16(cell_out->payload+2, htons(cell_in->handshake_len));
  900. memcpy(cell_out->payload + 4, cell_in->onionskin, cell_in->handshake_len);
  901. break;
  902. default:
  903. return -1;
  904. }
  905. return 0;
  906. }
  907. int
  908. create_cell_format(cell_t *cell_out, const create_cell_t *cell_in)
  909. {
  910. return create_cell_format_impl(cell_out, cell_in, 0);
  911. }
  912. int
  913. create_cell_format_relayed(cell_t *cell_out, const create_cell_t *cell_in)
  914. {
  915. return create_cell_format_impl(cell_out, cell_in, 1);
  916. }
  917. /** Fill <b>cell_out</b> with a correctly formatted version of the
  918. * CREATED{,_FAST,2} cell in <b>cell_in</b>. Return 0 on success, -1 on
  919. * failure. */
  920. int
  921. created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
  922. {
  923. if (check_created_cell(cell_in) < 0)
  924. return -1;
  925. memset(cell_out->payload, 0, sizeof(cell_out->payload));
  926. cell_out->command = cell_in->cell_type;
  927. switch (cell_in->cell_type) {
  928. case CELL_CREATED:
  929. case CELL_CREATED_FAST:
  930. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload));
  931. memcpy(cell_out->payload, cell_in->reply, cell_in->handshake_len);
  932. break;
  933. case CELL_CREATED2:
  934. tor_assert(cell_in->handshake_len <= sizeof(cell_out->payload)-2);
  935. set_uint16(cell_out->payload, htons(cell_in->handshake_len));
  936. memcpy(cell_out->payload + 2, cell_in->reply, cell_in->handshake_len);
  937. break;
  938. default:
  939. return -1;
  940. }
  941. return 0;
  942. }
  943. /** Format the EXTEND{,2} cell in <b>cell_in</b>, storing its relay payload in
  944. * <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  945. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  946. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  947. int
  948. extend_cell_format(uint8_t *command_out, uint16_t *len_out,
  949. uint8_t *payload_out, const extend_cell_t *cell_in)
  950. {
  951. uint8_t *p, *eop;
  952. if (check_extend_cell(cell_in) < 0)
  953. return -1;
  954. p = payload_out;
  955. eop = payload_out + RELAY_PAYLOAD_SIZE;
  956. memset(p, 0, RELAY_PAYLOAD_SIZE);
  957. switch (cell_in->cell_type) {
  958. case RELAY_COMMAND_EXTEND:
  959. {
  960. *command_out = RELAY_COMMAND_EXTEND;
  961. *len_out = 6 + TAP_ONIONSKIN_CHALLENGE_LEN + DIGEST_LEN;
  962. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  963. set_uint16(p+4, ntohs(cell_in->orport_ipv4.port));
  964. if (cell_in->create_cell.handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
  965. memcpy(p+6, NTOR_CREATE_MAGIC, 16);
  966. memcpy(p+22, cell_in->create_cell.onionskin, NTOR_ONIONSKIN_LEN);
  967. } else {
  968. memcpy(p+6, cell_in->create_cell.onionskin,
  969. TAP_ONIONSKIN_CHALLENGE_LEN);
  970. }
  971. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, cell_in->node_id, DIGEST_LEN);
  972. }
  973. break;
  974. case RELAY_COMMAND_EXTEND2:
  975. {
  976. uint8_t n = 2;
  977. *command_out = RELAY_COMMAND_EXTEND2;
  978. *p++ = n; /* 2 identifiers */
  979. *p++ = SPECTYPE_IPV4; /* First is IPV4. */
  980. *p++ = 6; /* It's 6 bytes long. */
  981. set_uint32(p, tor_addr_to_ipv4n(&cell_in->orport_ipv4.addr));
  982. set_uint16(p+4, htons(cell_in->orport_ipv4.port));
  983. p += 6;
  984. *p++ = SPECTYPE_LEGACY_ID; /* Next is an identity digest. */
  985. *p++ = 20; /* It's 20 bytes long */
  986. memcpy(p, cell_in->node_id, DIGEST_LEN);
  987. p += 20;
  988. /* Now we can send the handshake */
  989. set_uint16(p, htons(cell_in->create_cell.handshake_type));
  990. set_uint16(p+2, htons(cell_in->create_cell.handshake_len));
  991. p += 4;
  992. if (cell_in->create_cell.handshake_len > eop - p)
  993. return -1;
  994. memcpy(p, cell_in->create_cell.onionskin,
  995. cell_in->create_cell.handshake_len);
  996. p += cell_in->create_cell.handshake_len;
  997. *len_out = p - payload_out;
  998. }
  999. break;
  1000. default:
  1001. return -1;
  1002. }
  1003. return 0;
  1004. }
  1005. /** Format the EXTENDED{,2} cell in <b>cell_in</b>, storing its relay payload
  1006. * in <b>payload_out</b>, the number of bytes used in *<b>len_out</b>, and the
  1007. * relay command in *<b>command_out</b>. The <b>payload_out</b> must have
  1008. * RELAY_PAYLOAD_SIZE bytes available. Return 0 on success, -1 on failure. */
  1009. int
  1010. extended_cell_format(uint8_t *command_out, uint16_t *len_out,
  1011. uint8_t *payload_out, const extended_cell_t *cell_in)
  1012. {
  1013. uint8_t *p;
  1014. if (check_extended_cell(cell_in) < 0)
  1015. return -1;
  1016. p = payload_out;
  1017. memset(p, 0, RELAY_PAYLOAD_SIZE);
  1018. switch (cell_in->cell_type) {
  1019. case RELAY_COMMAND_EXTENDED:
  1020. {
  1021. *command_out = RELAY_COMMAND_EXTENDED;
  1022. *len_out = TAP_ONIONSKIN_REPLY_LEN;
  1023. memcpy(payload_out, cell_in->created_cell.reply,
  1024. TAP_ONIONSKIN_REPLY_LEN);
  1025. }
  1026. break;
  1027. case RELAY_COMMAND_EXTENDED2:
  1028. {
  1029. *command_out = RELAY_COMMAND_EXTENDED2;
  1030. *len_out = 2 + cell_in->created_cell.handshake_len;
  1031. set_uint16(payload_out, htons(cell_in->created_cell.handshake_len));
  1032. if (2+cell_in->created_cell.handshake_len > RELAY_PAYLOAD_SIZE)
  1033. return -1;
  1034. memcpy(payload_out+2, cell_in->created_cell.reply,
  1035. cell_in->created_cell.handshake_len);
  1036. }
  1037. break;
  1038. default:
  1039. return -1;
  1040. }
  1041. return 0;
  1042. }