onion.c 13 KB

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