onion.c 13 KB

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