rendclient.c 15 KB

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