onion.c 8.7 KB

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