rendclient.c 18 KB

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