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 rendclient_c_id
[] =
10 * \brief Client code to access location-hidden services.
15 /** Called when we've established a circuit to an introduction point:
16 * send the introduction request. */
18 rend_client_introcirc_has_opened(origin_circuit_t
*circ
)
20 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
21 tor_assert(circ
->cpath
);
23 log_info(LD_REND
,"introcirc is open");
24 connection_ap_attach_pending();
27 /** Send the establish-rendezvous cell along a rendezvous circuit. if
28 * it fails, mark the circ for close and return -1. else return 0.
31 rend_client_send_establish_rendezvous(origin_circuit_t
*circ
)
33 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
);
34 log_info(LD_REND
, "Sending an ESTABLISH_RENDEZVOUS cell");
36 if (crypto_rand(circ
->rend_cookie
, REND_COOKIE_LEN
) < 0) {
37 log_warn(LD_BUG
, "Internal error: Couldn't produce random cookie.");
38 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
41 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ
),
42 RELAY_COMMAND_ESTABLISH_RENDEZVOUS
,
43 circ
->rend_cookie
, REND_COOKIE_LEN
,
44 circ
->cpath
->prev
)<0) {
45 /* circ is already marked for close */
46 log_warn(LD_GENERAL
, "Couldn't send ESTABLISH_RENDEZVOUS cell");
53 /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
54 * down introcirc if possible.
57 rend_client_send_introduction(origin_circuit_t
*introcirc
,
58 origin_circuit_t
*rendcirc
)
62 char payload
[RELAY_PAYLOAD_SIZE
];
63 char tmp
[RELAY_PAYLOAD_SIZE
];
64 rend_cache_entry_t
*entry
;
67 crypto_pk_env_t
*intro_key
; /* either Bob's public key or an intro key. */
69 tor_assert(introcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
70 tor_assert(rendcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_REND_READY
);
71 tor_assert(!rend_cmp_service_ids(introcirc
->rend_query
,
72 rendcirc
->rend_query
));
74 if (rend_cache_lookup_entry(introcirc
->rend_query
, -1, &entry
) < 1) {
76 "query %s didn't have valid rend desc in cache. Failing.",
77 escaped_safe_str(introcirc
->rend_query
));
81 /* first 20 bytes of payload are the hash of bob's pk */
82 if (entry
->parsed
->version
== 0) { /* unversioned descriptor */
83 intro_key
= entry
->parsed
->pk
;
84 } else { /* versioned descriptor */
86 SMARTLIST_FOREACH(entry
->parsed
->intro_nodes
, rend_intro_point_t
*,
88 if (!memcmp(introcirc
->build_state
->chosen_exit
->identity_digest
,
89 intro
->extend_info
->identity_digest
, DIGEST_LEN
)) {
90 intro_key
= intro
->intro_key
;
95 log_warn(LD_BUG
, "Internal error: could not find intro key.");
99 if (crypto_pk_get_digest(intro_key
, payload
)<0) {
100 log_warn(LD_BUG
, "Internal error: couldn't hash public key.");
104 /* Initialize the pending_final_cpath and start the DH handshake. */
105 cpath
= rendcirc
->build_state
->pending_final_cpath
;
107 cpath
= rendcirc
->build_state
->pending_final_cpath
=
108 tor_malloc_zero(sizeof(crypt_path_t
));
109 cpath
->magic
= CRYPT_PATH_MAGIC
;
110 if (!(cpath
->dh_handshake_state
= crypto_dh_new())) {
111 log_warn(LD_BUG
, "Internal error: couldn't allocate DH.");
114 if (crypto_dh_generate_public(cpath
->dh_handshake_state
)<0) {
115 log_warn(LD_BUG
, "Internal error: couldn't generate g^x.");
120 /* write the remaining items into tmp */
121 if (entry
->parsed
->protocols
& (1<<2)) {
122 /* version 2 format */
123 extend_info_t
*extend_info
= rendcirc
->build_state
->chosen_exit
;
125 tmp
[0] = 2; /* version 2 of the cell format */
127 set_uint32(tmp
+1, tor_addr_to_ipv4h(&extend_info
->addr
));
128 set_uint16(tmp
+5, htons(extend_info
->port
));
129 memcpy(tmp
+7, extend_info
->identity_digest
, DIGEST_LEN
);
130 klen
= crypto_pk_asn1_encode(extend_info
->onion_key
, tmp
+7+DIGEST_LEN
+2,
131 sizeof(tmp
)-(7+DIGEST_LEN
+2));
132 set_uint16(tmp
+7+DIGEST_LEN
, htons(klen
));
133 memcpy(tmp
+7+DIGEST_LEN
+2+klen
, rendcirc
->rend_cookie
,
135 dh_offset
= 7+DIGEST_LEN
+2+klen
+REND_COOKIE_LEN
;
138 strncpy(tmp
, rendcirc
->build_state
->chosen_exit
->nickname
,
139 (MAX_NICKNAME_LEN
+1)); /* nul pads */
140 memcpy(tmp
+MAX_NICKNAME_LEN
+1, rendcirc
->rend_cookie
,
142 dh_offset
= MAX_NICKNAME_LEN
+1+REND_COOKIE_LEN
;
145 if (crypto_dh_get_public(cpath
->dh_handshake_state
, tmp
+dh_offset
,
147 log_warn(LD_BUG
, "Internal error: couldn't extract g^x.");
151 note_crypto_pk_op(REND_CLIENT
);
152 /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
153 * to avoid buffer overflows? */
154 r
= crypto_pk_public_hybrid_encrypt(intro_key
, payload
+DIGEST_LEN
,
156 (int)(dh_offset
+DH_KEY_LEN
),
157 PK_PKCS1_OAEP_PADDING
, 0);
159 log_warn(LD_BUG
,"Internal error: hybrid pk encrypt failed.");
163 payload_len
= DIGEST_LEN
+ r
;
164 tor_assert(payload_len
<= RELAY_PAYLOAD_SIZE
); /* we overran something */
166 log_info(LD_REND
, "Sending an INTRODUCE1 cell");
167 if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc
),
168 RELAY_COMMAND_INTRODUCE1
,
169 payload
, payload_len
,
170 introcirc
->cpath
->prev
)<0) {
171 /* introcirc is already marked for close. leave rendcirc alone. */
172 log_warn(LD_BUG
, "Couldn't send INTRODUCE1 cell");
176 /* Now, we wait for an ACK or NAK on this circuit. */
177 introcirc
->_base
.purpose
= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
;
181 circuit_mark_for_close(TO_CIRCUIT(introcirc
), END_CIRC_REASON_INTERNAL
);
182 circuit_mark_for_close(TO_CIRCUIT(rendcirc
), END_CIRC_REASON_INTERNAL
);
186 /** Called when a rendezvous circuit is open; sends a establish
187 * rendezvous circuit as appropriate. */
189 rend_client_rendcirc_has_opened(origin_circuit_t
*circ
)
191 tor_assert(circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
);
193 log_info(LD_REND
,"rendcirc is open");
195 /* generate a rendezvous cookie, store it in circ */
196 if (rend_client_send_establish_rendezvous(circ
) < 0) {
201 /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
204 rend_client_introduction_acked(origin_circuit_t
*circ
,
205 const char *request
, size_t request_len
)
207 origin_circuit_t
*rendcirc
;
208 (void) request
; // XXXX Use this.
210 if (circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
211 log_warn(LD_PROTOCOL
,
212 "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
213 circ
->_base
.n_circ_id
);
214 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
218 tor_assert(circ
->build_state
->chosen_exit
);
220 if (request_len
== 0) {
221 /* It's an ACK; the introduction point relayed our introduction request. */
222 /* Locate the rend circ which is waiting to hear about this ack,
225 log_info(LD_REND
,"Received ack. Telling rend circ...");
226 rendcirc
= circuit_get_by_rend_query_and_purpose(
227 circ
->rend_query
, CIRCUIT_PURPOSE_C_REND_READY
);
228 if (rendcirc
) { /* remember the ack */
229 rendcirc
->_base
.purpose
= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
;
231 log_info(LD_REND
,"...Found no rend circ. Dropping on the floor.");
233 /* close the circuit: we won't need it anymore. */
234 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
;
235 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_FINISHED
);
237 /* It's a NAK; the introduction point didn't relay our request. */
238 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_INTRODUCING
;
239 /* Remove this intro point from the set of viable introduction
240 * points. If any remain, extend to a new one and try again.
241 * If none remain, refetch the service descriptor.
243 if (rend_client_remove_intro_point(circ
->build_state
->chosen_exit
,
244 circ
->rend_query
) > 0) {
245 /* There are introduction points left. Re-extend the circuit to
246 * another intro point and try again. */
247 extend_info_t
*extend_info
;
249 extend_info
= rend_client_get_random_intro(circ
->rend_query
);
251 log_warn(LD_REND
, "No introduction points left for %s. Closing.",
252 escaped_safe_str(circ
->rend_query
));
253 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
257 "Got nack for %s from %s. Re-extending circ %d, "
259 escaped_safe_str(circ
->rend_query
),
260 circ
->build_state
->chosen_exit
->nickname
, circ
->_base
.n_circ_id
,
261 extend_info
->nickname
);
262 result
= circuit_extend_to_new_exit(circ
, extend_info
);
263 extend_info_free(extend_info
);
270 /** The period for which a hidden service directory cannot be queried for
271 * the same descriptor ID again. */
272 #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
274 /** Contains the last request times to hidden service directories for
275 * certain queries; keys are strings consisting of base32-encoded
276 * hidden service directory identities and base32-encoded descriptor IDs;
277 * values are pointers to timestamps of the last requests. */
278 static strmap_t
*last_hid_serv_requests
= NULL
;
280 /** Look up the last request time to hidden service directory <b>hs_dir</b>
281 * for descriptor ID <b>desc_id_base32</b>. If <b>set</b> is non-zero,
282 * assign the current time <b>now</b> and return that. Otherwise, return
283 * the most recent request time, or 0 if no such request has been sent
286 lookup_last_hid_serv_request(routerstatus_t
*hs_dir
,
287 const char *desc_id_base32
, time_t now
, int set
)
289 char hsdir_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
290 char hsdir_desc_comb_id
[2 * REND_DESC_ID_V2_LEN_BASE32
+ 1];
291 time_t *last_request_ptr
;
292 base32_encode(hsdir_id_base32
, sizeof(hsdir_id_base32
),
293 hs_dir
->identity_digest
, DIGEST_LEN
);
294 tor_snprintf(hsdir_desc_comb_id
, sizeof(hsdir_desc_comb_id
), "%s%s",
295 hsdir_id_base32
, desc_id_base32
);
297 last_request_ptr
= tor_malloc_zero(sizeof(time_t *));
298 *last_request_ptr
= now
;
299 strmap_set(last_hid_serv_requests
, hsdir_desc_comb_id
, last_request_ptr
);
301 last_request_ptr
= strmap_get_lc(last_hid_serv_requests
,
303 return (last_request_ptr
) ? *last_request_ptr
: 0;
306 /** Clean the history of request times to hidden service directories, so that
307 * it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD
308 * seconds any more. */
310 directory_clean_last_hid_serv_requests(void)
313 time_t cutoff
= time(NULL
) - REND_HID_SERV_DIR_REQUERY_PERIOD
;
314 if (!last_hid_serv_requests
)
315 last_hid_serv_requests
= strmap_new();
316 for (iter
= strmap_iter_init(last_hid_serv_requests
);
317 !strmap_iter_done(iter
); ) {
321 strmap_iter_get(iter
, &key
, &val
);
322 ent
= (time_t *) val
;
324 iter
= strmap_iter_next_rmv(last_hid_serv_requests
, iter
);
327 iter
= strmap_iter_next(last_hid_serv_requests
, iter
);
332 /** Determine the responsible hidden service directories for <b>desc_id</b>
333 * and fetch the descriptor belonging to that ID from one of them. Only
334 * send a request to hidden service directories that we did not try within
335 * the last REND_HID_SERV_DIR_REQUERY_PERIOD seconds; on success, return 1,
336 * in the case that no hidden service directory is left to ask for the
337 * descriptor, return 0, and in case of a failure -1. <b>query</b> is only
338 * passed for pretty log statements. */
340 directory_get_from_hs_dir(const char *desc_id
, const char *query
)
342 smartlist_t
*responsible_dirs
= smartlist_create();
343 routerstatus_t
*hs_dir
;
344 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
345 time_t now
= time(NULL
);
348 tor_assert(strlen(query
) == REND_SERVICE_ID_LEN_BASE32
);
349 /* Determine responsible dirs. Even if we can't get all we want,
350 * work with the ones we have. If it's empty, we'll notice below. */
351 (int) hid_serv_get_responsible_directories(responsible_dirs
, desc_id
);
353 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
354 desc_id
, DIGEST_LEN
);
356 /* Only select those hidden service directories to which we did not send
357 * a request recently. */
358 directory_clean_last_hid_serv_requests(); /* Clean request history first. */
360 SMARTLIST_FOREACH(responsible_dirs
, routerstatus_t
*, dir
, {
361 if (lookup_last_hid_serv_request(dir
, desc_id_base32
, 0, 0) +
362 REND_HID_SERV_DIR_REQUERY_PERIOD
>= now
)
363 SMARTLIST_DEL_CURRENT(responsible_dirs
, dir
);
366 hs_dir
= smartlist_choose(responsible_dirs
);
367 smartlist_free(responsible_dirs
);
369 log_info(LD_REND
, "Could not pick one of the responsible hidden "
370 "service directories, because we requested them all "
371 "recently without success.");
375 /* Remember, that we are requesting a descriptor from this hidden service
377 lookup_last_hid_serv_request(hs_dir
, desc_id_base32
, now
, 1);
379 /* Send fetch request. (Pass query as payload to write it to the directory
380 * connection so that it can be referred to when the response arrives.) */
381 directory_initiate_command_routerstatus(hs_dir
,
382 DIR_PURPOSE_FETCH_RENDDESC_V2
,
383 ROUTER_PURPOSE_GENERAL
,
384 1, desc_id_base32
, query
, 0, 0);
385 log_info(LD_REND
, "Sending fetch request for v2 descriptor for "
386 "service '%s' with descriptor ID '%s' to hidden "
387 "service directory '%s' on port %d.",
388 safe_str(query
), safe_str(desc_id_base32
), hs_dir
->nickname
,
393 /** If we are not currently fetching a rendezvous service descriptor
394 * for the service ID <b>query</b>, start a directory connection to fetch a
398 rend_client_refetch_renddesc(const char *query
)
400 if (!get_options()->FetchHidServDescriptors
)
402 log_info(LD_REND
, "Fetching rendezvous descriptor for service %s",
403 escaped_safe_str(query
));
404 if (connection_get_by_type_state_rendquery(CONN_TYPE_DIR
, 0, query
, 0)) {
405 log_info(LD_REND
,"Would fetch a new renddesc here (for %s), but one is "
406 "already in progress.", escaped_safe_str(query
));
408 /* not one already; initiate a dir rend desc lookup */
409 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC
,
410 ROUTER_PURPOSE_GENERAL
, query
, 1);
414 /** Start a connection to a hidden service directory to fetch a v2
415 * rendezvous service descriptor for the base32-encoded service ID
419 rend_client_refetch_v2_renddesc(const char *query
)
421 char descriptor_id
[DIGEST_LEN
];
422 int replicas_left_to_try
[REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
];
424 rend_cache_entry_t
*e
= NULL
;
426 tor_assert(strlen(query
) == REND_SERVICE_ID_LEN_BASE32
);
427 /* Are we configured to fetch descriptors? */
428 if (!get_options()->FetchHidServDescriptors
) {
429 log_warn(LD_REND
, "We received an onion address for a v2 rendezvous "
430 "service descriptor, but are not fetching service descriptors.");
433 /* Before fetching, check if we already have the descriptor here. */
434 if (rend_cache_lookup_entry(query
, -1, &e
) > 0) {
435 log_info(LD_REND
, "We would fetch a v2 rendezvous descriptor, but we "
436 "already have that descriptor here. Not fetching.");
439 log_debug(LD_REND
, "Fetching v2 rendezvous descriptor for service %s",
441 /* Randomly iterate over the replicas until a descriptor can be fetched
442 * from one of the consecutive nodes, or no options are left. */
443 tries_left
= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
;
444 for (i
= 0; i
< REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
; i
++)
445 replicas_left_to_try
[i
] = i
;
446 while (tries_left
> 0) {
447 int rand
= crypto_rand_int(tries_left
);
448 int chosen_replica
= replicas_left_to_try
[rand
];
449 replicas_left_to_try
[rand
] = replicas_left_to_try
[--tries_left
];
451 if (rend_compute_v2_desc_id(descriptor_id
, query
, NULL
, time(NULL
),
452 chosen_replica
) < 0) {
453 log_warn(LD_REND
, "Internal error: Computing v2 rendezvous "
454 "descriptor ID did not succeed.");
457 if (directory_get_from_hs_dir(descriptor_id
, query
) != 0)
458 return; /* either success or failure, but we're done */
460 /* If we come here, there are no hidden service directories left. */
461 log_info(LD_REND
, "Could not pick one of the responsible hidden "
462 "service directories to fetch descriptors, because "
463 "we already tried them all unsuccessfully.");
467 /** Remove failed_intro from ent. If ent now has no intro points, or
468 * service is unrecognized, then launch a new renddesc fetch.
470 * Return -1 if error, 0 if no intro points remain or service
471 * unrecognized, 1 if recognized and some intro points remain.
474 rend_client_remove_intro_point(extend_info_t
*failed_intro
, const char *query
)
477 rend_cache_entry_t
*ent
;
480 r
= rend_cache_lookup_entry(query
, -1, &ent
);
482 log_warn(LD_BUG
, "Malformed service ID %s.", escaped_safe_str(query
));
486 log_info(LD_REND
, "Unknown service %s. Re-fetching descriptor.",
487 escaped_safe_str(query
));
488 /* Fetch both, v0 and v2 rend descriptors in parallel. Use whichever
490 rend_client_refetch_v2_renddesc(query
);
491 rend_client_refetch_renddesc(query
);
495 for (i
= 0; i
< smartlist_len(ent
->parsed
->intro_nodes
); i
++) {
496 rend_intro_point_t
*intro
= smartlist_get(ent
->parsed
->intro_nodes
, i
);
497 if (!memcmp(failed_intro
->identity_digest
,
498 intro
->extend_info
->identity_digest
, DIGEST_LEN
)) {
499 rend_intro_point_free(intro
);
500 smartlist_del(ent
->parsed
->intro_nodes
, i
);
505 if (smartlist_len(ent
->parsed
->intro_nodes
) == 0) {
507 "No more intro points remain for %s. Re-fetching descriptor.",
508 escaped_safe_str(query
));
509 /* Fetch both, v0 and v2 rend descriptors in parallel. Use whichever
511 rend_client_refetch_v2_renddesc(query
);
512 rend_client_refetch_renddesc(query
);
514 /* move all pending streams back to renddesc_wait */
515 while ((conn
= connection_get_by_type_state_rendquery(CONN_TYPE_AP
,
516 AP_CONN_STATE_CIRCUIT_WAIT
, query
, -1))) {
517 conn
->state
= AP_CONN_STATE_RENDDESC_WAIT
;
522 log_info(LD_REND
,"%d options left for %s.",
523 smartlist_len(ent
->parsed
->intro_nodes
), escaped_safe_str(query
));
527 /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
528 * the circuit to C_REND_READY.
531 rend_client_rendezvous_acked(origin_circuit_t
*circ
, const char *request
,
536 /* we just got an ack for our establish-rendezvous. switch purposes. */
537 if (circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_ESTABLISH_REND
) {
538 log_warn(LD_PROTOCOL
,"Got a rendezvous ack when we weren't expecting one. "
540 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
543 log_info(LD_REND
,"Got rendezvous ack. This circuit is now ready for "
545 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_REND_READY
;
546 /*XXXX021 This is a pretty brute approach. It'd be better to
547 * attach only the connections that are waiting on this circuit, rather
548 * than trying to attach them all. */
549 /* If we already have the introduction circuit built, make sure we send
550 * the INTRODUCE cell _now_ */
551 connection_ap_attach_pending();
555 /** Bob sent us a rendezvous cell; join the circuits. */
557 rend_client_receive_rendezvous(origin_circuit_t
*circ
, const char *request
,
561 char keys
[DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
];
563 if ((circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_REND_READY
&&
564 circ
->_base
.purpose
!= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
)
565 || !circ
->build_state
->pending_final_cpath
) {
566 log_warn(LD_PROTOCOL
,"Got rendezvous2 cell from hidden service, but not "
567 "expecting it. Closing.");
568 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
572 if (request_len
!= DH_KEY_LEN
+DIGEST_LEN
) {
573 log_warn(LD_PROTOCOL
,"Incorrect length (%d) on RENDEZVOUS2 cell.",
578 log_info(LD_REND
,"Got RENDEZVOUS2 cell from hidden service.");
580 /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
581 tor_assert(circ
->build_state
);
582 tor_assert(circ
->build_state
->pending_final_cpath
);
583 hop
= circ
->build_state
->pending_final_cpath
;
584 tor_assert(hop
->dh_handshake_state
);
585 if (crypto_dh_compute_secret(hop
->dh_handshake_state
, request
, DH_KEY_LEN
,
586 keys
, DIGEST_LEN
+CPATH_KEY_MATERIAL_LEN
)<0) {
587 log_warn(LD_GENERAL
, "Couldn't complete DH handshake.");
590 /* ... and set up cpath. */
591 if (circuit_init_cpath_crypto(hop
, keys
+DIGEST_LEN
, 0)<0)
594 /* Check whether the digest is right... */
595 if (memcmp(keys
, request
+DH_KEY_LEN
, DIGEST_LEN
)) {
596 log_warn(LD_PROTOCOL
, "Incorrect digest of key material.");
600 crypto_dh_free(hop
->dh_handshake_state
);
601 hop
->dh_handshake_state
= NULL
;
603 /* All is well. Extend the circuit. */
604 circ
->_base
.purpose
= CIRCUIT_PURPOSE_C_REND_JOINED
;
605 hop
->state
= CPATH_STATE_OPEN
;
606 /* set the windows to default. these are the windows
607 * that alice thinks bob has.
609 hop
->package_window
= CIRCWINDOW_START
;
610 hop
->deliver_window
= CIRCWINDOW_START
;
612 onion_append_to_cpath(&circ
->cpath
, hop
);
613 circ
->build_state
->pending_final_cpath
= NULL
; /* prevent double-free */
614 /*XXXX021 This is a pretty brute approach. It'd be better to
615 * attach only the connections that are waiting on this circuit, rather
616 * than trying to attach them all. */
618 connection_ap_attach_pending();
621 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
625 /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
626 * are waiting on query. If there's a working cache entry here
627 * with at least one intro point, move them to the next state;
631 rend_client_desc_here(const char *query
)
633 edge_connection_t
*conn
;
634 rend_cache_entry_t
*entry
;
635 time_t now
= time(NULL
);
637 smartlist_t
*conns
= get_connection_array();
638 SMARTLIST_FOREACH(conns
, connection_t
*, _conn
,
640 if (_conn
->type
!= CONN_TYPE_AP
||
641 _conn
->state
!= AP_CONN_STATE_RENDDESC_WAIT
||
642 _conn
->marked_for_close
)
644 conn
= TO_EDGE_CONN(_conn
);
645 if (rend_cmp_service_ids(query
, conn
->rend_query
))
647 assert_connection_ok(TO_CONN(conn
), now
);
648 if (rend_cache_lookup_entry(conn
->rend_query
, -1, &entry
) == 1 &&
649 smartlist_len(entry
->parsed
->intro_nodes
) > 0) {
650 /* either this fetch worked, or it failed but there was a
651 * valid entry from before which we should reuse */
652 log_info(LD_REND
,"Rend desc is usable. Launching circuits.");
653 conn
->_base
.state
= AP_CONN_STATE_CIRCUIT_WAIT
;
655 /* restart their timeout values, so they get a fair shake at
656 * connecting to the hidden service. */
657 conn
->_base
.timestamp_created
= now
;
658 conn
->_base
.timestamp_lastread
= now
;
659 conn
->_base
.timestamp_lastwritten
= now
;
661 if (connection_ap_handshake_attach_circuit(conn
) < 0) {
662 /* it will never work */
663 log_warn(LD_REND
,"Rendezvous attempt failed. Closing.");
664 if (!conn
->_base
.marked_for_close
)
665 connection_mark_unattached_ap(conn
, END_STREAM_REASON_CANT_ATTACH
);
667 } else { /* 404, or fetch didn't get that far */
668 log_notice(LD_REND
,"Closing stream for '%s.onion': hidden service is "
669 "unavailable (try again later).", safe_str(query
));
670 connection_mark_unattached_ap(conn
, END_STREAM_REASON_RESOLVEFAILED
);
675 /** Return a newly allocated extend_info_t* for a randomly chosen introduction
676 * point for the named hidden service. Return NULL if all introduction points
677 * have been tried and failed.
680 rend_client_get_random_intro(const char *query
)
683 rend_cache_entry_t
*entry
;
684 rend_intro_point_t
*intro
;
685 routerinfo_t
*router
;
687 if (rend_cache_lookup_entry(query
, -1, &entry
) < 1) {
689 "Query '%s' didn't have valid rend desc in cache. Failing.",
695 if (smartlist_len(entry
->parsed
->intro_nodes
) == 0)
698 i
= crypto_rand_int(smartlist_len(entry
->parsed
->intro_nodes
));
699 intro
= smartlist_get(entry
->parsed
->intro_nodes
, i
);
700 /* Do we need to look up the router or is the extend info complete? */
701 if (!intro
->extend_info
->onion_key
) {
702 router
= router_get_by_nickname(intro
->extend_info
->nickname
, 0);
704 log_info(LD_REND
, "Unknown router with nickname '%s'; trying another.",
705 intro
->extend_info
->nickname
);
706 rend_intro_point_free(intro
);
707 smartlist_del(entry
->parsed
->intro_nodes
, i
);
710 extend_info_free(intro
->extend_info
);
711 intro
->extend_info
= extend_info_from_router(router
);
713 return extend_info_dup(intro
->extend_info
);
716 /** Client-side authorizations for hidden services; map of onion address to
717 * rend_service_authorization_t*. */
718 static strmap_t
*auth_hid_servs
= NULL
;
720 /** Look up the client-side authorization for the hidden service with
721 * <b>onion_address</b>. Return NULL if no authorization is available for
723 rend_service_authorization_t
*
724 rend_client_lookup_service_authorization(const char *onion_address
)
726 tor_assert(onion_address
);
727 if (!auth_hid_servs
) return NULL
;
728 return strmap_get(auth_hid_servs
, onion_address
);
731 /** Helper: Free storage held by rend_service_authorization_t. */
733 rend_service_authorization_free(rend_service_authorization_t
*auth
)
738 /** Helper for strmap_free. */
740 rend_service_authorization_strmap_item_free(void *service_auth
)
742 rend_service_authorization_free(service_auth
);
745 /** Release all the storage held in auth_hid_servs.
748 rend_service_authorization_free_all(void)
750 if (!auth_hid_servs
) {
753 strmap_free(auth_hid_servs
, rend_service_authorization_strmap_item_free
);
754 auth_hid_servs
= NULL
;
757 /** Parse <b>config_line</b> as a client-side authorization for a hidden
758 * service and add it to the local map of hidden service authorizations.
759 * Return 0 for success and -1 for failure. */
761 rend_parse_service_authorization(or_options_t
*options
, int validate_only
)
765 strmap_t
*parsed
= strmap_new();
766 smartlist_t
*sl
= smartlist_create();
768 for (line
= options
->HidServAuth
; line
; line
= line
->next
) {
769 char *onion_address
, *descriptor_cookie
;
770 char descriptor_cookie_tmp
[REND_DESC_COOKIE_LEN
+2];
771 char descriptor_cookie_base64ext
[REND_DESC_COOKIE_LEN_BASE64
+2+1];
772 rend_service_authorization_t
*auth
= NULL
;
773 int auth_type_val
= 0;
774 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
););
776 smartlist_split_string(sl
, line
->value
, " ",
777 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 3);
778 if (smartlist_len(sl
) < 2) {
779 log_warn(LD_CONFIG
, "Configuration line does not consist of "
780 "\"onion-address authorization-cookie [service-name]\": "
781 "'%s'", line
->value
);
784 auth
= tor_malloc_zero(sizeof(rend_service_authorization_t
));
785 /* Parse onion address. */
786 onion_address
= smartlist_get(sl
, 0);
787 if (strlen(onion_address
) != REND_SERVICE_ADDRESS_LEN
||
788 strcmpend(onion_address
, ".onion")) {
789 log_warn(LD_CONFIG
, "Onion address has wrong format: '%s'",
793 strlcpy(auth
->onion_address
, onion_address
, REND_SERVICE_ID_LEN_BASE32
+1);
794 if (!rend_valid_service_id(auth
->onion_address
)) {
795 log_warn(LD_CONFIG
, "Onion address has wrong format: '%s'",
799 /* Parse descriptor cookie. */
800 descriptor_cookie
= smartlist_get(sl
, 1);
801 if (strlen(descriptor_cookie
) != REND_DESC_COOKIE_LEN_BASE64
) {
802 log_warn(LD_CONFIG
, "Authorization cookie has wrong length: '%s'",
806 /* Add trailing zero bytes (AA) to make base64-decoding happy. */
807 tor_snprintf(descriptor_cookie_base64ext
,
808 REND_DESC_COOKIE_LEN_BASE64
+2+1,
809 "%sAA", descriptor_cookie
);
810 if (base64_decode(descriptor_cookie_tmp
, sizeof(descriptor_cookie_tmp
),
811 descriptor_cookie_base64ext
,
812 strlen(descriptor_cookie_base64ext
)) < 0) {
813 log_warn(LD_CONFIG
, "Decoding authorization cookie failed: '%s'",
817 auth_type_val
= (descriptor_cookie_tmp
[16] >> 4) + 1;
818 if (auth_type_val
< 1 || auth_type_val
> 2) {
819 log_warn(LD_CONFIG
, "Authorization cookie has unknown authorization "
823 auth
->auth_type
= auth_type_val
== 1 ? REND_BASIC_AUTH
: REND_STEALTH_AUTH
;
824 memcpy(auth
->descriptor_cookie
, descriptor_cookie_tmp
,
825 REND_DESC_COOKIE_LEN
);
826 if (strmap_get(parsed
, auth
->onion_address
)) {
827 log_warn(LD_CONFIG
, "Duplicate authorization for the same hidden "
831 strmap_set(parsed
, auth
->onion_address
, auth
);
838 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
););
840 if (!validate_only
&& res
== 0) {
841 rend_service_authorization_free_all();
842 auth_hid_servs
= parsed
;
844 strmap_free(parsed
, rend_service_authorization_strmap_item_free
);