2007-02-12 22:39:53 +01:00
|
|
|
/* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
|
2004-04-03 00:23:15 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char rendclient_c_id[] =
|
|
|
|
"$Id$";
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file rendclient.c
|
2004-12-01 04:48:14 +01:00
|
|
|
* \brief Client code to access location-hidden services.
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2004-04-03 00:23:15 +02:00
|
|
|
#include "or.h"
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we've established a circuit to an introduction point:
|
2004-05-05 23:32:43 +02:00
|
|
|
* send the introduction request. */
|
2004-04-03 00:23:15 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_introcirc_has_opened(origin_circuit_t *circ)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ->cpath);
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"introcirc is open");
|
2004-04-05 02:47:48 +02:00
|
|
|
connection_ap_attach_pending();
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Send the establish-rendezvous cell along a rendezvous circuit. if
|
2004-05-05 23:32:43 +02:00
|
|
|
* it fails, mark the circ for close and return -1. else return 0.
|
2004-04-05 09:41:31 +02:00
|
|
|
*/
|
2004-05-12 22:58:27 +02:00
|
|
|
static int
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_send_establish_rendezvous(origin_circuit_t *circ)
|
2004-04-03 06:22:22 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
|
2004-04-03 06:22:22 +02:00
|
|
|
|
2006-07-26 21:05:41 +02:00
|
|
|
if (crypto_rand(circ->rend_cookie, REND_COOKIE_LEN) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
|
2004-04-03 06:22:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-03-24 16:57:51 +01:00
|
|
|
if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
|
2004-04-03 06:22:22 +02:00
|
|
|
RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
|
2006-07-26 21:05:41 +02:00
|
|
|
circ->rend_cookie, REND_COOKIE_LEN,
|
2004-04-03 06:22:22 +02:00
|
|
|
circ->cpath->prev)<0) {
|
2004-04-05 09:41:31 +02:00
|
|
|
/* circ is already marked for close */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
|
2004-04-03 06:22:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
|
2004-04-06 05:44:36 +02:00
|
|
|
* down introcirc if possible.
|
|
|
|
*/
|
2004-04-05 09:41:31 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_send_introduction(origin_circuit_t *introcirc,
|
|
|
|
origin_circuit_t *rendcirc)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-14 04:47:09 +02:00
|
|
|
size_t payload_len;
|
|
|
|
int r;
|
2004-04-05 23:15:14 +02:00
|
|
|
char payload[RELAY_PAYLOAD_SIZE];
|
2005-06-29 23:46:55 +02:00
|
|
|
char tmp[RELAY_PAYLOAD_SIZE];
|
2004-04-08 00:41:00 +02:00
|
|
|
rend_cache_entry_t *entry;
|
2004-04-05 22:30:53 +02:00
|
|
|
crypt_path_t *cpath;
|
2005-06-29 23:46:55 +02:00
|
|
|
off_t dh_offset;
|
2004-04-05 09:41:31 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(introcirc->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
|
|
|
|
tor_assert(rendcirc->_base.purpose == CIRCUIT_PURPOSE_C_REND_READY);
|
2006-07-26 21:05:34 +02:00
|
|
|
tor_assert(!rend_cmp_service_ids(introcirc->rend_query,
|
|
|
|
rendcirc->rend_query));
|
2004-04-05 09:41:31 +02:00
|
|
|
|
2006-07-26 21:05:34 +02:00
|
|
|
if (rend_cache_lookup_entry(introcirc->rend_query, -1, &entry) < 1) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND,
|
2006-03-06 01:25:39 +01:00
|
|
|
"query %s didn't have valid rend desc in cache. Failing.",
|
2006-07-26 21:05:34 +02:00
|
|
|
escaped_safe_str(introcirc->rend_query));
|
2004-04-05 09:41:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first 20 bytes of payload are the hash of bob's pk */
|
2004-04-08 00:41:00 +02:00
|
|
|
if (crypto_pk_get_digest(entry->parsed->pk, payload)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Internal error: couldn't hash public key.");
|
2004-04-05 09:41:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-05 22:30:53 +02:00
|
|
|
/* Initialize the pending_final_cpath and start the DH handshake. */
|
2004-04-13 05:19:58 +02:00
|
|
|
cpath = rendcirc->build_state->pending_final_cpath;
|
|
|
|
if (!cpath) {
|
|
|
|
cpath = rendcirc->build_state->pending_final_cpath =
|
|
|
|
tor_malloc_zero(sizeof(crypt_path_t));
|
2005-03-23 07:21:48 +01:00
|
|
|
cpath->magic = CRYPT_PATH_MAGIC;
|
2005-05-03 00:35:18 +02:00
|
|
|
if (!(cpath->dh_handshake_state = crypto_dh_new())) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Internal error: couldn't allocate DH.");
|
2004-04-13 05:19:58 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2005-05-03 00:35:18 +02:00
|
|
|
if (crypto_dh_generate_public(cpath->dh_handshake_state)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Internal error: couldn't generate g^x.");
|
2004-04-13 05:19:58 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2004-04-05 22:30:53 +02:00
|
|
|
}
|
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
/* write the remaining items into tmp */
|
2005-06-29 23:46:55 +02:00
|
|
|
if (entry->parsed->protocols & (1<<2)) {
|
|
|
|
/* version 2 format */
|
|
|
|
extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
|
|
|
|
int klen;
|
|
|
|
tmp[0] = 2; /* version 2 of the cell format */
|
|
|
|
/* nul pads */
|
|
|
|
set_uint32(tmp+1, htonl(extend_info->addr));
|
|
|
|
set_uint16(tmp+5, htons(extend_info->port));
|
|
|
|
memcpy(tmp+7, extend_info->identity_digest, DIGEST_LEN);
|
|
|
|
klen = crypto_pk_asn1_encode(extend_info->onion_key, tmp+7+DIGEST_LEN+2,
|
|
|
|
sizeof(tmp)-(7+DIGEST_LEN+2));
|
|
|
|
set_uint16(tmp+7+DIGEST_LEN, htons(klen));
|
2006-07-26 21:05:41 +02:00
|
|
|
memcpy(tmp+7+DIGEST_LEN+2+klen, rendcirc->rend_cookie,
|
2006-07-23 09:37:35 +02:00
|
|
|
REND_COOKIE_LEN);
|
2005-06-29 23:46:55 +02:00
|
|
|
dh_offset = 7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
|
|
|
|
} else {
|
|
|
|
/* Version 0. */
|
2005-12-14 21:40:40 +01:00
|
|
|
strncpy(tmp, rendcirc->build_state->chosen_exit->nickname,
|
|
|
|
(MAX_NICKNAME_LEN+1)); /* nul pads */
|
2006-07-26 21:05:41 +02:00
|
|
|
memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie,
|
2006-07-23 09:37:35 +02:00
|
|
|
REND_COOKIE_LEN);
|
2005-06-29 23:46:55 +02:00
|
|
|
dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
|
|
|
|
}
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
if (crypto_dh_get_public(cpath->dh_handshake_state, tmp+dh_offset,
|
2004-04-06 05:44:36 +02:00
|
|
|
DH_KEY_LEN)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Internal error: couldn't extract g^x.");
|
2004-04-05 22:30:53 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2004-04-05 09:41:31 +02:00
|
|
|
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(REND_CLIENT);
|
2004-10-24 21:08:07 +02:00
|
|
|
/*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
|
|
|
|
* to avoid buffer overflows? */
|
2005-12-14 21:40:40 +01:00
|
|
|
r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, payload+DIGEST_LEN,
|
|
|
|
tmp,
|
2006-11-14 02:07:52 +01:00
|
|
|
(int)(dh_offset+DH_KEY_LEN),
|
2004-04-06 22:55:46 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING, 0);
|
2004-04-05 23:15:14 +02:00
|
|
|
if (r<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
|
2004-04-05 09:41:31 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
payload_len = DIGEST_LEN + r;
|
2007-05-15 23:17:48 +02:00
|
|
|
tor_assert(payload_len <= RELAY_PAYLOAD_SIZE); /* we overran something */
|
2004-04-05 23:15:14 +02:00
|
|
|
|
2007-03-24 16:57:51 +01:00
|
|
|
if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc),
|
2004-04-05 09:41:31 +02:00
|
|
|
RELAY_COMMAND_INTRODUCE1,
|
2004-04-05 23:15:14 +02:00
|
|
|
payload, payload_len,
|
2004-04-05 09:41:31 +02:00
|
|
|
introcirc->cpath->prev)<0) {
|
|
|
|
/* introcirc is already marked for close. leave rendcirc alone. */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
|
2004-04-05 09:41:31 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-13 03:41:39 +02:00
|
|
|
/* Now, we wait for an ACK or NAK on this circuit. */
|
2006-07-23 09:37:35 +02:00
|
|
|
introcirc->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
|
2004-04-05 17:17:34 +02:00
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
return 0;
|
|
|
|
err:
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(introcirc), END_CIRC_REASON_INTERNAL);
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(rendcirc), END_CIRC_REASON_INTERNAL);
|
2004-04-05 09:41:31 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when a rendezvous circuit is open; sends a establish
|
2004-05-05 23:32:43 +02:00
|
|
|
* rendezvous circuit as appropriate. */
|
2004-04-03 00:23:15 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_rendcirc_has_opened(origin_circuit_t *circ)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"rendcirc is open");
|
2004-04-03 05:06:06 +02:00
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
/* generate a rendezvous cookie, store it in circ */
|
|
|
|
if (rend_client_send_establish_rendezvous(circ) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-04-03 05:06:06 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
|
2004-04-13 01:33:47 +02:00
|
|
|
*/
|
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_introduction_acked(origin_circuit_t *circ,
|
2004-10-14 04:47:09 +02:00
|
|
|
const char *request, size_t request_len)
|
2004-04-13 01:33:47 +02:00
|
|
|
{
|
2006-07-26 21:05:34 +02:00
|
|
|
origin_circuit_t *rendcirc;
|
2006-06-05 00:42:13 +02:00
|
|
|
(void) request; // XXXX Use this.
|
2004-04-13 04:31:52 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (circ->_base.purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,
|
|
|
|
"Received REND_INTRODUCE_ACK on unexpected circuit %d.",
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.n_circ_id);
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
|
2004-04-13 03:41:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
tor_assert(circ->build_state->chosen_exit);
|
2004-04-13 04:31:52 +02:00
|
|
|
|
2004-04-13 01:33:47 +02:00
|
|
|
if (request_len == 0) {
|
|
|
|
/* It's an ACK; the introduction point relayed our introduction request. */
|
2004-04-14 00:56:24 +02:00
|
|
|
/* Locate the rend circ which is waiting to hear about this ack,
|
|
|
|
* and tell it.
|
|
|
|
*/
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"Received ack. Telling rend circ...");
|
2004-04-14 00:56:24 +02:00
|
|
|
rendcirc = circuit_get_by_rend_query_and_purpose(
|
2006-07-26 21:05:34 +02:00
|
|
|
circ->rend_query, CIRCUIT_PURPOSE_C_REND_READY);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (rendcirc) { /* remember the ack */
|
2006-07-26 21:05:34 +02:00
|
|
|
rendcirc->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
} else {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"...Found no rend circ. Dropping on the floor.");
|
2004-04-14 00:56:24 +02:00
|
|
|
}
|
|
|
|
/* close the circuit: we won't need it anymore. */
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
|
2006-10-17 17:20:00 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
|
2004-04-13 01:33:47 +02:00
|
|
|
} else {
|
|
|
|
/* It's a NAK; the introduction point didn't relay our request. */
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
|
2004-04-18 09:37:16 +02:00
|
|
|
/* Remove this intro point from the set of viable introduction
|
|
|
|
* points. If any remain, extend to a new one and try again.
|
|
|
|
* If none remain, refetch the service descriptor.
|
2004-04-13 03:41:39 +02:00
|
|
|
*/
|
2005-06-29 23:46:55 +02:00
|
|
|
if (rend_client_remove_intro_point(circ->build_state->chosen_exit,
|
2006-07-26 21:05:34 +02:00
|
|
|
circ->rend_query) > 0) {
|
2005-07-14 10:43:19 +02:00
|
|
|
/* There are introduction points left. Re-extend the circuit to
|
2004-04-13 04:31:52 +02:00
|
|
|
* another intro point and try again. */
|
2005-11-15 04:05:23 +01:00
|
|
|
extend_info_t *extend_info;
|
2005-06-29 23:46:55 +02:00
|
|
|
int result;
|
2006-07-26 21:05:34 +02:00
|
|
|
extend_info = rend_client_get_random_intro(circ->rend_query);
|
2005-11-15 04:05:23 +01:00
|
|
|
if (!extend_info) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND, "No introduction points left for %s. Closing.",
|
2006-07-26 21:05:34 +02:00
|
|
|
escaped_safe_str(circ->rend_query));
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
|
2004-04-18 09:37:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"Got nack for %s from %s. Re-extending circ %d, "
|
|
|
|
"this time to %s.",
|
2006-07-26 21:05:34 +02:00
|
|
|
escaped_safe_str(circ->rend_query),
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->build_state->chosen_exit->nickname, circ->_base.n_circ_id,
|
2006-02-13 11:33:00 +01:00
|
|
|
extend_info->nickname);
|
2005-11-15 04:05:23 +01:00
|
|
|
result = circuit_extend_to_new_exit(circ, extend_info);
|
|
|
|
extend_info_free(extend_info);
|
2005-06-29 23:46:55 +02:00
|
|
|
return result;
|
2004-04-13 04:31:52 +02:00
|
|
|
}
|
2004-04-13 01:33:47 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** If we are not currently fetching a rendezvous service descriptor
|
2004-05-10 06:34:48 +02:00
|
|
|
* for the service ID <b>query</b>, start a directory connection to fetch a
|
2004-05-05 23:32:43 +02:00
|
|
|
* new one.
|
|
|
|
*/
|
2004-04-18 09:37:16 +02:00
|
|
|
void
|
2004-05-05 23:32:43 +02:00
|
|
|
rend_client_refetch_renddesc(const char *query)
|
2004-04-18 09:37:16 +02:00
|
|
|
{
|
2006-02-19 23:02:02 +01:00
|
|
|
if (!get_options()->FetchHidServDescriptors)
|
|
|
|
return;
|
2005-01-20 00:15:59 +01:00
|
|
|
if (connection_get_by_type_state_rendquery(CONN_TYPE_DIR, 0, query)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"Would fetch a new renddesc here (for %s), but one is "
|
2006-03-06 01:25:39 +01:00
|
|
|
"already in progress.", escaped_safe_str(query));
|
2004-04-18 09:37:16 +02:00
|
|
|
} else {
|
|
|
|
/* not one already; initiate a dir rend desc lookup */
|
2007-07-22 02:16:48 +02:00
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC,
|
|
|
|
ROUTER_PURPOSE_GENERAL, query, 1);
|
2004-04-18 09:37:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-17 05:16:59 +01:00
|
|
|
/** Remove failed_intro from ent. If ent now has no intro points, or
|
2004-04-18 09:37:16 +02:00
|
|
|
* service is unrecognized, then launch a new renddesc fetch.
|
|
|
|
*
|
|
|
|
* Return -1 if error, 0 if no intro points remain or service
|
|
|
|
* unrecognized, 1 if recognized and some intro points remain.
|
|
|
|
*/
|
|
|
|
int
|
2005-06-29 23:46:55 +02:00
|
|
|
rend_client_remove_intro_point(extend_info_t *failed_intro, const char *query)
|
2004-04-18 09:37:16 +02:00
|
|
|
{
|
|
|
|
int i, r;
|
|
|
|
rend_cache_entry_t *ent;
|
2005-01-20 00:15:59 +01:00
|
|
|
connection_t *conn;
|
2004-04-18 09:37:16 +02:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
r = rend_cache_lookup_entry(query, -1, &ent);
|
2004-04-18 09:37:16 +02:00
|
|
|
if (r<0) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "Malformed service ID %s.", escaped_safe_str(query));
|
2004-04-18 09:37:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r==0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
|
2006-03-06 01:25:39 +01:00
|
|
|
escaped_safe_str(query));
|
2004-04-18 09:37:16 +02:00
|
|
|
rend_client_refetch_renddesc(query);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
if (ent->parsed->intro_point_extend_info) {
|
|
|
|
for (i=0; i < ent->parsed->n_intro_points; ++i) {
|
|
|
|
if (!memcmp(failed_intro->identity_digest,
|
|
|
|
ent->parsed->intro_point_extend_info[i]->identity_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
tor_assert(!strcmp(ent->parsed->intro_points[i],
|
|
|
|
ent->parsed->intro_point_extend_info[i]->nickname));
|
|
|
|
tor_free(ent->parsed->intro_points[i]);
|
|
|
|
extend_info_free(ent->parsed->intro_point_extend_info[i]);
|
|
|
|
--ent->parsed->n_intro_points;
|
|
|
|
ent->parsed->intro_points[i] =
|
|
|
|
ent->parsed->intro_points[ent->parsed->n_intro_points];
|
|
|
|
ent->parsed->intro_point_extend_info[i] =
|
|
|
|
ent->parsed->intro_point_extend_info[ent->parsed->n_intro_points];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i=0; i < ent->parsed->n_intro_points; ++i) {
|
|
|
|
if (!strcasecmp(ent->parsed->intro_points[i], failed_intro->nickname)) {
|
|
|
|
tor_free(ent->parsed->intro_points[i]);
|
|
|
|
ent->parsed->intro_points[i] =
|
|
|
|
ent->parsed->intro_points[--ent->parsed->n_intro_points];
|
|
|
|
break;
|
|
|
|
}
|
2004-04-18 09:37:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!ent->parsed->n_intro_points) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"No more intro points remain for %s. Re-fetching descriptor.",
|
2006-03-06 01:25:39 +01:00
|
|
|
escaped_safe_str(query));
|
2004-04-18 09:37:16 +02:00
|
|
|
rend_client_refetch_renddesc(query);
|
2005-01-20 00:15:59 +01:00
|
|
|
|
|
|
|
/* move all pending streams back to renddesc_wait */
|
|
|
|
while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
|
|
|
|
AP_CONN_STATE_CIRCUIT_WAIT, query))) {
|
|
|
|
conn->state = AP_CONN_STATE_RENDDESC_WAIT;
|
|
|
|
}
|
|
|
|
|
2004-04-18 09:37:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"%d options left for %s.",
|
2006-03-06 01:25:39 +01:00
|
|
|
ent->parsed->n_intro_points, escaped_safe_str(query));
|
2004-04-18 09:37:16 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
|
2004-04-06 05:44:36 +02:00
|
|
|
* the circuit to C_REND_READY.
|
|
|
|
*/
|
2004-04-05 02:47:48 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_rendezvous_acked(origin_circuit_t *circ, const char *request,
|
2005-12-14 21:40:40 +01:00
|
|
|
size_t request_len)
|
2004-04-05 02:47:48 +02:00
|
|
|
{
|
2006-06-05 00:42:13 +02:00
|
|
|
(void) request;
|
|
|
|
(void) request_len;
|
2004-04-05 02:47:48 +02:00
|
|
|
/* we just got an ack for our establish-rendezvous. switch purposes. */
|
2006-07-23 09:37:35 +02:00
|
|
|
if (circ->_base.purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. "
|
|
|
|
"Closing circ.");
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
|
2004-04-05 02:47:48 +02:00
|
|
|
return -1;
|
2004-04-03 05:06:06 +02:00
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"Got rendezvous ack. This circuit is now ready for "
|
|
|
|
"rendezvous.");
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY;
|
2004-04-05 02:47:48 +02:00
|
|
|
return 0;
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Bob sent us a rendezvous cell; join the circuits. */
|
2004-04-05 09:41:31 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
rend_client_receive_rendezvous(origin_circuit_t *circ, const char *request,
|
2005-12-14 21:40:40 +01:00
|
|
|
size_t request_len)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
2004-04-05 22:53:50 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if ((circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY &&
|
|
|
|
circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
|
2004-04-14 07:18:21 +02:00
|
|
|
|| !circ->build_state->pending_final_cpath) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not "
|
|
|
|
"expecting it. Closing.");
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
|
2004-04-05 09:41:31 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-04-05 22:53:50 +02:00
|
|
|
if (request_len != DH_KEY_LEN+DIGEST_LEN) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",
|
|
|
|
(int)request_len);
|
2004-04-05 22:53:50 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-04-05 22:53:50 +02:00
|
|
|
/* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ->build_state);
|
|
|
|
tor_assert(circ->build_state->pending_final_cpath);
|
2004-04-05 22:53:50 +02:00
|
|
|
hop = circ->build_state->pending_final_cpath;
|
2005-05-03 00:35:18 +02:00
|
|
|
tor_assert(hop->dh_handshake_state);
|
|
|
|
if (crypto_dh_compute_secret(hop->dh_handshake_state, request, DH_KEY_LEN,
|
2004-04-05 22:53:50 +02:00
|
|
|
keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
|
2004-04-05 22:53:50 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* ... and set up cpath. */
|
|
|
|
if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Check whether the digest is right... */
|
2004-04-05 23:39:18 +02:00
|
|
|
if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
|
2004-04-05 22:53:50 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
crypto_dh_free(hop->dh_handshake_state);
|
|
|
|
hop->dh_handshake_state = NULL;
|
2004-04-06 22:23:58 +02:00
|
|
|
|
2004-04-05 22:53:50 +02:00
|
|
|
/* All is well. Extend the circuit. */
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
|
2004-04-06 01:40:59 +02:00
|
|
|
hop->state = CPATH_STATE_OPEN;
|
2004-04-06 23:25:11 +02:00
|
|
|
/* set the windows to default. these are the windows
|
|
|
|
* that alice thinks bob has.
|
|
|
|
*/
|
|
|
|
hop->package_window = CIRCWINDOW_START;
|
|
|
|
hop->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
2004-04-05 22:53:50 +02:00
|
|
|
onion_append_to_cpath(&circ->cpath, hop);
|
|
|
|
circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
|
2004-04-05 09:41:31 +02:00
|
|
|
return 0;
|
2004-04-05 22:53:50 +02:00
|
|
|
err:
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
|
2004-04-05 22:53:50 +02:00
|
|
|
return -1;
|
2004-04-05 09:41:31 +02:00
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
|
2005-01-20 00:15:59 +01:00
|
|
|
* are waiting on query. If there's a working cache entry here
|
|
|
|
* with at least one intro point, move them to the next state;
|
|
|
|
* else fail them.
|
2004-04-03 01:38:26 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-06-29 23:46:55 +02:00
|
|
|
rend_client_desc_here(const char *query)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2004-04-08 00:41:00 +02:00
|
|
|
rend_cache_entry_t *entry;
|
2005-04-27 00:36:00 +02:00
|
|
|
time_t now = time(NULL);
|
2006-01-17 05:16:59 +01:00
|
|
|
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, _conn,
|
|
|
|
{
|
|
|
|
if (_conn->type != CONN_TYPE_AP ||
|
|
|
|
_conn->state != AP_CONN_STATE_RENDDESC_WAIT ||
|
|
|
|
_conn->marked_for_close)
|
2006-07-26 21:07:26 +02:00
|
|
|
continue;
|
2007-05-22 17:49:14 +02:00
|
|
|
conn = TO_EDGE_CONN(_conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
if (rend_cmp_service_ids(query, conn->rend_query))
|
2006-01-17 05:16:59 +01:00
|
|
|
continue;
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_ok(TO_CONN(conn), now);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (rend_cache_lookup_entry(conn->rend_query, -1, &entry) == 1 &&
|
2005-01-20 00:15:59 +01:00
|
|
|
entry->parsed->n_intro_points > 0) {
|
2004-04-08 00:41:00 +02:00
|
|
|
/* either this fetch worked, or it failed but there was a
|
|
|
|
* valid entry from before which we should reuse */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"Rend desc is usable. Launching circuits.");
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
|
2005-04-27 00:36:00 +02:00
|
|
|
|
|
|
|
/* restart their timeout values, so they get a fair shake at
|
|
|
|
* connecting to the hidden service. */
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.timestamp_created = now;
|
|
|
|
conn->_base.timestamp_lastread = now;
|
|
|
|
conn->_base.timestamp_lastwritten = now;
|
2005-04-27 00:36:00 +02:00
|
|
|
|
2004-04-03 01:38:26 +02:00
|
|
|
if (connection_ap_handshake_attach_circuit(conn) < 0) {
|
|
|
|
/* it will never work */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND,"Rendezvous attempt failed. Closing.");
|
2005-04-03 00:11:24 +02:00
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
|
2004-04-03 01:38:26 +02:00
|
|
|
}
|
2004-04-08 00:41:00 +02:00
|
|
|
} else { /* 404, or fetch didn't get that far */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_notice(LD_REND,"Closing stream for '%s.onion': hidden service is "
|
|
|
|
"unavailable (try again later).", safe_str(query));
|
2007-03-09 09:48:53 +01:00
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_RESOLVEFAILED);
|
2004-04-03 01:38:26 +02:00
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** Return a newly allocated extend_info_t* for a randomly chosen introduction
|
|
|
|
* point for the named hidden service. Return NULL if all introduction points
|
|
|
|
* have been tried and failed.
|
2004-04-05 02:47:48 +02:00
|
|
|
*/
|
2005-06-29 23:46:55 +02:00
|
|
|
extend_info_t *
|
|
|
|
rend_client_get_random_intro(const char *query)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-04-05 02:47:48 +02:00
|
|
|
int i;
|
2004-04-08 04:11:49 +02:00
|
|
|
rend_cache_entry_t *entry;
|
2004-04-05 02:47:48 +02:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
if (rend_cache_lookup_entry(query, -1, &entry) < 1) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND,
|
|
|
|
"Query '%s' didn't have valid rend desc in cache. Failing.",
|
|
|
|
safe_str(query));
|
2004-04-05 02:47:48 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
again:
|
|
|
|
if (!entry->parsed->n_intro_points)
|
|
|
|
return NULL;
|
2004-04-05 02:47:48 +02:00
|
|
|
|
2005-10-07 00:18:01 +02:00
|
|
|
i = crypto_rand_int(entry->parsed->n_intro_points);
|
2004-04-05 02:47:48 +02:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
if (entry->parsed->intro_point_extend_info) {
|
|
|
|
return extend_info_dup(entry->parsed->intro_point_extend_info[i]);
|
|
|
|
} else {
|
|
|
|
/* add the intro point nicknames */
|
|
|
|
char *choice = entry->parsed->intro_points[i];
|
2005-10-05 00:23:31 +02:00
|
|
|
routerinfo_t *router = router_get_by_nickname(choice, 0);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (!router) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND, "Unknown router with nickname '%s'; trying another.",
|
|
|
|
choice);
|
2005-06-29 23:46:55 +02:00
|
|
|
tor_free(choice);
|
|
|
|
entry->parsed->intro_points[i] =
|
|
|
|
entry->parsed->intro_points[--entry->parsed->n_intro_points];
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
return extend_info_from_router(router);
|
2004-04-08 05:18:03 +02:00
|
|
|
}
|
2004-04-05 02:47:48 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|