2004-03-31 04:07:38 +02:00
|
|
|
/* Copyright 2004 Roger Dingledine */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
2004-04-06 22:25:18 +02:00
|
|
|
/* Free the storage held by 'desc'.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2004-03-31 04:07:38 +02:00
|
|
|
void rend_service_descriptor_free(rend_service_descriptor_t *desc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (desc->pk)
|
|
|
|
crypto_free_pk_env(desc->pk);
|
|
|
|
if (desc->intro_points) {
|
|
|
|
for (i=0; i < desc->n_intro_points; ++i) {
|
|
|
|
tor_free(desc->intro_points[i]);
|
|
|
|
}
|
|
|
|
tor_free(desc->intro_points);
|
|
|
|
}
|
|
|
|
tor_free(desc);
|
|
|
|
}
|
|
|
|
|
2004-04-06 22:25:18 +02:00
|
|
|
/* Encode a service descriptor for 'desc', and sign it with 'key'. Store
|
|
|
|
* the descriptor in *str_out, and set *len_out to its length.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2004-03-31 04:07:38 +02:00
|
|
|
int
|
|
|
|
rend_encode_service_descriptor(rend_service_descriptor_t *desc,
|
2004-04-02 00:12:00 +02:00
|
|
|
crypto_pk_env_t *key,
|
|
|
|
char **str_out, int *len_out)
|
2004-03-31 04:07:38 +02:00
|
|
|
{
|
|
|
|
char *buf, *cp, *ipoint;
|
|
|
|
int i, keylen, asn1len;
|
|
|
|
keylen = crypto_pk_keysize(desc->pk);
|
|
|
|
buf = tor_malloc(keylen*2); /* XXXX */
|
|
|
|
asn1len = crypto_pk_asn1_encode(desc->pk, buf, keylen*2);
|
|
|
|
if (asn1len<0) {
|
|
|
|
tor_free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*len_out = 2 + asn1len + 4 + 2 + keylen;
|
|
|
|
for (i = 0; i < desc->n_intro_points; ++i) {
|
|
|
|
*len_out += strlen(desc->intro_points[i]) + 1;
|
|
|
|
}
|
|
|
|
cp = *str_out = tor_malloc(*len_out);
|
|
|
|
set_uint16(cp, (uint16_t)asn1len);
|
|
|
|
cp += 2;
|
|
|
|
memcpy(cp, buf, asn1len);
|
|
|
|
tor_free(buf);
|
|
|
|
cp += asn1len;
|
|
|
|
set_uint32(cp, (uint32_t)desc->timestamp);
|
|
|
|
cp += 4;
|
|
|
|
set_uint16(cp, (uint16_t)desc->n_intro_points);
|
|
|
|
cp += 2;
|
|
|
|
for (i=0; i < desc->n_intro_points; ++i) {
|
|
|
|
ipoint = (char*)desc->intro_points[i];
|
|
|
|
strcpy(cp, ipoint);
|
|
|
|
cp += strlen(ipoint)+1;
|
|
|
|
}
|
2004-04-02 00:12:00 +02:00
|
|
|
i = crypto_pk_private_sign_digest(key, *str_out, cp-*str_out, cp);
|
2004-03-31 04:07:38 +02:00
|
|
|
if (i<0) {
|
|
|
|
tor_free(*str_out);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp += i;
|
|
|
|
assert(*len_out == (cp-*str_out));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
/* malloc a service_descriptor_t and return it.
|
|
|
|
* return NULL if invalid descriptor or error */
|
2004-03-31 04:07:38 +02:00
|
|
|
rend_service_descriptor_t *rend_parse_service_descriptor(
|
2004-04-03 04:14:20 +02:00
|
|
|
const char *str, int len)
|
2004-03-31 04:07:38 +02:00
|
|
|
{
|
|
|
|
rend_service_descriptor_t *result = NULL;
|
|
|
|
int keylen, asn1len, i;
|
|
|
|
const char *end, *cp, *eos;
|
2004-04-02 00:12:00 +02:00
|
|
|
|
2004-03-31 04:07:38 +02:00
|
|
|
result = tor_malloc_zero(sizeof(rend_service_descriptor_t));
|
|
|
|
cp = str;
|
|
|
|
end = str+len;
|
|
|
|
if (end-cp < 2) goto truncated;
|
|
|
|
asn1len = get_uint16(cp);
|
|
|
|
cp += 2;
|
|
|
|
if (end-cp < asn1len) goto truncated;
|
|
|
|
result->pk = crypto_pk_asn1_decode(cp, asn1len);
|
|
|
|
if (!result->pk) goto truncated;
|
|
|
|
cp += asn1len;
|
|
|
|
if (end-cp < 4) goto truncated;
|
|
|
|
result->timestamp = (time_t) get_uint32(cp);
|
|
|
|
cp += 4;
|
|
|
|
if (end-cp < 2) goto truncated;
|
|
|
|
result->n_intro_points = get_uint16(cp);
|
2004-04-05 02:47:48 +02:00
|
|
|
if(result->n_intro_points < 1) {
|
|
|
|
log_fn(LOG_WARN,"Service descriptor listed no intro points.");
|
|
|
|
goto error;
|
|
|
|
}
|
2004-03-31 04:07:38 +02:00
|
|
|
result->intro_points = tor_malloc_zero(sizeof(char*)*result->n_intro_points);
|
|
|
|
cp += 2;
|
|
|
|
for (i=0;i<result->n_intro_points;++i) {
|
|
|
|
if (end-cp < 2) goto truncated;
|
|
|
|
eos = (const char *)memchr(cp,'\0',end-cp);
|
|
|
|
if (!eos) goto truncated;
|
|
|
|
result->intro_points[i] = tor_strdup(cp);
|
|
|
|
cp = eos+1;
|
|
|
|
}
|
|
|
|
keylen = crypto_pk_keysize(result->pk);
|
2004-04-03 05:37:11 +02:00
|
|
|
if (end-cp < keylen) goto truncated;
|
|
|
|
if (end-cp > keylen) {
|
|
|
|
log_fn(LOG_WARN, "Signature too long on service descriptor");
|
|
|
|
goto error;
|
|
|
|
}
|
2004-04-02 00:12:00 +02:00
|
|
|
if (crypto_pk_public_checksig_digest(result->pk,
|
2004-04-03 04:14:20 +02:00
|
|
|
(char*)str,cp-str, /* data */
|
|
|
|
(char*)cp,end-cp /* signature*/
|
|
|
|
)<0) {
|
2004-04-02 00:12:00 +02:00
|
|
|
log_fn(LOG_WARN, "Bad signature on service descriptor");
|
2004-03-31 04:07:38 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
truncated:
|
|
|
|
log_fn(LOG_WARN, "Truncated service descriptor");
|
|
|
|
error:
|
|
|
|
rend_service_descriptor_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
/* Sets out to the first 10 bytes of the digest of 'pk', base32 encoded.
|
|
|
|
* NUL-terminates out.
|
|
|
|
*/
|
2004-03-31 05:42:56 +02:00
|
|
|
int rend_get_service_id(crypto_pk_env_t *pk, char *out)
|
|
|
|
{
|
2004-04-03 04:40:30 +02:00
|
|
|
char buf[DIGEST_LEN];
|
2004-03-31 05:42:56 +02:00
|
|
|
assert(pk);
|
|
|
|
if (crypto_pk_get_digest(pk, buf) < 0)
|
|
|
|
return -1;
|
|
|
|
if (base32_encode(out, REND_SERVICE_ID_LEN+1, buf, 10) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-31 06:10:10 +02:00
|
|
|
/* ==== Rendezvous service descriptor cache. */
|
|
|
|
#define REND_CACHE_MAX_AGE 24*60*60
|
|
|
|
#define REND_CACHE_MAX_SKEW 60*60
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2004-03-31 06:10:10 +02:00
|
|
|
typedef struct rend_cache_entry_t {
|
2004-04-06 05:44:36 +02:00
|
|
|
int len; /* Length of desc */
|
|
|
|
char *desc; /* Service descriptor */
|
2004-04-06 22:25:18 +02:00
|
|
|
rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
|
2004-03-31 06:10:10 +02:00
|
|
|
} rend_cache_entry_t;
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2004-03-31 06:10:10 +02:00
|
|
|
static strmap_t *rend_cache = NULL;
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
/* Initializes the service descriptor cache.
|
|
|
|
*/
|
2004-03-31 06:10:10 +02:00
|
|
|
void rend_cache_init(void)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache = strmap_new();
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
/* Removes all old entries from the service descriptor cache.
|
|
|
|
*/
|
2004-03-31 06:10:10 +02:00
|
|
|
void rend_cache_clean(void)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
|
|
|
strmap_iter_t *iter;
|
|
|
|
const char *key;
|
|
|
|
void *val;
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache_entry_t *ent;
|
2004-03-31 05:42:56 +02:00
|
|
|
time_t cutoff;
|
2004-03-31 06:10:10 +02:00
|
|
|
cutoff = time(NULL) - REND_CACHE_MAX_AGE;
|
|
|
|
for (iter = strmap_iter_init(rend_cache); !strmap_iter_done(iter); ) {
|
2004-03-31 05:42:56 +02:00
|
|
|
strmap_iter_get(iter, &key, &val);
|
2004-03-31 06:10:10 +02:00
|
|
|
ent = (rend_cache_entry_t*)val;
|
2004-03-31 05:42:56 +02:00
|
|
|
if (ent->parsed->timestamp < cutoff) {
|
2004-03-31 06:10:10 +02:00
|
|
|
iter = strmap_iter_next_rmv(rend_cache, iter);
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_free(ent->parsed);
|
|
|
|
tor_free(ent->desc);
|
|
|
|
tor_free(ent);
|
|
|
|
} else {
|
2004-03-31 06:10:10 +02:00
|
|
|
iter = strmap_iter_next(rend_cache, iter);
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-01 21:39:11 +02:00
|
|
|
/* return 1 if query is a valid service id, else return 0. */
|
|
|
|
int rend_valid_service_id(char *query) {
|
|
|
|
if(strlen(query) != REND_SERVICE_ID_LEN)
|
|
|
|
return 0;
|
|
|
|
|
2004-04-03 01:30:54 +02:00
|
|
|
if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN)
|
|
|
|
return 0;
|
|
|
|
|
2004-04-01 21:39:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'query' is a base-32'ed service id. If it's malformed, return -1.
|
|
|
|
* Else look it up.
|
|
|
|
* If it is found, point *desc to it, and write its length into
|
|
|
|
* *desc_len, and return 1.
|
|
|
|
* If it is not found, return 0.
|
|
|
|
*/
|
2004-03-31 06:10:10 +02:00
|
|
|
int rend_cache_lookup(char *query, const char **desc, int *desc_len)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache_entry_t *e;
|
|
|
|
assert(rend_cache);
|
2004-04-01 21:39:11 +02:00
|
|
|
if (!rend_valid_service_id(query))
|
|
|
|
return -1;
|
2004-03-31 06:10:10 +02:00
|
|
|
e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, query);
|
2004-03-31 05:42:56 +02:00
|
|
|
if (!e)
|
|
|
|
return 0;
|
|
|
|
*desc = e->desc;
|
|
|
|
*desc_len = e->len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-04-01 21:39:11 +02:00
|
|
|
/* Calculate desc's service id, and store it.
|
|
|
|
* Return -1 if it's malformed or otherwise rejected, else return 0.
|
|
|
|
*/
|
2004-03-31 06:10:10 +02:00
|
|
|
int rend_cache_store(char *desc, int desc_len)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache_entry_t *e;
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_t *parsed;
|
|
|
|
char query[REND_SERVICE_ID_LEN+1];
|
|
|
|
time_t now;
|
2004-03-31 06:10:10 +02:00
|
|
|
assert(rend_cache);
|
2004-03-31 05:42:56 +02:00
|
|
|
parsed = rend_parse_service_descriptor(desc,desc_len);
|
|
|
|
if (!parsed) {
|
|
|
|
log_fn(LOG_WARN,"Couldn't parse service descriptor");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (rend_get_service_id(parsed->pk, query)<0) {
|
|
|
|
log_fn(LOG_WARN,"Couldn't compute service ID");
|
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
now = time(NULL);
|
2004-03-31 06:10:10 +02:00
|
|
|
if (parsed->timestamp < now-REND_CACHE_MAX_AGE) {
|
2004-03-31 05:42:56 +02:00
|
|
|
log_fn(LOG_WARN,"Service descriptor is too old");
|
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-31 06:10:10 +02:00
|
|
|
if (parsed->timestamp > now+REND_CACHE_MAX_SKEW) {
|
2004-03-31 05:42:56 +02:00
|
|
|
log_fn(LOG_WARN,"Service descriptor is too far in the future");
|
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-31 06:10:10 +02:00
|
|
|
e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, query);
|
2004-03-31 05:42:56 +02:00
|
|
|
if (e && e->parsed->timestamp > parsed->timestamp) {
|
|
|
|
log_fn(LOG_WARN,"We already have a newer service descriptor with the same ID");
|
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (e && e->len == desc_len && !memcmp(desc,e->desc,desc_len)) {
|
|
|
|
log_fn(LOG_WARN,"We already have this service descriptor");
|
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!e) {
|
2004-03-31 06:10:10 +02:00
|
|
|
e = tor_malloc_zero(sizeof(rend_cache_entry_t));
|
|
|
|
strmap_set_lc(rend_cache, query, e);
|
2004-03-31 05:42:56 +02:00
|
|
|
} else {
|
|
|
|
rend_service_descriptor_free(e->parsed);
|
|
|
|
tor_free(e->desc);
|
|
|
|
}
|
|
|
|
e->parsed = parsed;
|
|
|
|
e->len = desc_len;
|
2004-04-03 05:39:31 +02:00
|
|
|
e->desc = tor_malloc(desc_len);
|
|
|
|
memcpy(e->desc, desc, desc_len);
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2004-04-03 05:33:57 +02:00
|
|
|
log_fn(LOG_INFO,"Successfully stored rend desc '%s', len %d", query, desc_len);
|
2004-03-31 05:42:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-03 05:37:11 +02:00
|
|
|
/* Dispatch on rendezvous relay command. */
|
|
|
|
void rend_process_relay_cell(circuit_t *circ, int command, int length,
|
|
|
|
const char *payload)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
switch(command) {
|
|
|
|
case RELAY_COMMAND_ESTABLISH_INTRO:
|
|
|
|
r = rend_mid_establish_intro(circ,payload,length);
|
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
|
|
|
|
r = rend_mid_establish_rendezvous(circ,payload,length);
|
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_INTRODUCE1:
|
|
|
|
r = rend_mid_introduce(circ,payload,length);
|
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_INTRODUCE2:
|
|
|
|
r = rend_service_introduce(circ,payload,length);
|
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS1:
|
|
|
|
r = rend_mid_rendezvous(circ,payload,length);
|
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS2:
|
2004-04-05 09:41:31 +02:00
|
|
|
r = rend_client_receive_rendezvous(circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
2004-04-03 06:55:22 +02:00
|
|
|
case RELAY_COMMAND_INTRO_ESTABLISHED:
|
|
|
|
r = rend_service_intro_established(circ,payload,length);
|
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
|
2004-04-05 02:47:48 +02:00
|
|
|
r = rend_client_rendezvous_acked(circ,payload,length);
|
2004-04-03 06:55:22 +02:00
|
|
|
break;
|
2004-04-03 05:37:11 +02:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-02 00:12:00 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|