rendclient.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendclient_c_id[] = "$Id$";
  5. /**
  6. * \file rendclient.c
  7. * \brief Client code to access location-hidden services.
  8. **/
  9. #include "or.h"
  10. /** Called when we've established a circuit to an introduction point:
  11. * send the introduction request. */
  12. void
  13. rend_client_introcirc_has_opened(circuit_t *circ)
  14. {
  15. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  16. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  17. tor_assert(circ->cpath);
  18. log_fn(LOG_INFO,"introcirc is open");
  19. connection_ap_attach_pending();
  20. }
  21. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  22. * it fails, mark the circ for close and return -1. else return 0.
  23. */
  24. static int
  25. rend_client_send_establish_rendezvous(circuit_t *circ)
  26. {
  27. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  28. log_fn(LOG_INFO, "Sending an ESTABLISH_RENDEZVOUS cell");
  29. if (crypto_rand(circ->rend_cookie, REND_COOKIE_LEN) < 0) {
  30. log_fn(LOG_WARN, "Couldn't get random cookie");
  31. circuit_mark_for_close(circ);
  32. return -1;
  33. }
  34. if (connection_edge_send_command(NULL,circ,
  35. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  36. circ->rend_cookie, REND_COOKIE_LEN,
  37. circ->cpath->prev)<0) {
  38. /* circ is already marked for close */
  39. log_fn(LOG_WARN, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  45. * down introcirc if possible.
  46. */
  47. int
  48. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
  49. size_t payload_len;
  50. int r;
  51. char payload[RELAY_PAYLOAD_SIZE];
  52. char tmp[1+(MAX_HEX_NICKNAME_LEN+1)+REND_COOKIE_LEN+DH_KEY_LEN];
  53. rend_cache_entry_t *entry;
  54. crypt_path_t *cpath;
  55. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  56. tor_assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  57. tor_assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
  58. if (rend_cache_lookup_entry(introcirc->rend_query, &entry) < 1) {
  59. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
  60. introcirc->rend_query);
  61. goto err;
  62. }
  63. /* first 20 bytes of payload are the hash of bob's pk */
  64. if (crypto_pk_get_digest(entry->parsed->pk, payload)<0) {
  65. log_fn(LOG_WARN, "Couldn't hash public key.");
  66. goto err;
  67. }
  68. /* Initialize the pending_final_cpath and start the DH handshake. */
  69. cpath = rendcirc->build_state->pending_final_cpath;
  70. if (!cpath) {
  71. cpath = rendcirc->build_state->pending_final_cpath =
  72. tor_malloc_zero(sizeof(crypt_path_t));
  73. if (!(cpath->handshake_state = crypto_dh_new())) {
  74. log_fn(LOG_WARN, "Couldn't allocate DH");
  75. goto err;
  76. }
  77. if (crypto_dh_generate_public(cpath->handshake_state)<0) {
  78. log_fn(LOG_WARN, "Couldn't generate g^x");
  79. goto err;
  80. }
  81. }
  82. /* write the remaining items into tmp */
  83. tmp[0] = 1; /* version 1 of the cell format */
  84. strncpy(tmp+1, rendcirc->build_state->chosen_exit_name, (MAX_HEX_NICKNAME_LEN+1)); /* nul pads */
  85. memcpy(tmp+1+MAX_HEX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
  86. if (crypto_dh_get_public(cpath->handshake_state,
  87. tmp+1+MAX_HEX_NICKNAME_LEN+1+REND_COOKIE_LEN,
  88. DH_KEY_LEN)<0) {
  89. log_fn(LOG_WARN, "Couldn't extract g^x");
  90. goto err;
  91. }
  92. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  93. * to avoid buffer overflows? */
  94. r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, payload+DIGEST_LEN, tmp,
  95. 1+MAX_HEX_NICKNAME_LEN+1+REND_COOKIE_LEN+DH_KEY_LEN,
  96. PK_PKCS1_OAEP_PADDING, 0);
  97. if (r<0) {
  98. log_fn(LOG_WARN,"hybrid pk encrypt failed.");
  99. goto err;
  100. }
  101. tor_assert(DIGEST_LEN + r <= RELAY_PAYLOAD_SIZE); /* we overran something */
  102. payload_len = DIGEST_LEN + r;
  103. if (connection_edge_send_command(NULL, introcirc,
  104. RELAY_COMMAND_INTRODUCE1,
  105. payload, payload_len,
  106. introcirc->cpath->prev)<0) {
  107. /* introcirc is already marked for close. leave rendcirc alone. */
  108. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  109. return -1;
  110. }
  111. /* Now, we wait for an ACK or NAK on this circuit. */
  112. introcirc->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  113. return 0;
  114. err:
  115. circuit_mark_for_close(introcirc);
  116. circuit_mark_for_close(rendcirc);
  117. return -1;
  118. }
  119. /** Called when a rendezvous circuit is open; sends a establish
  120. * rendezvous circuit as appropriate. */
  121. void
  122. rend_client_rendcirc_has_opened(circuit_t *circ)
  123. {
  124. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  125. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  126. log_fn(LOG_INFO,"rendcirc is open");
  127. /* generate a rendezvous cookie, store it in circ */
  128. if (rend_client_send_establish_rendezvous(circ) < 0) {
  129. return;
  130. }
  131. connection_ap_attach_pending();
  132. }
  133. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  134. */
  135. int
  136. rend_client_introduction_acked(circuit_t *circ,
  137. const char *request, size_t request_len)
  138. {
  139. char *nickname;
  140. circuit_t *rendcirc;
  141. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  142. log_fn(LOG_WARN, "Received REND_INTRODUCE_ACK on unexpected circuit %d",
  143. circ->n_circ_id);
  144. circuit_mark_for_close(circ);
  145. return -1;
  146. }
  147. tor_assert(circ->build_state->chosen_exit_name);
  148. if (request_len == 0) {
  149. /* It's an ACK; the introduction point relayed our introduction request. */
  150. /* Locate the rend circ which is waiting to hear about this ack,
  151. * and tell it.
  152. */
  153. log_fn(LOG_INFO,"Received ack. Telling rend circ.");
  154. rendcirc = circuit_get_by_rend_query_and_purpose(
  155. circ->rend_query, CIRCUIT_PURPOSE_C_REND_READY);
  156. if (rendcirc) { /* remember the ack */
  157. rendcirc->purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  158. }
  159. /* close the circuit: we won't need it anymore. */
  160. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
  161. circuit_mark_for_close(circ);
  162. } else {
  163. /* It's a NAK; the introduction point didn't relay our request. */
  164. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  165. /* Remove this intro point from the set of viable introduction
  166. * points. If any remain, extend to a new one and try again.
  167. * If none remain, refetch the service descriptor.
  168. */
  169. if (rend_client_remove_intro_point(circ->build_state->chosen_exit_name,
  170. circ->rend_query) > 0) {
  171. /* There are introduction points left. re-extend the circuit to
  172. * another intro point and try again. */
  173. routerinfo_t *r;
  174. nickname = rend_client_get_random_intro(circ->rend_query);
  175. tor_assert(nickname);
  176. log_fn(LOG_INFO,"Got nack for %s from %s, extending to %s.", circ->rend_query, circ->build_state->chosen_exit_name, nickname);
  177. if (!(r = router_get_by_nickname(nickname))) {
  178. log_fn(LOG_WARN, "Advertised intro point '%s' for %s is not known. Closing.",
  179. nickname, circ->rend_query);
  180. tor_free(nickname);
  181. circuit_mark_for_close(circ);
  182. return -1;
  183. }
  184. log_fn(LOG_INFO, "Chose new intro point %s for %s (circ %d)",
  185. nickname, circ->rend_query, circ->n_circ_id);
  186. circ->state = CIRCUIT_STATE_BUILDING;
  187. tor_free(circ->build_state->chosen_exit_name);
  188. /* no need to strdup, since rend_client_get_random_intro() made
  189. * it just for us: */
  190. circ->build_state->chosen_exit_name = nickname;
  191. memcpy(circ->build_state->chosen_exit_digest, r->identity_digest, DIGEST_LEN);
  192. ++circ->build_state->desired_path_len;
  193. if (circuit_send_next_onion_skin(circ)<0) {
  194. log_fn(LOG_WARN, "Couldn't extend circuit to new intro point.");
  195. circuit_mark_for_close(circ);
  196. return -1;
  197. }
  198. }
  199. }
  200. return 0;
  201. }
  202. /** If we are not currently fetching a rendezvous service descriptor
  203. * for the service ID <b>query</b>, start a directory connection to fetch a
  204. * new one.
  205. */
  206. void
  207. rend_client_refetch_renddesc(const char *query)
  208. {
  209. if (connection_get_by_type_rendquery(CONN_TYPE_DIR, query)) {
  210. log_fn(LOG_INFO,"Would fetch a new renddesc here (for %s), but one is already in progress.", query);
  211. } else {
  212. /* not one already; initiate a dir rend desc lookup */
  213. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC, query, 1);
  214. }
  215. }
  216. /** remove failed_intro from ent. if ent now has no intro points, or
  217. * service is unrecognized, then launch a new renddesc fetch.
  218. *
  219. * Return -1 if error, 0 if no intro points remain or service
  220. * unrecognized, 1 if recognized and some intro points remain.
  221. */
  222. int
  223. rend_client_remove_intro_point(char *failed_intro, const char *query)
  224. {
  225. int i, r;
  226. rend_cache_entry_t *ent;
  227. r = rend_cache_lookup_entry(query, &ent);
  228. if (r<0) {
  229. log_fn(LOG_WARN, "Malformed service ID '%s'", query);
  230. return -1;
  231. }
  232. if (r==0) {
  233. log_fn(LOG_INFO, "Unknown service %s. Re-fetching descriptor.", query);
  234. rend_client_refetch_renddesc(query);
  235. return 0;
  236. }
  237. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  238. if (!strcasecmp(ent->parsed->intro_points[i], failed_intro)) {
  239. tor_free(ent->parsed->intro_points[i]);
  240. ent->parsed->intro_points[i] =
  241. ent->parsed->intro_points[--ent->parsed->n_intro_points];
  242. break;
  243. }
  244. }
  245. if (!ent->parsed->n_intro_points) {
  246. log_fn(LOG_INFO,"No more intro points remain for %s. Re-fetching descriptor.", query);
  247. rend_client_refetch_renddesc(query);
  248. return 0;
  249. }
  250. log_fn(LOG_INFO,"%d options left for %s.", ent->parsed->n_intro_points, query);
  251. return 1;
  252. }
  253. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  254. * the circuit to C_REND_READY.
  255. */
  256. int
  257. rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len)
  258. {
  259. /* we just got an ack for our establish-rendezvous. switch purposes. */
  260. if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  261. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  262. circuit_mark_for_close(circ);
  263. return -1;
  264. }
  265. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  266. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  267. return 0;
  268. }
  269. /** Bob sent us a rendezvous cell; join the circuits. */
  270. int
  271. rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len)
  272. {
  273. crypt_path_t *hop;
  274. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  275. if ((circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  276. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  277. || !circ->build_state->pending_final_cpath) {
  278. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  279. circuit_mark_for_close(circ);
  280. return -1;
  281. }
  282. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  283. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",(int)request_len);
  284. goto err;
  285. }
  286. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  287. tor_assert(circ->build_state);
  288. tor_assert(circ->build_state->pending_final_cpath);
  289. hop = circ->build_state->pending_final_cpath;
  290. tor_assert(hop->handshake_state);
  291. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  292. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  293. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  294. goto err;
  295. }
  296. /* ... and set up cpath. */
  297. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  298. goto err;
  299. /* Check whether the digest is right... */
  300. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  301. log_fn(LOG_WARN, "Incorrect digest of key material");
  302. goto err;
  303. }
  304. crypto_dh_free(hop->handshake_state);
  305. hop->handshake_state = NULL;
  306. /* All is well. Extend the circuit. */
  307. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  308. hop->state = CPATH_STATE_OPEN;
  309. /* set the windows to default. these are the windows
  310. * that alice thinks bob has.
  311. */
  312. hop->package_window = CIRCWINDOW_START;
  313. hop->deliver_window = CIRCWINDOW_START;
  314. onion_append_to_cpath(&circ->cpath, hop);
  315. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  316. return 0;
  317. err:
  318. circuit_mark_for_close(circ);
  319. return -1;
  320. }
  321. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  322. * are waiting on query. If status==1, move them to the next state.
  323. * If status==0, fail them.
  324. */
  325. void rend_client_desc_fetched(char *query, int status) {
  326. connection_t **carray;
  327. connection_t *conn;
  328. int n, i;
  329. rend_cache_entry_t *entry;
  330. get_connection_array(&carray, &n);
  331. for (i = 0; i < n; ++i) {
  332. conn = carray[i];
  333. if (conn->type != CONN_TYPE_AP ||
  334. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  335. continue;
  336. if (rend_cmp_service_ids(conn->rend_query, query))
  337. continue;
  338. /* great, this guy was waiting */
  339. if (status!=0 ||
  340. rend_cache_lookup_entry(conn->rend_query, &entry) == 1) {
  341. /* either this fetch worked, or it failed but there was a
  342. * valid entry from before which we should reuse */
  343. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  344. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  345. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  346. /* it will never work */
  347. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  348. conn->has_sent_end = 1;
  349. connection_mark_for_close(conn);
  350. }
  351. } else { /* 404, or fetch didn't get that far */
  352. log_fn(LOG_WARN,"Failed to fetch service id '%s', and not in cache. Closing conn.", query);
  353. conn->has_sent_end = 1;
  354. connection_mark_for_close(conn);
  355. }
  356. }
  357. }
  358. /** strdup a nickname for a random introduction
  359. * point of query. return NULL if error.
  360. */
  361. char *rend_client_get_random_intro(char *query) {
  362. int i;
  363. smartlist_t *sl;
  364. char *choice;
  365. char *nickname;
  366. rend_cache_entry_t *entry;
  367. if (rend_cache_lookup_entry(query, &entry) < 1) {
  368. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  369. return NULL;
  370. }
  371. sl = smartlist_create();
  372. /* add the intro point nicknames */
  373. for (i=0;i<entry->parsed->n_intro_points;i++)
  374. smartlist_add(sl,entry->parsed->intro_points[i]);
  375. choice = smartlist_choose(sl);
  376. if (!choice) {
  377. smartlist_free(sl);
  378. return NULL;
  379. }
  380. nickname = tor_strdup(choice);
  381. smartlist_free(sl);
  382. return nickname;
  383. }