aboutsummaryrefslogtreecommitdiff
path: root/src/or/rendcommon.c
blob: 83b19672f9271ffe26a59b6467586936641da569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Copyright 2004 Roger Dingledine */
/* See LICENSE for licensing information */
/* $Id$ */

#include "or.h"

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);
}

int
rend_encode_service_descriptor(rend_service_descriptor_t *desc,
                               crypto_pk_env_t *key,
                               char **str_out, int *len_out)
{
  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;
  }
  i = crypto_pk_private_sign_digest(key, *str_out, cp-*str_out, cp);
  if (i<0) {
    tor_free(*str_out);
    return -1;
  }
  cp += i;
  assert(*len_out == (cp-*str_out));
  return 0;
}

rend_service_descriptor_t *rend_parse_service_descriptor(
                           const char *str, int len)
{
  rend_service_descriptor_t *result = NULL;
  int keylen, asn1len, i;
  const char *end, *cp, *eos;

  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);
  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);
  if (end-cp < keylen) goto truncated;
  if (end-cp > keylen) {
    log_fn(LOG_WARN, "Signature too long on service descriptor");
    goto error;
  }
  if (crypto_pk_public_checksig_digest(result->pk,
                                       (char*)str,cp-str, /* data */
                                       (char*)cp,end-cp  /* signature*/
                                       )<0) {
    log_fn(LOG_WARN, "Bad signature on service descriptor");
    goto error;
  }

  return result;
 truncated:
  log_fn(LOG_WARN, "Truncated service descriptor");
 error:
  rend_service_descriptor_free(result);
  return NULL;
}

int rend_get_service_id(crypto_pk_env_t *pk, char *out)
{
  char buf[DIGEST_LEN];
  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;
}

/* ==== Rendezvous service descriptor cache. */
#define REND_CACHE_MAX_AGE 24*60*60
#define REND_CACHE_MAX_SKEW 60*60

typedef struct rend_cache_entry_t {
  int len;
  char *desc;
  rend_service_descriptor_t *parsed;
} rend_cache_entry_t;

static strmap_t *rend_cache = NULL;

void rend_cache_init(void)
{
  rend_cache = strmap_new();
}

void rend_cache_clean(void)
{
  strmap_iter_t *iter;
  const char *key;
  void *val;
  rend_cache_entry_t *ent;
  time_t cutoff;
  cutoff = time(NULL) - REND_CACHE_MAX_AGE;
  for (iter = strmap_iter_init(rend_cache); !strmap_iter_done(iter); ) {
    strmap_iter_get(iter, &key, &val);
    ent = (rend_cache_entry_t*)val;
    if (ent->parsed->timestamp < cutoff) {
      iter = strmap_iter_next_rmv(rend_cache, iter);
      rend_service_descriptor_free(ent->parsed);
      tor_free(ent->desc);
      tor_free(ent);
    } else {
      iter = strmap_iter_next(rend_cache, iter);
    }
  }
}

/* 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;

  if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN)
    return 0;

  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.
 */
int rend_cache_lookup(char *query, const char **desc, int *desc_len)
{
  rend_cache_entry_t *e;
  assert(rend_cache);
  if (!rend_valid_service_id(query))
    return -1;
  e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, query);
  if (!e)
    return 0;
  *desc = e->desc;
  *desc_len = e->len;
  return 1;
}

/* Calculate desc's service id, and store it.
 * Return -1 if it's malformed or otherwise rejected, else return 0.
 */
int rend_cache_store(char *desc, int desc_len)
{
  rend_cache_entry_t *e;
  rend_service_descriptor_t *parsed;
  char query[REND_SERVICE_ID_LEN+1];
  time_t now;
  assert(rend_cache);
  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);
  if (parsed->timestamp < now-REND_CACHE_MAX_AGE) {
    log_fn(LOG_WARN,"Service descriptor is too old");
    rend_service_descriptor_free(parsed);
    return -1;
  }
  if (parsed->timestamp > now+REND_CACHE_MAX_SKEW) {
    log_fn(LOG_WARN,"Service descriptor is too far in the future");
    rend_service_descriptor_free(parsed);
    return -1;
  }
  e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, query);
  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) {
    e = tor_malloc_zero(sizeof(rend_cache_entry_t));
    strmap_set_lc(rend_cache, query, e);
  } else {
    rend_service_descriptor_free(e->parsed);
    tor_free(e->desc);
  }
  e->parsed = parsed;
  e->len = desc_len;
  e->desc = tor_strdup(desc);

  log_fn(LOG_INFO,"Successfully stored rend desc '%s', len %d", query, desc_len);
  return 0;
}

/* 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:
      /* r = rend_client_rendezvous(circ,payload,length); */
      log_fn(LOG_NOTICE, "Ignoring a rendezvous2 cell");
      break;
    default:
      assert(0);
  }
}

/*
  Local Variables:
  mode:c
  indent-tabs-mode:nil
  c-basic-offset:2
  End:
*/