2004-04-03 00:23:15 +02:00
|
|
|
/* Copyright 2004 Roger Dingledine */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
|
|
|
/* send the introduce cell */
|
|
|
|
void
|
2004-04-05 02:47:48 +02:00
|
|
|
rend_client_introcirc_is_open(circuit_t *circ)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
2004-04-05 02:47:48 +02:00
|
|
|
assert(circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
|
2004-04-08 04:24:06 +02:00
|
|
|
assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath);
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
log_fn(LOG_INFO,"introcirc is open");
|
|
|
|
connection_ap_attach_pending();
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
/* send the establish-rendezvous cell. if it fails, mark
|
|
|
|
* the circ for close and return -1. else return 0.
|
|
|
|
*/
|
2004-04-03 06:22:22 +02:00
|
|
|
int
|
|
|
|
rend_client_send_establish_rendezvous(circuit_t *circ)
|
|
|
|
{
|
|
|
|
assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
|
|
|
|
log_fn(LOG_INFO, "Sending an ESTABLISH_RENDEZVOUS cell");
|
|
|
|
|
|
|
|
if (crypto_rand(REND_COOKIE_LEN, circ->rend_cookie)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't get random cookie");
|
2004-04-05 09:41:31 +02:00
|
|
|
circuit_mark_for_close(circ);
|
2004-04-03 06:22:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connection_edge_send_command(NULL,circ,
|
|
|
|
RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
|
|
|
|
circ->rend_cookie, REND_COOKIE_LEN,
|
|
|
|
circ->cpath->prev)<0) {
|
2004-04-05 09:41:31 +02:00
|
|
|
/* circ is already marked for close */
|
2004-04-03 06:22:22 +02:00
|
|
|
log_fn(LOG_WARN, "Couldn't send ESTABLISH_RENDEZVOUS cell");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
/* Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
|
|
|
|
* down introcirc if possible.
|
|
|
|
*/
|
2004-04-05 09:41:31 +02:00
|
|
|
int
|
|
|
|
rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc) {
|
2004-04-08 00:41:00 +02:00
|
|
|
int payload_len, r;
|
2004-04-05 23:15:14 +02:00
|
|
|
char payload[RELAY_PAYLOAD_SIZE];
|
2004-04-06 05:44:36 +02:00
|
|
|
char tmp[(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+DH_KEY_LEN];
|
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;
|
2004-04-05 09:41:31 +02:00
|
|
|
|
|
|
|
assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
|
|
|
|
assert(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY);
|
|
|
|
assert(!rend_cmp_service_ids(introcirc->rend_query, rendcirc->rend_query));
|
|
|
|
|
2004-04-08 00:41:00 +02:00
|
|
|
if(rend_cache_lookup_entry(introcirc->rend_query, &entry) < 1) {
|
2004-04-05 09:41:31 +02:00
|
|
|
log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.",
|
|
|
|
introcirc->rend_query);
|
|
|
|
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) {
|
2004-04-05 09:41:31 +02:00
|
|
|
log_fn(LOG_WARN, "Couldn't hash public key.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-05 22:30:53 +02:00
|
|
|
/* Initialize the pending_final_cpath and start the DH handshake. */
|
|
|
|
cpath = rendcirc->build_state->pending_final_cpath =
|
|
|
|
tor_malloc_zero(sizeof(crypt_path_t));
|
|
|
|
if (!(cpath->handshake_state = crypto_dh_new())) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't allocate DH");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (crypto_dh_generate_public(cpath->handshake_state)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't generate g^x");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
/* write the remaining items into tmp */
|
2004-04-06 05:44:36 +02:00
|
|
|
strncpy(tmp, rendcirc->build_state->chosen_exit, (MAX_NICKNAME_LEN+1)); /* nul pads */
|
|
|
|
memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie, REND_COOKIE_LEN);
|
|
|
|
if (crypto_dh_get_public(cpath->handshake_state,
|
|
|
|
tmp+MAX_NICKNAME_LEN+1+REND_COOKIE_LEN,
|
|
|
|
DH_KEY_LEN)<0) {
|
2004-04-05 22:30:53 +02:00
|
|
|
log_fn(LOG_WARN, "Couldn't extract g^x");
|
|
|
|
goto err;
|
|
|
|
}
|
2004-04-05 09:41:31 +02:00
|
|
|
|
2004-04-08 00:41:00 +02:00
|
|
|
r = crypto_pk_public_hybrid_encrypt(entry->parsed->pk, tmp,
|
2004-04-06 05:44:36 +02:00
|
|
|
MAX_NICKNAME_LEN+1+REND_COOKIE_LEN+DH_KEY_LEN,
|
|
|
|
payload+DIGEST_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) {
|
2004-04-05 09:41:31 +02:00
|
|
|
log_fn(LOG_WARN,"hybrid pk encrypt failed.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
payload_len = DIGEST_LEN + r;
|
2004-04-05 23:15:14 +02:00
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
if (connection_edge_send_command(NULL, introcirc,
|
|
|
|
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. */
|
|
|
|
log_fn(LOG_WARN, "Couldn't send INTRODUCE1 cell");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-05 17:17:34 +02:00
|
|
|
/* we don't need it anymore, plus it's been used. send the destroy. */
|
|
|
|
circuit_mark_for_close(introcirc);
|
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
circuit_mark_for_close(introcirc);
|
|
|
|
circuit_mark_for_close(rendcirc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-03 00:23:15 +02:00
|
|
|
/* send the rendezvous cell */
|
|
|
|
void
|
2004-04-05 02:47:48 +02:00
|
|
|
rend_client_rendcirc_is_open(circuit_t *circ)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
2004-04-03 05:06:06 +02:00
|
|
|
assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
|
2004-04-08 04:24:06 +02:00
|
|
|
assert(CIRCUIT_IS_ORIGIN(circ));
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
log_fn(LOG_INFO,"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;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_ap_attach_pending();
|
|
|
|
}
|
2004-04-03 05:06:06 +02:00
|
|
|
|
2004-04-06 22:25:18 +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
|
|
|
|
rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len)
|
|
|
|
{
|
|
|
|
/* we just got an ack for our establish-rendezvous. switch purposes. */
|
|
|
|
if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
|
|
|
|
log_fn(LOG_WARN,"Got a rendezvous ack when we weren't expecting one. Closing circ.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return -1;
|
2004-04-03 05:06:06 +02:00
|
|
|
}
|
2004-04-05 09:41:31 +02:00
|
|
|
log_fn(LOG_INFO,"Got rendezvous ack. This circuit is now ready for rendezvous.");
|
2004-04-05 02:47:48 +02:00
|
|
|
circ->purpose = CIRCUIT_PURPOSE_C_REND_READY;
|
|
|
|
return 0;
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* bob sent us a rendezvous cell, join the circs. */
|
2004-04-05 09:41:31 +02:00
|
|
|
int
|
|
|
|
rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
2004-04-05 09:41:31 +02:00
|
|
|
connection_t *apconn;
|
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
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
if(circ->purpose != CIRCUIT_PURPOSE_C_REND_READY ||
|
|
|
|
!circ->build_state->pending_final_cpath) {
|
|
|
|
log_fn(LOG_WARN,"Got rendezvous2 cell from Bob, but not expecting it. Closing.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
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) {
|
|
|
|
log_fn(LOG_WARN,"Incorrect length (%d) on RENDEZVOUS2 cell.",request_len);
|
|
|
|
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...*/
|
|
|
|
assert(circ->build_state && circ->build_state->pending_final_cpath);
|
|
|
|
hop = circ->build_state->pending_final_cpath;
|
|
|
|
assert(hop->handshake_state);
|
|
|
|
if (crypto_dh_compute_secret(hop->handshake_state, request, DH_KEY_LEN,
|
|
|
|
keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
|
|
|
|
log_fn(LOG_WARN, "Couldn't complete DH handshake");
|
|
|
|
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)) {
|
2004-04-05 22:53:50 +02:00
|
|
|
log_fn(LOG_WARN, "Incorrect digest of key material");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-04-06 22:23:58 +02:00
|
|
|
crypto_dh_free(hop->handshake_state);
|
|
|
|
hop->handshake_state = NULL;
|
|
|
|
|
2004-04-05 22:53:50 +02:00
|
|
|
/* All is well. Extend the circuit. */
|
2004-04-05 09:41:31 +02:00
|
|
|
circ->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
|
|
|
for(apconn = circ->p_streams; apconn; apconn = apconn->next_stream) {
|
2004-04-06 00:01:35 +02:00
|
|
|
apconn->cpath_layer = circ->cpath->prev;
|
|
|
|
/* now the last hop is different. be sure to send all the way. */
|
2004-04-05 09:41:31 +02:00
|
|
|
if(connection_ap_handshake_send_begin(apconn, circ) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2004-04-05 22:53:50 +02:00
|
|
|
err:
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return -1;
|
2004-04-05 09:41:31 +02:00
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-04-05 09:41:31 +02:00
|
|
|
/* Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
|
2004-04-03 01:38:26 +02:00
|
|
|
* are waiting on query. If success==1, move them to the next state.
|
|
|
|
* If success==0, fail them.
|
|
|
|
*/
|
|
|
|
void rend_client_desc_fetched(char *query, int success) {
|
|
|
|
connection_t **carray;
|
|
|
|
connection_t *conn;
|
|
|
|
int n, i;
|
2004-04-08 00:41:00 +02:00
|
|
|
rend_cache_entry_t *entry;
|
2004-04-03 01:38:26 +02:00
|
|
|
|
|
|
|
get_connection_array(&carray, &n);
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
conn = carray[i];
|
|
|
|
if (conn->type != CONN_TYPE_AP ||
|
2004-04-05 02:47:48 +02:00
|
|
|
conn->state != AP_CONN_STATE_RENDDESC_WAIT)
|
2004-04-03 01:38:26 +02:00
|
|
|
continue;
|
2004-04-03 01:54:48 +02:00
|
|
|
if (rend_cmp_service_ids(conn->rend_query, query))
|
2004-04-03 01:38:26 +02:00
|
|
|
continue;
|
|
|
|
/* great, this guy was waiting */
|
2004-04-08 00:41:00 +02:00
|
|
|
if(success ||
|
|
|
|
rend_cache_lookup_entry(conn->rend_query, &entry) == 1) {
|
|
|
|
/* either this fetch worked, or it failed but there was a
|
|
|
|
* valid entry from before which we should reuse */
|
2004-04-05 02:47:48 +02:00
|
|
|
log_fn(LOG_INFO,"Rend desc retrieved. Launching circuits.");
|
|
|
|
conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
|
2004-04-03 01:38:26 +02:00
|
|
|
if (connection_ap_handshake_attach_circuit(conn) < 0) {
|
|
|
|
/* it will never work */
|
|
|
|
log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
|
|
|
|
connection_mark_for_close(conn,0);
|
|
|
|
}
|
2004-04-08 00:41:00 +02:00
|
|
|
} else { /* 404, or fetch didn't get that far */
|
|
|
|
log_fn(LOG_WARN,"service id '%s' fetched failed, and not in cache. Closing conn.", query);
|
2004-04-03 01:38:26 +02:00
|
|
|
connection_mark_for_close(conn,0);
|
|
|
|
}
|
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2004-04-03 03:59:53 +02:00
|
|
|
int rend_cmp_service_ids(char *one, char *two) {
|
|
|
|
return strcasecmp(one,two);
|
|
|
|
}
|
|
|
|
|
2004-04-05 19:56:34 +02:00
|
|
|
/* strdup a nickname for a random introduction
|
2004-04-05 02:47:48 +02:00
|
|
|
* point of query. return NULL if error.
|
|
|
|
*/
|
2004-04-05 19:51:00 +02:00
|
|
|
char *rend_client_get_random_intro(char *query) {
|
2004-04-05 02:47:48 +02:00
|
|
|
int i;
|
|
|
|
smartlist_t *sl;
|
|
|
|
char *choice;
|
2004-04-05 19:56:34 +02:00
|
|
|
char *nickname;
|
2004-04-08 04:11:49 +02:00
|
|
|
rend_cache_entry_t *entry;
|
2004-04-05 02:47:48 +02:00
|
|
|
|
2004-04-08 04:11:49 +02:00
|
|
|
if(rend_cache_lookup_entry(query, &entry) < 1) {
|
2004-04-05 02:47:48 +02:00
|
|
|
log_fn(LOG_WARN,"query '%s' didn't have valid rend desc in cache. Failing.", query);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sl = smartlist_create();
|
|
|
|
|
|
|
|
/* add the intro point nicknames */
|
2004-04-08 04:11:49 +02:00
|
|
|
for(i=0;i<entry->parsed->n_intro_points;i++)
|
|
|
|
smartlist_add(sl,entry->parsed->intro_points[i]);
|
2004-04-05 02:47:48 +02:00
|
|
|
|
|
|
|
choice = smartlist_choose(sl);
|
2004-04-05 19:56:34 +02:00
|
|
|
nickname = tor_strdup(choice);
|
2004-04-05 02:47:48 +02:00
|
|
|
smartlist_free(sl);
|
2004-04-05 19:56:34 +02:00
|
|
|
return nickname;
|
2004-04-05 02:47:48 +02:00
|
|
|
}
|
|
|
|
|
2004-04-03 03:59:53 +02:00
|
|
|
/* If address is of the form "y.onion" with a well-formed handle y,
|
|
|
|
* then put a '\0' after y, lower-case it, and return 0.
|
|
|
|
* Else return -1 and change nothing.
|
|
|
|
*/
|
|
|
|
int rend_parse_rendezvous_address(char *address) {
|
|
|
|
char *s;
|
|
|
|
char query[REND_SERVICE_ID_LEN+1];
|
|
|
|
|
|
|
|
s = strrchr(address,'.');
|
|
|
|
if(!s) return -1; /* no dot */
|
|
|
|
if (strcasecmp(s+1,"onion"))
|
|
|
|
return -1; /* not .onion */
|
|
|
|
|
|
|
|
*s = 0; /* null terminate it */
|
|
|
|
if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
|
|
|
|
goto failed;
|
|
|
|
tor_strlower(query);
|
|
|
|
if(rend_valid_service_id(query)) {
|
|
|
|
tor_strlower(address);
|
|
|
|
return 0; /* success */
|
|
|
|
}
|
|
|
|
failed:
|
|
|
|
/* otherwise, return to previous state and return -1 */
|
|
|
|
*s = '.';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-03 00:23:15 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|