onion.c 9.4 KB

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