onion.c 13 KB

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