rendclient.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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(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. int
  38. rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
  39. const char *descp;
  40. int desc_len, payload_len, r;
  41. char payload[RELAY_PAYLOAD_SIZE];
  42. char tmp[20+20+128];
  43. rend_service_descriptor_t *parsed=NULL;
  44. crypt_path_t *cpath;
  45. assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  46. assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
  47. assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
  48. if(rend_cache_lookup(introcirc->rend_query, &descp, &desc_len) < 1) {
  49. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
  50. introcirc->rend_query);
  51. goto err;
  52. }
  53. parsed = rend_parse_service_descriptor(descp,desc_len);
  54. if (!parsed) {
  55. log_fn(LOG_WARN,"Couldn't parse service descriptor");
  56. goto err;
  57. }
  58. /* first 20 bytes of payload are the hash of bob's pk */
  59. if (crypto_pk_get_digest(parsed->pk, payload)<0) {
  60. log_fn(LOG_WARN, "Couldn't hash public key.");
  61. goto err;
  62. }
  63. /* Initialize the pending_final_cpath and start the DH handshake. */
  64. cpath = rendcirc->build_state->pending_final_cpath =
  65. tor_malloc_zero(sizeof(crypt_path_t));
  66. if (!(cpath->handshake_state = crypto_dh_new())) {
  67. log_fn(LOG_WARN, "Couldn't allocate DH");
  68. goto err;
  69. }
  70. if (crypto_dh_generate_public(cpath->handshake_state)<0) {
  71. log_fn(LOG_WARN, "Couldn't generate g^x");
  72. goto err;
  73. }
  74. /* write the remaining items into tmp */
  75. strncpy(tmp, rendcirc->build_state->chosen_exit, 20); /* nul pads */
  76. memcpy(tmp+20, rendcirc->rend_cookie, 20);
  77. if (crypto_dh_get_public(cpath->handshake_state, tmp+40, 128)<0) {
  78. log_fn(LOG_WARN, "Couldn't extract g^x");
  79. goto err;
  80. }
  81. r = crypto_pk_public_hybrid_encrypt(parsed->pk, tmp,
  82. 20+20+128, payload+20,
  83. PK_PKCS1_OAEP_PADDING);
  84. if (r<0) {
  85. log_fn(LOG_WARN,"hybrid pk encrypt failed.");
  86. goto err;
  87. }
  88. payload_len = 20 + r;
  89. rend_service_descriptor_free(parsed);
  90. if (connection_edge_send_command(NULL, introcirc,
  91. RELAY_COMMAND_INTRODUCE1,
  92. payload, payload_len,
  93. introcirc->cpath->prev)<0) {
  94. /* introcirc is already marked for close. leave rendcirc alone. */
  95. log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
  96. return -1;
  97. }
  98. /* we don't need it anymore, plus it's been used. send the destroy. */
  99. circuit_mark_for_close(introcirc);
  100. return 0;
  101. err:
  102. if(parsed)
  103. rend_service_descriptor_free(parsed);
  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(circ->cpath);
  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. int
  122. rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
  123. {
  124. /* we just got an ack for our establish-rendezvous. switch purposes. */
  125. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  126. log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
  127. circuit_mark_for_close(circ);
  128. return -1;
  129. }
  130. log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
  131. circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
  132. return 0;
  133. }
  134. /* bob sent us a rendezvous cell, join the circs. */
  135. int
  136. rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
  137. {
  138. connection_t *apconn;
  139. crypt_path_t *hop;
  140. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  141. if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
  142. !circ->build_state->pending_final_cpath) {
  143. log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
  144. circuit_mark_for_close(circ);
  145. return -1;
  146. }
  147. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  148. log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",request_len);
  149. goto err;
  150. }
  151. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  152. assert(circ->build_state && circ->build_state->pending_final_cpath);
  153. hop = circ->build_state->pending_final_cpath;
  154. assert(hop->handshake_state);
  155. if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
  156. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  157. log_fn(LOG_WARN, "Couldn't complete DH handshake");
  158. goto err;
  159. }
  160. /* ... and set up cpath. */
  161. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  162. goto err;
  163. /* Check whether the digest is right... */
  164. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  165. log_fn(LOG_WARN, "Incorrect digest of key material");
  166. goto err;
  167. }
  168. /* All is well. Extend the circuit. */
  169. circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  170. onion_append_to_cpath(&circ->cpath, hop);
  171. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  172. for(apconn = circ->p_streams; apconn; apconn = apconn->next_stream) {
  173. apconn->cpath_layer = circ->cpath->prev;
  174. /* now the last hop is different. be sure to send all the way. */
  175. if(connection_ap_handshake_send_begin(apconn, circ) < 0)
  176. return -1;
  177. }
  178. return 0;
  179. err:
  180. circuit_mark_for_close(circ);
  181. return -1;
  182. }
  183. /* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  184. * are waiting on query. If success==1, move them to the next state.
  185. * If success==0, fail them.
  186. */
  187. void rend_client_desc_fetched(char *query, int success) {
  188. connection_t **carray;
  189. connection_t *conn;
  190. int n, i;
  191. get_connection_array(&carray, &n);
  192. for (i = 0; i < n; ++i) {
  193. conn = carray[i];
  194. if (conn->type != CONN_TYPE_AP ||
  195. conn->state != AP_CONN_STATE_RENDDESC_WAIT)
  196. continue;
  197. if (rend_cmp_service_ids(conn->rend_query, query))
  198. continue;
  199. /* great, this guy was waiting */
  200. if(success) {
  201. log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
  202. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  203. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  204. /* it will never work */
  205. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  206. connection_mark_for_close(conn,0);
  207. }
  208. } else { /* 404 */
  209. log_fn(LOG_WARN,"service id '%s' not found. Closing conn.", query);
  210. connection_mark_for_close(conn,0);
  211. }
  212. }
  213. }
  214. int rend_cmp_service_ids(char *one, char *two) {
  215. return strcasecmp(one,two);
  216. }
  217. /* strdup a nickname for a random introduction
  218. * point of query. return NULL if error.
  219. */
  220. char *rend_client_get_random_intro(char *query) {
  221. const char *descp;
  222. int desc_len;
  223. int i;
  224. smartlist_t *sl;
  225. rend_service_descriptor_t *parsed;
  226. char *choice;
  227. char *nickname;
  228. if(rend_cache_lookup(query, &descp, &desc_len) < 1) {
  229. log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
  230. return NULL;
  231. }
  232. parsed = rend_parse_service_descriptor(descp,desc_len);
  233. if (!parsed) {
  234. log_fn(LOG_WARN,"Couldn't parse service descriptor");
  235. return NULL;
  236. }
  237. sl = smartlist_create();
  238. /* add the intro point nicknames */
  239. for(i=0;i<parsed->n_intro_points;i++)
  240. smartlist_add(sl,parsed->intro_points[i]);
  241. choice = smartlist_choose(sl);
  242. nickname = tor_strdup(choice);
  243. smartlist_free(sl);
  244. rend_service_descriptor_free(parsed);
  245. return nickname;
  246. }
  247. /* If address is of the form "y.onion" with a well-formed handle y,
  248. * then put a '\0' after y, lower-case it, and return 0.
  249. * Else return -1 and change nothing.
  250. */
  251. int rend_parse_rendezvous_address(char *address) {
  252. char *s;
  253. char query[REND_SERVICE_ID_LEN+1];
  254. s = strrchr(address,'.');
  255. if(!s) return -1; /* no dot */
  256. if (strcasecmp(s+1,"onion"))
  257. return -1; /* not .onion */
  258. *s = 0; /* null terminate it */
  259. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  260. goto failed;
  261. tor_strlower(query);
  262. if(rend_valid_service_id(query)) {
  263. tor_strlower(address);
  264. return 0; /* success */
  265. }
  266. failed:
  267. /* otherwise, return to previous state and return -1 */
  268. *s = '.';
  269. return -1;
  270. }
  271. /*
  272. Local Variables:
  273. mode:c
  274. indent-tabs-mode:nil
  275. c-basic-offset:2
  276. End:
  277. */