onion.c 13 KB

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