rendclient.c 15 KB

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