rendclient.c 16 KB

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