rendclient.c 17 KB

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