rendclient.c 14 KB

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