1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2009, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Client code to access location-hidden services.
12 /** Called when we've established a circuit to an introduction point:
13 * send the introduction request. */
15 rend_client_introcirc_has_opened(origin_circuit_t
*circ
)
17 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
18 tor_assert(circ
->cpath
);
20 log_info(LD_REND
,"introcirc is open");
21 connection_ap_attach_pending();
24 /** Send the establish-rendezvous cell along a rendezvous circuit. if
25 * it fails, mark the circ for close and return -1. else return 0.
28 rend_client_send_establish_rendezvous(origin_circuit_t
*circ
)
30 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
);
31 tor_assert(circ
->rend_data
);
32 log_info(LD_REND
, "Sending an ESTABLISH_RENDEZVOUS cell");
34 if (crypto_rand(circ
->rend_data
->rend_cookie
, REND_COOKIE_LEN
) < 0) {
35 log_warn(LD_BUG
, "Internal error: Couldn't produce random cookie.");
36 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
39 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ
),
40 RELAY_COMMAND_ESTABLISH_RENDEZVOUS
,
41 circ
->rend_data
->rend_cookie
,
43 circ
->cpath
->prev
)<0) {
44 /* circ is already marked for close */
45 log_warn(LD_GENERAL
, "Couldn't send ESTABLISH_RENDEZVOUS cell");
52 /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
53 * down introcirc if possible.
56 rend_client_send_introduction(origin_circuit_t
*introcirc
,
57 origin_circuit_t
*rendcirc
)
61 char payload
[RELAY_PAYLOAD_SIZE
];
62 char tmp
[RELAY_PAYLOAD_SIZE
];
63 rend_cache_entry_t
*entry
;
66 crypto_pk_env_t
*intro_key
= NULL
;
68 tor_assert(introcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
69 tor_assert(rendcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_REND_READY
);
70 tor_assert(introcirc
->rend_data
);
71 tor_assert(rendcirc
->rend_data
);
72 tor_assert(!rend_cmp_service_ids(introcirc
->rend_data
->onion_address
,
73 rendcirc
->rend_data
->onion_address
));
75 if (rend_cache_lookup_entry(introcirc
->rend_data
->onion_address
, -1,
78 "query %s didn't have valid rend desc in cache. Failing.",
79 escaped_safe_str(introcirc
->rend_data
->onion_address
));
83 /* first 20 bytes of payload are the hash of the intro key */
85 SMARTLIST_FOREACH(entry
->parsed
->intro_nodes
, rend_intro_point_t
*,
87 if (!memcmp(introcirc
->build_state
->chosen_exit
->identity_digest
,
88 intro
->extend_info
->identity_digest
, DIGEST_LEN
)) {
89 intro_key
= intro
->intro_key
;
94 log_warn(LD_BUG
, "Internal error: could not find intro key.");
97 if (crypto_pk_get_digest(intro_key
, payload
)<0) {
98 log_warn(LD_BUG
, "Internal error: couldn't hash public key.");
102 /* Initialize the pending_final_cpath and start the DH handshake. */
103 cpath
= rendcirc
->build_state
->pending_final_cpath
;
105 cpath
= rendcirc
->build_state
->pending_final_cpath
=
106 tor_malloc_zero(sizeof(crypt_path_t
));
107 cpath
->magic
= CRYPT_PATH_MAGIC
;
108 if (!(cpath
->dh_handshake_state
= crypto_dh_new())) {
109 log_warn(LD_BUG
, "Internal error: couldn't allocate DH.");
112 if (crypto_dh_generate_public(cpath
->dh_handshake_state
)<0) {
113 log_warn(LD_BUG
, "Internal error: couldn't generate g^x.");
118 /* If version is 3, write (optional) auth data and timestamp. */
119 if (entry
->parsed
->protocols
& (1<<3)) {
120 tmp
[0] = 3; /* version 3 of the cell format */
121 tmp
[1] = (uint8_t)introcirc
->rend_data
->auth_type
; /* auth type, if any */
123 if (introcirc
->rend_data
->auth_type
!= REND_NO_AUTH
) {
124 set_uint16(tmp
+2, htons(REND_DESC_COOKIE_LEN
));
125 memcpy(tmp
+4, introcirc
->rend_data
->descriptor_cookie
,
126 REND_DESC_COOKIE_LEN
);
127 v3_shift
+= 2+REND_DESC_COOKIE_LEN
;
129 set_uint32(tmp
+v3_shift
+1, htonl(time(NULL
)));
131 } /* if version 2 only write version number */
132 else if (entry
->parsed
->protocols
& (1<<2)) {
133 tmp
[0] = 2; /* version 2 of the cell format */
136 /* write the remaining items into tmp */
137 if (entry
->parsed
->protocols
& (1<<3) || entry
->parsed
->protocols
& (1<<2)) {
138 /* version 2 format */
139 extend_info_t
*extend_info
= rendcirc
->build_state
->chosen_exit
;
142 set_uint32(tmp
+v3_shift
+1, tor_addr_to_ipv4h(&extend_info
->addr
));
143 set_uint16(tmp
+v3_shift
+5, htons(extend_info
->port
));
144 memcpy(tmp
+v3_shift
+7, extend_info
->identity_digest
, DIGEST_LEN
);
145 klen
= crypto_pk_asn1_encode(extend_info
->onion_key
,
146 tmp
+v3_shift
+7+DIGEST_LEN
+2,
147 sizeof(tmp
)-(v3_shift
+7+DIGEST_LEN
+2));
148 set_uint16(tmp
+v3_shift
+7+DIGEST_LEN
, htons(klen
));
149 memcpy(tmp
+v3_shift
+7+DIGEST_LEN
+2+klen
, rendcirc
->rend_data
->rend_cookie
,
151 dh_offset
= v3_shift
+7+DIGEST_LEN
+2+klen
+REND_COOKIE_LEN
;
154 strncpy(tmp
, rendcirc
->build_state
->chosen_exit
->nickname
,
155 (MAX_NICKNAME_LEN
+1)); /* nul pads */
156 memcpy(tmp
+MAX_NICKNAME_LEN
+1, rendcirc
->rend_data
->rend_cookie
,
158 dh_offset
= MAX_NICKNAME_LEN
+1+REND_COOKIE_LEN
;
161 if (crypto_dh_get_public(cpath
->dh_handshake_state
, tmp
+dh_offset
,
163 log_warn(LD_BUG
, "Internal error: couldn't extract g^x.");
167 note_crypto_pk_op(REND_CLIENT
);
168 /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
169 * to avoid buffer overflows? */
170 r
= crypto_pk_public_hybrid_encrypt(intro_key
, payload
+DIGEST_LEN
,
172 (int)(dh_offset
+DH_KEY_LEN
),
173 PK_PKCS1_OAEP_PADDING
, 0);
175 log_warn(LD_BUG
,"Internal error: hybrid pk encrypt failed.");
179 payload_len
= DIGEST_LEN
+ r
;
180 tor_assert(payload_len
<= RELAY_PAYLOAD_SIZE
); /* we overran something */
182 log_info(LD_REND
, "Sending an INTRODUCE1 cell");
183 if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc
),
184 RELAY_COMMAND_INTRODUCE1
,
185 payload
, payload_len
,
186 introcirc
->cpath
->prev
)<0) {
187 /* introcirc is already marked for close. leave rendcirc alone. */
188 log_warn(LD_BUG
, "Couldn't send INTRODUCE1 cell");
192 /* Now, we wait for an ACK or NAK on this circuit. */
193 introcirc
->_base
.purpose
= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
;
197 circuit_mark_for_close(TO_CIRCUIT(introcirc
), END_CIRC_REASON_INTERNAL
);
198 circuit_mark_for_close(TO_CIRCUIT(rendcirc
), END_CIRC_REASON_INTERNAL
);
202 /** Called when a rendezvous circuit is open; sends a establish
203 * rendezvous circuit as appropriate. */
205 rend_client_rendcirc_has_opened(origin_circuit_t
*circ
)
207 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
);
209 log_info(LD_REND
,"rendcirc is open");
211 /* generate a rendezvous cookie, store it in circ */
212 if (rend_client_send_establish_rendezvous(circ
) < 0) {
217 /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
220 rend_client_introduction_acked(origin_circuit_t
*circ
,
221 const char *request
, size_t request_len
)
223 origin_circuit_t
*rendcirc
;
224 (void) request
; // XXXX Use this.
226 if (circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
227 log_warn(LD_PROTOCOL
,
228 "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
229 circ
->_base
.n_circ_id
);
230 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
234 tor_assert(circ
->build_state
->chosen_exit
);
235 tor_assert(circ
->rend_data
);
237 if (request_len
== 0) {
238 /* It's an ACK; the introduction point relayed our introduction request. */
239 /* Locate the rend circ which is waiting to hear about this ack,
242 log_info(LD_REND
,"Received ack. Telling rend circ...");
243 rendcirc
= circuit_get_by_rend_query_and_purpose(
244 circ
->rend_data
->onion_address
, CIRCUIT_PURPOSE_C_REND_READY
);
245 if (rendcirc
) { /* remember the ack */
246 rendcirc
->_base
.purpose
= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
;
248 log_info(LD_REND
,"...Found no rend circ. Dropping on the floor.");
250 /* close the circuit: we won't need it anymore. */
251 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
;
252 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_FINISHED
);
254 /* It's a NAK; the introduction point didn't relay our request. */
255 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_INTRODUCING
;
256 /* Remove this intro point from the set of viable introduction
257 * points. If any remain, extend to a new one and try again.
258 * If none remain, refetch the service descriptor.
260 if (rend_client_remove_intro_point(circ
->build_state
->chosen_exit
,
261 circ
->rend_data
) > 0) {
262 /* There are introduction points left. Re-extend the circuit to
263 * another intro point and try again. */
264 extend_info_t
*extend_info
;
266 extend_info
= rend_client_get_random_intro(circ
->rend_data
);
268 log_warn(LD_REND
, "No introduction points left for %s. Closing.",
269 escaped_safe_str(circ
->rend_data
->onion_address
));
270 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
273 if (circ
->remaining_relay_early_cells
) {
275 "Got nack for %s from %s. Re-extending circ %d, "
277 escaped_safe_str(circ
->rend_data
->onion_address
),
278 circ
->build_state
->chosen_exit
->nickname
,
279 circ
->_base
.n_circ_id
, extend_info
->nickname
);
280 result
= circuit_extend_to_new_exit(circ
, extend_info
);
283 "Got nack for %s from %s. Building a new introduction "
284 "circuit, this time to %s.",
285 escaped_safe_str(circ
->rend_data
->onion_address
),
286 circ
->build_state
->chosen_exit
->nickname
,
287 extend_info
->nickname
);
288 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_FINISHED
);
289 if (!circuit_launch_by_extend_info(CIRCUIT_PURPOSE_C_INTRODUCING
,
291 CIRCLAUNCH_IS_INTERNAL
)) {
292 log_warn(LD_REND
, "Building introduction circuit failed.");
298 extend_info_free(extend_info
);
305 /** The period for which a hidden service directory cannot be queried for
306 * the same descriptor ID again. */
307 #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
309 /** Contains the last request times to hidden service directories for
310 * certain queries; keys are strings consisting of base32-encoded
311 * hidden service directory identities and base32-encoded descriptor IDs;
312 * values are pointers to timestamps of the last requests. */
313 static strmap_t
*last_hid_serv_requests
= NULL
;
315 /** Look up the last request time to hidden service directory <b>hs_dir</b>
316 * for descriptor ID <b>desc_id_base32</b>. If <b>set</b> is non-zero,
317 * assign the current time <b>now</b> and return that. Otherwise, return
318 * the most recent request time, or 0 if no such request has been sent
321 lookup_last_hid_serv_request(routerstatus_t
*hs_dir
,
322 const char *desc_id_base32
, time_t now
, int set
)
324 char hsdir_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
325 char hsdir_desc_comb_id
[2 * REND_DESC_ID_V2_LEN_BASE32
+ 1];
326 time_t *last_request_ptr
;
327 base32_encode(hsdir_id_base32
, sizeof(hsdir_id_base32
),
328 hs_dir
->identity_digest
, DIGEST_LEN
);
329 tor_snprintf(hsdir_desc_comb_id
, sizeof(hsdir_desc_comb_id
), "%s%s",
330 hsdir_id_base32
, desc_id_base32
);
332 last_request_ptr
= tor_malloc_zero(sizeof(time_t *));
333 *last_request_ptr
= now
;
334 strmap_set(last_hid_serv_requests
, hsdir_desc_comb_id
, last_request_ptr
);
336 last_request_ptr
= strmap_get_lc(last_hid_serv_requests
,
338 return (last_request_ptr
) ? *last_request_ptr
: 0;
341 /** Clean the history of request times to hidden service directories, so that
342 * it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD
343 * seconds any more. */
345 directory_clean_last_hid_serv_requests(void)
348 time_t cutoff
= time(NULL
) - REND_HID_SERV_DIR_REQUERY_PERIOD
;
349 if (!last_hid_serv_requests
)
350 last_hid_serv_requests
= strmap_new();
351 for (iter
= strmap_iter_init(last_hid_serv_requests
);
352 !strmap_iter_done(iter
); ) {
356 strmap_iter_get(iter
, &key
, &val
);
357 ent
= (time_t *) val
;
359 iter
= strmap_iter_next_rmv(last_hid_serv_requests
, iter
);
362 iter
= strmap_iter_next(last_hid_serv_requests
, iter
);
367 /** Determine the responsible hidden service directories for <b>desc_id</b>
368 * and fetch the descriptor belonging to that ID from one of them. Only
369 * send a request to hidden service directories that we did not try within
370 * the last REND_HID_SERV_DIR_REQUERY_PERIOD seconds; on success, return 1,
371 * in the case that no hidden service directory is left to ask for the
372 * descriptor, return 0, and in case of a failure -1. <b>query</b> is only
373 * passed for pretty log statements. */
375 directory_get_from_hs_dir(const char *desc_id
, const rend_data_t
*rend_query
)
377 smartlist_t
*responsible_dirs
= smartlist_create();
378 routerstatus_t
*hs_dir
;
379 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
380 time_t now
= time(NULL
);
381 char descriptor_cookie_base64
[3*REND_DESC_COOKIE_LEN_BASE64
];
383 tor_assert(rend_query
);
384 /* Determine responsible dirs. Even if we can't get all we want,
385 * work with the ones we have. If it's empty, we'll notice below. */
386 (int) hid_serv_get_responsible_directories(responsible_dirs
, desc_id
);
388 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
389 desc_id
, DIGEST_LEN
);
391 /* Only select those hidden service directories to which we did not send
392 * a request recently and for which we have a router descriptor here. */
393 directory_clean_last_hid_serv_requests(); /* Clean request history first. */
395 SMARTLIST_FOREACH(responsible_dirs
, routerstatus_t
*, dir
, {
396 if (lookup_last_hid_serv_request(dir
, desc_id_base32
, 0, 0) +
397 REND_HID_SERV_DIR_REQUERY_PERIOD
>= now
||
398 !router_get_by_digest(dir
->identity_digest
))
399 SMARTLIST_DEL_CURRENT(responsible_dirs
, dir
);
402 hs_dir
= smartlist_choose(responsible_dirs
);
403 smartlist_free(responsible_dirs
);
405 log_info(LD_REND
, "Could not pick one of the responsible hidden "
406 "service directories, because we requested them all "
407 "recently without success.");
411 /* Remember, that we are requesting a descriptor from this hidden service
413 lookup_last_hid_serv_request(hs_dir
, desc_id_base32
, now
, 1);
415 /* Encode descriptor cookie for logging purposes. */
416 if (rend_query
->auth_type
!= REND_NO_AUTH
) {
417 if (base64_encode(descriptor_cookie_base64
,
418 sizeof(descriptor_cookie_base64
),
419 rend_query
->descriptor_cookie
, REND_DESC_COOKIE_LEN
)<0) {
420 log_warn(LD_BUG
, "Could not base64-encode descriptor cookie.");
423 /* Remove == signs and newline. */
424 descriptor_cookie_base64
[strlen(descriptor_cookie_base64
)-3] = '\0';
426 strlcpy(descriptor_cookie_base64
, "(none)",
427 sizeof(descriptor_cookie_base64
));
430 /* Send fetch request. (Pass query and possibly descriptor cookie so that
431 * they can be written to the directory connection and be referred to when
432 * the response arrives. */
433 directory_initiate_command_routerstatus_rend(hs_dir
,
434 DIR_PURPOSE_FETCH_RENDDESC_V2
,
435 ROUTER_PURPOSE_GENERAL
,
436 1, desc_id_base32
, NULL
, 0, 0,
438 log_info(LD_REND
, "Sending fetch request for v2 descriptor for "
439 "service '%s' with descriptor ID '%s', auth type %d, "
440 "and descriptor cookie '%s' to hidden service "
441 "directory '%s' on port %d.",
442 rend_query
->onion_address
, desc_id_base32
,
443 rend_query
->auth_type
,
444 (rend_query
->auth_type
== REND_NO_AUTH
? "[none]" :
445 escaped_safe_str(descriptor_cookie_base64
)),
446 hs_dir
->nickname
, hs_dir
->dir_port
);
450 /** Start a connection to a hidden service directory to fetch a v2
451 * rendezvous service descriptor for the base32-encoded service ID
455 rend_client_refetch_v2_renddesc(const rend_data_t
*rend_query
)
457 char descriptor_id
[DIGEST_LEN
];
458 int replicas_left_to_try
[REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
];
460 rend_cache_entry_t
*e
= NULL
;
461 tor_assert(rend_query
);
462 /* Are we configured to fetch descriptors? */
463 if (!get_options()->FetchHidServDescriptors
) {
464 log_warn(LD_REND
, "We received an onion address for a v2 rendezvous "
465 "service descriptor, but are not fetching service descriptors.");
468 /* Before fetching, check if we already have the descriptor here. */
469 if (rend_cache_lookup_entry(rend_query
->onion_address
, -1, &e
) > 0) {
470 log_info(LD_REND
, "We would fetch a v2 rendezvous descriptor, but we "
471 "already have that descriptor here. Not fetching.");
474 log_debug(LD_REND
, "Fetching v2 rendezvous descriptor for service %s",
475 safe_str(rend_query
->onion_address
));
476 /* Randomly iterate over the replicas until a descriptor can be fetched
477 * from one of the consecutive nodes, or no options are left. */
478 tries_left
= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
;
479 for (i
= 0; i
< REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
; i
++)
480 replicas_left_to_try
[i
] = i
;
481 while (tries_left
> 0) {
482 int rand
= crypto_rand_int(tries_left
);
483 int chosen_replica
= replicas_left_to_try
[rand
];
484 replicas_left_to_try
[rand
] = replicas_left_to_try
[--tries_left
];
486 if (rend_compute_v2_desc_id(descriptor_id
, rend_query
->onion_address
,
487 rend_query
->auth_type
== REND_STEALTH_AUTH
?
488 rend_query
->descriptor_cookie
: NULL
,
489 time(NULL
), chosen_replica
) < 0) {
490 log_warn(LD_REND
, "Internal error: Computing v2 rendezvous "
491 "descriptor ID did not succeed.");
494 if (directory_get_from_hs_dir(descriptor_id
, rend_query
) != 0)
495 return; /* either success or failure, but we're done */
497 /* If we come here, there are no hidden service directories left. */
498 log_info(LD_REND
, "Could not pick one of the responsible hidden "
499 "service directories to fetch descriptors, because "
500 "we already tried them all unsuccessfully.");
501 /* Close pending connections. */
502 rend_client_desc_trynow(rend_query
->onion_address
);
506 /** Remove failed_intro from ent. If ent now has no intro points, or
507 * service is unrecognized, then launch a new renddesc fetch.
509 * Return -1 if error, 0 if no intro points remain or service
510 * unrecognized, 1 if recognized and some intro points remain.
513 rend_client_remove_intro_point(extend_info_t
*failed_intro
,
514 const rend_data_t
*rend_query
)
517 rend_cache_entry_t
*ent
;
520 r
= rend_cache_lookup_entry(rend_query
->onion_address
, -1, &ent
);
522 log_warn(LD_BUG
, "Malformed service ID %s.",
523 escaped_safe_str(rend_query
->onion_address
));
527 log_info(LD_REND
, "Unknown service %s. Re-fetching descriptor.",
528 escaped_safe_str(rend_query
->onion_address
));
529 rend_client_refetch_v2_renddesc(rend_query
);
533 for (i
= 0; i
< smartlist_len(ent
->parsed
->intro_nodes
); i
++) {
534 rend_intro_point_t
*intro
= smartlist_get(ent
->parsed
->intro_nodes
, i
);
535 if (!memcmp(failed_intro
->identity_digest
,
536 intro
->extend_info
->identity_digest
, DIGEST_LEN
)) {
537 rend_intro_point_free(intro
);
538 smartlist_del(ent
->parsed
->intro_nodes
, i
);
543 if (smartlist_len(ent
->parsed
->intro_nodes
) == 0) {
545 "No more intro points remain for %s. Re-fetching descriptor.",
546 escaped_safe_str(rend_query
->onion_address
));
547 rend_client_refetch_v2_renddesc(rend_query
);
549 /* move all pending streams back to renddesc_wait */
550 while ((conn
= connection_get_by_type_state_rendquery(CONN_TYPE_AP
,
551 AP_CONN_STATE_CIRCUIT_WAIT
,
552 rend_query
->onion_address
))) {
553 conn
->state
= AP_CONN_STATE_RENDDESC_WAIT
;
558 log_info(LD_REND
,"%d options left for %s.",
559 smartlist_len(ent
->parsed
->intro_nodes
),
560 escaped_safe_str(rend_query
->onion_address
));
564 /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
565 * the circuit to C_REND_READY.
568 rend_client_rendezvous_acked(origin_circuit_t
*circ
, const char *request
,
573 /* we just got an ack for our establish-rendezvous. switch purposes. */
574 if (circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_ESTABLISH_REND
) {
575 log_warn(LD_PROTOCOL
,"Got a rendezvous ack when we weren't expecting one. "
577 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
580 log_info(LD_REND
,"Got rendezvous ack. This circuit is now ready for "
582 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_REND_READY
;
583 /* XXXX022 This is a pretty brute-force approach. It'd be better to
584 * attach only the connections that are waiting on this circuit, rather
585 * than trying to attach them all. See comments bug 743. */
586 /* If we already have the introduction circuit built, make sure we send
587 * the INTRODUCE cell _now_ */
588 connection_ap_attach_pending();
592 /** Bob sent us a rendezvous cell; join the circuits. */
594 rend_client_receive_rendezvous(origin_circuit_t
*circ
, const char *request
,
598 char keys
[DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
];
600 if ((circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_REND_READY
&&
601 circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
)
602 || !circ
->build_state
->pending_final_cpath
) {
603 log_warn(LD_PROTOCOL
,"Got rendezvous2 cell from hidden service, but not "
604 "expecting it. Closing.");
605 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
609 if (request_len
!= DH_KEY_LEN
+DIGEST_LEN
) {
610 log_warn(LD_PROTOCOL
,"Incorrect length (%d) on RENDEZVOUS2 cell.",
615 log_info(LD_REND
,"Got RENDEZVOUS2 cell from hidden service.");
617 /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
618 tor_assert(circ
->build_state
);
619 tor_assert(circ
->build_state
->pending_final_cpath
);
620 hop
= circ
->build_state
->pending_final_cpath
;
621 tor_assert(hop
->dh_handshake_state
);
622 if (crypto_dh_compute_secret(hop
->dh_handshake_state
, request
, DH_KEY_LEN
,
623 keys
, DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
)<0) {
624 log_warn(LD_GENERAL
, "Couldn't complete DH handshake.");
627 /* ... and set up cpath. */
628 if (circuit_init_cpath_crypto(hop
, keys
+DIGEST_LEN
, 0)<0)
631 /* Check whether the digest is right... */
632 if (memcmp(keys
, request
+DH_KEY_LEN
, DIGEST_LEN
)) {
633 log_warn(LD_PROTOCOL
, "Incorrect digest of key material.");
637 crypto_dh_free(hop
->dh_handshake_state
);
638 hop
->dh_handshake_state
= NULL
;
640 /* All is well. Extend the circuit. */
641 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_REND_JOINED
;
642 hop
->state
= CPATH_STATE_OPEN
;
643 /* set the windows to default. these are the windows
644 * that alice thinks bob has.
646 hop
->package_window
= CIRCWINDOW_START
;
647 hop
->deliver_window
= CIRCWINDOW_START
;
649 onion_append_to_cpath(&circ
->cpath
, hop
);
650 circ
->build_state
->pending_final_cpath
= NULL
; /* prevent double-free */
651 /* XXXX022 This is a pretty brute-force approach. It'd be better to
652 * attach only the connections that are waiting on this circuit, rather
653 * than trying to attach them all. See comments bug 743. */
654 connection_ap_attach_pending();
657 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
661 /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that are
662 * waiting on <b>query</b>. If there's a working cache entry here with at
663 * least one intro point, move them to the next state. */
665 rend_client_desc_trynow(const char *query
)
667 edge_connection_t
*conn
;
668 rend_cache_entry_t
*entry
;
669 time_t now
= time(NULL
);
670 smartlist_t
*conns
= get_connection_array();
672 SMARTLIST_FOREACH(conns
, connection_t
*, _conn
,
674 if (_conn
->type
!= CONN_TYPE_AP
||
675 _conn
->state
!= AP_CONN_STATE_RENDDESC_WAIT
||
676 _conn
->marked_for_close
)
678 conn
= TO_EDGE_CONN(_conn
);
679 if (!conn
->rend_data
)
681 if (rend_cmp_service_ids(query
, conn
->rend_data
->onion_address
))
683 assert_connection_ok(TO_CONN(conn
), now
);
684 if (rend_cache_lookup_entry(conn
->rend_data
->onion_address
, -1,
686 smartlist_len(entry
->parsed
->intro_nodes
) > 0) {
687 /* either this fetch worked, or it failed but there was a
688 * valid entry from before which we should reuse */
689 log_info(LD_REND
,"Rend desc is usable. Launching circuits.");
690 conn
->_base
.state
= AP_CONN_STATE_CIRCUIT_WAIT
;
692 /* restart their timeout values, so they get a fair shake at
693 * connecting to the hidden service. */
694 conn
->_base
.timestamp_created
= now
;
695 conn
->_base
.timestamp_lastread
= now
;
696 conn
->_base
.timestamp_lastwritten
= now
;
698 if (connection_ap_handshake_attach_circuit(conn
) < 0) {
699 /* it will never work */
700 log_warn(LD_REND
,"Rendezvous attempt failed. Closing.");
701 if (!conn
->_base
.marked_for_close
)
702 connection_mark_unattached_ap(conn
, END_STREAM_REASON_CANT_ATTACH
);
704 } else { /* 404, or fetch didn't get that far */
705 log_notice(LD_REND
,"Closing stream for '%s.onion': hidden service is "
706 "unavailable (try again later).", safe_str(query
));
707 connection_mark_unattached_ap(conn
, END_STREAM_REASON_RESOLVEFAILED
);
712 /** Return a newly allocated extend_info_t* for a randomly chosen introduction
713 * point for the named hidden service. Return NULL if all introduction points
714 * have been tried and failed.
717 rend_client_get_random_intro(const rend_data_t
*rend_query
)
720 rend_cache_entry_t
*entry
;
721 rend_intro_point_t
*intro
;
722 routerinfo_t
*router
;
724 if (rend_cache_lookup_entry(rend_query
->onion_address
, -1, &entry
) < 1) {
726 "Query '%s' didn't have valid rend desc in cache. Failing.",
727 safe_str(rend_query
->onion_address
));
732 if (smartlist_len(entry
->parsed
->intro_nodes
) == 0)
735 i
= crypto_rand_int(smartlist_len(entry
->parsed
->intro_nodes
));
736 intro
= smartlist_get(entry
->parsed
->intro_nodes
, i
);
737 /* Do we need to look up the router or is the extend info complete? */
738 if (!intro
->extend_info
->onion_key
) {
739 router
= router_get_by_nickname(intro
->extend_info
->nickname
, 0);
741 log_info(LD_REND
, "Unknown router with nickname '%s'; trying another.",
742 intro
->extend_info
->nickname
);
743 rend_intro_point_free(intro
);
744 smartlist_del(entry
->parsed
->intro_nodes
, i
);
747 extend_info_free(intro
->extend_info
);
748 intro
->extend_info
= extend_info_from_router(router
);
750 return extend_info_dup(intro
->extend_info
);
753 /** Client-side authorizations for hidden services; map of onion address to
754 * rend_service_authorization_t*. */
755 static strmap_t
*auth_hid_servs
= NULL
;
757 /** Look up the client-side authorization for the hidden service with
758 * <b>onion_address</b>. Return NULL if no authorization is available for
760 rend_service_authorization_t
*
761 rend_client_lookup_service_authorization(const char *onion_address
)
763 tor_assert(onion_address
);
764 if (!auth_hid_servs
) return NULL
;
765 return strmap_get(auth_hid_servs
, onion_address
);
768 /** Helper: Free storage held by rend_service_authorization_t. */
770 rend_service_authorization_free(rend_service_authorization_t
*auth
)
775 /** Helper for strmap_free. */
777 rend_service_authorization_strmap_item_free(void *service_auth
)
779 rend_service_authorization_free(service_auth
);
782 /** Release all the storage held in auth_hid_servs.
785 rend_service_authorization_free_all(void)
787 if (!auth_hid_servs
) {
790 strmap_free(auth_hid_servs
, rend_service_authorization_strmap_item_free
);
791 auth_hid_servs
= NULL
;
794 /** Parse <b>config_line</b> as a client-side authorization for a hidden
795 * service and add it to the local map of hidden service authorizations.
796 * Return 0 for success and -1 for failure. */
798 rend_parse_service_authorization(or_options_t
*options
, int validate_only
)
802 strmap_t
*parsed
= strmap_new();
803 smartlist_t
*sl
= smartlist_create();
804 rend_service_authorization_t
*auth
= NULL
;
806 for (line
= options
->HidServAuth
; line
; line
= line
->next
) {
807 char *onion_address
, *descriptor_cookie
;
808 char descriptor_cookie_tmp
[REND_DESC_COOKIE_LEN
+2];
809 char descriptor_cookie_base64ext
[REND_DESC_COOKIE_LEN_BASE64
+2+1];
810 int auth_type_val
= 0;
812 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
););
814 smartlist_split_string(sl
, line
->value
, " ",
815 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 3);
816 if (smartlist_len(sl
) < 2) {
817 log_warn(LD_CONFIG
, "Configuration line does not consist of "
818 "\"onion-address authorization-cookie [service-name]\": "
819 "'%s'", line
->value
);
822 auth
= tor_malloc_zero(sizeof(rend_service_authorization_t
));
823 /* Parse onion address. */
824 onion_address
= smartlist_get(sl
, 0);
825 if (strlen(onion_address
) != REND_SERVICE_ADDRESS_LEN
||
826 strcmpend(onion_address
, ".onion")) {
827 log_warn(LD_CONFIG
, "Onion address has wrong format: '%s'",
831 strlcpy(auth
->onion_address
, onion_address
, REND_SERVICE_ID_LEN_BASE32
+1);
832 if (!rend_valid_service_id(auth
->onion_address
)) {
833 log_warn(LD_CONFIG
, "Onion address has wrong format: '%s'",
837 /* Parse descriptor cookie. */
838 descriptor_cookie
= smartlist_get(sl
, 1);
839 if (strlen(descriptor_cookie
) != REND_DESC_COOKIE_LEN_BASE64
) {
840 log_warn(LD_CONFIG
, "Authorization cookie has wrong length: '%s'",
844 /* Add trailing zero bytes (AA) to make base64-decoding happy. */
845 tor_snprintf(descriptor_cookie_base64ext
,
846 REND_DESC_COOKIE_LEN_BASE64
+2+1,
847 "%sAA", descriptor_cookie
);
848 if (base64_decode(descriptor_cookie_tmp
, sizeof(descriptor_cookie_tmp
),
849 descriptor_cookie_base64ext
,
850 strlen(descriptor_cookie_base64ext
)) < 0) {
851 log_warn(LD_CONFIG
, "Decoding authorization cookie failed: '%s'",
855 auth_type_val
= (descriptor_cookie_tmp
[16] >> 4) + 1;
856 if (auth_type_val
< 1 || auth_type_val
> 2) {
857 log_warn(LD_CONFIG
, "Authorization cookie has unknown authorization "
861 auth
->auth_type
= auth_type_val
== 1 ? REND_BASIC_AUTH
: REND_STEALTH_AUTH
;
862 memcpy(auth
->descriptor_cookie
, descriptor_cookie_tmp
,
863 REND_DESC_COOKIE_LEN
);
864 if (strmap_get(parsed
, auth
->onion_address
)) {
865 log_warn(LD_CONFIG
, "Duplicate authorization for the same hidden "
869 strmap_set(parsed
, auth
->onion_address
, auth
);
878 rend_service_authorization_free(auth
);
879 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
););
881 if (!validate_only
&& res
== 0) {
882 rend_service_authorization_free_all();
883 auth_hid_servs
= parsed
;
885 strmap_free(parsed
, rend_service_authorization_strmap_item_free
);