onion.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char onion_c_id[] = "$Id$";
  7. /**
  8. * \file onion.c
  9. * \brief Functions to queue create cells, and handle onionskin
  10. * parsing and creation.
  11. **/
  12. #include "or.h"
  13. struct onion_queue_t {
  14. circuit_t *circ;
  15. time_t when_added;
  16. struct onion_queue_t *next;
  17. };
  18. /** 5 seconds on the onion queue til we just send back a destroy */
  19. #define ONIONQUEUE_WAIT_CUTOFF 5
  20. /** Global (within this file) variables used by the next few functions */
  21. static struct onion_queue_t *ol_list=NULL;
  22. static struct onion_queue_t *ol_tail=NULL;
  23. /** Length of ol_list */
  24. static int ol_length=0;
  25. /** Add <b>circ</b> to the end of ol_list and return 0, except
  26. * if ol_list is too long, in which case do nothing and return -1.
  27. */
  28. int
  29. onion_pending_add(circuit_t *circ)
  30. {
  31. struct onion_queue_t *tmp;
  32. time_t now = time(NULL);
  33. tmp = tor_malloc_zero(sizeof(struct onion_queue_t));
  34. tmp->circ = circ;
  35. tmp->when_added = now;
  36. if (!ol_tail) {
  37. tor_assert(!ol_list);
  38. tor_assert(!ol_length);
  39. ol_list = tmp;
  40. ol_tail = tmp;
  41. ol_length++;
  42. return 0;
  43. }
  44. tor_assert(ol_list);
  45. tor_assert(!ol_tail->next);
  46. if (ol_length >= get_options()->MaxOnionsPending) {
  47. log_fn(LOG_NOTICE,"Already have %d onions queued. Closing.", ol_length);
  48. tor_free(tmp);
  49. return -1;
  50. }
  51. ol_length++;
  52. ol_tail->next = tmp;
  53. ol_tail = tmp;
  54. while ((int)(now - ol_list->when_added) >= ONIONQUEUE_WAIT_CUTOFF) {
  55. /* cull elderly requests. */
  56. circ = ol_list->circ;
  57. onion_pending_remove(ol_list->circ);
  58. log_fn(LOG_INFO,"Circuit create request is too old; cancelling due to overload.");
  59. circuit_mark_for_close(circ);
  60. }
  61. return 0;
  62. }
  63. /** Remove the first item from ol_list and return it, or return
  64. * NULL if the list is empty.
  65. */
  66. circuit_t *
  67. onion_next_task(void)
  68. {
  69. circuit_t *circ;
  70. if (!ol_list)
  71. return NULL; /* no onions pending, we're done */
  72. tor_assert(ol_list->circ);
  73. tor_assert(ol_list->circ->p_conn); /* make sure it's still valid */
  74. tor_assert(ol_length > 0);
  75. circ = ol_list->circ;
  76. onion_pending_remove(ol_list->circ);
  77. return circ;
  78. }
  79. /** Go through ol_list, find the onion_queue_t element which points to
  80. * circ, remove and free that element. Leave circ itself alone.
  81. */
  82. void
  83. onion_pending_remove(circuit_t *circ)
  84. {
  85. struct onion_queue_t *tmpo, *victim;
  86. if (!ol_list)
  87. return; /* nothing here. */
  88. /* first check to see if it's the first entry */
  89. tmpo = ol_list;
  90. if (tmpo->circ == circ) {
  91. /* it's the first one. remove it from the list. */
  92. ol_list = tmpo->next;
  93. if (!ol_list)
  94. ol_tail = NULL;
  95. ol_length--;
  96. victim = tmpo;
  97. } else { /* we need to hunt through the rest of the list */
  98. for ( ;tmpo->next && tmpo->next->circ != circ; tmpo=tmpo->next) ;
  99. if (!tmpo->next) {
  100. log_fn(LOG_DEBUG,"circ (p_circ_id %d) not in list, probably at cpuworker.",circ->p_circ_id);
  101. return;
  102. }
  103. /* now we know tmpo->next->circ == circ */
  104. victim = tmpo->next;
  105. tmpo->next = victim->next;
  106. if (ol_tail == victim)
  107. ol_tail = tmpo;
  108. ol_length--;
  109. }
  110. /* now victim points to the element that needs to be removed */
  111. tor_free(victim);
  112. }
  113. /*----------------------------------------------------------------------*/
  114. /** Given a router's 128 byte public key,
  115. * stores the following in onion_skin_out:
  116. * - [42 bytes] OAEP padding
  117. * - [16 bytes] Symmetric key for encrypting blob past RSA
  118. * - [70 bytes] g^x part 1 (inside the RSA)
  119. * - [58 bytes] g^x part 2 (symmetrically encrypted)
  120. *
  121. * Stores the DH private key into handshake_state_out for later completion
  122. * of the handshake.
  123. *
  124. * The meeting point/cookies and auth are zeroed out for now.
  125. */
  126. int
  127. onion_skin_create(crypto_pk_env_t *dest_router_key,
  128. crypto_dh_env_t **handshake_state_out,
  129. char *onion_skin_out) /* Must be ONIONSKIN_CHALLENGE_LEN bytes */
  130. {
  131. char *challenge = NULL;
  132. crypto_dh_env_t *dh = NULL;
  133. int dhbytes, pkbytes;
  134. *handshake_state_out = NULL;
  135. memset(onion_skin_out, 0, ONIONSKIN_CHALLENGE_LEN);
  136. if (!(dh = crypto_dh_new()))
  137. goto err;
  138. dhbytes = crypto_dh_get_bytes(dh);
  139. pkbytes = crypto_pk_keysize(dest_router_key);
  140. tor_assert(dhbytes == 128);
  141. tor_assert(pkbytes == 128);
  142. challenge = tor_malloc_zero(DH_KEY_LEN);
  143. if (crypto_dh_get_public(dh, challenge, dhbytes))
  144. goto err;
  145. #ifdef DEBUG_ONION_SKINS
  146. #define PA(a,n) \
  147. { int _i; for (_i = 0; _i<n; ++_i) printf("%02x ",((int)(a)[_i])&0xFF); }
  148. printf("Client: client g^x:");
  149. PA(challenge+16,3);
  150. printf("...");
  151. PA(challenge+141,3);
  152. puts("");
  153. printf("Client: client symkey:");
  154. PA(challenge+0,16);
  155. puts("");
  156. #endif
  157. /* set meeting point, meeting cookie, etc here. Leave zero for now. */
  158. if (crypto_pk_public_hybrid_encrypt(dest_router_key, onion_skin_out,
  159. challenge, DH_KEY_LEN,
  160. PK_PKCS1_OAEP_PADDING, 1)<0)
  161. goto err;
  162. tor_free(challenge);
  163. *handshake_state_out = dh;
  164. return 0;
  165. err:
  166. tor_free(challenge);
  167. if (dh) crypto_dh_free(dh);
  168. return -1;
  169. }
  170. /** Given an encrypted DH public key as generated by onion_skin_create,
  171. * and the private key for this onion router, generate the reply (128-byte
  172. * DH plus the first 20 bytes of shared key material), and store the
  173. * next key_out_len bytes of key material in key_out.
  174. */
  175. int
  176. onion_skin_server_handshake(const char *onion_skin, /* ONIONSKIN_CHALLENGE_LEN bytes */
  177. crypto_pk_env_t *private_key,
  178. crypto_pk_env_t *prev_private_key,
  179. char *handshake_reply_out, /* ONIONSKIN_REPLY_LEN bytes */
  180. char *key_out,
  181. size_t key_out_len)
  182. {
  183. char challenge[ONIONSKIN_CHALLENGE_LEN];
  184. crypto_dh_env_t *dh = NULL;
  185. int len;
  186. char *key_material=NULL;
  187. int i;
  188. crypto_pk_env_t *k;
  189. len = -1;
  190. for (i=0;i<2;++i) {
  191. k = i==0?private_key:prev_private_key;
  192. if (!k)
  193. break;
  194. len = crypto_pk_private_hybrid_decrypt(k, challenge,
  195. onion_skin, ONIONSKIN_CHALLENGE_LEN,
  196. PK_PKCS1_OAEP_PADDING,0);
  197. if (len>0)
  198. break;
  199. }
  200. if (len<0) {
  201. log_fn(LOG_INFO, "Couldn't decrypt onionskin: client may be using old onion key");
  202. goto err;
  203. } else if (len != DH_KEY_LEN) {
  204. log_fn(LOG_WARN, "Unexpected onionskin length after decryption: %d",
  205. len);
  206. goto err;
  207. }
  208. dh = crypto_dh_new();
  209. if (crypto_dh_get_public(dh, handshake_reply_out, DH_KEY_LEN))
  210. goto err;
  211. #ifdef DEBUG_ONION_SKINS
  212. printf("Server: server g^y:");
  213. PA(handshake_reply_out+0,3);
  214. printf("...");
  215. PA(handshake_reply_out+125,3);
  216. puts("");
  217. #endif
  218. key_material = tor_malloc(DIGEST_LEN+key_out_len);
  219. len = crypto_dh_compute_secret(dh, challenge, DH_KEY_LEN,
  220. key_material, DIGEST_LEN+key_out_len);
  221. if (len < 0)
  222. goto err;
  223. /* send back H(K|0) as proof that we learned K. */
  224. memcpy(handshake_reply_out+DH_KEY_LEN, key_material, DIGEST_LEN);
  225. /* use the rest of the key material for our shared keys, digests, etc */
  226. memcpy(key_out, key_material+DIGEST_LEN, key_out_len);
  227. #ifdef DEBUG_ONION_SKINS
  228. printf("Server: key material:");
  229. PA(buf, DH_KEY_LEN);
  230. puts("");
  231. printf("Server: keys out:");
  232. PA(key_out, key_out_len);
  233. puts("");
  234. #endif
  235. tor_free(key_material);
  236. crypto_dh_free(dh);
  237. return 0;
  238. err:
  239. tor_free(key_material);
  240. if (dh) crypto_dh_free(dh);
  241. return -1;
  242. }
  243. /** Finish the client side of the DH handshake.
  244. * Given the 128 byte DH reply + 20 byte hash as generated by
  245. * onion_skin_server_handshake and the handshake state generated by
  246. * onion_skin_create, verify H(K) with the first 20 bytes of shared
  247. * key material, then generate key_out_len more bytes of shared key
  248. * material and store them in key_out.
  249. *
  250. * After the invocation, call crypto_dh_free on handshake_state.
  251. */
  252. int
  253. onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  254. const char *handshake_reply, /* Must be ONIONSKIN_REPLY_LEN bytes */
  255. char *key_out,
  256. size_t key_out_len)
  257. {
  258. int len;
  259. char *key_material=NULL;
  260. tor_assert(crypto_dh_get_bytes(handshake_state) == DH_KEY_LEN);
  261. #ifdef DEBUG_ONION_SKINS
  262. printf("Client: server g^y:");
  263. PA(handshake_reply+0,3);
  264. printf("...");
  265. PA(handshake_reply+125,3);
  266. puts("");
  267. #endif
  268. key_material = tor_malloc(20+key_out_len);
  269. len = crypto_dh_compute_secret(handshake_state, handshake_reply, DH_KEY_LEN,
  270. key_material, 20+key_out_len);
  271. if (len < 0)
  272. return -1;
  273. if (memcmp(key_material, handshake_reply+DH_KEY_LEN, 20)) {
  274. /* H(K) does *not* match. Something fishy. */
  275. tor_free(key_material);
  276. log_fn(LOG_WARN,"Digest DOES NOT MATCH on onion handshake. Bug or attack.");
  277. return -1;
  278. }
  279. /* use the rest of the key material for our shared keys, digests, etc */
  280. memcpy(key_out, key_material+20, key_out_len);
  281. #ifdef DEBUG_ONION_SKINS
  282. printf("Client: keys out:");
  283. PA(key_out, key_out_len);
  284. puts("");
  285. #endif
  286. tor_free(key_material);
  287. return 0;
  288. }
  289. /** DOCDOC */
  290. int
  291. fast_server_handshake(const char *key_in, /* DIGEST_LEN bytes */
  292. char *handshake_reply_out, /* DIGEST_LEN*2 bytes */
  293. char *key_out,
  294. size_t key_out_len)
  295. {
  296. char tmp[DIGEST_LEN+DIGEST_LEN+1];
  297. char digest[DIGEST_LEN];
  298. int i;
  299. if (crypto_rand(handshake_reply_out, DIGEST_LEN)<0)
  300. return -1;
  301. memcpy(tmp, key_in, DIGEST_LEN);
  302. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  303. tmp[DIGEST_LEN+DIGEST_LEN] = 0;
  304. crypto_digest(handshake_reply_out+DIGEST_LEN, tmp, sizeof(tmp));
  305. for (i = 0; i*DIGEST_LEN < (int)key_out_len; ++i) {
  306. size_t len;
  307. tmp[DIGEST_LEN+DIGEST_LEN] = i+1;
  308. crypto_digest(digest, tmp, sizeof(tmp));
  309. len = key_out_len - i*DIGEST_LEN;
  310. if (len > DIGEST_LEN) len = DIGEST_LEN;
  311. memcpy(key_out+i*DIGEST_LEN, digest, len);
  312. }
  313. return 0;
  314. }
  315. /** DOCDOC */
  316. int
  317. fast_client_handshake(const char *handshake_state, /* DIGEST_LEN bytes */
  318. const char *handshake_reply_out, /* DIGEST_LEN*2 bytes */
  319. char *key_out,
  320. size_t key_out_len)
  321. {
  322. char tmp[DIGEST_LEN+DIGEST_LEN+1];
  323. char digest[DIGEST_LEN];
  324. int i;
  325. memcpy(tmp, handshake_state, DIGEST_LEN);
  326. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  327. tmp[DIGEST_LEN+DIGEST_LEN] = 0;
  328. crypto_digest(digest, tmp, sizeof(tmp));
  329. if (memcmp(digest, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) {
  330. /* H(K) does *not* match. Something fishy. */
  331. log_fn(LOG_WARN,"Digest DOES NOT MATCH on fast handshake. Bug or attack.");
  332. return -1;
  333. }
  334. for (i = 0; i*DIGEST_LEN < (int)key_out_len; ++i) {
  335. size_t len;
  336. tmp[DIGEST_LEN+DIGEST_LEN] = i+1;
  337. crypto_digest(digest, tmp, sizeof(tmp));
  338. len = key_out_len - i*DIGEST_LEN;
  339. if (len > DIGEST_LEN) len = DIGEST_LEN;
  340. memcpy(key_out+i*DIGEST_LEN, digest, len);
  341. }
  342. return 0;
  343. }
  344. /** Remove all circuits from the pending list. Called from tor_free_all. */
  345. void
  346. clear_pending_onions(void)
  347. {
  348. while (ol_list) {
  349. struct onion_queue_t *victim = ol_list;
  350. ol_list = victim->next;
  351. tor_free(victim);
  352. }
  353. ol_list = ol_tail = NULL;
  354. ol_length = 0;
  355. }