rendclient.c 14 KB

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