rendclient.c 15 KB

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