rendclient.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  10. 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. 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. assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  48. assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  49. 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. payload_len = DIGEST_LEN + r;
  92. if (connection_edge_send_command(NULL, introcirc,
  93. RELAY_COMMAND_INTRODUCE1,
  94. payload, payload_len,
  95. introcirc->cpath->prev)<0) {
  96. /* introcirc is already marked for close. leave rendcirc alone. */
  97. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  98. return -1;
  99. }
  100. /* Now, we wait for an ACK or NAK on this circuit. */
  101. introcirc->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  102. return 0;
  103. err:
  104. circuit_mark_for_close(introcirc);
  105. circuit_mark_for_close(rendcirc);
  106. return -1;
  107. }
  108. /* send the rendezvous cell */
  109. void
  110. rend_client_rendcirc_is_open(circuit_t *circ)
  111. {
  112. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  113. assert(CIRCUIT_IS_ORIGIN(circ));
  114. log_fn(LOG_INFO,"rendcirc is open");
  115. /* generate a rendezvous cookie, store it in circ */
  116. if (rend_client_send_establish_rendezvous(circ) < 0) {
  117. return;
  118. }
  119. connection_ap_attach_pending();
  120. }
  121. /* Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  122. */
  123. int
  124. rend_client_introduction_acked(circuit_t *circ,
  125. const char *request, int request_len)
  126. {
  127. int i, r;
  128. rend_cache_entry_t *ent;
  129. char *nickname;
  130. circuit_t *rendcirc;
  131. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  132. log_fn(LOG_WARN, "Received REND_INTRODUCE_ACK on unexpected circuit %d",
  133. circ->n_circ_id);
  134. circuit_mark_for_close(circ);
  135. return -1;
  136. }
  137. assert(circ->build_state->chosen_exit);
  138. if (request_len == 0) {
  139. /* It's an ACK; the introduction point relayed our introduction request. */
  140. /* Locate the rend circ which is waiting to hear about this ack,
  141. * and tell it.
  142. */
  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. circuit_mark_for_close(circ);
  150. } else {
  151. /* It's a NAK; the introduction point didn't relay our request. */
  152. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  153. /* XXXX
  154. * Now become non-open, extend to another one of Bob's
  155. * introduction points, and try again. Maybe mark the service as
  156. * non-functional at the first intro point somehow?
  157. *
  158. * Or re-fetch the service descriptor? Hm....
  159. */
  160. r = rend_cache_lookup_entry(circ->rend_query, &ent);
  161. if (r<0) {
  162. log_fn(LOG_WARN, "Malformed service ID '%s'", circ->rend_query);
  163. return -1;
  164. }
  165. if (r>0) {
  166. /* Okay, we found the right service desc. First, remove this intro point
  167. * from the parsed descriptor (if it's still there!)
  168. */
  169. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  170. if (!strcasecmp(ent->parsed->intro_points[i],
  171. circ->build_state->chosen_exit)) {
  172. tor_free(ent->parsed->intro_points[i]);
  173. ent->parsed->intro_points[i] =
  174. ent->parsed->intro_points[--ent->parsed->n_intro_points];
  175. break;
  176. }
  177. }
  178. /* If there are any introduction points left, re-extend the circuit to
  179. * another intro point and try again. */
  180. if (ent->parsed->n_intro_points) {
  181. nickname = rend_client_get_random_intro(circ->rend_query);
  182. assert(nickname);
  183. if (!router_get_by_nickname(nickname)) {
  184. log_fn(LOG_WARN, "Advertised intro point '%s' is not known. Closing.",
  185. nickname);
  186. circuit_mark_for_close(circ);
  187. return -1;
  188. }
  189. log_fn(LOG_INFO, "Chose new intro point %s for %s (circ %d)",
  190. nickname, circ->rend_query, circ->n_circ_id);
  191. circ->state = CIRCUIT_STATE_BUILDING;
  192. tor_free(circ->build_state->chosen_exit);
  193. circ->build_state->chosen_exit = tor_strdup(nickname);
  194. ++circ->build_state->desired_path_len;
  195. if (circuit_send_next_onion_skin(circ)<0) {
  196. log_fn(LOG_WARN, "Couldn't extend circuit to new intro point.");
  197. circuit_mark_for_close(circ);
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. }
  203. /* Either we have no service desc, or all the intro points in that
  204. * descriptor failed. So re-fetch the descriptor and try again. */
  205. /* XXXX What do we do to this circ in the meantime? */
  206. /* Refetch descriptor */
  207. if(!connection_get_by_type_rendquery(CONN_TYPE_DIR, circ->rend_query)) {
  208. /* not one already; initiate a dir rend desc lookup */
  209. directory_initiate_command(router_pick_directory_server(),
  210. DIR_PURPOSE_FETCH_RENDDESC,
  211. circ->rend_query, strlen(circ->rend_query));
  212. }
  213. }
  214. return 0;
  215. }
  216. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  217. * the circuit to C_REND_READY.
  218. */
  219. int
  220. rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
  221. {
  222. /* we just got an ack for our establish-rendezvous. switch purposes. */
  223. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  224. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  225. circuit_mark_for_close(circ);
  226. return -1;
  227. }
  228. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  229. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  230. return 0;
  231. }
  232. /* bob sent us a rendezvous cell, join the circs. */
  233. int
  234. rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
  235. {
  236. crypt_path_t *hop;
  237. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  238. if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
  239. !circ->build_state->pending_final_cpath) {
  240. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  241. circuit_mark_for_close(circ);
  242. return -1;
  243. }
  244. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  245. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",request_len);
  246. goto err;
  247. }
  248. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  249. assert(circ->build_state && circ->build_state->pending_final_cpath);
  250. hop = circ->build_state->pending_final_cpath;
  251. assert(hop->handshake_state);
  252. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  253. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  254. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  255. goto err;
  256. }
  257. /* ... and set up cpath. */
  258. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  259. goto err;
  260. /* Check whether the digest is right... */
  261. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  262. log_fn(LOG_WARN, "Incorrect digest of key material");
  263. goto err;
  264. }
  265. crypto_dh_free(hop->handshake_state);
  266. hop->handshake_state = NULL;
  267. /* All is well. Extend the circuit. */
  268. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  269. hop->state = CPATH_STATE_OPEN;
  270. /* set the windows to default. these are the windows
  271. * that alice thinks bob has.
  272. */
  273. hop->package_window = CIRCWINDOW_START;
  274. hop->deliver_window = CIRCWINDOW_START;
  275. onion_append_to_cpath(&circ->cpath, hop);
  276. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  277. return 0;
  278. err:
  279. circuit_mark_for_close(circ);
  280. return -1;
  281. }
  282. /* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  283. * are waiting on query. If success==1, move them to the next state.
  284. * If success==0, fail them.
  285. */
  286. void rend_client_desc_fetched(char *query, int success) {
  287. connection_t **carray;
  288. connection_t *conn;
  289. int n, i;
  290. rend_cache_entry_t *entry;
  291. get_connection_array(&carray, &n);
  292. for (i = 0; i < n; ++i) {
  293. conn = carray[i];
  294. if (conn->type != CONN_TYPE_AP ||
  295. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  296. continue;
  297. if (rend_cmp_service_ids(conn->rend_query, query))
  298. continue;
  299. /* great, this guy was waiting */
  300. if(success ||
  301. rend_cache_lookup_entry(conn->rend_query, &entry) == 1) {
  302. /* either this fetch worked, or it failed but there was a
  303. * valid entry from before which we should reuse */
  304. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  305. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  306. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  307. /* it will never work */
  308. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  309. connection_mark_for_close(conn,0);
  310. }
  311. } else { /* 404, or fetch didn't get that far */
  312. log_fn(LOG_WARN,"service id '%s' fetched failed, and not in cache. Closing conn.", query);
  313. connection_mark_for_close(conn,0);
  314. }
  315. }
  316. }
  317. int rend_cmp_service_ids(const char *one, const char *two) {
  318. return strcasecmp(one,two);
  319. }
  320. /* strdup a nickname for a random introduction
  321. * point of query. return NULL if error.
  322. */
  323. char *rend_client_get_random_intro(char *query) {
  324. int i;
  325. smartlist_t *sl;
  326. char *choice;
  327. char *nickname;
  328. rend_cache_entry_t *entry;
  329. if(rend_cache_lookup_entry(query, &entry) < 1) {
  330. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  331. return NULL;
  332. }
  333. sl = smartlist_create();
  334. /* add the intro point nicknames */
  335. for(i=0;i<entry->parsed->n_intro_points;i++)
  336. smartlist_add(sl,entry->parsed->intro_points[i]);
  337. choice = smartlist_choose(sl);
  338. if(!choice) {
  339. smartlist_free(sl);
  340. return NULL;
  341. }
  342. nickname = tor_strdup(choice);
  343. smartlist_free(sl);
  344. return nickname;
  345. }
  346. /* If address is of the form "y.onion" with a well-formed handle y,
  347. * then put a '\0' after y, lower-case it, and return 0.
  348. * Else return -1 and change nothing.
  349. */
  350. int rend_parse_rendezvous_address(char *address) {
  351. char *s;
  352. char query[REND_SERVICE_ID_LEN+1];
  353. s = strrchr(address,'.');
  354. if(!s) return -1; /* no dot */
  355. if (strcasecmp(s+1,"onion"))
  356. return -1; /* not .onion */
  357. *s = 0; /* null terminate it */
  358. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  359. goto failed;
  360. tor_strlower(query);
  361. if(rend_valid_service_id(query)) {
  362. tor_strlower(address);
  363. return 0; /* success */
  364. }
  365. failed:
  366. /* otherwise, return to previous state and return -1 */
  367. *s = '.';
  368. return -1;
  369. }
  370. /*
  371. Local Variables:
  372. mode:c
  373. indent-tabs-mode:nil
  374. c-basic-offset:2
  375. End:
  376. */