onion.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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-2012, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion.c
  8. * \brief Functions to queue create cells, handle onionskin
  9. * parsing and creation, and wrap the various onionskin types.
  10. **/
  11. #include "or.h"
  12. #include "circuitlist.h"
  13. #include "config.h"
  14. #include "onion.h"
  15. #include "onion_fast.h"
  16. #include "onion_ntor.h"
  17. #include "onion_tap.h"
  18. #include "rephist.h"
  19. #include "router.h"
  20. /** Type for a linked list of circuits that are waiting for a free CPU worker
  21. * to process a waiting onion handshake. */
  22. typedef struct onion_queue_t {
  23. or_circuit_t *circ;
  24. char *onionskin;
  25. time_t when_added;
  26. struct onion_queue_t *next;
  27. } onion_queue_t;
  28. /** 5 seconds on the onion queue til we just send back a destroy */
  29. #define ONIONQUEUE_WAIT_CUTOFF 5
  30. /** First and last elements in the linked list of circuits waiting for CPU
  31. * workers, or NULL if the list is empty.
  32. * @{ */
  33. static onion_queue_t *ol_list=NULL;
  34. static onion_queue_t *ol_tail=NULL;
  35. /**@}*/
  36. /** Length of ol_list */
  37. static int ol_length=0;
  38. /* XXXX Check lengths vs MAX_ONIONSKIN_{CHALLENGE,REPLY}_LEN */
  39. /** Add <b>circ</b> to the end of ol_list and return 0, except
  40. * if ol_list is too long, in which case do nothing and return -1.
  41. */
  42. int
  43. onion_pending_add(or_circuit_t *circ, char *onionskin)
  44. {
  45. onion_queue_t *tmp;
  46. time_t now = time(NULL);
  47. tmp = tor_malloc_zero(sizeof(onion_queue_t));
  48. tmp->circ = circ;
  49. tmp->onionskin = onionskin;
  50. tmp->when_added = now;
  51. if (!ol_tail) {
  52. tor_assert(!ol_list);
  53. tor_assert(!ol_length);
  54. ol_list = tmp;
  55. ol_tail = tmp;
  56. ol_length++;
  57. return 0;
  58. }
  59. tor_assert(ol_list);
  60. tor_assert(!ol_tail->next);
  61. if (ol_length >= get_options()->MaxOnionsPending) {
  62. #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
  63. static ratelim_t last_warned =
  64. RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
  65. char *m;
  66. if ((m = rate_limit_log(&last_warned, approx_time()))) {
  67. log_warn(LD_GENERAL,
  68. "Your computer is too slow to handle this many circuit "
  69. "creation requests! Please consider using the "
  70. "MaxAdvertisedBandwidth config option or choosing a more "
  71. "restricted exit policy.%s",m);
  72. tor_free(m);
  73. }
  74. tor_free(tmp);
  75. return -1;
  76. }
  77. ol_length++;
  78. ol_tail->next = tmp;
  79. ol_tail = tmp;
  80. while ((int)(now - ol_list->when_added) >= ONIONQUEUE_WAIT_CUTOFF) {
  81. /* cull elderly requests. */
  82. circ = ol_list->circ;
  83. onion_pending_remove(ol_list->circ);
  84. log_info(LD_CIRC,
  85. "Circuit create request is too old; canceling due to overload.");
  86. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  87. }
  88. return 0;
  89. }
  90. /** Remove the first item from ol_list and return it, or return
  91. * NULL if the list is empty.
  92. */
  93. or_circuit_t *
  94. onion_next_task(char **onionskin_out)
  95. {
  96. or_circuit_t *circ;
  97. if (!ol_list)
  98. return NULL; /* no onions pending, we're done */
  99. tor_assert(ol_list->circ);
  100. tor_assert(ol_list->circ->p_chan); /* make sure it's still valid */
  101. tor_assert(ol_length > 0);
  102. circ = ol_list->circ;
  103. *onionskin_out = ol_list->onionskin;
  104. ol_list->onionskin = NULL; /* prevent free. */
  105. onion_pending_remove(ol_list->circ);
  106. return circ;
  107. }
  108. /** Go through ol_list, find the onion_queue_t element which points to
  109. * circ, remove and free that element. Leave circ itself alone.
  110. */
  111. void
  112. onion_pending_remove(or_circuit_t *circ)
  113. {
  114. onion_queue_t *tmpo, *victim;
  115. if (!ol_list)
  116. return; /* nothing here. */
  117. /* first check to see if it's the first entry */
  118. tmpo = ol_list;
  119. if (tmpo->circ == circ) {
  120. /* it's the first one. remove it from the list. */
  121. ol_list = tmpo->next;
  122. if (!ol_list)
  123. ol_tail = NULL;
  124. ol_length--;
  125. victim = tmpo;
  126. } else { /* we need to hunt through the rest of the list */
  127. for ( ;tmpo->next && tmpo->next->circ != circ; tmpo=tmpo->next) ;
  128. if (!tmpo->next) {
  129. log_debug(LD_GENERAL,
  130. "circ (p_circ_id %d) not in list, probably at cpuworker.",
  131. circ->p_circ_id);
  132. return;
  133. }
  134. /* now we know tmpo->next->circ == circ */
  135. victim = tmpo->next;
  136. tmpo->next = victim->next;
  137. if (ol_tail == victim)
  138. ol_tail = tmpo;
  139. ol_length--;
  140. }
  141. /* now victim points to the element that needs to be removed */
  142. tor_free(victim->onionskin);
  143. tor_free(victim);
  144. }
  145. /** Remove all circuits from the pending list. Called from tor_free_all. */
  146. void
  147. clear_pending_onions(void)
  148. {
  149. while (ol_list) {
  150. onion_queue_t *victim = ol_list;
  151. ol_list = victim->next;
  152. tor_free(victim->onionskin);
  153. tor_free(victim);
  154. }
  155. ol_list = ol_tail = NULL;
  156. ol_length = 0;
  157. }
  158. /* ============================================================ */
  159. /** Fill in a server_onion_keys_t object at <b>keys</b> with all of the keys
  160. * and other info we might need to do onion handshakes. (We make a copy of
  161. * our keys for each cpuworker to avoid race conditions with the main thread,
  162. * and to avoid locking) */
  163. void
  164. setup_server_onion_keys(server_onion_keys_t *keys)
  165. {
  166. memset(keys, 0, sizeof(server_onion_keys_t));
  167. memcpy(keys->my_identity, router_get_my_id_digest(), DIGEST_LEN);
  168. dup_onion_keys(&keys->onion_key, &keys->last_onion_key);
  169. #ifdef CURVE25519_ENABLED
  170. keys->curve25519_key_map = construct_ntor_key_map();
  171. #endif
  172. }
  173. /** Release all storage held in <b>keys</b>, but do not free <b>keys</b>
  174. * itself (as it's likely to be stack-allocated.) */
  175. void
  176. release_server_onion_keys(server_onion_keys_t *keys)
  177. {
  178. if (! keys)
  179. return;
  180. crypto_pk_free(keys->onion_key);
  181. crypto_pk_free(keys->last_onion_key);
  182. #ifdef CURVE25519_ENABLED
  183. ntor_key_map_free(keys->curve25519_key_map);
  184. #endif
  185. memset(keys, 0, sizeof(server_onion_keys_t));
  186. }
  187. /** Release whatever storage is held in <b>state</b>, depending on its
  188. * type, and clear its pointer. */
  189. void
  190. onion_handshake_state_release(onion_handshake_state_t *state)
  191. {
  192. switch (state->tag) {
  193. case ONION_HANDSHAKE_TYPE_TAP:
  194. crypto_dh_free(state->u.tap);
  195. state->u.tap = NULL;
  196. break;
  197. case ONION_HANDSHAKE_TYPE_FAST:
  198. fast_handshake_state_free(state->u.fast);
  199. state->u.fast = NULL;
  200. break;
  201. #ifdef CURVE25519_ENABLED
  202. case ONION_HANDSHAKE_TYPE_NTOR:
  203. ntor_handshake_state_free(state->u.ntor);
  204. state->u.ntor = NULL;
  205. break;
  206. #endif
  207. default:
  208. log_warn(LD_BUG, "called with unknown handshake state type %d",
  209. (int)state->tag);
  210. tor_fragile_assert();
  211. }
  212. }
  213. /** Perform the first step of a circuit-creation handshake of type <b>type</b>
  214. * (one of ONION_HANDSHAKE_TYPE_*): generate the initial "onion skin" in
  215. * <b>onion_skin_out</b>, and store any state information in <b>state_out</b>.
  216. * Return -1 on failure, and the length of the onionskin on acceptance.
  217. */
  218. int
  219. onion_skin_create(int type,
  220. const extend_info_t *node,
  221. onion_handshake_state_t *state_out,
  222. uint8_t *onion_skin_out)
  223. {
  224. int r = -1;
  225. switch (type) {
  226. case ONION_HANDSHAKE_TYPE_TAP:
  227. if (!node->onion_key)
  228. return -1;
  229. if (onion_skin_TAP_create(node->onion_key,
  230. &state_out->u.tap,
  231. (char*)onion_skin_out) < 0)
  232. return -1;
  233. r = TAP_ONIONSKIN_CHALLENGE_LEN;
  234. break;
  235. case ONION_HANDSHAKE_TYPE_FAST:
  236. if (fast_onionskin_create(&state_out->u.fast, onion_skin_out) < 0)
  237. return -1;
  238. r = CREATE_FAST_LEN;
  239. break;
  240. case ONION_HANDSHAKE_TYPE_NTOR:
  241. #ifdef CURVE25519_ENABLED
  242. if (tor_mem_is_zero((const char*)node->curve25519_onion_key.public_key,
  243. CURVE25519_PUBKEY_LEN))
  244. return -1;
  245. if (onion_skin_ntor_create((const uint8_t*)node->identity_digest,
  246. &node->curve25519_onion_key,
  247. &state_out->u.ntor,
  248. onion_skin_out) < 0)
  249. return -1;
  250. r = NTOR_ONIONSKIN_LEN;
  251. #else
  252. return -1;
  253. #endif
  254. break;
  255. default:
  256. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  257. tor_fragile_assert();
  258. r = -1;
  259. }
  260. if (r > 0)
  261. state_out->tag = (uint16_t) type;
  262. return r;
  263. }
  264. /** Perform the second (server-side) step of a circuit-creation handshake of
  265. * type <b>type</b>, responding to the client request in <b>onion_skin</b>
  266. * using the keys in <b>keys</b>. On success, write our response into
  267. * <b>reply_out</b>, generate <b>keys_out_len</b> bytes worth of key material
  268. * in <b>keys_out_len</b>, and return the length of the reply. On failure,
  269. * return -1. */
  270. int
  271. onion_skin_server_handshake(int type,
  272. const uint8_t *onion_skin,
  273. const server_onion_keys_t *keys,
  274. uint8_t *reply_out,
  275. uint8_t *keys_out, size_t keys_out_len)
  276. {
  277. int r = -1;
  278. switch (type) {
  279. case ONION_HANDSHAKE_TYPE_TAP:
  280. if (onion_skin_TAP_server_handshake((const char*)onion_skin,
  281. keys->onion_key, keys->last_onion_key,
  282. (char*)reply_out,
  283. (char*)keys_out, keys_out_len)<0)
  284. return -1;
  285. r = TAP_ONIONSKIN_REPLY_LEN;
  286. break;
  287. case ONION_HANDSHAKE_TYPE_FAST:
  288. if (fast_server_handshake(onion_skin, reply_out, keys_out, keys_out_len)<0)
  289. return -1;
  290. r = CREATED_FAST_LEN;
  291. break;
  292. case ONION_HANDSHAKE_TYPE_NTOR:
  293. #ifdef CURVE25519_ENABLED
  294. if (onion_skin_ntor_server_handshake(onion_skin, keys->curve25519_key_map,
  295. keys->my_identity,
  296. reply_out, keys_out, keys_out_len)<0)
  297. return -1;
  298. r = NTOR_REPLY_LEN;
  299. #else
  300. return -1;
  301. #endif
  302. break;
  303. default:
  304. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  305. tor_fragile_assert();
  306. return -1;
  307. }
  308. /* XXXX we should generate the rendezvous nonce stuff too. Some notes
  309. * below */
  310. // memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
  311. //memcpy(hop->handshake_digest, reply+DIGEST_LEN, DIGEST_LEN);
  312. return r;
  313. }
  314. /** Perform the final (client-side) step of a circuit-creation handshake of
  315. * type <b>type</b>, using our state in <b>handshake_state</b> and the
  316. * server's response in <b>reply</b> On success, generate <b>keys_out_len</b>
  317. * bytes worth of key material in <b>keys_out_len</b>, set
  318. * <b>rend_authenticator_out</b> to the "KH" field that can be used to
  319. * establish introduction points at this hop, and return 0. On failure,
  320. * return -1. */
  321. int
  322. onion_skin_client_handshake(int type,
  323. const onion_handshake_state_t *handshake_state,
  324. const uint8_t *reply,
  325. uint8_t *keys_out, size_t keys_out_len,
  326. uint8_t *rend_authenticator_out)
  327. {
  328. if (handshake_state->tag != type)
  329. return -1;
  330. switch (type) {
  331. case ONION_HANDSHAKE_TYPE_TAP:
  332. if (onion_skin_TAP_client_handshake(handshake_state->u.tap,
  333. (const char*)reply,
  334. (char *)keys_out, keys_out_len) < 0)
  335. return -1;
  336. memcpy(rend_authenticator_out, reply+DH_KEY_LEN, DIGEST_LEN);
  337. return 0;
  338. case ONION_HANDSHAKE_TYPE_FAST:
  339. if (fast_client_handshake(handshake_state->u.fast, reply,
  340. keys_out, keys_out_len) < 0)
  341. return -1;
  342. memcpy(rend_authenticator_out, reply+DIGEST_LEN, DIGEST_LEN);
  343. return 0;
  344. #ifdef CURVE25519_ENABLED
  345. case ONION_HANDSHAKE_TYPE_NTOR:
  346. {
  347. size_t keys_tmp_len = keys_out_len + DIGEST_LEN;
  348. uint8_t *keys_tmp = tor_malloc(keys_tmp_len);
  349. if (onion_skin_ntor_client_handshake(handshake_state->u.ntor,
  350. reply,
  351. keys_tmp, keys_tmp_len) < 0) {
  352. tor_free(keys_tmp);
  353. return -1;
  354. }
  355. memcpy(keys_out, keys_tmp, keys_out_len);
  356. memcpy(rend_authenticator_out, keys_tmp + keys_out_len, DIGEST_LEN);
  357. memwipe(keys_tmp, 0, keys_tmp_len);
  358. tor_free(keys_tmp);
  359. }
  360. return 0;
  361. #endif
  362. default:
  363. log_warn(LD_BUG, "called with unknown handshake state type %d", type);
  364. tor_fragile_assert();
  365. return -1;
  366. }
  367. }