rendclient.c 13 KB

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