rendclient.c 15 KB

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