rendclient.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /* we don't need it anymore, plus it's been used. send the destroy. */
  98. circuit_mark_for_close(introcirc);
  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 *introcirc,
  122. const char *request, int request_len)
  123. {
  124. if (request_len == 0) {
  125. /* It's an ACK; the introduction point relayed our introduction request. */
  126. /* XXXX writeme */
  127. } else {
  128. /* It's a NAK; the introduction point didn't relay our request. */
  129. /* XXXX writeme */
  130. }
  131. return 0;
  132. }
  133. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  134. * the circuit to C_REND_READY.
  135. */
  136. int
  137. rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
  138. {
  139. /* we just got an ack for our establish-rendezvous. switch purposes. */
  140. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  141. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  142. circuit_mark_for_close(circ);
  143. return -1;
  144. }
  145. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  146. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  147. return 0;
  148. }
  149. /* bob sent us a rendezvous cell, join the circs. */
  150. int
  151. rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
  152. {
  153. connection_t *apconn;
  154. crypt_path_t *hop;
  155. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  156. if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
  157. !circ->build_state->pending_final_cpath) {
  158. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  159. circuit_mark_for_close(circ);
  160. return -1;
  161. }
  162. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  163. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",request_len);
  164. goto err;
  165. }
  166. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  167. assert(circ->build_state && circ->build_state->pending_final_cpath);
  168. hop = circ->build_state->pending_final_cpath;
  169. assert(hop->handshake_state);
  170. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  171. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  172. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  173. goto err;
  174. }
  175. /* ... and set up cpath. */
  176. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  177. goto err;
  178. /* Check whether the digest is right... */
  179. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  180. log_fn(LOG_WARN, "Incorrect digest of key material");
  181. goto err;
  182. }
  183. crypto_dh_free(hop->handshake_state);
  184. hop->handshake_state = NULL;
  185. /* All is well. Extend the circuit. */
  186. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  187. hop->state = CPATH_STATE_OPEN;
  188. /* set the windows to default. these are the windows
  189. * that alice thinks bob has.
  190. */
  191. hop->package_window = CIRCWINDOW_START;
  192. hop->deliver_window = CIRCWINDOW_START;
  193. onion_append_to_cpath(&circ->cpath, hop);
  194. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  195. for(apconn = circ->p_streams; apconn; apconn = apconn->next_stream) {
  196. apconn->cpath_layer = circ->cpath->prev;
  197. /* now the last hop is different. be sure to send all the way. */
  198. if(connection_ap_handshake_send_begin(apconn, circ) < 0)
  199. return -1;
  200. }
  201. return 0;
  202. err:
  203. circuit_mark_for_close(circ);
  204. return -1;
  205. }
  206. /* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  207. * are waiting on query. If success==1, move them to the next state.
  208. * If success==0, fail them.
  209. */
  210. void rend_client_desc_fetched(char *query, int success) {
  211. connection_t **carray;
  212. connection_t *conn;
  213. int n, i;
  214. rend_cache_entry_t *entry;
  215. get_connection_array(&carray, &n);
  216. for (i = 0; i < n; ++i) {
  217. conn = carray[i];
  218. if (conn->type != CONN_TYPE_AP ||
  219. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  220. continue;
  221. if (rend_cmp_service_ids(conn->rend_query, query))
  222. continue;
  223. /* great, this guy was waiting */
  224. if(success ||
  225. rend_cache_lookup_entry(conn->rend_query, &entry) == 1) {
  226. /* either this fetch worked, or it failed but there was a
  227. * valid entry from before which we should reuse */
  228. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  229. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  230. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  231. /* it will never work */
  232. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  233. connection_mark_for_close(conn,0);
  234. }
  235. } else { /* 404, or fetch didn't get that far */
  236. log_fn(LOG_WARN,"service id '%s' fetched failed, and not in cache. Closing conn.", query);
  237. connection_mark_for_close(conn,0);
  238. }
  239. }
  240. }
  241. int rend_cmp_service_ids(char *one, char *two) {
  242. return strcasecmp(one,two);
  243. }
  244. /* strdup a nickname for a random introduction
  245. * point of query. return NULL if error.
  246. */
  247. char *rend_client_get_random_intro(char *query) {
  248. int i;
  249. smartlist_t *sl;
  250. char *choice;
  251. char *nickname;
  252. rend_cache_entry_t *entry;
  253. if(rend_cache_lookup_entry(query, &entry) < 1) {
  254. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  255. return NULL;
  256. }
  257. sl = smartlist_create();
  258. /* add the intro point nicknames */
  259. for(i=0;i<entry->parsed->n_intro_points;i++)
  260. smartlist_add(sl,entry->parsed->intro_points[i]);
  261. choice = smartlist_choose(sl);
  262. if(!choice) {
  263. smartlist_free(sl);
  264. return NULL;
  265. }
  266. nickname = tor_strdup(choice);
  267. smartlist_free(sl);
  268. return nickname;
  269. }
  270. /* If address is of the form "y.onion" with a well-formed handle y,
  271. * then put a '\0' after y, lower-case it, and return 0.
  272. * Else return -1 and change nothing.
  273. */
  274. int rend_parse_rendezvous_address(char *address) {
  275. char *s;
  276. char query[REND_SERVICE_ID_LEN+1];
  277. s = strrchr(address,'.');
  278. if(!s) return -1; /* no dot */
  279. if (strcasecmp(s+1,"onion"))
  280. return -1; /* not .onion */
  281. *s = 0; /* null terminate it */
  282. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  283. goto failed;
  284. tor_strlower(query);
  285. if(rend_valid_service_id(query)) {
  286. tor_strlower(address);
  287. return 0; /* success */
  288. }
  289. failed:
  290. /* otherwise, return to previous state and return -1 */
  291. *s = '.';
  292. return -1;
  293. }
  294. /*
  295. Local Variables:
  296. mode:c
  297. indent-tabs-mode:nil
  298. c-basic-offset:2
  299. End:
  300. */