rendclient.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* Copyright 2004-2006 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendclient_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file rendclient.c
  8. * \brief Client code to access location-hidden services.
  9. **/
  10. #include "or.h"
  11. /** Called when we've established a circuit to an introduction point:
  12. * send the introduction request. */
  13. void
  14. rend_client_introcirc_has_opened(circuit_t *circ)
  15. {
  16. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  17. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  18. tor_assert(circ->cpath);
  19. log_info(LD_REND,"introcirc is open");
  20. connection_ap_attach_pending();
  21. }
  22. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  23. * it fails, mark the circ for close and return -1. else return 0.
  24. */
  25. static int
  26. rend_client_send_establish_rendezvous(circuit_t *circ)
  27. {
  28. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  29. log_info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
  30. if (crypto_rand(circ->rend_cookie, REND_COOKIE_LEN) < 0) {
  31. log_warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
  32. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  33. return -1;
  34. }
  35. if (connection_edge_send_command(NULL,circ,
  36. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  37. circ->rend_cookie, REND_COOKIE_LEN,
  38. circ->cpath->prev)<0) {
  39. /* circ is already marked for close */
  40. log_warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  46. * down introcirc if possible.
  47. */
  48. int
  49. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc)
  50. {
  51. size_t payload_len;
  52. int r;
  53. char payload[RELAY_PAYLOAD_SIZE];
  54. char tmp[RELAY_PAYLOAD_SIZE];
  55. rend_cache_entry_t *entry;
  56. crypt_path_t *cpath;
  57. off_t dh_offset;
  58. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  59. tor_assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  60. tor_assert(!rend_cmp_service_ids(introcirc->rend_query,
  61. rendcirc->rend_query));
  62. if (rend_cache_lookup_entry(introcirc->rend_query, -1, &entry) < 1) {
  63. log_warn(LD_REND,
  64. "query %s didn't have valid rend desc in cache. Failing.",
  65. escaped_safe_str(introcirc->rend_query));
  66. goto err;
  67. }
  68. /* first 20 bytes of payload are the hash of bob's pk */
  69. if (crypto_pk_get_digest(entry->parsed->pk, payload)<0) {
  70. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  71. goto err;
  72. }
  73. /* Initialize the pending_final_cpath and start the DH handshake. */
  74. cpath = rendcirc->build_state->pending_final_cpath;
  75. if (!cpath) {
  76. cpath = rendcirc->build_state->pending_final_cpath =
  77. tor_malloc_zero(sizeof(crypt_path_t));
  78. cpath->magic = CRYPT_PATH_MAGIC;
  79. if (!(cpath->dh_handshake_state = crypto_dh_new())) {
  80. log_warn(LD_BUG, "Internal error: couldn't allocate DH.");
  81. goto err;
  82. }
  83. if (crypto_dh_generate_public(cpath->dh_handshake_state)<0) {
  84. log_warn(LD_BUG, "Internal error: couldn't generate g^x.");
  85. goto err;
  86. }
  87. }
  88. /* write the remaining items into tmp */
  89. if (entry->parsed->protocols & (1<<2)) {
  90. /* version 2 format */
  91. extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
  92. int klen;
  93. tmp[0] = 2; /* version 2 of the cell format */
  94. /* nul pads */
  95. set_uint32(tmp+1, htonl(extend_info->addr));
  96. set_uint16(tmp+5, htons(extend_info->port));
  97. memcpy(tmp+7, extend_info->identity_digest, DIGEST_LEN);
  98. klen = crypto_pk_asn1_encode(extend_info->onion_key, tmp+7+DIGEST_LEN+2,
  99. sizeof(tmp)-(7+DIGEST_LEN+2));
  100. set_uint16(tmp+7+DIGEST_LEN, htons(klen));
  101. memcpy(tmp+7+DIGEST_LEN+2+klen, rendcirc->rend_cookie, REND_COOKIE_LEN);
  102. dh_offset = 7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
  103. } else {
  104. /* Version 0. */
  105. strncpy(tmp, rendcirc->build_state->chosen_exit->nickname,
  106. (MAX_NICKNAME_LEN+1)); /* nul pads */
  107. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
  108. dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
  109. }
  110. if (crypto_dh_get_public(cpath->dh_handshake_state, tmp+dh_offset,
  111. DH_KEY_LEN)<0) {
  112. log_warn(LD_BUG, "Internal error: couldn't extract g^x.");
  113. goto err;
  114. }
  115. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  116. * to avoid buffer overflows? */
  117. r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, payload+DIGEST_LEN,
  118. tmp,
  119. dh_offset+DH_KEY_LEN,
  120. PK_PKCS1_OAEP_PADDING, 0);
  121. if (r<0) {
  122. log_warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
  123. goto err;
  124. }
  125. tor_assert(DIGEST_LEN + r <= RELAY_PAYLOAD_SIZE); /* we overran something */
  126. payload_len = DIGEST_LEN + r;
  127. if (connection_edge_send_command(NULL, introcirc,
  128. RELAY_COMMAND_INTRODUCE1,
  129. payload, payload_len,
  130. introcirc->cpath->prev)<0) {
  131. /* introcirc is already marked for close. leave rendcirc alone. */
  132. log_warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
  133. return -1;
  134. }
  135. /* Now, we wait for an ACK or NAK on this circuit. */
  136. introcirc->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  137. return 0;
  138. err:
  139. circuit_mark_for_close(introcirc, END_CIRC_AT_ORIGIN);
  140. circuit_mark_for_close(rendcirc, END_CIRC_AT_ORIGIN);
  141. return -1;
  142. }
  143. /** Called when a rendezvous circuit is open; sends a establish
  144. * rendezvous circuit as appropriate. */
  145. void
  146. rend_client_rendcirc_has_opened(circuit_t *circ)
  147. {
  148. tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  149. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  150. log_info(LD_REND,"rendcirc is open");
  151. /* generate a rendezvous cookie, store it in circ */
  152. if (rend_client_send_establish_rendezvous(circ) < 0) {
  153. return;
  154. }
  155. }
  156. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  157. */
  158. int
  159. rend_client_introduction_acked(circuit_t *circ,
  160. const char *request, size_t request_len)
  161. {
  162. circuit_t *rendcirc;
  163. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  164. log_warn(LD_PROTOCOL,
  165. "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
  166. circ->n_circ_id);
  167. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  168. return -1;
  169. }
  170. tor_assert(circ->build_state->chosen_exit);
  171. tor_assert(circ->build_state->chosen_exit->nickname);
  172. if (request_len == 0) {
  173. /* It's an ACK; the introduction point relayed our introduction request. */
  174. /* Locate the rend circ which is waiting to hear about this ack,
  175. * and tell it.
  176. */
  177. log_info(LD_REND,"Received ack. Telling rend circ...");
  178. rendcirc = circuit_get_by_rend_query_and_purpose(
  179. circ->rend_query, CIRCUIT_PURPOSE_C_REND_READY);
  180. if (rendcirc) { /* remember the ack */
  181. rendcirc->purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  182. } else {
  183. log_info(LD_REND,"...Found no rend circ. Dropping on the floor.");
  184. }
  185. /* close the circuit: we won't need it anymore. */
  186. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
  187. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  188. } else {
  189. /* It's a NAK; the introduction point didn't relay our request. */
  190. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  191. /* Remove this intro point from the set of viable introduction
  192. * points. If any remain, extend to a new one and try again.
  193. * If none remain, refetch the service descriptor.
  194. */
  195. if (rend_client_remove_intro_point(circ->build_state->chosen_exit,
  196. circ->rend_query) > 0) {
  197. /* There are introduction points left. Re-extend the circuit to
  198. * another intro point and try again. */
  199. extend_info_t *extend_info;
  200. int result;
  201. extend_info = rend_client_get_random_intro(circ->rend_query);
  202. if (!extend_info) {
  203. log_warn(LD_REND, "No introduction points left for %s. Closing.",
  204. escaped_safe_str(circ->rend_query));
  205. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  206. return -1;
  207. }
  208. log_info(LD_REND,
  209. "Got nack for %s from %s. Re-extending circ %d, "
  210. "this time to %s.",
  211. escaped_safe_str(circ->rend_query),
  212. circ->build_state->chosen_exit->nickname, circ->n_circ_id,
  213. extend_info->nickname);
  214. result = circuit_extend_to_new_exit(circ, extend_info);
  215. extend_info_free(extend_info);
  216. return result;
  217. }
  218. }
  219. return 0;
  220. }
  221. /** If we are not currently fetching a rendezvous service descriptor
  222. * for the service ID <b>query</b>, start a directory connection to fetch a
  223. * new one.
  224. */
  225. void
  226. rend_client_refetch_renddesc(const char *query)
  227. {
  228. if (!get_options()->FetchHidServDescriptors)
  229. return;
  230. if (connection_get_by_type_state_rendquery(CONN_TYPE_DIR, 0, query)) {
  231. log_info(LD_REND,"Would fetch a new renddesc here (for %s), but one is "
  232. "already in progress.", escaped_safe_str(query));
  233. } else {
  234. /* not one already; initiate a dir rend desc lookup */
  235. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC, query, 1);
  236. }
  237. }
  238. /** Remove failed_intro from ent. If ent now has no intro points, or
  239. * service is unrecognized, then launch a new renddesc fetch.
  240. *
  241. * Return -1 if error, 0 if no intro points remain or service
  242. * unrecognized, 1 if recognized and some intro points remain.
  243. */
  244. int
  245. rend_client_remove_intro_point(extend_info_t *failed_intro, const char *query)
  246. {
  247. int i, r;
  248. rend_cache_entry_t *ent;
  249. connection_t *conn;
  250. r = rend_cache_lookup_entry(query, -1, &ent);
  251. if (r<0) {
  252. log_warn(LD_BUG, "Bug: malformed service ID %s.", escaped_safe_str(query));
  253. return -1;
  254. }
  255. if (r==0) {
  256. log_info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
  257. escaped_safe_str(query));
  258. rend_client_refetch_renddesc(query);
  259. return 0;
  260. }
  261. if (ent->parsed->intro_point_extend_info) {
  262. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  263. if (!memcmp(failed_intro->identity_digest,
  264. ent->parsed->intro_point_extend_info[i]->identity_digest,
  265. DIGEST_LEN)) {
  266. tor_assert(!strcmp(ent->parsed->intro_points[i],
  267. ent->parsed->intro_point_extend_info[i]->nickname));
  268. tor_free(ent->parsed->intro_points[i]);
  269. extend_info_free(ent->parsed->intro_point_extend_info[i]);
  270. --ent->parsed->n_intro_points;
  271. ent->parsed->intro_points[i] =
  272. ent->parsed->intro_points[ent->parsed->n_intro_points];
  273. ent->parsed->intro_point_extend_info[i] =
  274. ent->parsed->intro_point_extend_info[ent->parsed->n_intro_points];
  275. break;
  276. }
  277. }
  278. } else {
  279. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  280. if (!strcasecmp(ent->parsed->intro_points[i], failed_intro->nickname)) {
  281. tor_free(ent->parsed->intro_points[i]);
  282. ent->parsed->intro_points[i] =
  283. ent->parsed->intro_points[--ent->parsed->n_intro_points];
  284. break;
  285. }
  286. }
  287. }
  288. if (!ent->parsed->n_intro_points) {
  289. log_info(LD_REND,
  290. "No more intro points remain for %s. Re-fetching descriptor.",
  291. escaped_safe_str(query));
  292. rend_client_refetch_renddesc(query);
  293. /* move all pending streams back to renddesc_wait */
  294. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  295. AP_CONN_STATE_CIRCUIT_WAIT, query))) {
  296. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  297. }
  298. return 0;
  299. }
  300. log_info(LD_REND,"%d options left for %s.",
  301. ent->parsed->n_intro_points, escaped_safe_str(query));
  302. return 1;
  303. }
  304. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  305. * the circuit to C_REND_READY.
  306. */
  307. int
  308. rend_client_rendezvous_acked(circuit_t *circ, const char *request,
  309. size_t request_len)
  310. {
  311. /* we just got an ack for our establish-rendezvous. switch purposes. */
  312. if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  313. log_warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. "
  314. "Closing circ.");
  315. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  316. return -1;
  317. }
  318. log_info(LD_REND,"Got rendezvous ack. This circuit is now ready for "
  319. "rendezvous.");
  320. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  321. return 0;
  322. }
  323. /** Bob sent us a rendezvous cell; join the circuits. */
  324. int
  325. rend_client_receive_rendezvous(circuit_t *circ, const char *request,
  326. size_t request_len)
  327. {
  328. crypt_path_t *hop;
  329. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  330. if ((circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  331. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  332. || !circ->build_state->pending_final_cpath) {
  333. log_warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not "
  334. "expecting it. Closing.");
  335. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  336. return -1;
  337. }
  338. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  339. log_warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",
  340. (int)request_len);
  341. goto err;
  342. }
  343. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  344. tor_assert(circ->build_state);
  345. tor_assert(circ->build_state->pending_final_cpath);
  346. hop = circ->build_state->pending_final_cpath;
  347. tor_assert(hop->dh_handshake_state);
  348. if (crypto_dh_compute_secret(hop->dh_handshake_state, request, DH_KEY_LEN,
  349. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  350. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  351. goto err;
  352. }
  353. /* ... and set up cpath. */
  354. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  355. goto err;
  356. /* Check whether the digest is right... */
  357. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  358. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  359. goto err;
  360. }
  361. crypto_dh_free(hop->dh_handshake_state);
  362. hop->dh_handshake_state = NULL;
  363. /* All is well. Extend the circuit. */
  364. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  365. hop->state = CPATH_STATE_OPEN;
  366. /* set the windows to default. these are the windows
  367. * that alice thinks bob has.
  368. */
  369. hop->package_window = CIRCWINDOW_START;
  370. hop->deliver_window = CIRCWINDOW_START;
  371. onion_append_to_cpath(&circ->cpath, hop);
  372. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  373. return 0;
  374. err:
  375. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  376. return -1;
  377. }
  378. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  379. * are waiting on query. If there's a working cache entry here
  380. * with at least one intro point, move them to the next state;
  381. * else fail them.
  382. */
  383. void
  384. rend_client_desc_here(const char *query)
  385. {
  386. connection_t *conn;
  387. rend_cache_entry_t *entry;
  388. time_t now = time(NULL);
  389. int i, n_conns;
  390. connection_t **carray;
  391. get_connection_array(&carray, &n_conns);
  392. for (i = 0; i < n_conns; ++i) {
  393. conn = carray[i];
  394. if (conn->type != CONN_TYPE_AP ||
  395. conn->state != AP_CONN_STATE_RENDDESC_WAIT ||
  396. conn->marked_for_close ||
  397. rend_cmp_service_ids(query, conn->rend_query))
  398. continue;
  399. assert_connection_ok(conn, now);
  400. if (rend_cache_lookup_entry(conn->rend_query, -1, &entry) == 1 &&
  401. entry->parsed->n_intro_points > 0) {
  402. /* either this fetch worked, or it failed but there was a
  403. * valid entry from before which we should reuse */
  404. log_info(LD_REND,"Rend desc is usable. Launching circuits.");
  405. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  406. /* restart their timeout values, so they get a fair shake at
  407. * connecting to the hidden service. */
  408. conn->timestamp_created = now;
  409. conn->timestamp_lastread = now;
  410. conn->timestamp_lastwritten = now;
  411. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  412. /* it will never work */
  413. log_warn(LD_REND,"Rendezvous attempt failed. Closing.");
  414. connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
  415. }
  416. } else { /* 404, or fetch didn't get that far */
  417. log_notice(LD_REND,"Closing stream for '%s.onion': hidden service is "
  418. "unavailable (try again later).", safe_str(query));
  419. connection_mark_unattached_ap(conn, END_STREAM_REASON_TIMEOUT);
  420. }
  421. }
  422. }
  423. /** Return a newly allocated extend_info_t* for a randomly chosen introduction
  424. * point for the named hidden service. Return NULL if all introduction points
  425. * have been tried and failed.
  426. */
  427. extend_info_t *
  428. rend_client_get_random_intro(const char *query)
  429. {
  430. int i;
  431. rend_cache_entry_t *entry;
  432. if (rend_cache_lookup_entry(query, -1, &entry) < 1) {
  433. log_warn(LD_REND,
  434. "Query '%s' didn't have valid rend desc in cache. Failing.",
  435. safe_str(query));
  436. return NULL;
  437. }
  438. again:
  439. if (!entry->parsed->n_intro_points)
  440. return NULL;
  441. i = crypto_rand_int(entry->parsed->n_intro_points);
  442. if (entry->parsed->intro_point_extend_info) {
  443. return extend_info_dup(entry->parsed->intro_point_extend_info[i]);
  444. } else {
  445. /* add the intro point nicknames */
  446. char *choice = entry->parsed->intro_points[i];
  447. routerinfo_t *router = router_get_by_nickname(choice, 0);
  448. if (!router) {
  449. log_info(LD_REND, "Unknown router with nickname '%s'; trying another.",
  450. choice);
  451. tor_free(choice);
  452. entry->parsed->intro_points[i] =
  453. entry->parsed->intro_points[--entry->parsed->n_intro_points];
  454. goto again;
  455. }
  456. return extend_info_from_router(router);
  457. }
  458. }