onion.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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-2011, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion.c
  8. * \brief Functions to queue create cells, and handle onionskin
  9. * parsing and creation.
  10. **/
  11. #include "or.h"
  12. #include "circuitlist.h"
  13. #include "config.h"
  14. #include "onion.h"
  15. #include "rephist.h"
  16. /** Type for a linked list of circuits that are waiting for a free CPU worker
  17. * to process a waiting onion handshake. */
  18. typedef struct onion_queue_t {
  19. or_circuit_t *circ;
  20. char *onionskin;
  21. time_t when_added;
  22. struct onion_queue_t *next;
  23. } onion_queue_t;
  24. /** 5 seconds on the onion queue til we just send back a destroy */
  25. #define ONIONQUEUE_WAIT_CUTOFF 5
  26. /** First and last elements in the linked list of circuits waiting for CPU
  27. * workers, or NULL if the list is empty. */
  28. static onion_queue_t *ol_list=NULL;
  29. static onion_queue_t *ol_tail=NULL;
  30. /** Length of ol_list */
  31. static int ol_length=0;
  32. /** Add <b>circ</b> to the end of ol_list and return 0, except
  33. * if ol_list is too long, in which case do nothing and return -1.
  34. */
  35. int
  36. onion_pending_add(or_circuit_t *circ, char *onionskin)
  37. {
  38. onion_queue_t *tmp;
  39. time_t now = time(NULL);
  40. tmp = tor_malloc_zero(sizeof(onion_queue_t));
  41. tmp->circ = circ;
  42. tmp->onionskin = onionskin;
  43. tmp->when_added = now;
  44. if (!ol_tail) {
  45. tor_assert(!ol_list);
  46. tor_assert(!ol_length);
  47. ol_list = tmp;
  48. ol_tail = tmp;
  49. ol_length++;
  50. return 0;
  51. }
  52. tor_assert(ol_list);
  53. tor_assert(!ol_tail->next);
  54. if (ol_length >= get_options()->MaxOnionsPending) {
  55. #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
  56. static ratelim_t last_warned =
  57. RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
  58. char *m;
  59. if ((m = rate_limit_log(&last_warned, approx_time()))) {
  60. log_warn(LD_GENERAL,
  61. "Your computer is too slow to handle this many circuit "
  62. "creation requests! Please consider using the "
  63. "MaxAdvertisedBandwidth config option or choosing a more "
  64. "restricted exit policy.%s",m);
  65. tor_free(m);
  66. }
  67. tor_free(tmp);
  68. return -1;
  69. }
  70. ol_length++;
  71. ol_tail->next = tmp;
  72. ol_tail = tmp;
  73. while ((int)(now - ol_list->when_added) >= ONIONQUEUE_WAIT_CUTOFF) {
  74. /* cull elderly requests. */
  75. circ = ol_list->circ;
  76. onion_pending_remove(ol_list->circ);
  77. log_info(LD_CIRC,
  78. "Circuit create request is too old; canceling due to overload.");
  79. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  80. }
  81. return 0;
  82. }
  83. /** Remove the first item from ol_list and return it, or return
  84. * NULL if the list is empty.
  85. */
  86. or_circuit_t *
  87. onion_next_task(char **onionskin_out)
  88. {
  89. or_circuit_t *circ;
  90. if (!ol_list)
  91. return NULL; /* no onions pending, we're done */
  92. tor_assert(ol_list->circ);
  93. tor_assert(ol_list->circ->p_conn); /* make sure it's still valid */
  94. tor_assert(ol_length > 0);
  95. circ = ol_list->circ;
  96. *onionskin_out = ol_list->onionskin;
  97. ol_list->onionskin = NULL; /* prevent free. */
  98. onion_pending_remove(ol_list->circ);
  99. return circ;
  100. }
  101. /** Go through ol_list, find the onion_queue_t element which points to
  102. * circ, remove and free that element. Leave circ itself alone.
  103. */
  104. void
  105. onion_pending_remove(or_circuit_t *circ)
  106. {
  107. onion_queue_t *tmpo, *victim;
  108. if (!ol_list)
  109. return; /* nothing here. */
  110. /* first check to see if it's the first entry */
  111. tmpo = ol_list;
  112. if (tmpo->circ == circ) {
  113. /* it's the first one. remove it from the list. */
  114. ol_list = tmpo->next;
  115. if (!ol_list)
  116. ol_tail = NULL;
  117. ol_length--;
  118. victim = tmpo;
  119. } else { /* we need to hunt through the rest of the list */
  120. for ( ;tmpo->next && tmpo->next->circ != circ; tmpo=tmpo->next) ;
  121. if (!tmpo->next) {
  122. log_debug(LD_GENERAL,
  123. "circ (p_circ_id %d) not in list, probably at cpuworker.",
  124. circ->p_circ_id);
  125. return;
  126. }
  127. /* now we know tmpo->next->circ == circ */
  128. victim = tmpo->next;
  129. tmpo->next = victim->next;
  130. if (ol_tail == victim)
  131. ol_tail = tmpo;
  132. ol_length--;
  133. }
  134. /* now victim points to the element that needs to be removed */
  135. tor_free(victim->onionskin);
  136. tor_free(victim);
  137. }
  138. /*----------------------------------------------------------------------*/
  139. /** Given a router's 128 byte public key,
  140. * stores the following in onion_skin_out:
  141. * - [42 bytes] OAEP padding
  142. * - [16 bytes] Symmetric key for encrypting blob past RSA
  143. * - [70 bytes] g^x part 1 (inside the RSA)
  144. * - [58 bytes] g^x part 2 (symmetrically encrypted)
  145. *
  146. * Stores the DH private key into handshake_state_out for later completion
  147. * of the handshake.
  148. *
  149. * The meeting point/cookies and auth are zeroed out for now.
  150. */
  151. int
  152. onion_skin_create(crypto_pk_t *dest_router_key,
  153. crypto_dh_t **handshake_state_out,
  154. char *onion_skin_out) /* ONIONSKIN_CHALLENGE_LEN bytes */
  155. {
  156. char challenge[DH_KEY_LEN];
  157. crypto_dh_t *dh = NULL;
  158. int dhbytes, pkbytes;
  159. tor_assert(dest_router_key);
  160. tor_assert(handshake_state_out);
  161. tor_assert(onion_skin_out);
  162. *handshake_state_out = NULL;
  163. memset(onion_skin_out, 0, ONIONSKIN_CHALLENGE_LEN);
  164. if (!(dh = crypto_dh_new(DH_TYPE_CIRCUIT)))
  165. goto err;
  166. dhbytes = crypto_dh_get_bytes(dh);
  167. pkbytes = (int) crypto_pk_keysize(dest_router_key);
  168. tor_assert(dhbytes == 128);
  169. tor_assert(pkbytes == 128);
  170. if (crypto_dh_get_public(dh, challenge, dhbytes))
  171. goto err;
  172. note_crypto_pk_op(ENC_ONIONSKIN);
  173. /* set meeting point, meeting cookie, etc here. Leave zero for now. */
  174. if (crypto_pk_public_hybrid_encrypt(dest_router_key, onion_skin_out,
  175. ONIONSKIN_CHALLENGE_LEN,
  176. challenge, DH_KEY_LEN,
  177. PK_PKCS1_OAEP_PADDING, 1)<0)
  178. goto err;
  179. memset(challenge, 0, sizeof(challenge));
  180. *handshake_state_out = dh;
  181. return 0;
  182. err:
  183. memset(challenge, 0, sizeof(challenge));
  184. if (dh) crypto_dh_free(dh);
  185. return -1;
  186. }
  187. /** Given an encrypted DH public key as generated by onion_skin_create,
  188. * and the private key for this onion router, generate the reply (128-byte
  189. * DH plus the first 20 bytes of shared key material), and store the
  190. * next key_out_len bytes of key material in key_out.
  191. */
  192. int
  193. onion_skin_server_handshake(const char *onion_skin, /*ONIONSKIN_CHALLENGE_LEN*/
  194. crypto_pk_t *private_key,
  195. crypto_pk_t *prev_private_key,
  196. char *handshake_reply_out, /*ONIONSKIN_REPLY_LEN*/
  197. char *key_out,
  198. size_t key_out_len)
  199. {
  200. char challenge[ONIONSKIN_CHALLENGE_LEN];
  201. crypto_dh_t *dh = NULL;
  202. ssize_t len;
  203. char *key_material=NULL;
  204. size_t key_material_len=0;
  205. int i;
  206. crypto_pk_t *k;
  207. len = -1;
  208. for (i=0;i<2;++i) {
  209. k = i==0?private_key:prev_private_key;
  210. if (!k)
  211. break;
  212. note_crypto_pk_op(DEC_ONIONSKIN);
  213. len = crypto_pk_private_hybrid_decrypt(k, challenge,
  214. ONIONSKIN_CHALLENGE_LEN,
  215. onion_skin, ONIONSKIN_CHALLENGE_LEN,
  216. PK_PKCS1_OAEP_PADDING,0);
  217. if (len>0)
  218. break;
  219. }
  220. if (len<0) {
  221. log_info(LD_PROTOCOL,
  222. "Couldn't decrypt onionskin: client may be using old onion key");
  223. goto err;
  224. } else if (len != DH_KEY_LEN) {
  225. log_warn(LD_PROTOCOL, "Unexpected onionskin length after decryption: %ld",
  226. (long)len);
  227. goto err;
  228. }
  229. dh = crypto_dh_new(DH_TYPE_CIRCUIT);
  230. if (!dh) {
  231. log_warn(LD_BUG, "Couldn't allocate DH key");
  232. goto err;
  233. }
  234. if (crypto_dh_get_public(dh, handshake_reply_out, DH_KEY_LEN)) {
  235. log_info(LD_GENERAL, "crypto_dh_get_public failed.");
  236. goto err;
  237. }
  238. key_material_len = DIGEST_LEN+key_out_len;
  239. key_material = tor_malloc(key_material_len);
  240. len = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, dh, challenge,
  241. DH_KEY_LEN, key_material,
  242. key_material_len);
  243. if (len < 0) {
  244. log_info(LD_GENERAL, "crypto_dh_compute_secret failed.");
  245. goto err;
  246. }
  247. /* send back H(K|0) as proof that we learned K. */
  248. memcpy(handshake_reply_out+DH_KEY_LEN, key_material, DIGEST_LEN);
  249. /* use the rest of the key material for our shared keys, digests, etc */
  250. memcpy(key_out, key_material+DIGEST_LEN, key_out_len);
  251. memset(challenge, 0, sizeof(challenge));
  252. memset(key_material, 0, key_material_len);
  253. tor_free(key_material);
  254. crypto_dh_free(dh);
  255. return 0;
  256. err:
  257. memset(challenge, 0, sizeof(challenge));
  258. if (key_material) {
  259. memset(key_material, 0, key_material_len);
  260. tor_free(key_material);
  261. }
  262. if (dh) crypto_dh_free(dh);
  263. return -1;
  264. }
  265. /** Finish the client side of the DH handshake.
  266. * Given the 128 byte DH reply + 20 byte hash as generated by
  267. * onion_skin_server_handshake and the handshake state generated by
  268. * onion_skin_create, verify H(K) with the first 20 bytes of shared
  269. * key material, then generate key_out_len more bytes of shared key
  270. * material and store them in key_out.
  271. *
  272. * After the invocation, call crypto_dh_free on handshake_state.
  273. */
  274. int
  275. onion_skin_client_handshake(crypto_dh_t *handshake_state,
  276. const char *handshake_reply, /* ONIONSKIN_REPLY_LEN bytes */
  277. char *key_out,
  278. size_t key_out_len)
  279. {
  280. ssize_t len;
  281. char *key_material=NULL;
  282. size_t key_material_len;
  283. tor_assert(crypto_dh_get_bytes(handshake_state) == DH_KEY_LEN);
  284. key_material_len = DIGEST_LEN + key_out_len;
  285. key_material = tor_malloc(key_material_len);
  286. len = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, handshake_state,
  287. handshake_reply, DH_KEY_LEN, key_material,
  288. key_material_len);
  289. if (len < 0)
  290. goto err;
  291. if (tor_memneq(key_material, handshake_reply+DH_KEY_LEN, DIGEST_LEN)) {
  292. /* H(K) does *not* match. Something fishy. */
  293. log_warn(LD_PROTOCOL,"Digest DOES NOT MATCH on onion handshake. "
  294. "Bug or attack.");
  295. goto err;
  296. }
  297. /* use the rest of the key material for our shared keys, digests, etc */
  298. memcpy(key_out, key_material+DIGEST_LEN, key_out_len);
  299. memset(key_material, 0, key_material_len);
  300. tor_free(key_material);
  301. return 0;
  302. err:
  303. memset(key_material, 0, key_material_len);
  304. tor_free(key_material);
  305. return -1;
  306. }
  307. /** Implement the server side of the CREATE_FAST abbreviated handshake. The
  308. * client has provided DIGEST_LEN key bytes in <b>key_in</b> ("x"). We
  309. * generate a reply of DIGEST_LEN*2 bytes in <b>key_out</b>, consisting of a
  310. * new random "y", followed by H(x|y) to check for correctness. We set
  311. * <b>key_out_len</b> bytes of key material in <b>key_out</b>.
  312. * Return 0 on success, &lt;0 on failure.
  313. **/
  314. int
  315. fast_server_handshake(const uint8_t *key_in, /* DIGEST_LEN bytes */
  316. uint8_t *handshake_reply_out, /* DIGEST_LEN*2 bytes */
  317. uint8_t *key_out,
  318. size_t key_out_len)
  319. {
  320. char tmp[DIGEST_LEN+DIGEST_LEN];
  321. char *out = NULL;
  322. size_t out_len;
  323. int r = -1;
  324. if (crypto_rand((char*)handshake_reply_out, DIGEST_LEN)<0)
  325. return -1;
  326. memcpy(tmp, key_in, DIGEST_LEN);
  327. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  328. out_len = key_out_len+DIGEST_LEN;
  329. out = tor_malloc(out_len);
  330. if (crypto_expand_key_material(tmp, sizeof(tmp), out, out_len)) {
  331. goto done;
  332. }
  333. memcpy(handshake_reply_out+DIGEST_LEN, out, DIGEST_LEN);
  334. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  335. r = 0;
  336. done:
  337. memset(tmp, 0, sizeof(tmp));
  338. memset(out, 0, out_len);
  339. tor_free(out);
  340. return r;
  341. }
  342. /** Implement the second half of the client side of the CREATE_FAST handshake.
  343. * We sent the server <b>handshake_state</b> ("x") already, and the server
  344. * told us <b>handshake_reply_out</b> (y|H(x|y)). Make sure that the hash is
  345. * correct, and generate key material in <b>key_out</b>. Return 0 on success,
  346. * true on failure.
  347. *
  348. * NOTE: The "CREATE_FAST" handshake path is distinguishable from regular
  349. * "onionskin" handshakes, and is not secure if an adversary can see or modify
  350. * the messages. Therefore, it should only be used by clients, and only as
  351. * the first hop of a circuit (since the first hop is already authenticated
  352. * and protected by TLS).
  353. */
  354. int
  355. fast_client_handshake(const uint8_t *handshake_state,/*DIGEST_LEN bytes*/
  356. const uint8_t *handshake_reply_out,/*DIGEST_LEN*2 bytes*/
  357. uint8_t *key_out,
  358. size_t key_out_len)
  359. {
  360. char tmp[DIGEST_LEN+DIGEST_LEN];
  361. char *out;
  362. size_t out_len;
  363. int r = -1;
  364. memcpy(tmp, handshake_state, DIGEST_LEN);
  365. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  366. out_len = key_out_len+DIGEST_LEN;
  367. out = tor_malloc(out_len);
  368. if (crypto_expand_key_material(tmp, sizeof(tmp), out, out_len)) {
  369. goto done;
  370. }
  371. if (tor_memneq(out, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) {
  372. /* H(K) does *not* match. Something fishy. */
  373. log_warn(LD_PROTOCOL,"Digest DOES NOT MATCH on fast handshake. "
  374. "Bug or attack.");
  375. goto done;
  376. }
  377. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  378. r = 0;
  379. done:
  380. memset(tmp, 0, sizeof(tmp));
  381. memset(out, 0, out_len);
  382. tor_free(out);
  383. return r;
  384. }
  385. /** Remove all circuits from the pending list. Called from tor_free_all. */
  386. void
  387. clear_pending_onions(void)
  388. {
  389. while (ol_list) {
  390. onion_queue_t *victim = ol_list;
  391. ol_list = victim->next;
  392. tor_free(victim->onionskin);
  393. tor_free(victim);
  394. }
  395. ol_list = ol_tail = NULL;
  396. ol_length = 0;
  397. }