onion.c 14 KB

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