1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2008, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 const char rendservice_c_id
[] =
10 * \brief The hidden-service side of rendezvous functionality.
15 static origin_circuit_t
*find_intro_circuit(rend_intro_point_t
*intro
,
16 const char *pk_digest
,
19 /** Represents the mapping from a virtual port of a rendezvous service to
20 * a real port on some IP.
22 typedef struct rend_service_port_config_t
{
23 uint16_t virtual_port
;
26 } rend_service_port_config_t
;
28 /** Try to maintain this many intro points per service if possible. */
29 #define NUM_INTRO_POINTS 3
31 /** If we can't build our intro circuits, don't retry for this long. */
32 #define INTRO_CIRC_RETRY_PERIOD (60*5)
33 /** Don't try to build more than this many circuits before giving up
35 #define MAX_INTRO_CIRCS_PER_PERIOD 10
36 /** How many times will a hidden service operator attempt to connect to
37 * a requested rendezvous point before giving up? */
38 #define MAX_REND_FAILURES 30
39 /** How many seconds should we spend trying to connect to a requested
40 * rendezvous point before giving up? */
41 #define MAX_REND_TIMEOUT 30
44 typedef enum rend_auth_type_t
{
47 REND_STEALTH_AUTH
= 2,
50 /** Represents a single hidden service running at this OP. */
51 typedef struct rend_service_t
{
52 /* Fields specified in config file */
53 char *directory
; /**< where in the filesystem it stores it */
54 smartlist_t
*ports
; /**< List of rend_service_port_config_t */
55 int descriptor_version
; /**< Rendezvous descriptor version that will be
57 rend_auth_type_t auth_type
; /**< Client authorization type or 0 if no client
58 * authorization is performed. */
59 smartlist_t
*clients
; /**< List of rend_authorized_client_t's of
60 * clients that may access our service. */
62 crypto_pk_env_t
*private_key
; /**< Permanent hidden-service key. */
63 char service_id
[REND_SERVICE_ID_LEN_BASE32
+1]; /**< Onion address without
65 char pk_digest
[DIGEST_LEN
]; /**< Hash of permanent hidden-service key. */
66 smartlist_t
*intro_nodes
; /**< List of rend_intro_point_t's we have,
67 * or are trying to establish. */
68 time_t intro_period_started
; /**< Start of the current period to build
69 * introduction points. */
70 int n_intro_circuits_launched
; /**< count of intro circuits we have
71 * established in this period. */
72 rend_service_descriptor_t
*desc
; /**< Current hidden service descriptor. */
73 time_t desc_is_dirty
; /**< Time at which changes to the hidden service
74 * descriptor content occurred, or 0 if it's
76 time_t next_upload_time
; /**< Scheduled next hidden service descriptor
80 /** A list of rend_service_t's for services run on this OP.
82 static smartlist_t
*rend_service_list
= NULL
;
84 /** Return the number of rendezvous services we have configured. */
86 num_rend_services(void)
88 if (!rend_service_list
)
90 return smartlist_len(rend_service_list
);
93 /** Helper: free storage held by a single service authorized client entry. */
95 rend_authorized_client_free(rend_authorized_client_t
*client
)
98 if (client
->client_key
)
99 crypto_free_pk_env(client
->client_key
);
100 tor_free(client
->client_name
);
104 /** Helper for strmap_free. */
106 rend_authorized_client_strmap_item_free(void *authorized_client
)
108 rend_authorized_client_free(authorized_client
);
111 /** Release the storage held by <b>service</b>.
114 rend_service_free(rend_service_t
*service
)
116 if (!service
) return;
117 tor_free(service
->directory
);
118 SMARTLIST_FOREACH(service
->ports
, void*, p
, tor_free(p
));
119 smartlist_free(service
->ports
);
120 if (service
->private_key
)
121 crypto_free_pk_env(service
->private_key
);
122 if (service
->intro_nodes
) {
123 SMARTLIST_FOREACH(service
->intro_nodes
, rend_intro_point_t
*, intro
,
124 rend_intro_point_free(intro
););
125 smartlist_free(service
->intro_nodes
);
128 rend_service_descriptor_free(service
->desc
);
129 if (service
->clients
) {
130 SMARTLIST_FOREACH(service
->clients
, rend_authorized_client_t
*, c
,
131 rend_authorized_client_free(c
););
132 smartlist_free(service
->clients
);
137 /** Release all the storage held in rend_service_list.
140 rend_service_free_all(void)
142 if (!rend_service_list
) {
145 SMARTLIST_FOREACH(rend_service_list
, rend_service_t
*, ptr
,
146 rend_service_free(ptr
));
147 smartlist_free(rend_service_list
);
148 rend_service_list
= NULL
;
151 /** Validate <b>service</b> and add it to rend_service_list if possible.
154 rend_add_service(rend_service_t
*service
)
157 rend_service_port_config_t
*p
;
159 service
->intro_nodes
= smartlist_create();
161 /* If the service is configured to publish unversioned (v0) and versioned
162 * descriptors (v2 or higher), split it up into two separate services
163 * (unless it is configured to perform client authorization). */
164 if (service
->descriptor_version
== -1) {
165 if (service
->auth_type
== REND_NO_AUTH
) {
166 rend_service_t
*v0_service
= tor_malloc_zero(sizeof(rend_service_t
));
167 v0_service
->directory
= tor_strdup(service
->directory
);
168 v0_service
->ports
= smartlist_create();
169 SMARTLIST_FOREACH(service
->ports
, rend_service_port_config_t
*, p
, {
170 rend_service_port_config_t
*copy
=
171 tor_malloc_zero(sizeof(rend_service_port_config_t
));
172 memcpy(copy
, p
, sizeof(rend_service_port_config_t
));
173 smartlist_add(v0_service
->ports
, copy
);
175 v0_service
->intro_period_started
= service
->intro_period_started
;
176 v0_service
->descriptor_version
= 0; /* Unversioned descriptor. */
177 v0_service
->auth_type
= REND_NO_AUTH
;
178 rend_add_service(v0_service
);
181 service
->descriptor_version
= 2; /* Versioned descriptor. */
184 if (service
->auth_type
&& !service
->descriptor_version
) {
185 log_warn(LD_CONFIG
, "Hidden service with client authorization and "
186 "version 0 descriptors configured; ignoring.");
187 rend_service_free(service
);
191 if (service
->auth_type
&& smartlist_len(service
->clients
) == 0) {
192 log_warn(LD_CONFIG
, "Hidden service with client authorization but no "
193 "clients; ignoring.");
194 rend_service_free(service
);
198 if (!smartlist_len(service
->ports
)) {
199 log_warn(LD_CONFIG
, "Hidden service with no ports configured; ignoring.");
200 rend_service_free(service
);
202 smartlist_add(rend_service_list
, service
);
203 log_debug(LD_REND
,"Configuring service with directory \"%s\"",
205 for (i
= 0; i
< smartlist_len(service
->ports
); ++i
) {
206 p
= smartlist_get(service
->ports
, i
);
207 log_debug(LD_REND
,"Service maps port %d to %s:%d",
208 p
->virtual_port
, fmt_addr(&p
->real_addr
), p
->real_port
);
213 /** Parses a real-port to virtual-port mapping and returns a new
214 * rend_service_port_config_t.
216 * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
218 * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
220 static rend_service_port_config_t
*
221 parse_port_config(const char *string
)
228 const char *addrport
;
229 rend_service_port_config_t
*result
= NULL
;
231 sl
= smartlist_create();
232 smartlist_split_string(sl
, string
, " ",
233 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
234 if (smartlist_len(sl
) < 1 || smartlist_len(sl
) > 2) {
235 log_warn(LD_CONFIG
, "Bad syntax in hidden service port configuration.");
239 virtport
= (int)tor_parse_long(smartlist_get(sl
,0), 10, 1, 65535, NULL
,NULL
);
241 log_warn(LD_CONFIG
, "Missing or invalid port %s in hidden service port "
242 "configuration", escaped(smartlist_get(sl
,0)));
246 if (smartlist_len(sl
) == 1) {
247 /* No addr:port part; use default. */
249 tor_addr_from_ipv4h(&addr
, 0x7F000001u
); /* 127.0.0.1 */
251 addrport
= smartlist_get(sl
,1);
252 if (strchr(addrport
, ':') || strchr(addrport
, '.')) {
253 if (tor_addr_port_parse(addrport
, &addr
, &p
)<0) {
254 log_warn(LD_CONFIG
,"Unparseable address in hidden service port "
258 realport
= p
?p
:virtport
;
260 /* No addr:port, no addr -- must be port. */
261 realport
= (int)tor_parse_long(addrport
, 10, 1, 65535, NULL
, NULL
);
263 log_warn(LD_CONFIG
,"Unparseable or out-of-range port %s in hidden "
264 "service port configuration.", escaped(addrport
));
267 tor_addr_from_ipv4h(&addr
, 0x7F000001u
); /* Default to 127.0.0.1 */
271 result
= tor_malloc(sizeof(rend_service_port_config_t
));
272 result
->virtual_port
= virtport
;
273 result
->real_port
= realport
;
274 tor_addr_copy(&result
->real_addr
, &addr
);
276 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
281 /** Set up rend_service_list, based on the values of HiddenServiceDir and
282 * HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
283 * failure. (If <b>validate_only</b> is set, parse, warn and return as
284 * normal, but don't actually change the configured services.)
287 rend_config_services(or_options_t
*options
, int validate_only
)
290 rend_service_t
*service
= NULL
;
291 rend_service_port_config_t
*portcfg
;
293 if (!validate_only
) {
294 rend_service_free_all();
295 rend_service_list
= smartlist_create();
298 for (line
= options
->RendConfigLines
; line
; line
= line
->next
) {
299 if (!strcasecmp(line
->key
, "HiddenServiceDir")) {
302 rend_service_free(service
);
304 rend_add_service(service
);
306 service
= tor_malloc_zero(sizeof(rend_service_t
));
307 service
->directory
= tor_strdup(line
->value
);
308 service
->ports
= smartlist_create();
309 service
->intro_period_started
= time(NULL
);
310 service
->descriptor_version
= -1; /**< All descriptor versions. */
314 log_warn(LD_CONFIG
, "%s with no preceding HiddenServiceDir directive",
316 rend_service_free(service
);
319 if (!strcasecmp(line
->key
, "HiddenServicePort")) {
320 portcfg
= parse_port_config(line
->value
);
322 rend_service_free(service
);
325 smartlist_add(service
->ports
, portcfg
);
326 } else if (!strcasecmp(line
->key
, "HiddenServiceAuthorizeClient")) {
327 /* Parse auth type and comma-separated list of client names and add a
328 * rend_authorized_client_t for each client to the service's list
329 * of authorized clients. */
330 smartlist_t
*type_names_split
, *clients
;
331 const char *authname
;
332 if (service
->auth_type
) {
333 log_warn(LD_CONFIG
, "Got multiple HiddenServiceAuthorizeClient "
334 "lines for a single service.");
335 rend_service_free(service
);
338 type_names_split
= smartlist_create();
339 smartlist_split_string(type_names_split
, line
->value
, " ", 0, 0);
340 if (smartlist_len(type_names_split
) < 1) {
341 log_warn(LD_BUG
, "HiddenServiceAuthorizeClient has no value. This "
342 "should have been prevented when parsing the "
344 smartlist_free(type_names_split
);
345 rend_service_free(service
);
348 authname
= smartlist_get(type_names_split
, 0);
349 if (!strcasecmp(authname
, "basic") || !strcmp(authname
, "1")) {
350 service
->auth_type
= REND_BASIC_AUTH
;
351 } else if (!strcasecmp(authname
, "stealth") || !strcmp(authname
, "2")) {
352 service
->auth_type
= REND_STEALTH_AUTH
;
354 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains "
355 "unrecognized auth-type '%s'. Only 1 or 2 are recognized.",
356 (char *) smartlist_get(type_names_split
, 0));
357 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
358 smartlist_free(type_names_split
);
359 rend_service_free(service
);
362 service
->clients
= smartlist_create();
363 if (smartlist_len(type_names_split
) < 2) {
364 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains "
365 "authorization type %d, but no client names.",
367 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
368 smartlist_free(type_names_split
);
371 if (smartlist_len(type_names_split
) > 2) {
372 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains "
373 "illegal value '%s'. Must be formatted "
374 "as 'HiddenServiceAuthorizeClient auth-type "
375 "client-name,client-name,...' (without "
376 "additional spaces in comma-separated client "
379 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
380 smartlist_free(type_names_split
);
381 rend_service_free(service
);
384 clients
= smartlist_create();
385 smartlist_split_string(clients
, smartlist_get(type_names_split
, 1),
387 SMARTLIST_FOREACH(type_names_split
, char *, cp
, tor_free(cp
));
388 smartlist_free(type_names_split
);
389 SMARTLIST_FOREACH_BEGIN(clients
, const char *, client_name
)
391 rend_authorized_client_t
*client
;
392 size_t len
= strlen(client_name
);
393 int found_duplicate
= 0;
394 /* XXXX proposal 121 Why 19? Also, this should be a constant. */
395 if (len
< 1 || len
> 19) {
396 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains an "
397 "illegal client name: '%s'. Length must be "
398 "between 1 and 19 characters.",
400 SMARTLIST_FOREACH(clients
, char *, cp
, tor_free(cp
));
401 smartlist_free(clients
);
402 rend_service_free(service
);
405 if (strspn(client_name
, REND_LEGAL_CLIENTNAME_CHARACTERS
) != len
) {
406 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains an "
407 "illegal client name: '%s'. Valid "
408 "characters are [A-Za-z0-9+-_].",
410 SMARTLIST_FOREACH(clients
, char *, cp
, tor_free(cp
));
411 smartlist_free(clients
);
412 rend_service_free(service
);
415 /* Check if client name is duplicate. */
416 /*XXXX proposal 121 This is O(N^2). That's not so good. */
417 SMARTLIST_FOREACH(service
->clients
, rend_authorized_client_t
*, c
, {
418 if (!strcmp(c
->client_name
, client_name
)) {
419 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains a "
420 "duplicate client name: '%s'; ignoring.", client_name
);
427 client
= tor_malloc_zero(sizeof(rend_authorized_client_t
));
428 client
->client_name
= tor_strdup(client_name
);
429 smartlist_add(service
->clients
, client
);
430 log_debug(LD_REND
, "Adding client name '%s'", client_name
);
432 SMARTLIST_FOREACH_END(client_name
);
433 SMARTLIST_FOREACH(clients
, char *, cp
, tor_free(cp
));
434 smartlist_free(clients
);
435 /* Ensure maximum number of clients. */
436 if ((service
->auth_type
== REND_BASIC_AUTH
&&
437 smartlist_len(service
->clients
) > 512) ||
438 (service
->auth_type
== REND_STEALTH_AUTH
&&
439 smartlist_len(service
->clients
) > 16)) {
440 log_warn(LD_CONFIG
, "HiddenServiceAuthorizeClient contains %d "
441 "client authorization entries, but only a "
442 "maximum of %d entries is allowed for "
443 "authorization type %d.",
444 smartlist_len(service
->clients
),
445 service
->auth_type
== REND_BASIC_AUTH
? 512 : 16,
446 (int)service
->auth_type
);
447 rend_service_free(service
);
451 smartlist_t
*versions
;
453 int i
, version
, ver_ok
=1, versions_bitmask
= 0;
454 tor_assert(!strcasecmp(line
->key
, "HiddenServiceVersion"));
455 versions
= smartlist_create();
456 smartlist_split_string(versions
, line
->value
, ",",
457 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
458 for (i
= 0; i
< smartlist_len(versions
); i
++) {
459 version_str
= smartlist_get(versions
, i
);
460 if (strlen(version_str
) != 1 || strspn(version_str
, "02") != 1) {
462 "HiddenServiceVersion can only be 0 and/or 2.");
463 SMARTLIST_FOREACH(versions
, char *, cp
, tor_free(cp
));
464 smartlist_free(versions
);
465 rend_service_free(service
);
468 version
= (int)tor_parse_long(version_str
, 10, 0, INT_MAX
, &ver_ok
,
472 versions_bitmask
|= 1 << version
;
474 /* If exactly one version is set, change descriptor_version to that
475 * value; otherwise leave it at -1. */
476 if (versions_bitmask
== 1 << 0) service
->descriptor_version
= 0;
477 if (versions_bitmask
== 1 << 2) service
->descriptor_version
= 2;
478 SMARTLIST_FOREACH(versions
, char *, cp
, tor_free(cp
));
479 smartlist_free(versions
);
484 rend_service_free(service
);
486 rend_add_service(service
);
492 /** Replace the old value of <b>service</b>-\>desc with one that reflects
493 * the other fields in service.
496 rend_service_update_descriptor(rend_service_t
*service
)
498 rend_service_descriptor_t
*d
;
499 origin_circuit_t
*circ
;
502 rend_service_descriptor_free(service
->desc
);
503 service
->desc
= NULL
;
505 d
= service
->desc
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
506 d
->pk
= crypto_pk_dup_key(service
->private_key
);
507 d
->timestamp
= time(NULL
);
508 d
->version
= service
->descriptor_version
;
509 d
->intro_nodes
= smartlist_create();
510 /* Whoever understands descriptor version 2 also understands intro
511 * protocol 2. So we only support 2. */
512 d
->protocols
= 1 << 2;
514 for (i
= 0; i
< smartlist_len(service
->intro_nodes
); ++i
) {
515 rend_intro_point_t
*intro_svc
= smartlist_get(service
->intro_nodes
, i
);
516 rend_intro_point_t
*intro_desc
;
517 circ
= find_intro_circuit(intro_svc
, service
->pk_digest
, d
->version
);
518 if (!circ
|| circ
->_base
.purpose
!= CIRCUIT_PURPOSE_S_INTRO
)
521 /* We have an entirely established intro circuit. */
522 intro_desc
= tor_malloc_zero(sizeof(rend_intro_point_t
));
523 intro_desc
->extend_info
= extend_info_dup(intro_svc
->extend_info
);
524 if (intro_svc
->intro_key
)
525 intro_desc
->intro_key
= crypto_pk_dup_key(intro_svc
->intro_key
);
526 smartlist_add(d
->intro_nodes
, intro_desc
);
530 /** Load and/or generate private keys for all hidden services, possibly
531 * including keys for client authorization. Return 0 on success, -1 on
535 rend_service_load_keys(void)
541 SMARTLIST_FOREACH_BEGIN(rend_service_list
, rend_service_t
*, s
) {
544 log_info(LD_REND
, "Loading hidden-service keys from \"%s\"",
547 /* Check/create directory */
548 if (check_private_dir(s
->directory
, CPD_CREATE
) < 0)
552 if (strlcpy(fname
,s
->directory
,sizeof(fname
)) >= sizeof(fname
) ||
553 strlcat(fname
,PATH_SEPARATOR
"private_key",sizeof(fname
))
555 log_warn(LD_CONFIG
, "Directory name too long to store key file: \"%s\".",
559 s
->private_key
= init_key_from_file(fname
, 1, LOG_ERR
);
563 /* Create service file */
564 if (rend_get_service_id(s
->private_key
, s
->service_id
)<0) {
565 log_warn(LD_BUG
, "Internal error: couldn't encode service ID.");
568 if (crypto_pk_get_digest(s
->private_key
, s
->pk_digest
)<0) {
569 log_warn(LD_BUG
, "Couldn't compute hash of public key.");
572 if (strlcpy(fname
,s
->directory
,sizeof(fname
)) >= sizeof(fname
) ||
573 strlcat(fname
,PATH_SEPARATOR
"hostname",sizeof(fname
))
575 log_warn(LD_CONFIG
, "Directory name too long to store hostname file:"
576 " \"%s\".", s
->directory
);
579 tor_snprintf(buf
, sizeof(buf
),"%s.onion\n", s
->service_id
);
580 if (write_str_to_file(fname
,buf
,0)<0) {
581 log_warn(LD_CONFIG
, "Could not write onion address to hostname file.");
585 /* If client authorization is configured, load or generate keys. */
587 char *client_keys_str
= NULL
;
588 strmap_t
*parsed_clients
= strmap_new();
591 open_file_t
*open_cfile
= NULL
, *open_hfile
= NULL
;
593 /* Load client keys and descriptor cookies, if available. */
594 if (tor_snprintf(cfname
, sizeof(cfname
), "%s"PATH_SEPARATOR
"client_keys",
596 log_warn(LD_CONFIG
, "Directory name too long to store client keys "
597 "file: \"%s\".", s
->directory
);
600 client_keys_str
= read_file_to_str(cfname
, RFTS_IGNORE_MISSING
, NULL
);
601 if (client_keys_str
) {
602 if (rend_parse_client_keys(parsed_clients
, client_keys_str
) < 0) {
603 log_warn(LD_CONFIG
, "Previously stored client_keys file could not "
607 log_info(LD_CONFIG
, "Parsed %d previously stored client entries.",
608 strmap_size(parsed_clients
));
609 tor_free(client_keys_str
);
613 /* Prepare client_keys and hostname files. */
614 if (!(cfile
= start_writing_to_stdio_file(cfname
, OPEN_FLAGS_REPLACE
,
615 0600, &open_cfile
))) {
616 log_warn(LD_CONFIG
, "Could not open client_keys file %s",
620 if (!(hfile
= start_writing_to_stdio_file(fname
, OPEN_FLAGS_REPLACE
,
621 0600, &open_hfile
))) {
622 log_warn(LD_CONFIG
, "Could not open hostname file %s", escaped(fname
));
626 /* Either use loaded keys for configured clients or generate new
627 * ones if a client is new. */
628 SMARTLIST_FOREACH_BEGIN(s
->clients
, rend_authorized_client_t
*, client
)
630 char desc_cook_out
[3*REND_DESC_COOKIE_LEN_BASE64
+1];
631 char service_id
[16+1];
632 rend_authorized_client_t
*parsed
=
633 strmap_get(parsed_clients
, client
->client_name
);
636 /* Copy descriptor cookie from parsed entry or create new one. */
638 memcpy(client
->descriptor_cookie
, parsed
->descriptor_cookie
,
639 REND_DESC_COOKIE_LEN
);
641 crypto_rand(client
->descriptor_cookie
, REND_DESC_COOKIE_LEN
);
643 if (base64_encode(desc_cook_out
, 3*REND_DESC_COOKIE_LEN_BASE64
+1,
644 client
->descriptor_cookie
,
645 REND_DESC_COOKIE_LEN
) < 0) {
646 log_warn(LD_BUG
, "Could not base64-encode descriptor cookie.");
647 strmap_free(parsed_clients
, rend_authorized_client_strmap_item_free
);
650 /* Copy client key from parsed entry or create new one if required. */
651 if (parsed
&& parsed
->client_key
) {
652 client
->client_key
= crypto_pk_dup_key(parsed
->client_key
);
653 } else if (s
->auth_type
== REND_STEALTH_AUTH
) {
654 /* Create private key for client. */
655 crypto_pk_env_t
*prkey
= NULL
;
656 if (!(prkey
= crypto_new_pk_env())) {
657 log_warn(LD_BUG
,"Error constructing client key");
660 if (crypto_pk_generate_key(prkey
)) {
661 log_warn(LD_BUG
,"Error generating client key");
664 if (crypto_pk_check_key(prkey
) <= 0) {
665 log_warn(LD_BUG
,"Generated client key seems invalid");
666 crypto_free_pk_env(prkey
);
669 client
->client_key
= prkey
;
671 /* Add entry to client_keys file. */
672 desc_cook_out
[strlen(desc_cook_out
)-1] = '\0'; /* Remove newline. */
673 written
= tor_snprintf(buf
, sizeof(buf
),
674 "client-name %s\ndescriptor-cookie %s\n",
675 client
->client_name
, desc_cook_out
);
677 log_warn(LD_BUG
, "Could not write client entry.");
681 if (client
->client_key
) {
682 char *client_key_out
;
683 crypto_pk_write_private_key_to_string(client
->client_key
,
684 &client_key_out
, &len
);
685 if (rend_get_service_id(client
->client_key
, service_id
)<0) {
686 log_warn(LD_BUG
, "Internal error: couldn't encode service ID.");
689 written
= tor_snprintf(buf
+ written
, sizeof(buf
) - written
,
690 "client-key\n%s", client_key_out
);
692 log_warn(LD_BUG
, "Could not write client entry.");
697 if (fputs(buf
, cfile
) < 0) {
698 log_warn(LD_FS
, "Could not append client entry to file: %s",
703 /* Add line to hostname file. */
704 if (s
->auth_type
== REND_BASIC_AUTH
) {
705 /* Remove == signs (newline has been removed above). */
706 desc_cook_out
[strlen(desc_cook_out
)-2] = '\0';
707 tor_snprintf(buf
, sizeof(buf
),"%s.onion %s # client: %s\n",
708 s
->service_id
, desc_cook_out
, client
->client_name
);
710 char extended_desc_cookie
[REND_DESC_COOKIE_LEN
+1];
711 memcpy(extended_desc_cookie
, client
->descriptor_cookie
,
712 REND_DESC_COOKIE_LEN
);
713 extended_desc_cookie
[REND_DESC_COOKIE_LEN
] = (s
->auth_type
- 1) << 4;
714 if (base64_encode(desc_cook_out
, 3*REND_DESC_COOKIE_LEN_BASE64
+1,
715 extended_desc_cookie
,
716 REND_DESC_COOKIE_LEN
+1) < 0) {
717 log_warn(LD_BUG
, "Could not base64-encode descriptor cookie.");
720 desc_cook_out
[strlen(desc_cook_out
)-3] = '\0'; /* Remove A= and
722 tor_snprintf(buf
, sizeof(buf
),"%s.onion %s # client: %s\n",
723 service_id
, desc_cook_out
, client
->client_name
);
726 if (fputs(buf
, hfile
)<0) {
727 log_warn(LD_FS
, "Could not append host entry to file: %s",
732 SMARTLIST_FOREACH_END(client
);
738 tor_free(client_keys_str
);
739 strmap_free(parsed_clients
, rend_authorized_client_strmap_item_free
);
741 abort_writing_to_file(open_cfile
);
742 abort_writing_to_file(open_hfile
);
745 finish_writing_to_file(open_cfile
);
746 finish_writing_to_file(open_hfile
);
749 } SMARTLIST_FOREACH_END(s
);
753 /** Return the service whose public key has a digest of <b>digest</b> and
754 * which publishes the given descriptor <b>version</b>. Return NULL if no
755 * such service exists.
757 static rend_service_t
*
758 rend_service_get_by_pk_digest_and_version(const char* digest
,
761 SMARTLIST_FOREACH(rend_service_list
, rend_service_t
*, s
,
762 if (!memcmp(s
->pk_digest
,digest
,DIGEST_LEN
) &&
763 s
->descriptor_version
== version
) return s
);
767 /** Return 1 if any virtual port in <b>service</b> wants a circuit
768 * to have good uptime. Else return 0.
771 rend_service_requires_uptime(rend_service_t
*service
)
774 rend_service_port_config_t
*p
;
776 for (i
=0; i
< smartlist_len(service
->ports
); ++i
) {
777 p
= smartlist_get(service
->ports
, i
);
778 if (smartlist_string_num_isin(get_options()->LongLivedPorts
,
789 /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
793 rend_service_introduce(origin_circuit_t
*circuit
, const char *request
,
796 char *ptr
, *r_cookie
;
797 extend_info_t
*extend_info
= NULL
;
798 char buf
[RELAY_PAYLOAD_SIZE
];
799 char keys
[DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
]; /* Holds KH, Df, Db, Kf, Kb */
800 rend_service_t
*service
;
803 crypto_dh_env_t
*dh
= NULL
;
804 origin_circuit_t
*launched
= NULL
;
805 crypt_path_t
*cpath
= NULL
;
806 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
808 int circ_needs_uptime
;
809 int reason
= END_CIRC_REASON_TORPROTOCOL
;
810 crypto_pk_env_t
*intro_key
;
811 char intro_key_digest
[DIGEST_LEN
];
813 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
814 circuit
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
815 log_info(LD_REND
, "Received INTRODUCE2 cell for service %s on circ %d.",
816 escaped(serviceid
), circuit
->_base
.n_circ_id
);
818 if (circuit
->_base
.purpose
!= CIRCUIT_PURPOSE_S_INTRO
) {
819 log_warn(LD_PROTOCOL
,
820 "Got an INTRODUCE2 over a non-introduction circuit %d.",
821 circuit
->_base
.n_circ_id
);
825 /* min key length plus digest length plus nickname length */
826 if (request_len
< DIGEST_LEN
+REND_COOKIE_LEN
+(MAX_NICKNAME_LEN
+1)+
828 log_warn(LD_PROTOCOL
, "Got a truncated INTRODUCE2 cell on circ %d.",
829 circuit
->_base
.n_circ_id
);
833 /* look up service depending on circuit. */
834 service
= rend_service_get_by_pk_digest_and_version(
835 circuit
->rend_pk_digest
, circuit
->rend_desc_version
);
837 log_warn(LD_REND
, "Got an INTRODUCE2 cell for an unrecognized service %s.",
842 /* if descriptor version is 2, use intro key instead of service key. */
843 if (circuit
->rend_desc_version
== 0) {
844 intro_key
= service
->private_key
;
846 intro_key
= circuit
->intro_key
;
849 /* first DIGEST_LEN bytes of request is intro or service pk digest */
850 crypto_pk_get_digest(intro_key
, intro_key_digest
);
851 if (memcmp(intro_key_digest
, request
, DIGEST_LEN
)) {
852 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
853 request
, REND_SERVICE_ID_LEN
);
854 log_warn(LD_REND
, "Got an INTRODUCE2 cell for the wrong service (%s).",
859 keylen
= crypto_pk_keysize(intro_key
);
860 if (request_len
< keylen
+DIGEST_LEN
) {
861 log_warn(LD_PROTOCOL
,
862 "PK-encrypted portion of INTRODUCE2 cell was truncated.");
865 /* Next N bytes is encrypted with service key */
866 note_crypto_pk_op(REND_SERVER
);
867 r
= crypto_pk_private_hybrid_decrypt(
868 intro_key
,buf
,request
+DIGEST_LEN
,request_len
-DIGEST_LEN
,
869 PK_PKCS1_OAEP_PADDING
,1);
871 log_warn(LD_PROTOCOL
, "Couldn't decrypt INTRODUCE2 cell.");
876 /* Version 2 INTRODUCE2 cell. */
878 extend_info
= tor_malloc_zero(sizeof(extend_info_t
));
879 tor_addr_from_ipv4n(&extend_info
->addr
, get_uint32(buf
+1));
880 extend_info
->port
= ntohs(get_uint16(buf
+5));
881 memcpy(extend_info
->identity_digest
, buf
+7, DIGEST_LEN
);
882 extend_info
->nickname
[0] = '$';
883 base16_encode(extend_info
->nickname
+1, sizeof(extend_info
->nickname
)-1,
884 extend_info
->identity_digest
, DIGEST_LEN
);
886 klen
= ntohs(get_uint16(buf
+7+DIGEST_LEN
));
887 if ((int)len
!= 7+DIGEST_LEN
+2+klen
+20+128) {
888 log_warn(LD_PROTOCOL
, "Bad length %u for version 2 INTRODUCE2 cell.",
890 reason
= END_CIRC_REASON_TORPROTOCOL
;
893 extend_info
->onion_key
= crypto_pk_asn1_decode(buf
+7+DIGEST_LEN
+2, klen
);
894 if (!extend_info
->onion_key
) {
895 log_warn(LD_PROTOCOL
,
896 "Error decoding onion key in version 2 INTRODUCE2 cell.");
897 reason
= END_CIRC_REASON_TORPROTOCOL
;
900 ptr
= buf
+7+DIGEST_LEN
+2+klen
;
901 len
-= 7+DIGEST_LEN
+2+klen
;
904 size_t nickname_field_len
;
905 routerinfo_t
*router
;
909 nickname_field_len
= MAX_HEX_NICKNAME_LEN
+1;
912 nickname_field_len
= MAX_NICKNAME_LEN
+1;
916 ptr
=memchr(rp_nickname
,0,nickname_field_len
);
917 if (!ptr
|| ptr
== rp_nickname
) {
918 log_warn(LD_PROTOCOL
,
919 "Couldn't find a nul-padded nickname in INTRODUCE2 cell.");
922 if ((version
== 0 && !is_legal_nickname(rp_nickname
)) ||
923 (version
== 1 && !is_legal_nickname_or_hexdigest(rp_nickname
))) {
924 log_warn(LD_PROTOCOL
, "Bad nickname in INTRODUCE2 cell.");
927 /* Okay, now we know that a nickname is at the start of the buffer. */
928 ptr
= rp_nickname
+nickname_field_len
;
929 len
-= nickname_field_len
;
930 len
-= rp_nickname
- buf
; /* also remove header space used by version, if
932 router
= router_get_by_nickname(rp_nickname
, 0);
934 log_info(LD_REND
, "Couldn't find router %s named in introduce2 cell.",
935 escaped_safe_str(rp_nickname
));
936 /* XXXX Add a no-such-router reason? */
937 reason
= END_CIRC_REASON_TORPROTOCOL
;
941 extend_info
= extend_info_from_router(router
);
944 if (len
!= REND_COOKIE_LEN
+DH_KEY_LEN
) {
945 log_warn(LD_PROTOCOL
, "Bad length %u for INTRODUCE2 cell.", (int)len
);
946 reason
= END_CIRC_REASON_TORPROTOCOL
;
951 base16_encode(hexcookie
,9,r_cookie
,4);
953 /* Try DH handshake... */
954 dh
= crypto_dh_new();
955 if (!dh
|| crypto_dh_generate_public(dh
)<0) {
956 log_warn(LD_BUG
,"Internal error: couldn't build DH state "
957 "or generate public key.");
958 reason
= END_CIRC_REASON_INTERNAL
;
961 if (crypto_dh_compute_secret(dh
, ptr
+REND_COOKIE_LEN
, DH_KEY_LEN
, keys
,
962 DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
)<0) {
963 log_warn(LD_BUG
, "Internal error: couldn't complete DH handshake");
964 reason
= END_CIRC_REASON_INTERNAL
;
968 circ_needs_uptime
= rend_service_requires_uptime(service
);
970 /* help predict this next time */
971 rep_hist_note_used_internal(time(NULL
), circ_needs_uptime
, 1);
973 /* Launch a circuit to alice's chosen rendezvous point.
975 for (i
=0;i
<MAX_REND_FAILURES
;i
++) {
976 int flags
= CIRCLAUNCH_NEED_CAPACITY
| CIRCLAUNCH_IS_INTERNAL
;
977 if (circ_needs_uptime
) flags
|= CIRCLAUNCH_NEED_UPTIME
;
978 launched
= circuit_launch_by_extend_info(
979 CIRCUIT_PURPOSE_S_CONNECT_REND
, extend_info
, flags
);
984 if (!launched
) { /* give up */
985 log_warn(LD_REND
, "Giving up launching first hop of circuit to rendezvous "
986 "point %s for service %s.",
987 escaped_safe_str(extend_info
->nickname
), serviceid
);
988 reason
= END_CIRC_REASON_CONNECTFAILED
;
992 "Accepted intro; launching circuit to %s "
993 "(cookie %s) for service %s.",
994 escaped_safe_str(extend_info
->nickname
), hexcookie
, serviceid
);
995 tor_assert(launched
->build_state
);
996 /* Fill in the circuit's state. */
997 memcpy(launched
->rend_pk_digest
, circuit
->rend_pk_digest
,
999 memcpy(launched
->rend_cookie
, r_cookie
, REND_COOKIE_LEN
);
1000 strlcpy(launched
->rend_query
, service
->service_id
,
1001 sizeof(launched
->rend_query
));
1002 launched
->rend_desc_version
= service
->descriptor_version
;
1003 launched
->build_state
->pending_final_cpath
= cpath
=
1004 tor_malloc_zero(sizeof(crypt_path_t
));
1005 cpath
->magic
= CRYPT_PATH_MAGIC
;
1006 launched
->build_state
->expiry_time
= time(NULL
) + MAX_REND_TIMEOUT
;
1008 cpath
->dh_handshake_state
= dh
;
1010 if (circuit_init_cpath_crypto(cpath
,keys
+DIGEST_LEN
,1)<0)
1012 memcpy(cpath
->handshake_digest
, keys
, DIGEST_LEN
);
1013 if (extend_info
) extend_info_free(extend_info
);
1017 if (dh
) crypto_dh_free(dh
);
1019 circuit_mark_for_close(TO_CIRCUIT(launched
), reason
);
1020 if (extend_info
) extend_info_free(extend_info
);
1024 /** Called when we fail building a rendezvous circuit at some point other
1025 * than the last hop: launches a new circuit to the same rendezvous point.
1028 rend_service_relaunch_rendezvous(origin_circuit_t
*oldcirc
)
1030 origin_circuit_t
*newcirc
;
1031 cpath_build_state_t
*newstate
, *oldstate
;
1033 tor_assert(oldcirc
->_base
.purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
);
1035 if (!oldcirc
->build_state
||
1036 oldcirc
->build_state
->failure_count
> MAX_REND_FAILURES
||
1037 oldcirc
->build_state
->expiry_time
< time(NULL
)) {
1039 "Attempt to build circuit to %s for rendezvous has failed "
1040 "too many times or expired; giving up.",
1041 oldcirc
->build_state
?
1042 oldcirc
->build_state
->chosen_exit
->nickname
: "*unknown*");
1046 oldstate
= oldcirc
->build_state
;
1047 tor_assert(oldstate
);
1049 if (oldstate
->pending_final_cpath
== NULL
) {
1050 log_info(LD_REND
,"Skipping relaunch of circ that failed on its first hop. "
1051 "Initiator will retry.");
1055 log_info(LD_REND
,"Reattempting rendezvous circuit to '%s'",
1056 oldstate
->chosen_exit
->nickname
);
1058 newcirc
= circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND
,
1059 oldstate
->chosen_exit
,
1060 CIRCLAUNCH_NEED_CAPACITY
|CIRCLAUNCH_IS_INTERNAL
);
1063 log_warn(LD_REND
,"Couldn't relaunch rendezvous circuit to '%s'.",
1064 oldstate
->chosen_exit
->nickname
);
1067 newstate
= newcirc
->build_state
;
1068 tor_assert(newstate
);
1069 newstate
->failure_count
= oldstate
->failure_count
+1;
1070 newstate
->expiry_time
= oldstate
->expiry_time
;
1071 newstate
->pending_final_cpath
= oldstate
->pending_final_cpath
;
1072 oldstate
->pending_final_cpath
= NULL
;
1074 memcpy(newcirc
->rend_query
, oldcirc
->rend_query
,
1075 REND_SERVICE_ID_LEN_BASE32
+1);
1076 memcpy(newcirc
->rend_pk_digest
, oldcirc
->rend_pk_digest
,
1078 memcpy(newcirc
->rend_cookie
, oldcirc
->rend_cookie
,
1080 newcirc
->rend_desc_version
= oldcirc
->rend_desc_version
;
1083 /** Launch a circuit to serve as an introduction point for the service
1084 * <b>service</b> at the introduction point <b>nickname</b>
1087 rend_service_launch_establish_intro(rend_service_t
*service
,
1088 rend_intro_point_t
*intro
)
1090 origin_circuit_t
*launched
;
1093 "Launching circuit to introduction point %s for service %s",
1094 escaped_safe_str(intro
->extend_info
->nickname
),
1095 service
->service_id
);
1097 rep_hist_note_used_internal(time(NULL
), 1, 0);
1099 ++service
->n_intro_circuits_launched
;
1100 launched
= circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
,
1102 CIRCLAUNCH_NEED_UPTIME
|CIRCLAUNCH_IS_INTERNAL
);
1106 "Can't launch circuit to establish introduction at %s.",
1107 escaped_safe_str(intro
->extend_info
->nickname
));
1111 if (memcmp(intro
->extend_info
->identity_digest
,
1112 launched
->build_state
->chosen_exit
->identity_digest
, DIGEST_LEN
)) {
1113 char cann
[HEX_DIGEST_LEN
+1], orig
[HEX_DIGEST_LEN
+1];
1114 base16_encode(cann
, sizeof(cann
),
1115 launched
->build_state
->chosen_exit
->identity_digest
,
1117 base16_encode(orig
, sizeof(orig
),
1118 intro
->extend_info
->identity_digest
, DIGEST_LEN
);
1119 log_info(LD_REND
, "The intro circuit we just cannibalized ends at $%s, "
1120 "but we requested an intro circuit to $%s. Updating "
1121 "our service.", cann
, orig
);
1122 extend_info_free(intro
->extend_info
);
1123 intro
->extend_info
= extend_info_dup(launched
->build_state
->chosen_exit
);
1126 strlcpy(launched
->rend_query
, service
->service_id
,
1127 sizeof(launched
->rend_query
));
1128 memcpy(launched
->rend_pk_digest
, service
->pk_digest
, DIGEST_LEN
);
1129 launched
->rend_desc_version
= service
->descriptor_version
;
1130 if (service
->descriptor_version
== 2)
1131 launched
->intro_key
= crypto_pk_dup_key(intro
->intro_key
);
1132 if (launched
->_base
.state
== CIRCUIT_STATE_OPEN
)
1133 rend_service_intro_has_opened(launched
);
1137 /** Called when we're done building a circuit to an introduction point:
1138 * sends a RELAY_ESTABLISH_INTRO cell.
1141 rend_service_intro_has_opened(origin_circuit_t
*circuit
)
1143 rend_service_t
*service
;
1146 char buf
[RELAY_PAYLOAD_SIZE
];
1147 char auth
[DIGEST_LEN
+ 9];
1148 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1149 int reason
= END_CIRC_REASON_TORPROTOCOL
;
1150 crypto_pk_env_t
*intro_key
;
1152 tor_assert(circuit
->_base
.purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
);
1153 tor_assert(circuit
->cpath
);
1155 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
1156 circuit
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1158 service
= rend_service_get_by_pk_digest_and_version(
1159 circuit
->rend_pk_digest
, circuit
->rend_desc_version
);
1161 log_warn(LD_REND
, "Unrecognized service ID %s on introduction circuit %d.",
1162 serviceid
, circuit
->_base
.n_circ_id
);
1163 reason
= END_CIRC_REASON_NOSUCHSERVICE
;
1168 "Established circuit %d as introduction point for service %s",
1169 circuit
->_base
.n_circ_id
, serviceid
);
1171 /* If the introduction point will not be used in an unversioned
1172 * descriptor, use the intro key instead of the service key in
1173 * ESTABLISH_INTRO. */
1174 if (service
->descriptor_version
== 0)
1175 intro_key
= service
->private_key
;
1177 intro_key
= circuit
->intro_key
;
1178 /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
1179 r
= crypto_pk_asn1_encode(intro_key
, buf
+2,
1180 RELAY_PAYLOAD_SIZE
-2);
1182 log_warn(LD_BUG
, "Internal error; failed to establish intro point.");
1183 reason
= END_CIRC_REASON_INTERNAL
;
1187 set_uint16(buf
, htons((uint16_t)len
));
1189 memcpy(auth
, circuit
->cpath
->prev
->handshake_digest
, DIGEST_LEN
);
1190 memcpy(auth
+DIGEST_LEN
, "INTRODUCE", 9);
1191 if (crypto_digest(buf
+len
, auth
, DIGEST_LEN
+9))
1194 note_crypto_pk_op(REND_SERVER
);
1195 r
= crypto_pk_private_sign_digest(intro_key
, buf
+len
, buf
, len
);
1197 log_warn(LD_BUG
, "Internal error: couldn't sign introduction request.");
1198 reason
= END_CIRC_REASON_INTERNAL
;
1203 if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit
),
1204 RELAY_COMMAND_ESTABLISH_INTRO
,
1205 buf
, len
, circuit
->cpath
->prev
)<0) {
1206 log_info(LD_GENERAL
,
1207 "Couldn't send introduction request for service %s on circuit %d",
1208 serviceid
, circuit
->_base
.n_circ_id
);
1209 reason
= END_CIRC_REASON_INTERNAL
;
1215 circuit_mark_for_close(TO_CIRCUIT(circuit
), reason
);
1218 /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
1219 * live introduction point, and note that the service descriptor is
1220 * now out-of-date.*/
1222 rend_service_intro_established(origin_circuit_t
*circuit
, const char *request
,
1225 rend_service_t
*service
;
1226 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1230 if (circuit
->_base
.purpose
!= CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
) {
1231 log_warn(LD_PROTOCOL
,
1232 "received INTRO_ESTABLISHED cell on non-intro circuit.");
1235 service
= rend_service_get_by_pk_digest_and_version(
1236 circuit
->rend_pk_digest
, circuit
->rend_desc_version
);
1238 log_warn(LD_REND
, "Unknown service on introduction circuit %d.",
1239 circuit
->_base
.n_circ_id
);
1242 service
->desc_is_dirty
= time(NULL
);
1243 circuit
->_base
.purpose
= CIRCUIT_PURPOSE_S_INTRO
;
1245 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+ 1,
1246 circuit
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1248 "Received INTRO_ESTABLISHED cell on circuit %d for service %s",
1249 circuit
->_base
.n_circ_id
, serviceid
);
1253 circuit_mark_for_close(TO_CIRCUIT(circuit
), END_CIRC_REASON_TORPROTOCOL
);
1257 /** Called once a circuit to a rendezvous point is established: sends a
1258 * RELAY_COMMAND_RENDEZVOUS1 cell.
1261 rend_service_rendezvous_has_opened(origin_circuit_t
*circuit
)
1263 rend_service_t
*service
;
1264 char buf
[RELAY_PAYLOAD_SIZE
];
1266 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1270 tor_assert(circuit
->_base
.purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
);
1271 tor_assert(circuit
->cpath
);
1272 tor_assert(circuit
->build_state
);
1273 hop
= circuit
->build_state
->pending_final_cpath
;
1276 base16_encode(hexcookie
,9,circuit
->rend_cookie
,4);
1277 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
1278 circuit
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1281 "Done building circuit %d to rendezvous with "
1282 "cookie %s for service %s",
1283 circuit
->_base
.n_circ_id
, hexcookie
, serviceid
);
1285 service
= rend_service_get_by_pk_digest_and_version(
1286 circuit
->rend_pk_digest
, circuit
->rend_desc_version
);
1288 log_warn(LD_GENERAL
, "Internal error: unrecognized service ID on "
1289 "introduction circuit.");
1290 reason
= END_CIRC_REASON_INTERNAL
;
1294 /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
1295 memcpy(buf
, circuit
->rend_cookie
, REND_COOKIE_LEN
);
1296 if (crypto_dh_get_public(hop
->dh_handshake_state
,
1297 buf
+REND_COOKIE_LEN
, DH_KEY_LEN
)<0) {
1298 log_warn(LD_GENERAL
,"Couldn't get DH public key.");
1299 reason
= END_CIRC_REASON_INTERNAL
;
1302 memcpy(buf
+REND_COOKIE_LEN
+DH_KEY_LEN
, hop
->handshake_digest
,
1306 if (relay_send_command_from_edge(0, TO_CIRCUIT(circuit
),
1307 RELAY_COMMAND_RENDEZVOUS1
,
1308 buf
, REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
,
1309 circuit
->cpath
->prev
)<0) {
1310 log_warn(LD_GENERAL
, "Couldn't send RENDEZVOUS1 cell.");
1311 reason
= END_CIRC_REASON_INTERNAL
;
1315 crypto_dh_free(hop
->dh_handshake_state
);
1316 hop
->dh_handshake_state
= NULL
;
1318 /* Append the cpath entry. */
1319 hop
->state
= CPATH_STATE_OPEN
;
1320 /* set the windows to default. these are the windows
1321 * that bob thinks alice has.
1323 hop
->package_window
= CIRCWINDOW_START
;
1324 hop
->deliver_window
= CIRCWINDOW_START
;
1326 onion_append_to_cpath(&circuit
->cpath
, hop
);
1327 circuit
->build_state
->pending_final_cpath
= NULL
; /* prevent double-free */
1329 /* Change the circuit purpose. */
1330 circuit
->_base
.purpose
= CIRCUIT_PURPOSE_S_REND_JOINED
;
1334 circuit_mark_for_close(TO_CIRCUIT(circuit
), reason
);
1338 * Manage introduction points
1341 /** Return the (possibly non-open) introduction circuit ending at
1342 * <b>intro</b> for the service whose public key is <b>pk_digest</b> and
1343 * which publishes descriptor of version <b>desc_version</b>. Return
1344 * NULL if no such service is found.
1346 static origin_circuit_t
*
1347 find_intro_circuit(rend_intro_point_t
*intro
, const char *pk_digest
,
1350 origin_circuit_t
*circ
= NULL
;
1353 while ((circ
= circuit_get_next_by_pk_and_purpose(circ
,pk_digest
,
1354 CIRCUIT_PURPOSE_S_INTRO
))) {
1355 if (!memcmp(circ
->build_state
->chosen_exit
->identity_digest
,
1356 intro
->extend_info
->identity_digest
, DIGEST_LEN
) &&
1357 circ
->rend_desc_version
== desc_version
) {
1363 while ((circ
= circuit_get_next_by_pk_and_purpose(circ
,pk_digest
,
1364 CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
))) {
1365 if (!memcmp(circ
->build_state
->chosen_exit
->identity_digest
,
1366 intro
->extend_info
->identity_digest
, DIGEST_LEN
) &&
1367 circ
->rend_desc_version
== desc_version
) {
1374 /** Determine the responsible hidden service directories for the
1375 * rend_encoded_v2_service_descriptor_t's in <b>descs</b> and upload them;
1376 * <b>service_id</b> and <b>seconds_valid</b> are only passed for logging
1379 directory_post_to_hs_dir(smartlist_t
*descs
, const char *service_id
,
1383 smartlist_t
*responsible_dirs
= smartlist_create();
1384 routerstatus_t
*hs_dir
;
1385 for (i
= 0; i
< smartlist_len(descs
); i
++) {
1386 rend_encoded_v2_service_descriptor_t
*desc
= smartlist_get(descs
, i
);
1387 /* Determine responsible dirs. */
1388 if (hid_serv_get_responsible_directories(responsible_dirs
,
1389 desc
->desc_id
) < 0) {
1390 log_warn(LD_REND
, "Could not determine the responsible hidden service "
1391 "directories to post descriptors to.");
1392 smartlist_free(responsible_dirs
);
1395 for (j
= 0; j
< smartlist_len(responsible_dirs
); j
++) {
1396 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
1397 hs_dir
= smartlist_get(responsible_dirs
, j
);
1398 /* Send publish request. */
1399 directory_initiate_command_routerstatus(hs_dir
,
1400 DIR_PURPOSE_UPLOAD_RENDDESC_V2
,
1401 ROUTER_PURPOSE_GENERAL
,
1402 1, NULL
, desc
->desc_str
,
1403 strlen(desc
->desc_str
), 0);
1404 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
1405 desc
->desc_id
, DIGEST_LEN
);
1406 log_info(LD_REND
, "Sending publish request for v2 descriptor for "
1407 "service '%s' with descriptor ID '%s' with validity "
1408 "of %d seconds to hidden service directory '%s' on "
1410 safe_str(service_id
),
1411 safe_str(desc_id_base32
),
1416 smartlist_clear(responsible_dirs
);
1418 smartlist_free(responsible_dirs
);
1421 /** Encode and sign up-to-date v0 and/or v2 service descriptors for
1422 * <b>service</b>, and upload it/them to all the dirservers/to the
1423 * responsible hidden service directories.
1426 upload_service_descriptor(rend_service_t
*service
)
1428 time_t now
= time(NULL
);
1430 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1433 /* Update the descriptor. */
1434 rend_service_update_descriptor(service
);
1436 rendpostperiod
= get_options()->RendPostPeriod
;
1438 /* Upload unversioned (v0) descriptor? */
1439 if (service
->descriptor_version
== 0 &&
1440 get_options()->PublishHidServDescriptors
) {
1443 /* Encode the descriptor. */
1444 if (rend_encode_service_descriptor(service
->desc
,
1445 service
->private_key
,
1446 &desc
, &desc_len
)<0) {
1447 log_warn(LD_BUG
, "Internal error: couldn't encode service descriptor; "
1452 /* Post it to the dirservers */
1453 rend_get_service_id(service
->desc
->pk
, serviceid
);
1454 log_info(LD_REND
, "Sending publish request for hidden service %s",
1456 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC
,
1457 ROUTER_PURPOSE_GENERAL
,
1458 HIDSERV_AUTHORITY
, desc
, desc_len
, 0);
1460 service
->next_upload_time
= now
+ rendpostperiod
;
1464 /* Upload v2 descriptor? */
1465 if (service
->descriptor_version
== 2 &&
1466 get_options()->PublishHidServDescriptors
) {
1467 networkstatus_t
*c
= networkstatus_get_latest_consensus();
1468 if (c
&& smartlist_len(c
->routerstatus_list
) > 0) {
1470 smartlist_t
*descs
= smartlist_create();
1472 /* Encode the current descriptor. */
1473 seconds_valid
= rend_encode_v2_descriptors(descs
, service
->desc
, now
,
1475 if (seconds_valid
< 0) {
1476 log_warn(LD_BUG
, "Internal error: couldn't encode service descriptor; "
1478 smartlist_free(descs
);
1481 /* Post the current descriptors to the hidden service directories. */
1482 rend_get_service_id(service
->desc
->pk
, serviceid
);
1483 log_info(LD_REND
, "Sending publish request for hidden service %s",
1485 directory_post_to_hs_dir(descs
, serviceid
, seconds_valid
);
1486 /* Free memory for descriptors. */
1487 for (i
= 0; i
< smartlist_len(descs
); i
++)
1488 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
1489 smartlist_clear(descs
);
1490 /* Update next upload time. */
1491 if (seconds_valid
- REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
1493 service
->next_upload_time
= now
+ rendpostperiod
;
1494 else if (seconds_valid
< REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
)
1495 service
->next_upload_time
= now
+ seconds_valid
+ 1;
1497 service
->next_upload_time
= now
+ seconds_valid
-
1498 REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
+ 1;
1499 /* Post also the next descriptors, if necessary. */
1500 if (seconds_valid
< REND_TIME_PERIOD_OVERLAPPING_V2_DESCS
) {
1501 seconds_valid
= rend_encode_v2_descriptors(descs
, service
->desc
,
1503 if (seconds_valid
< 0) {
1504 log_warn(LD_BUG
, "Internal error: couldn't encode service "
1505 "descriptor; not uploading.");
1506 smartlist_free(descs
);
1509 directory_post_to_hs_dir(descs
, serviceid
, seconds_valid
);
1510 /* Free memory for descriptors. */
1511 for (i
= 0; i
< smartlist_len(descs
); i
++)
1512 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
1514 smartlist_free(descs
);
1516 log_info(LD_REND
, "Successfully uploaded v2 rend descriptors!");
1520 /* If not uploaded, try again in one minute. */
1522 service
->next_upload_time
= now
+ 60;
1524 /* Unmark dirty flag of this service. */
1525 service
->desc_is_dirty
= 0;
1528 /** For every service, check how many intro points it currently has, and:
1529 * - Pick new intro points as necessary.
1530 * - Launch circuits to any new intro points.
1533 rend_services_introduce(void)
1536 routerinfo_t
*router
;
1537 rend_service_t
*service
;
1538 rend_intro_point_t
*intro
;
1539 int changed
, prev_intro_nodes
;
1540 smartlist_t
*intro_routers
;
1542 or_options_t
*options
= get_options();
1544 intro_routers
= smartlist_create();
1547 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1548 smartlist_clear(intro_routers
);
1549 service
= smartlist_get(rend_service_list
, i
);
1551 tor_assert(service
);
1553 if (now
> service
->intro_period_started
+INTRO_CIRC_RETRY_PERIOD
) {
1554 /* One period has elapsed; we can try building circuits again. */
1555 service
->intro_period_started
= now
;
1556 service
->n_intro_circuits_launched
= 0;
1557 } else if (service
->n_intro_circuits_launched
>=
1558 MAX_INTRO_CIRCS_PER_PERIOD
) {
1559 /* We have failed too many times in this period; wait for the next
1560 * one before we try again. */
1564 /* Find out which introduction points we have in progress for this
1566 for (j
=0; j
< smartlist_len(service
->intro_nodes
); ++j
) {
1567 intro
= smartlist_get(service
->intro_nodes
, j
);
1568 router
= router_get_by_digest(intro
->extend_info
->identity_digest
);
1569 if (!router
|| !find_intro_circuit(intro
, service
->pk_digest
,
1570 service
->descriptor_version
)) {
1571 log_info(LD_REND
,"Giving up on %s as intro point for %s.",
1572 intro
->extend_info
->nickname
, service
->service_id
);
1573 if (service
->desc
) {
1574 SMARTLIST_FOREACH(service
->desc
->intro_nodes
, rend_intro_point_t
*,
1576 if (!memcmp(dintro
->extend_info
->identity_digest
,
1577 intro
->extend_info
->identity_digest
, DIGEST_LEN
)) {
1578 log_info(LD_REND
, "The intro point we are giving up on was "
1579 "included in the last published descriptor. "
1580 "Marking current descriptor as dirty.");
1581 service
->desc_is_dirty
= now
;
1585 rend_intro_point_free(intro
);
1586 smartlist_del(service
->intro_nodes
,j
--);
1590 smartlist_add(intro_routers
, router
);
1593 /* We have enough intro points, and the intro points we thought we had were
1596 if (!changed
&& smartlist_len(service
->intro_nodes
) >= NUM_INTRO_POINTS
) {
1597 /* We have all our intro points! Start a fresh period and reset the
1599 service
->intro_period_started
= now
;
1600 service
->n_intro_circuits_launched
= 0;
1604 /* Remember how many introduction circuits we started with. */
1605 prev_intro_nodes
= smartlist_len(service
->intro_nodes
);
1607 /* The directory is now here. Pick three ORs as intro points. */
1608 for (j
=prev_intro_nodes
; j
< NUM_INTRO_POINTS
; ++j
) {
1609 router_crn_flags_t flags
= CRN_NEED_UPTIME
;
1610 if (get_options()->_AllowInvalid
& ALLOW_INVALID_INTRODUCTION
)
1611 flags
|= CRN_ALLOW_INVALID
;
1612 router
= router_choose_random_node(NULL
, intro_routers
,
1613 options
->ExcludeNodes
, flags
);
1616 "Could only establish %d introduction points for %s.",
1617 smartlist_len(service
->intro_nodes
), service
->service_id
);
1621 smartlist_add(intro_routers
, router
);
1622 intro
= tor_malloc_zero(sizeof(rend_intro_point_t
));
1623 intro
->extend_info
= extend_info_from_router(router
);
1624 if (service
->descriptor_version
== 2) {
1625 intro
->intro_key
= crypto_new_pk_env();
1626 tor_assert(!crypto_pk_generate_key(intro
->intro_key
));
1628 smartlist_add(service
->intro_nodes
, intro
);
1629 log_info(LD_REND
, "Picked router %s as an intro point for %s.",
1630 router
->nickname
, service
->service_id
);
1633 /* If there's no need to launch new circuits, stop here. */
1637 /* Establish new introduction points. */
1638 for (j
=prev_intro_nodes
; j
< smartlist_len(service
->intro_nodes
); ++j
) {
1639 intro
= smartlist_get(service
->intro_nodes
, j
);
1640 r
= rend_service_launch_establish_intro(service
, intro
);
1642 log_warn(LD_REND
, "Error launching circuit to node %s for service %s.",
1643 intro
->extend_info
->nickname
, service
->service_id
);
1647 smartlist_free(intro_routers
);
1650 /** Regenerate and upload rendezvous service descriptors for all
1651 * services, if necessary. If the descriptor has been dirty enough
1652 * for long enough, definitely upload; else only upload when the
1653 * periodic timeout has expired.
1655 * For the first upload, pick a random time between now and two periods
1656 * from now, and pick it independently for each service.
1659 rend_consider_services_upload(time_t now
)
1662 rend_service_t
*service
;
1663 int rendpostperiod
= get_options()->RendPostPeriod
;
1665 if (!get_options()->PublishHidServDescriptors
)
1668 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1669 service
= smartlist_get(rend_service_list
, i
);
1670 if (!service
->next_upload_time
) { /* never been uploaded yet */
1671 /* The fixed lower bound of 30 seconds ensures that the descriptor
1672 * is stable before being published. See comment below. */
1673 service
->next_upload_time
=
1674 now
+ 30 + crypto_rand_int(2*rendpostperiod
);
1676 if (service
->next_upload_time
< now
||
1677 (service
->desc_is_dirty
&&
1678 service
->desc_is_dirty
< now
-30)) {
1679 /* if it's time, or if the directory servers have a wrong service
1680 * descriptor and ours has been stable for 30 seconds, upload a
1681 * new one of each format. */
1682 upload_service_descriptor(service
);
1687 /** Log the status of introduction points for all rendezvous services
1688 * at log severity <b>severity</b>.
1691 rend_service_dump_stats(int severity
)
1694 rend_service_t
*service
;
1695 rend_intro_point_t
*intro
;
1696 const char *safe_name
;
1697 origin_circuit_t
*circ
;
1699 for (i
=0; i
< smartlist_len(rend_service_list
); ++i
) {
1700 service
= smartlist_get(rend_service_list
, i
);
1701 log(severity
, LD_GENERAL
, "Service configured in \"%s\":",
1702 service
->directory
);
1703 for (j
=0; j
< smartlist_len(service
->intro_nodes
); ++j
) {
1704 intro
= smartlist_get(service
->intro_nodes
, j
);
1705 safe_name
= safe_str(intro
->extend_info
->nickname
);
1707 circ
= find_intro_circuit(intro
, service
->pk_digest
,
1708 service
->descriptor_version
);
1710 log(severity
, LD_GENERAL
, " Intro point %d at %s: no circuit",
1714 log(severity
, LD_GENERAL
, " Intro point %d at %s: circuit is %s",
1715 j
, safe_name
, circuit_state_to_string(circ
->_base
.state
));
1720 /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
1721 * 'circ', and look up the port and address based on conn-\>port.
1722 * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
1726 rend_service_set_connection_addr_port(edge_connection_t
*conn
,
1727 origin_circuit_t
*circ
)
1729 rend_service_t
*service
;
1730 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
1731 smartlist_t
*matching_ports
;
1732 rend_service_port_config_t
*chosen_port
;
1734 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_S_REND_JOINED
);
1735 log_debug(LD_REND
,"beginning to hunt for addr/port");
1736 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
1737 circ
->rend_pk_digest
, REND_SERVICE_ID_LEN
);
1738 service
= rend_service_get_by_pk_digest_and_version(circ
->rend_pk_digest
,
1739 circ
->rend_desc_version
);
1741 log_warn(LD_REND
, "Couldn't find any service associated with pk %s on "
1742 "rendezvous circuit %d; closing.",
1743 serviceid
, circ
->_base
.n_circ_id
);
1746 matching_ports
= smartlist_create();
1747 SMARTLIST_FOREACH(service
->ports
, rend_service_port_config_t
*, p
,
1749 if (conn
->_base
.port
== p
->virtual_port
) {
1750 smartlist_add(matching_ports
, p
);
1753 chosen_port
= smartlist_choose(matching_ports
);
1754 smartlist_free(matching_ports
);
1756 tor_addr_copy(&conn
->_base
.addr
, &chosen_port
->real_addr
);
1757 conn
->_base
.port
= chosen_port
->real_port
;
1760 log_info(LD_REND
, "No virtual port mapping exists for port %d on service %s",
1761 conn
->_base
.port
,serviceid
);