rendclient.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /* send the introduce cell */
  6. void
  7. rend_client_introcirc_is_open(circuit_t *circ)
  8. {
  9. assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  10. assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath);
  11. log_fn(LOG_INFO,"introcirc is open");
  12. connection_ap_attach_pending();
  13. }
  14. /* send the establish-rendezvous cell. if it fails, mark
  15. * the circ for close and return -1. else return 0.
  16. */
  17. int
  18. rend_client_send_establish_rendezvous(circuit_t *circ)
  19. {
  20. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  21. log_fn(LOG_INFO, "Sending an ESTABLISH_RENDEZVOUS cell");
  22. if (crypto_rand(REND_COOKIE_LEN, circ->rend_cookie)<0) {
  23. log_fn(LOG_WARN, "Couldn't get random cookie");
  24. circuit_mark_for_close(circ);
  25. return -1;
  26. }
  27. if (connection_edge_send_command(NULL,circ,
  28. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  29. circ->rend_cookie, REND_COOKIE_LEN,
  30. circ->cpath->prev)<0) {
  31. /* circ is already marked for close */
  32. log_fn(LOG_WARN, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  33. return -1;
  34. }
  35. return 0;
  36. }
  37. /* Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  38. * down introcirc if possible.
  39. */
  40. int
  41. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
  42. int payload_len, r;
  43. char payload[RELAY_PAYLOAD_SIZE];
  44. char tmp[(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+DH_KEY_LEN];
  45. rend_cache_entry_t *entry;
  46. crypt_path_t *cpath;
  47. assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  48. assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  49. assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
  50. if(rend_cache_lookup_entry(introcirc->rend_query, &entry) < 1) {
  51. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
  52. introcirc->rend_query);
  53. goto err;
  54. }
  55. /* first 20 bytes of payload are the hash of bob's pk */
  56. if (crypto_pk_get_digest(entry->parsed->pk, payload)<0) {
  57. log_fn(LOG_WARN, "Couldn't hash public key.");
  58. goto err;
  59. }
  60. /* Initialize the pending_final_cpath and start the DH handshake. */
  61. cpath = rendcirc->build_state->pending_final_cpath =
  62. tor_malloc_zero(sizeof(crypt_path_t));
  63. if (!(cpath->handshake_state = crypto_dh_new())) {
  64. log_fn(LOG_WARN, "Couldn't allocate DH");
  65. goto err;
  66. }
  67. if (crypto_dh_generate_public(cpath->handshake_state)<0) {
  68. log_fn(LOG_WARN, "Couldn't generate g^x");
  69. goto err;
  70. }
  71. /* write the remaining items into tmp */
  72. strncpy(tmp, rendcirc->build_state->chosen_exit, (MAX_NICKNAME_LEN+1)); /* nul pads */
  73. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
  74. if (crypto_dh_get_public(cpath->handshake_state,
  75. tmp+MAX_NICKNAME_LEN+1+REND_COOKIE_LEN,
  76. DH_KEY_LEN)<0) {
  77. log_fn(LOG_WARN, "Couldn't extract g^x");
  78. goto err;
  79. }
  80. r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, tmp,
  81. MAX_NICKNAME_LEN+1+REND_COOKIE_LEN+DH_KEY_LEN,
  82. payload+DIGEST_LEN,
  83. PK_PKCS1_OAEP_PADDING, 0);
  84. if (r<0) {
  85. log_fn(LOG_WARN,"hybrid pk encrypt failed.");
  86. goto err;
  87. }
  88. payload_len = DIGEST_LEN + r;
  89. if (connection_edge_send_command(NULL, introcirc,
  90. RELAY_COMMAND_INTRODUCE1,
  91. payload, payload_len,
  92. introcirc->cpath->prev)<0) {
  93. /* introcirc is already marked for close. leave rendcirc alone. */
  94. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  95. return -1;
  96. }
  97. /* we don't need it anymore, plus it's been used. send the destroy. */
  98. circuit_mark_for_close(introcirc);
  99. return 0;
  100. err:
  101. circuit_mark_for_close(introcirc);
  102. circuit_mark_for_close(rendcirc);
  103. return -1;
  104. }
  105. /* send the rendezvous cell */
  106. void
  107. rend_client_rendcirc_is_open(circuit_t *circ)
  108. {
  109. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  110. assert(CIRCUIT_IS_ORIGIN(circ));
  111. log_fn(LOG_INFO,"rendcirc is open");
  112. /* generate a rendezvous cookie, store it in circ */
  113. if (rend_client_send_establish_rendezvous(circ) < 0) {
  114. return;
  115. }
  116. connection_ap_attach_pending();
  117. }
  118. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  119. * the circuit to C_REND_READY.
  120. */
  121. int
  122. rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
  123. {
  124. /* we just got an ack for our establish-rendezvous. switch purposes. */
  125. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  126. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  127. circuit_mark_for_close(circ);
  128. return -1;
  129. }
  130. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  131. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  132. return 0;
  133. }
  134. /* bob sent us a rendezvous cell, join the circs. */
  135. int
  136. rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
  137. {
  138. connection_t *apconn;
  139. crypt_path_t *hop;
  140. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  141. if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
  142. !circ->build_state->pending_final_cpath) {
  143. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  144. circuit_mark_for_close(circ);
  145. return -1;
  146. }
  147. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  148. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",request_len);
  149. goto err;
  150. }
  151. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  152. assert(circ->build_state && circ->build_state->pending_final_cpath);
  153. hop = circ->build_state->pending_final_cpath;
  154. assert(hop->handshake_state);
  155. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  156. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  157. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  158. goto err;
  159. }
  160. /* ... and set up cpath. */
  161. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  162. goto err;
  163. /* Check whether the digest is right... */
  164. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  165. log_fn(LOG_WARN, "Incorrect digest of key material");
  166. goto err;
  167. }
  168. crypto_dh_free(hop->handshake_state);
  169. hop->handshake_state = NULL;
  170. /* All is well. Extend the circuit. */
  171. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  172. hop->state = CPATH_STATE_OPEN;
  173. /* set the windows to default. these are the windows
  174. * that alice thinks bob has.
  175. */
  176. hop->package_window = CIRCWINDOW_START;
  177. hop->deliver_window = CIRCWINDOW_START;
  178. onion_append_to_cpath(&circ->cpath, hop);
  179. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  180. for(apconn = circ->p_streams; apconn; apconn = apconn->next_stream) {
  181. apconn->cpath_layer = circ->cpath->prev;
  182. /* now the last hop is different. be sure to send all the way. */
  183. if(connection_ap_handshake_send_begin(apconn, circ) < 0)
  184. return -1;
  185. }
  186. return 0;
  187. err:
  188. circuit_mark_for_close(circ);
  189. return -1;
  190. }
  191. /* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  192. * are waiting on query. If success==1, move them to the next state.
  193. * If success==0, fail them.
  194. */
  195. void rend_client_desc_fetched(char *query, int success) {
  196. connection_t **carray;
  197. connection_t *conn;
  198. int n, i;
  199. rend_cache_entry_t *entry;
  200. get_connection_array(&carray, &n);
  201. for (i = 0; i < n; ++i) {
  202. conn = carray[i];
  203. if (conn->type != CONN_TYPE_AP ||
  204. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  205. continue;
  206. if (rend_cmp_service_ids(conn->rend_query, query))
  207. continue;
  208. /* great, this guy was waiting */
  209. if(success ||
  210. rend_cache_lookup_entry(conn->rend_query, &entry) == 1) {
  211. /* either this fetch worked, or it failed but there was a
  212. * valid entry from before which we should reuse */
  213. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  214. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  215. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  216. /* it will never work */
  217. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  218. connection_mark_for_close(conn,0);
  219. }
  220. } else { /* 404, or fetch didn't get that far */
  221. log_fn(LOG_WARN,"service id '%s' fetched failed, and not in cache. Closing conn.", query);
  222. connection_mark_for_close(conn,0);
  223. }
  224. }
  225. }
  226. int rend_cmp_service_ids(char *one, char *two) {
  227. return strcasecmp(one,two);
  228. }
  229. /* strdup a nickname for a random introduction
  230. * point of query. return NULL if error.
  231. */
  232. char *rend_client_get_random_intro(char *query) {
  233. int i;
  234. smartlist_t *sl;
  235. char *choice;
  236. char *nickname;
  237. rend_cache_entry_t *entry;
  238. if(rend_cache_lookup_entry(query, &entry) < 1) {
  239. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  240. return NULL;
  241. }
  242. sl = smartlist_create();
  243. /* add the intro point nicknames */
  244. for(i=0;i<entry->parsed->n_intro_points;i++)
  245. smartlist_add(sl,entry->parsed->intro_points[i]);
  246. choice = smartlist_choose(sl);
  247. if(!choice) {
  248. smartlist_free(sl);
  249. return NULL;
  250. }
  251. nickname = tor_strdup(choice);
  252. smartlist_free(sl);
  253. return nickname;
  254. }
  255. /* If address is of the form "y.onion" with a well-formed handle y,
  256. * then put a '\0' after y, lower-case it, and return 0.
  257. * Else return -1 and change nothing.
  258. */
  259. int rend_parse_rendezvous_address(char *address) {
  260. char *s;
  261. char query[REND_SERVICE_ID_LEN+1];
  262. s = strrchr(address,'.');
  263. if(!s) return -1; /* no dot */
  264. if (strcasecmp(s+1,"onion"))
  265. return -1; /* not .onion */
  266. *s = 0; /* null terminate it */
  267. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  268. goto failed;
  269. tor_strlower(query);
  270. if(rend_valid_service_id(query)) {
  271. tor_strlower(address);
  272. return 0; /* success */
  273. }
  274. failed:
  275. /* otherwise, return to previous state and return -1 */
  276. *s = '.';
  277. return -1;
  278. }
  279. /*
  280. Local Variables:
  281. mode:c
  282. indent-tabs-mode:nil
  283. c-basic-offset:2
  284. End:
  285. */