onion.c 12 KB

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