Make new logging stuff work on windows; fix a couple of windows typos.
[tor.git] / src / or / rendclient.c
blob07e8d02f244082f73c92701630fed68505201d61
1 /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
4 const char rendclient_c_id[] = "$Id$";
6 /**
7 * \file rendclient.c
8 * \brief Client code to access location-hidden services.
9 **/
11 #include "or.h"
13 /** Called when we've established a circuit to an introduction point:
14 * send the introduction request. */
15 void
16 rend_client_introcirc_has_opened(circuit_t *circ)
18 tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
19 tor_assert(CIRCUIT_IS_ORIGIN(circ));
20 tor_assert(circ->cpath);
22 info(LD_REND,"introcirc is open");
23 connection_ap_attach_pending();
26 /** Send the establish-rendezvous cell along a rendezvous circuit. if
27 * it fails, mark the circ for close and return -1. else return 0.
29 static int
30 rend_client_send_establish_rendezvous(circuit_t *circ)
32 tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
33 info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
35 if (crypto_rand(circ->rend_cookie, REND_COOKIE_LEN) < 0) {
36 warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
37 circuit_mark_for_close(circ);
38 return -1;
40 if (connection_edge_send_command(NULL,circ,
41 RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
42 circ->rend_cookie, REND_COOKIE_LEN,
43 circ->cpath->prev)<0) {
44 /* circ is already marked for close */
45 warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
46 return -1;
49 return 0;
52 /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
53 * down introcirc if possible.
55 int
56 rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc)
58 size_t payload_len;
59 int r;
60 char payload[RELAY_PAYLOAD_SIZE];
61 char tmp[RELAY_PAYLOAD_SIZE];
62 rend_cache_entry_t *entry;
63 crypt_path_t *cpath;
64 off_t dh_offset;
66 tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
67 tor_assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
68 tor_assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
70 if (rend_cache_lookup_entry(introcirc->rend_query, -1, &entry) < 1) {
71 warn(LD_REND,"query '%s' didn't have valid rend desc in cache. Failing.",
72 safe_str(introcirc->rend_query));
73 goto err;
76 /* first 20 bytes of payload are the hash of bob's pk */
77 if (crypto_pk_get_digest(entry->parsed->pk, payload)<0) {
78 warn(LD_BUG, "Internal error: couldn't hash public key.");
79 goto err;
82 /* Initialize the pending_final_cpath and start the DH handshake. */
83 cpath = rendcirc->build_state->pending_final_cpath;
84 if (!cpath) {
85 cpath = rendcirc->build_state->pending_final_cpath =
86 tor_malloc_zero(sizeof(crypt_path_t));
87 cpath->magic = CRYPT_PATH_MAGIC;
88 if (!(cpath->dh_handshake_state = crypto_dh_new())) {
89 warn(LD_BUG, "Internal error: couldn't allocate DH.");
90 goto err;
92 if (crypto_dh_generate_public(cpath->dh_handshake_state)<0) {
93 warn(LD_BUG, "Internal error: couldn't generate g^x.");
94 goto err;
98 /* write the remaining items into tmp */
99 if (entry->parsed->protocols & (1<<2)) {
100 /* version 2 format */
101 extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
102 int klen;
103 tmp[0] = 2; /* version 2 of the cell format */
104 /* nul pads */
105 set_uint32(tmp+1, htonl(extend_info->addr));
106 set_uint16(tmp+5, htons(extend_info->port));
107 memcpy(tmp+7, extend_info->identity_digest, DIGEST_LEN);
108 klen = crypto_pk_asn1_encode(extend_info->onion_key, tmp+7+DIGEST_LEN+2,
109 sizeof(tmp)-(7+DIGEST_LEN+2));
110 set_uint16(tmp+7+DIGEST_LEN, htons(klen));
111 memcpy(tmp+7+DIGEST_LEN+2+klen, rendcirc->rend_cookie, REND_COOKIE_LEN);
112 dh_offset = 7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
113 } else {
114 /* Version 0. */
115 strncpy(tmp, rendcirc->build_state->chosen_exit->nickname, (MAX_NICKNAME_LEN+1)); /* nul pads */
116 memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
117 dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
120 if (crypto_dh_get_public(cpath->dh_handshake_state, tmp+dh_offset,
121 DH_KEY_LEN)<0) {
122 warn(LD_BUG, "Internal error: couldn't extract g^x.");
123 goto err;
126 /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
127 * to avoid buffer overflows? */
128 r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, payload+DIGEST_LEN, tmp,
129 dh_offset+DH_KEY_LEN,
130 PK_PKCS1_OAEP_PADDING, 0);
131 if (r<0) {
132 warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
133 goto err;
136 tor_assert(DIGEST_LEN + r <= RELAY_PAYLOAD_SIZE); /* we overran something */
137 payload_len = DIGEST_LEN + r;
139 if (connection_edge_send_command(NULL, introcirc,
140 RELAY_COMMAND_INTRODUCE1,
141 payload, payload_len,
142 introcirc->cpath->prev)<0) {
143 /* introcirc is already marked for close. leave rendcirc alone. */
144 warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
145 return -1;
148 /* Now, we wait for an ACK or NAK on this circuit. */
149 introcirc->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
151 return 0;
152 err:
153 circuit_mark_for_close(introcirc);
154 circuit_mark_for_close(rendcirc);
155 return -1;
158 /** Called when a rendezvous circuit is open; sends a establish
159 * rendezvous circuit as appropriate. */
160 void
161 rend_client_rendcirc_has_opened(circuit_t *circ)
163 tor_assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
164 tor_assert(CIRCUIT_IS_ORIGIN(circ));
166 info(LD_REND,"rendcirc is open");
168 /* generate a rendezvous cookie, store it in circ */
169 if (rend_client_send_establish_rendezvous(circ) < 0) {
170 return;
174 /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
177 rend_client_introduction_acked(circuit_t *circ,
178 const char *request, size_t request_len)
180 circuit_t *rendcirc;
182 if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
183 warn(LD_PROTOCOL, "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
184 circ->n_circ_id);
185 circuit_mark_for_close(circ);
186 return -1;
189 tor_assert(circ->build_state->chosen_exit);
190 tor_assert(circ->build_state->chosen_exit->nickname);
192 if (request_len == 0) {
193 /* It's an ACK; the introduction point relayed our introduction request. */
194 /* Locate the rend circ which is waiting to hear about this ack,
195 * and tell it.
197 info(LD_REND,"Received ack. Telling rend circ...");
198 rendcirc = circuit_get_by_rend_query_and_purpose(
199 circ->rend_query, CIRCUIT_PURPOSE_C_REND_READY);
200 if (rendcirc) { /* remember the ack */
201 rendcirc->purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
202 } else {
203 info(LD_REND,"...Found no rend circ. Dropping on the floor.");
205 /* close the circuit: we won't need it anymore. */
206 circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
207 circuit_mark_for_close(circ);
208 } else {
209 /* It's a NAK; the introduction point didn't relay our request. */
210 circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
211 /* Remove this intro point from the set of viable introduction
212 * points. If any remain, extend to a new one and try again.
213 * If none remain, refetch the service descriptor.
215 if (rend_client_remove_intro_point(circ->build_state->chosen_exit,
216 circ->rend_query) > 0) {
217 /* There are introduction points left. Re-extend the circuit to
218 * another intro point and try again. */
219 extend_info_t *extend_info;
220 int result;
221 extend_info = rend_client_get_random_intro(circ->rend_query);
222 if (!extend_info) {
223 warn(LD_REND, "No introduction points left for %s. Closing.",
224 safe_str(circ->rend_query));
225 circuit_mark_for_close(circ);
226 return -1;
228 info(LD_REND,
229 "Got nack for %s from %s. Re-extending circ %d, this time to %s.",
230 safe_str(circ->rend_query),
231 circ->build_state->chosen_exit->nickname, circ->n_circ_id,
232 extend_info->nickname);
233 result = circuit_extend_to_new_exit(circ, extend_info);
234 extend_info_free(extend_info);
235 return result;
238 return 0;
241 /** If we are not currently fetching a rendezvous service descriptor
242 * for the service ID <b>query</b>, start a directory connection to fetch a
243 * new one.
245 void
246 rend_client_refetch_renddesc(const char *query)
248 if (connection_get_by_type_state_rendquery(CONN_TYPE_DIR, 0, query)) {
249 info(LD_REND,"Would fetch a new renddesc here (for %s), but one is already in progress.", safe_str(query));
250 } else {
251 /* not one already; initiate a dir rend desc lookup */
252 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC, query, 1);
256 /** remove failed_intro from ent. if ent now has no intro points, or
257 * service is unrecognized, then launch a new renddesc fetch.
259 * Return -1 if error, 0 if no intro points remain or service
260 * unrecognized, 1 if recognized and some intro points remain.
263 rend_client_remove_intro_point(extend_info_t *failed_intro, const char *query)
265 int i, r;
266 rend_cache_entry_t *ent;
267 connection_t *conn;
269 r = rend_cache_lookup_entry(query, -1, &ent);
270 if (r<0) {
271 warn(LD_BUG, "Bug: malformed service ID '%s'.", safe_str(query));
272 return -1;
274 if (r==0) {
275 info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
276 safe_str(query));
277 rend_client_refetch_renddesc(query);
278 return 0;
281 if (ent->parsed->intro_point_extend_info) {
282 for (i=0; i < ent->parsed->n_intro_points; ++i) {
283 if (!memcmp(failed_intro->identity_digest,
284 ent->parsed->intro_point_extend_info[i]->identity_digest,
285 DIGEST_LEN)) {
286 tor_assert(!strcmp(ent->parsed->intro_points[i],
287 ent->parsed->intro_point_extend_info[i]->nickname));
288 tor_free(ent->parsed->intro_points[i]);
289 extend_info_free(ent->parsed->intro_point_extend_info[i]);
290 --ent->parsed->n_intro_points;
291 ent->parsed->intro_points[i] =
292 ent->parsed->intro_points[ent->parsed->n_intro_points];
293 ent->parsed->intro_point_extend_info[i] =
294 ent->parsed->intro_point_extend_info[ent->parsed->n_intro_points];
295 break;
298 } else {
299 for (i=0; i < ent->parsed->n_intro_points; ++i) {
300 if (!strcasecmp(ent->parsed->intro_points[i], failed_intro->nickname)) {
301 tor_free(ent->parsed->intro_points[i]);
302 ent->parsed->intro_points[i] =
303 ent->parsed->intro_points[--ent->parsed->n_intro_points];
304 break;
309 if (!ent->parsed->n_intro_points) {
310 info(LD_REND,"No more intro points remain for %s. Re-fetching descriptor.",
311 safe_str(query));
312 rend_client_refetch_renddesc(query);
314 /* move all pending streams back to renddesc_wait */
315 while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
316 AP_CONN_STATE_CIRCUIT_WAIT, query))) {
317 conn->state = AP_CONN_STATE_RENDDESC_WAIT;
320 return 0;
322 info(LD_REND,"%d options left for %s.",
323 ent->parsed->n_intro_points, safe_str(query));
324 return 1;
327 /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
328 * the circuit to C_REND_READY.
331 rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len)
333 /* we just got an ack for our establish-rendezvous. switch purposes. */
334 if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
335 warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
336 circuit_mark_for_close(circ);
337 return -1;
339 info(LD_REND,"Got rendezvous ack. This circuit is now ready for rendezvous.");
340 circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
341 return 0;
344 /** Bob sent us a rendezvous cell; join the circuits. */
346 rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len)
348 crypt_path_t *hop;
349 char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
351 if ((circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
352 circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
353 || !circ->build_state->pending_final_cpath) {
354 warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not expecting it. Closing.");
355 circuit_mark_for_close(circ);
356 return -1;
359 if (request_len != DH_KEY_LEN+DIGEST_LEN) {
360 warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",(int)request_len);
361 goto err;
364 /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
365 tor_assert(circ->build_state);
366 tor_assert(circ->build_state->pending_final_cpath);
367 hop = circ->build_state->pending_final_cpath;
368 tor_assert(hop->dh_handshake_state);
369 if (crypto_dh_compute_secret(hop->dh_handshake_state, request, DH_KEY_LEN,
370 keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
371 warn(LD_GENERAL, "Couldn't complete DH handshake.");
372 goto err;
374 /* ... and set up cpath. */
375 if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
376 goto err;
378 /* Check whether the digest is right... */
379 if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
380 warn(LD_PROTOCOL, "Incorrect digest of key material.");
381 goto err;
384 crypto_dh_free(hop->dh_handshake_state);
385 hop->dh_handshake_state = NULL;
387 /* All is well. Extend the circuit. */
388 circ->purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
389 hop->state = CPATH_STATE_OPEN;
390 /* set the windows to default. these are the windows
391 * that alice thinks bob has.
393 hop->package_window = CIRCWINDOW_START;
394 hop->deliver_window = CIRCWINDOW_START;
396 onion_append_to_cpath(&circ->cpath, hop);
397 circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
398 return 0;
399 err:
400 circuit_mark_for_close(circ);
401 return -1;
404 /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
405 * are waiting on query. If there's a working cache entry here
406 * with at least one intro point, move them to the next state;
407 * else fail them.
409 void
410 rend_client_desc_here(const char *query)
412 connection_t *conn;
413 rend_cache_entry_t *entry;
414 time_t now = time(NULL);
416 while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
417 AP_CONN_STATE_RENDDESC_WAIT, query))) {
418 if (rend_cache_lookup_entry(conn->rend_query, -1, &entry) == 1 &&
419 entry->parsed->n_intro_points > 0) {
420 /* either this fetch worked, or it failed but there was a
421 * valid entry from before which we should reuse */
422 info(LD_REND,"Rend desc is usable. Launching circuits.");
423 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
425 /* restart their timeout values, so they get a fair shake at
426 * connecting to the hidden service. */
427 conn->timestamp_created = now;
428 conn->timestamp_lastread = now;
429 conn->timestamp_lastwritten = now;
431 if (connection_ap_handshake_attach_circuit(conn) < 0) {
432 /* it will never work */
433 warn(LD_REND,"Rendezvous attempt failed. Closing.");
434 connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
436 tor_assert(conn->state != AP_CONN_STATE_RENDDESC_WAIT); /* avoid loop */
437 } else { /* 404, or fetch didn't get that far */
438 notice(LD_REND,"Closing stream for '%s.onion': hidden service is unavailable (try again later).", safe_str(query));
439 connection_mark_unattached_ap(conn, END_STREAM_REASON_TIMEOUT);
444 /** Return a newly allocated extend_info_t* for a randomly chosen introduction
445 * point for the named hidden service. Return NULL if all introduction points
446 * have been tried and failed.
448 extend_info_t *
449 rend_client_get_random_intro(const char *query)
451 int i;
452 rend_cache_entry_t *entry;
454 if (rend_cache_lookup_entry(query, -1, &entry) < 1) {
455 warn(LD_REND,"Query '%s' didn't have valid rend desc in cache. Failing.",
456 safe_str(query));
457 return NULL;
460 again:
461 if (!entry->parsed->n_intro_points)
462 return NULL;
464 i = crypto_rand_int(entry->parsed->n_intro_points);
466 if (entry->parsed->intro_point_extend_info) {
467 return extend_info_dup(entry->parsed->intro_point_extend_info[i]);
468 } else {
469 /* add the intro point nicknames */
470 char *choice = entry->parsed->intro_points[i];
471 routerinfo_t *router = router_get_by_nickname(choice, 0);
472 if (!router) {
473 info(LD_REND, "Unknown router with nickname '%s'; trying another.",choice);
474 tor_free(choice);
475 entry->parsed->intro_points[i] =
476 entry->parsed->intro_points[--entry->parsed->n_intro_points];
477 goto again;
479 return extend_info_from_router(router);