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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/* Copyright 2004 Roger Dingledine */
/* See LICENSE for licensing information */
/* $Id$ */
/* This module implements the hidden-service side of rendezvous functionality.
*/
#include "or.h"
/* Represents the mapping from a virtual port of a rendezvous service to
* a real port on some IP.
*/
typedef struct rend_service_port_config_t {
uint16_t virtual_port;
uint16_t real_port;
uint32_t real_address;
} rend_service_port_config_t;
/* Represents a single hidden service running at this OP.
*/
typedef struct rend_service_t {
/* Fields specified in config file */
char *directory;
smartlist_t *ports;
/* Other fields */
crypto_pk_env_t *private_key;
char service_id[REND_SERVICE_ID_LEN+1];
char pk_digest[20];
} rend_service_t;
/* A list of rend_service_t.
*/
static smartlist_t *rend_service_list = NULL;
static void rend_service_free(rend_service_t *config)
{
int i;
if (!config) return;
tor_free(config->directory);
for (i=0; i<config->ports->num_used; ++i) {
tor_free(config->ports->list[i]);
}
smartlist_free(config->ports);
if (config->private_key)
crypto_free_pk_env(config->private_key);
}
static void rend_service_free_all(void)
{
int i;
if (!rend_service_list) {
rend_service_list = smartlist_create();
return;
}
for (i=0; i < rend_service_list->num_used; ++i) {
rend_service_free(rend_service_list->list[i]);
}
smartlist_free(rend_service_list);
rend_service_list = smartlist_create();
}
static void add_service(rend_service_t *service)
{
int i;
rend_service_port_config_t *p;
struct in_addr addr;
if (!service->ports->num_used) {
log_fn(LOG_WARN, "Hidden service with no ports configured; ignoring.");
rend_service_free(service);
} else {
smartlist_set_capacity(service->ports, service->ports->num_used);
smartlist_add(rend_service_list, service);
log_fn(LOG_INFO,"Configuring service with directory %s",service->directory);
for (i = 0; i < service->ports->num_used; ++i) {
p = (rend_service_port_config_t *) service->ports->list[i];
addr.s_addr = htonl(p->real_address);
log_fn(LOG_INFO,"Service maps port %d to %s:%d",
p->virtual_port, inet_ntoa(addr), p->real_port);
}
}
}
/* Format: VirtualPort (IP|RealPort|IP:RealPort)?
* IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
*/
static rend_service_port_config_t *parse_port_config(const char *string)
{
int virtport, realport, r;
struct in_addr addr;
char *endptr, *colon, *addrstring;
rend_service_port_config_t *result;
virtport = (int) strtol(string, &endptr, 10);
if (endptr == string) {
log_fn(LOG_WARN, "Missing port in hidden service port configuration");
return NULL;
}
if (virtport < 1 || virtport > 65535) {
log_fn(LOG_WARN, "Port out of range in hidden service port configuration");
return NULL;
}
string = endptr + strspn(endptr, " \t");
if (!*string) {
/* No addr:port part; use default. */
realport = virtport;
addr.s_addr = htonl(0x7F000001u);
} else {
colon = strchr(string, ':');
if (colon) {
/* Try to parse addr:port. */
addrstring = tor_strndup(string, colon-string);
r = tor_inet_aton(addrstring, &addr);
tor_free(addrstring);
if (!r) {
log_fn(LOG_WARN,"Unparseable address in hidden service port configuration");
return NULL;
}
realport = strtol(colon+1, &endptr, 10);
if (*endptr) {
log_fn(LOG_WARN,"Unparseable or missing port in hidden service port configuration.");
return NULL;
}
} else if (strchr(string, '.') && tor_inet_aton(string, &addr)) {
/* We have addr; use deafult port. */
realport = virtport;
} else {
/* No addr:port, no addr -- must be port. */
realport = strtol(string, &endptr, 10);
if (*endptr) {
log_fn(LOG_WARN, "Unparseable of missing port in hidden service port configuration.");
return NULL;
}
addr.s_addr = htonl(0x7F000001u); /* Default to 127.0.0.1 */
}
}
if (realport < 1 || realport > 65535) {
log_fn(LOG_WARN, "Port out of range in hidden service port configuration.");
return NULL;
}
result = tor_malloc(sizeof(rend_service_port_config_t));
result->virtual_port = virtport;
result->real_port = realport;
result->real_address = (uint32_t) ntohl(addr.s_addr);
return result;
}
/* Set up rend_service_list, based on the values of HiddenServiceDir and
* HiddenServicePort in 'options'. Return 0 on success and -1 on
* failure.
*/
int rend_config_services(or_options_t *options)
{
struct config_line_t *line;
rend_service_t *service = NULL;
rend_service_port_config_t *portcfg;
rend_service_free_all();
for (line = options->RendConfigLines; line; line = line->next) {
if (!strcasecmp(line->key, "HiddenServiceDir")) {
if (service)
add_service(service);
service = tor_malloc_zero(sizeof(rend_service_t));
service->directory = tor_strdup(line->value);
service->ports = smartlist_create();
} else {
assert(!strcasecmp(line->key, "HiddenServicePort"));
if (!service) {
log_fn(LOG_WARN, "HiddenServicePort with no preceeding HiddenServiceDir directive");
rend_service_free(service);
return -1;
}
portcfg = parse_port_config(line->value);
if (!portcfg) {
rend_service_free(service);
return -1;
}
smartlist_add(service->ports, portcfg);
}
}
if (service)
add_service(service);
return 0;
}
/* Load and/or generate private keys for all hidden services. Return 0 on
* success, -1 on failure.
*/
int rend_service_init_keys(void)
{
int i;
rend_service_t *s;
char fname[512];
char buf[128];
for (i=0; i < rend_service_list->num_used; ++i) {
s = (rend_service_t*) rend_service_list->list[i];
if (s->private_key)
continue;
/* Check/create directory */
if (check_private_dir(s->directory, 1) < 0)
return -1;
/* Load key */
if (strlcpy(fname,s->directory,512) >= 512 ||
strlcat(fname,"/private_key",512) >= 512) {
log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
return -1;
}
s->private_key = init_key_from_file(fname);
if (!s->private_key)
return -1;
/* Create service file */
if (rend_get_service_id(s->private_key, s->service_id)<0) {
log_fn(LOG_WARN, "Couldn't encode service ID");
return -1;
}
if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
log_fn(LOG_WARN, "Couldn't compute hash of public key");
return -1;
}
if (strlcpy(fname,s->directory,512) >= 512 ||
strlcat(fname,"/hostname",512) >= 512) {
log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
return -1;
}
sprintf(buf, "%s.onion\n", s->service_id);
if (write_str_to_file(fname,buf)<0)
return -1;
}
return 0;
}
/*DOCDOC*/
rend_service_t *
rend_service_get_by_pk_digest(const char* digest)
{
int i;
rend_service_t *s;
for (i = 0; i < rend_service_list->num_used; ++i) {
s = (rend_service_t*)rend_service_list->list[i];
if (!memcmp(s->pk_digest, digest, 20))
return s;
}
return NULL;
}
/******
* Handle cells
******/
typedef struct rend_introduction_t {
/* Digest of the hidden service's PK. */
char key_digest[20];
/* Nickname of OR running rendezvous point. */
char *rendezvous_point;
/* Cookie that we'll use to recognize the rendezvous point. */
char cookie[20];
/* g^xy */
char shared_secret[128];
} rend_introduction_t;
int
rend_service_introduce(circuit_t *circuit, char *request, int request_len)
{
char *ptr, *rp_nickname, *r_cookie;
char buf[RELAY_PAYLOAD_SIZE];
char secret[20+2*16]; /* Holds KH, Kf, Kb */
rend_service_t *service;
int len, keylen;
crypto_dh_env_t *dh = NULL;
circuit_t *launched = NULL;
if (circuit->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
log_fn(LOG_WARN, "Got an INTRODUCE2 over a non-introduction circuit.");
return -1;
}
/* min key length plus digest length */
if (request_len < 148) {
log_fn(LOG_WARN, "Got a truncated INTRODUCE2 cell.");
return -1;
}
/* first 20 bytes of request is service pk digest */
service = rend_service_get_by_pk_digest(request);
if (!service) {
log_fn(LOG_WARN, "Got an INTRODUCE2 cell for an unrecognized service");
return -1;
}
if (!memcmp(circuit->rend_service, request, 20)) {
log_fn(LOG_WARN, "Got an INTRODUCE2 cell for the wrong service");
return -1;
}
keylen = crypto_pk_keysize(service->private_key);
if (request_len < keylen+20) {
log_fn(LOG_WARN, "PK-encrypted portion of INTRODUCE2 cell was truncated");
return -1;
}
/* Next N bytes is encrypted with service key */
len = crypto_pk_private_hybrid_decrypt(
service->private_key,request,request_len-20,buf, RSA_PKCS1_PADDING);
if (len<0) {
log_fn(LOG_WARN, "Couldn't decrypt INTRODUCE2 cell");
return -1;
}
ptr=memchr(buf,0,len);
if (!ptr || ptr == buf) {
log_fn(LOG_WARN, "Couldn't find a null-terminated nickname in INTRODUCE2 cell");
return -1;
}
if (strspn(buf,LEGAL_NICKNAME_CHARACTERS) != ptr-buf) {
log_fn(LOG_WARN, "Nickname in INTRODUCE2 cell contains illegal character.");
return -1;
}
/* Okay, now we know that the nickname is at the start of the buffer. */
rp_nickname = buf;
++ptr;
len -= (ptr-buf);
if (len != 20+128) {
log_fn(LOG_WARN, "Bad length for INTRODUCE2 cell.");
return -1;
}
r_cookie = ptr;
/* Try DH handshake... */
dh = crypto_dh_new();
if (!dh || crypto_dh_generate_public(dh)<0) {
log_fn(LOG_WARN, "Couldn't build DH state or generate public key");
goto err;
}
if (crypto_dh_compute_secret(dh, ptr+20, 128, secret, 20+16*2)<0) {
log_fn(LOG_WARN, "Couldn't complete DH handshake");
goto err;
}
/* Launch a circuit to alice's chosen rendezvous point.
*/
launched = circuit_launch_new(CIRCUIT_PURPOSE_S_RENDEZVOUSING, rp_nickname);
if (!launched) {
log_fn(LOG_WARN, "Can't launch circuit to rendezvous point '%s'",
rp_nickname);
return -1;
}
assert(launched->build_state);
/* Fill in the circuit's state. */
memcpy(launched->rend_service, circuit->rend_service,CRYPTO_SHA1_DIGEST_LEN);
memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
memcpy(launched->build_state->rend_key_material, secret, 20+16*2);
launched->build_state->rend_handshake_state = dh;
dh = NULL;
return 0;
err:
if (dh) crypto_dh_free(dh);
if (launched) circuit_mark_for_close(launched);
return -1;
}
/******
* Manage introduction points
******/
#define NUM_INTRO_POINTS 3
int rend_services_init(void) {
int i;
routerinfo_t *router;
routerlist_t *rl;
circuit_t *circ;
router_get_routerlist(&rl);
//for each of bob's services,
/* The directory is now here. Pick three ORs as intro points. */
for (i=0;i<rl->n_routers;i++) {
router = rl->routers[i];
//...
// maybe built a smartlist of all of them, then pick at random
// until you have three? or something smarter.
}
/* build a service descriptor out of them, and tuck it away
* somewhere so we don't lose it */
/* post it to the dirservers */
//call router_post_to_dirservers(DIR_PURPOSE_UPLOAD_HIDSERV, desc, desc_len);
// for each intro point,
{
//circ = circuit_launch_new(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, intro->nickname);
// tell circ which hidden service this is about
}
// anything else?
}
/*
Local Variables:
mode:c
indent-tabs-mode:nil
c-basic-offset:2
End:
*/
|