1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2013, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Rendezvous implementation: shared code between
8 * introducers, services, clients, and rendezvous points.
12 #include "circuitbuild.h"
14 #include "rendclient.h"
15 #include "rendcommon.h"
17 #include "rendservice.h"
19 #include "routerlist.h"
20 #include "routerparse.h"
22 /** Return 0 if one and two are the same service ids, else -1 or 1 */
24 rend_cmp_service_ids(const char *one
, const char *two
)
26 return strcasecmp(one
,two
);
29 /** Free the storage held by the service descriptor <b>desc</b>.
32 rend_service_descriptor_free(rend_service_descriptor_t
*desc
)
37 crypto_pk_free(desc
->pk
);
38 if (desc
->intro_nodes
) {
39 SMARTLIST_FOREACH(desc
->intro_nodes
, rend_intro_point_t
*, intro
,
40 rend_intro_point_free(intro
););
41 smartlist_free(desc
->intro_nodes
);
43 if (desc
->successful_uploads
) {
44 SMARTLIST_FOREACH(desc
->successful_uploads
, char *, c
, tor_free(c
););
45 smartlist_free(desc
->successful_uploads
);
50 /** Length of the descriptor cookie that is used for versioned hidden
51 * service descriptors. */
52 #define REND_DESC_COOKIE_LEN 16
54 /** Length of the replica number that is used to determine the secret ID
55 * part of versioned hidden service descriptors. */
56 #define REND_REPLICA_LEN 1
58 /** Compute the descriptor ID for <b>service_id</b> of length
59 * <b>REND_SERVICE_ID_LEN</b> and <b>secret_id_part</b> of length
60 * <b>DIGEST_LEN</b>, and write it to <b>descriptor_id_out</b> of length
61 * <b>DIGEST_LEN</b>. */
63 rend_get_descriptor_id_bytes(char *descriptor_id_out
,
64 const char *service_id
,
65 const char *secret_id_part
)
67 crypto_digest_t
*digest
= crypto_digest_new();
68 crypto_digest_add_bytes(digest
, service_id
, REND_SERVICE_ID_LEN
);
69 crypto_digest_add_bytes(digest
, secret_id_part
, DIGEST_LEN
);
70 crypto_digest_get_digest(digest
, descriptor_id_out
, DIGEST_LEN
);
71 crypto_digest_free(digest
);
74 /** Compute the secret ID part for time_period,
75 * a <b>descriptor_cookie</b> of length
76 * <b>REND_DESC_COOKIE_LEN</b> which may also be <b>NULL</b> if no
77 * descriptor_cookie shall be used, and <b>replica</b>, and write it to
78 * <b>secret_id_part</b> of length DIGEST_LEN. */
80 get_secret_id_part_bytes(char *secret_id_part
, uint32_t time_period
,
81 const char *descriptor_cookie
, uint8_t replica
)
83 crypto_digest_t
*digest
= crypto_digest_new();
84 time_period
= htonl(time_period
);
85 crypto_digest_add_bytes(digest
, (char*)&time_period
, sizeof(uint32_t));
86 if (descriptor_cookie
) {
87 crypto_digest_add_bytes(digest
, descriptor_cookie
,
88 REND_DESC_COOKIE_LEN
);
90 crypto_digest_add_bytes(digest
, (const char *)&replica
, REND_REPLICA_LEN
);
91 crypto_digest_get_digest(digest
, secret_id_part
, DIGEST_LEN
);
92 crypto_digest_free(digest
);
95 /** Return the time period for time <b>now</b> plus a potentially
96 * intended <b>deviation</b> of one or more periods, based on the first byte
97 * of <b>service_id</b>. */
99 get_time_period(time_t now
, uint8_t deviation
, const char *service_id
)
101 /* The time period is the number of REND_TIME_PERIOD_V2_DESC_VALIDITY
102 * intervals that have passed since the epoch, offset slightly so that
103 * each service's time periods start and end at a fraction of that
104 * period based on their first byte. */
106 (now
+ ((uint8_t) *service_id
) * REND_TIME_PERIOD_V2_DESC_VALIDITY
/ 256)
107 / REND_TIME_PERIOD_V2_DESC_VALIDITY
+ deviation
;
110 /** Compute the time in seconds that a descriptor that is generated
111 * <b>now</b> for <b>service_id</b> will be valid. */
113 get_seconds_valid(time_t now
, const char *service_id
)
115 uint32_t result
= REND_TIME_PERIOD_V2_DESC_VALIDITY
-
117 (now
+ ((uint8_t) *service_id
) * REND_TIME_PERIOD_V2_DESC_VALIDITY
/ 256)
118 % REND_TIME_PERIOD_V2_DESC_VALIDITY
);
122 /** Compute the binary <b>desc_id_out</b> (DIGEST_LEN bytes long) for a given
123 * base32-encoded <b>service_id</b> and optional unencoded
124 * <b>descriptor_cookie</b> of length REND_DESC_COOKIE_LEN,
125 * at time <b>now</b> for replica number
126 * <b>replica</b>. <b>desc_id</b> needs to have <b>DIGEST_LEN</b> bytes
127 * free. Return 0 for success, -1 otherwise. */
129 rend_compute_v2_desc_id(char *desc_id_out
, const char *service_id
,
130 const char *descriptor_cookie
, time_t now
,
133 char service_id_binary
[REND_SERVICE_ID_LEN
];
134 char secret_id_part
[DIGEST_LEN
];
135 uint32_t time_period
;
137 strlen(service_id
) != REND_SERVICE_ID_LEN_BASE32
) {
138 log_warn(LD_REND
, "Could not compute v2 descriptor ID: "
139 "Illegal service ID: %s",
140 safe_str(service_id
));
143 if (replica
>= REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
) {
144 log_warn(LD_REND
, "Could not compute v2 descriptor ID: "
145 "Replica number out of range: %d", replica
);
148 /* Convert service ID to binary. */
149 if (base32_decode(service_id_binary
, REND_SERVICE_ID_LEN
,
150 service_id
, REND_SERVICE_ID_LEN_BASE32
) < 0) {
151 log_warn(LD_REND
, "Could not compute v2 descriptor ID: "
152 "Illegal characters in service ID: %s",
153 safe_str_client(service_id
));
156 /* Calculate current time-period. */
157 time_period
= get_time_period(now
, 0, service_id_binary
);
158 /* Calculate secret-id-part = h(time-period + replica). */
159 get_secret_id_part_bytes(secret_id_part
, time_period
, descriptor_cookie
,
161 /* Calculate descriptor ID. */
162 rend_get_descriptor_id_bytes(desc_id_out
, service_id_binary
, secret_id_part
);
166 /** Encode the introduction points in <b>desc</b> and write the result to a
167 * newly allocated string pointed to by <b>encoded</b>. Return 0 for
168 * success, -1 otherwise. */
170 rend_encode_v2_intro_points(char **encoded
, rend_service_descriptor_t
*desc
)
174 size_t unenc_written
= 0;
177 /* Assemble unencrypted list of introduction points. */
178 unenc_len
= smartlist_len(desc
->intro_nodes
) * 1000; /* too long, but ok. */
179 unenc
= tor_malloc_zero(unenc_len
);
180 for (i
= 0; i
< smartlist_len(desc
->intro_nodes
); i
++) {
181 char id_base32
[REND_INTRO_POINT_ID_LEN_BASE32
+ 1];
182 char *onion_key
= NULL
;
183 size_t onion_key_len
;
184 crypto_pk_t
*intro_key
;
185 char *service_key
= NULL
;
186 char *address
= NULL
;
187 size_t service_key_len
;
189 rend_intro_point_t
*intro
= smartlist_get(desc
->intro_nodes
, i
);
190 /* Obtain extend info with introduction point details. */
191 extend_info_t
*info
= intro
->extend_info
;
192 /* Encode introduction point ID. */
193 base32_encode(id_base32
, sizeof(id_base32
),
194 info
->identity_digest
, DIGEST_LEN
);
195 /* Encode onion key. */
196 if (crypto_pk_write_public_key_to_string(info
->onion_key
, &onion_key
,
197 &onion_key_len
) < 0) {
198 log_warn(LD_REND
, "Could not write onion key.");
201 /* Encode intro key. */
202 intro_key
= intro
->intro_key
;
204 crypto_pk_write_public_key_to_string(intro_key
, &service_key
,
205 &service_key_len
) < 0) {
206 log_warn(LD_REND
, "Could not write intro key.");
210 /* Assemble everything for this introduction point. */
211 address
= tor_dup_addr(&info
->addr
);
212 res
= tor_snprintf(unenc
+ unenc_written
, unenc_len
- unenc_written
,
213 "introduction-point %s\n"
225 tor_free(service_key
);
227 log_warn(LD_REND
, "Not enough space for writing introduction point "
231 /* Update total number of written bytes for unencrypted intro points. */
232 unenc_written
+= res
;
234 /* Finalize unencrypted introduction points. */
235 if (unenc_len
< unenc_written
+ 2) {
236 log_warn(LD_REND
, "Not enough space for finalizing introduction point "
240 unenc
[unenc_written
++] = '\n';
241 unenc
[unenc_written
++] = 0;
250 /** Encrypt the encoded introduction points in <b>encoded</b> using
251 * authorization type 'basic' with <b>client_cookies</b> and write the
252 * result to a newly allocated string pointed to by <b>encrypted_out</b> of
253 * length <b>encrypted_len_out</b>. Return 0 for success, -1 otherwise. */
255 rend_encrypt_v2_intro_points_basic(char **encrypted_out
,
256 size_t *encrypted_len_out
,
258 smartlist_t
*client_cookies
)
260 int r
= -1, i
, pos
, enclen
, client_blocks
;
261 size_t len
, client_entries_len
;
262 char *enc
= NULL
, iv
[CIPHER_IV_LEN
], *client_part
= NULL
,
263 session_key
[CIPHER_KEY_LEN
];
264 smartlist_t
*encrypted_session_keys
= NULL
;
265 crypto_digest_t
*digest
;
266 crypto_cipher_t
*cipher
;
268 tor_assert(client_cookies
&& smartlist_len(client_cookies
) > 0);
270 /* Generate session key. */
271 if (crypto_rand(session_key
, CIPHER_KEY_LEN
) < 0) {
272 log_warn(LD_REND
, "Unable to generate random session key to encrypt "
273 "introduction point string.");
277 /* Determine length of encrypted introduction points including session
279 client_blocks
= 1 + ((smartlist_len(client_cookies
) - 1) /
280 REND_BASIC_AUTH_CLIENT_MULTIPLE
);
281 client_entries_len
= client_blocks
* REND_BASIC_AUTH_CLIENT_MULTIPLE
*
282 REND_BASIC_AUTH_CLIENT_ENTRY_LEN
;
283 len
= 2 + client_entries_len
+ CIPHER_IV_LEN
+ strlen(encoded
);
284 if (client_blocks
>= 256) {
285 log_warn(LD_REND
, "Too many clients in introduction point string.");
288 enc
= tor_malloc_zero(len
);
289 enc
[0] = 0x01; /* type of authorization. */
290 enc
[1] = (uint8_t)client_blocks
;
292 /* Encrypt with random session key. */
293 enclen
= crypto_cipher_encrypt_with_iv(session_key
,
294 enc
+ 2 + client_entries_len
,
295 CIPHER_IV_LEN
+ strlen(encoded
), encoded
, strlen(encoded
));
298 log_warn(LD_REND
, "Could not encrypt introduction point string.");
301 memcpy(iv
, enc
+ 2 + client_entries_len
, CIPHER_IV_LEN
);
303 /* Encrypt session key for cookies, determine client IDs, and put both
305 encrypted_session_keys
= smartlist_new();
306 SMARTLIST_FOREACH_BEGIN(client_cookies
, const char *, cookie
) {
307 client_part
= tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN
);
308 /* Encrypt session key. */
309 cipher
= crypto_cipher_new(cookie
);
310 if (crypto_cipher_encrypt(cipher
, client_part
+
311 REND_BASIC_AUTH_CLIENT_ID_LEN
,
312 session_key
, CIPHER_KEY_LEN
) < 0) {
313 log_warn(LD_REND
, "Could not encrypt session key for client.");
314 crypto_cipher_free(cipher
);
315 tor_free(client_part
);
318 crypto_cipher_free(cipher
);
320 /* Determine client ID. */
321 digest
= crypto_digest_new();
322 crypto_digest_add_bytes(digest
, cookie
, REND_DESC_COOKIE_LEN
);
323 crypto_digest_add_bytes(digest
, iv
, CIPHER_IV_LEN
);
324 crypto_digest_get_digest(digest
, client_part
,
325 REND_BASIC_AUTH_CLIENT_ID_LEN
);
326 crypto_digest_free(digest
);
328 /* Put both together. */
329 smartlist_add(encrypted_session_keys
, client_part
);
330 } SMARTLIST_FOREACH_END(cookie
);
332 /* Add some fake client IDs and encrypted session keys. */
333 for (i
= (smartlist_len(client_cookies
) - 1) %
334 REND_BASIC_AUTH_CLIENT_MULTIPLE
;
335 i
< REND_BASIC_AUTH_CLIENT_MULTIPLE
- 1; i
++) {
336 client_part
= tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN
);
337 if (crypto_rand(client_part
, REND_BASIC_AUTH_CLIENT_ENTRY_LEN
) < 0) {
338 log_warn(LD_REND
, "Unable to generate fake client entry.");
339 tor_free(client_part
);
342 smartlist_add(encrypted_session_keys
, client_part
);
344 /* Sort smartlist and put elements in result in order. */
345 smartlist_sort_digests(encrypted_session_keys
);
347 SMARTLIST_FOREACH(encrypted_session_keys
, const char *, entry
, {
348 memcpy(enc
+ pos
, entry
, REND_BASIC_AUTH_CLIENT_ENTRY_LEN
);
349 pos
+= REND_BASIC_AUTH_CLIENT_ENTRY_LEN
;
351 *encrypted_out
= enc
;
352 *encrypted_len_out
= len
;
353 enc
= NULL
; /* prevent free. */
357 if (encrypted_session_keys
) {
358 SMARTLIST_FOREACH(encrypted_session_keys
, char *, d
, tor_free(d
););
359 smartlist_free(encrypted_session_keys
);
364 /** Encrypt the encoded introduction points in <b>encoded</b> using
365 * authorization type 'stealth' with <b>descriptor_cookie</b> of length
366 * REND_DESC_COOKIE_LEN and write the result to a newly allocated string
367 * pointed to by <b>encrypted_out</b> of length <b>encrypted_len_out</b>.
368 * Return 0 for success, -1 otherwise. */
370 rend_encrypt_v2_intro_points_stealth(char **encrypted_out
,
371 size_t *encrypted_len_out
,
373 const char *descriptor_cookie
)
378 tor_assert(descriptor_cookie
);
380 enc
= tor_malloc_zero(1 + CIPHER_IV_LEN
+ strlen(encoded
));
381 enc
[0] = 0x02; /* Auth type */
382 enclen
= crypto_cipher_encrypt_with_iv(descriptor_cookie
,
384 CIPHER_IV_LEN
+strlen(encoded
),
385 encoded
, strlen(encoded
));
387 log_warn(LD_REND
, "Could not encrypt introduction point string.");
390 *encrypted_out
= enc
;
391 *encrypted_len_out
= enclen
;
392 enc
= NULL
; /* prevent free */
399 /** Attempt to parse the given <b>desc_str</b> and return true if this
400 * succeeds, false otherwise. */
402 rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t
*desc
)
404 rend_service_descriptor_t
*test_parsed
= NULL
;
405 char test_desc_id
[DIGEST_LEN
];
406 char *test_intro_content
= NULL
;
407 size_t test_intro_size
;
408 size_t test_encoded_size
;
409 const char *test_next
;
410 int res
= rend_parse_v2_service_descriptor(&test_parsed
, test_desc_id
,
414 &test_next
, desc
->desc_str
);
415 rend_service_descriptor_free(test_parsed
);
416 tor_free(test_intro_content
);
420 /** Free the storage held by an encoded v2 service descriptor. */
422 rend_encoded_v2_service_descriptor_free(
423 rend_encoded_v2_service_descriptor_t
*desc
)
427 tor_free(desc
->desc_str
);
431 /** Free the storage held by an introduction point info. */
433 rend_intro_point_free(rend_intro_point_t
*intro
)
438 extend_info_free(intro
->extend_info
);
439 crypto_pk_free(intro
->intro_key
);
441 if (intro
->accepted_intro_rsa_parts
!= NULL
) {
442 replaycache_free(intro
->accepted_intro_rsa_parts
);
448 /** Encode a set of rend_encoded_v2_service_descriptor_t's for <b>desc</b>
449 * at time <b>now</b> using <b>service_key</b>, depending on
450 * <b>auth_type</b> a <b>descriptor_cookie</b> and a list of
451 * <b>client_cookies</b> (which are both <b>NULL</b> if no client
452 * authorization is performed), and <b>period</b> (e.g. 0 for the current
453 * period, 1 for the next period, etc.) and add them to the existing list
454 * <b>descs_out</b>; return the number of seconds that the descriptors will
455 * be found by clients, or -1 if the encoding was not successful. */
457 rend_encode_v2_descriptors(smartlist_t
*descs_out
,
458 rend_service_descriptor_t
*desc
, time_t now
,
459 uint8_t period
, rend_auth_type_t auth_type
,
460 crypto_pk_t
*client_key
,
461 smartlist_t
*client_cookies
)
463 char service_id
[DIGEST_LEN
];
464 uint32_t time_period
;
465 char *ipos_base64
= NULL
, *ipos
= NULL
, *ipos_encrypted
= NULL
,
466 *descriptor_cookie
= NULL
;
467 size_t ipos_len
= 0, ipos_encrypted_len
= 0;
469 uint32_t seconds_valid
;
470 crypto_pk_t
*service_key
;
472 log_warn(LD_BUG
, "Could not encode v2 descriptor: No desc given.");
475 service_key
= (auth_type
== REND_STEALTH_AUTH
) ? client_key
: desc
->pk
;
476 tor_assert(service_key
);
477 if (auth_type
== REND_STEALTH_AUTH
) {
478 descriptor_cookie
= smartlist_get(client_cookies
, 0);
479 tor_assert(descriptor_cookie
);
481 /* Obtain service_id from public key. */
482 crypto_pk_get_digest(service_key
, service_id
);
483 /* Calculate current time-period. */
484 time_period
= get_time_period(now
, period
, service_id
);
485 /* Determine how many seconds the descriptor will be valid. */
486 seconds_valid
= period
* REND_TIME_PERIOD_V2_DESC_VALIDITY
+
487 get_seconds_valid(now
, service_id
);
488 /* Assemble, possibly encrypt, and encode introduction points. */
489 if (smartlist_len(desc
->intro_nodes
) > 0) {
490 if (rend_encode_v2_intro_points(&ipos
, desc
) < 0) {
491 log_warn(LD_REND
, "Encoding of introduction points did not succeed.");
496 ipos_len
= strlen(ipos
);
498 case REND_BASIC_AUTH
:
499 if (rend_encrypt_v2_intro_points_basic(&ipos_encrypted
,
500 &ipos_encrypted_len
, ipos
,
501 client_cookies
) < 0) {
502 log_warn(LD_REND
, "Encrypting of introduction points did not "
508 ipos
= ipos_encrypted
;
509 ipos_len
= ipos_encrypted_len
;
511 case REND_STEALTH_AUTH
:
512 if (rend_encrypt_v2_intro_points_stealth(&ipos_encrypted
,
513 &ipos_encrypted_len
, ipos
,
514 descriptor_cookie
) < 0) {
515 log_warn(LD_REND
, "Encrypting of introduction points did not "
521 ipos
= ipos_encrypted
;
522 ipos_len
= ipos_encrypted_len
;
525 log_warn(LD_REND
|LD_BUG
, "Unrecognized authorization type %d",
530 /* Base64-encode introduction points. */
531 ipos_base64
= tor_malloc_zero(ipos_len
* 2);
532 if (base64_encode(ipos_base64
, ipos_len
* 2, ipos
, ipos_len
)<0) {
533 log_warn(LD_REND
, "Could not encode introduction point string to "
534 "base64. length=%d", (int)ipos_len
);
535 tor_free(ipos_base64
);
541 /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
542 for (k
= 0; k
< REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
; k
++) {
543 char secret_id_part
[DIGEST_LEN
];
544 char secret_id_part_base32
[REND_SECRET_ID_PART_LEN_BASE32
+ 1];
545 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
546 char *permanent_key
= NULL
;
547 size_t permanent_key_len
;
548 char published
[ISO_TIME_LEN
+1];
550 char protocol_versions_string
[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
551 size_t protocol_versions_written
;
553 char *desc_str
= NULL
;
556 char desc_digest
[DIGEST_LEN
];
557 rend_encoded_v2_service_descriptor_t
*enc
=
558 tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t
));
559 /* Calculate secret-id-part = h(time-period + cookie + replica). */
560 get_secret_id_part_bytes(secret_id_part
, time_period
, descriptor_cookie
,
562 base32_encode(secret_id_part_base32
, sizeof(secret_id_part_base32
),
563 secret_id_part
, DIGEST_LEN
);
564 /* Calculate descriptor ID. */
565 rend_get_descriptor_id_bytes(enc
->desc_id
, service_id
, secret_id_part
);
566 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
567 enc
->desc_id
, DIGEST_LEN
);
568 /* PEM-encode the public key */
569 if (crypto_pk_write_public_key_to_string(service_key
, &permanent_key
,
570 &permanent_key_len
) < 0) {
571 log_warn(LD_BUG
, "Could not write public key to string.");
572 rend_encoded_v2_service_descriptor_free(enc
);
575 /* Encode timestamp. */
576 format_iso_time(published
, desc
->timestamp
);
577 /* Write protocol-versions bitmask to comma-separated value string. */
578 protocol_versions_written
= 0;
579 for (i
= 0; i
< 8; i
++) {
580 if (desc
->protocols
& 1 << i
) {
581 tor_snprintf(protocol_versions_string
+ protocol_versions_written
,
582 16 - protocol_versions_written
, "%d,", i
);
583 protocol_versions_written
+= 2;
586 if (protocol_versions_written
)
587 protocol_versions_string
[protocol_versions_written
- 1] = '\0';
589 protocol_versions_string
[0]= '\0';
590 /* Assemble complete descriptor. */
591 desc_len
= 2000 + smartlist_len(desc
->intro_nodes
) * 1000; /* far too long,
593 enc
->desc_str
= desc_str
= tor_malloc_zero(desc_len
);
594 result
= tor_snprintf(desc_str
, desc_len
,
595 "rendezvous-service-descriptor %s\n"
598 "secret-id-part %s\n"
599 "publication-time %s\n"
600 "protocol-versions %s\n",
603 secret_id_part_base32
,
605 protocol_versions_string
);
606 tor_free(permanent_key
);
608 log_warn(LD_BUG
, "Descriptor ran out of room.");
609 rend_encoded_v2_service_descriptor_free(enc
);
613 /* Add introduction points. */
615 result
= tor_snprintf(desc_str
+ written
, desc_len
- written
,
616 "introduction-points\n"
617 "-----BEGIN MESSAGE-----\n%s"
618 "-----END MESSAGE-----\n",
621 log_warn(LD_BUG
, "could not write introduction points.");
622 rend_encoded_v2_service_descriptor_free(enc
);
628 strlcpy(desc_str
+ written
, "signature\n", desc_len
- written
);
629 written
+= strlen(desc_str
+ written
);
630 if (crypto_digest(desc_digest
, desc_str
, written
) < 0) {
631 log_warn(LD_BUG
, "could not create digest.");
632 rend_encoded_v2_service_descriptor_free(enc
);
635 if (router_append_dirobj_signature(desc_str
+ written
,
637 desc_digest
, DIGEST_LEN
,
639 log_warn(LD_BUG
, "Couldn't sign desc.");
640 rend_encoded_v2_service_descriptor_free(enc
);
643 written
+= strlen(desc_str
+written
);
644 if (written
+2 > desc_len
) {
645 log_warn(LD_BUG
, "Could not finish desc.");
646 rend_encoded_v2_service_descriptor_free(enc
);
649 desc_str
[written
++] = '\n';
650 desc_str
[written
++] = 0;
651 /* Check if we can parse our own descriptor. */
652 if (!rend_desc_v2_is_parsable(enc
)) {
653 log_warn(LD_BUG
, "Could not parse my own descriptor: %s", desc_str
);
654 rend_encoded_v2_service_descriptor_free(enc
);
657 smartlist_add(descs_out
, enc
);
660 log_info(LD_REND
, "Successfully encoded a v2 descriptor and "
661 "confirmed that it is parsable.");
665 SMARTLIST_FOREACH(descs_out
, rend_encoded_v2_service_descriptor_t
*, d
,
666 rend_encoded_v2_service_descriptor_free(d
););
667 smartlist_clear(descs_out
);
671 tor_free(ipos_base64
);
672 return seconds_valid
;
675 /** Parse a service descriptor at <b>str</b> (<b>len</b> bytes). On
676 * success, return a newly alloced service_descriptor_t. On failure,
679 rend_service_descriptor_t
*
680 rend_parse_service_descriptor(const char *str
, size_t len
)
682 rend_service_descriptor_t
*result
= NULL
;
683 int i
, n_intro_points
;
684 size_t keylen
, asn1len
;
685 const char *end
, *cp
, *eos
;
686 rend_intro_point_t
*intro
;
688 result
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
691 if (end
-cp
<2) goto truncated
;
693 if (end
-cp
< 2) goto truncated
;
694 asn1len
= ntohs(get_uint16(cp
));
696 if ((size_t)(end
-cp
) < asn1len
) goto truncated
;
697 result
->pk
= crypto_pk_asn1_decode(cp
, asn1len
);
698 if (!result
->pk
) goto truncated
;
700 if (end
-cp
< 4) goto truncated
;
701 result
->timestamp
= (time_t) ntohl(get_uint32(cp
));
703 result
->protocols
= 1<<2; /* always use intro format 2 */
704 if (end
-cp
< 2) goto truncated
;
705 n_intro_points
= ntohs(get_uint16(cp
));
708 result
->intro_nodes
= smartlist_new();
709 for (i
=0;i
<n_intro_points
;++i
) {
710 if (end
-cp
< 2) goto truncated
;
711 eos
= (const char *)memchr(cp
,'\0',end
-cp
);
712 if (!eos
) goto truncated
;
713 /* Write nickname to extend info, but postpone the lookup whether
714 * we know that router. It's not part of the parsing process. */
715 intro
= tor_malloc_zero(sizeof(rend_intro_point_t
));
716 intro
->extend_info
= tor_malloc_zero(sizeof(extend_info_t
));
717 strlcpy(intro
->extend_info
->nickname
, cp
,
718 sizeof(intro
->extend_info
->nickname
));
719 smartlist_add(result
->intro_nodes
, intro
);
722 keylen
= crypto_pk_keysize(result
->pk
);
723 tor_assert(end
-cp
>= 0);
724 if ((size_t)(end
-cp
) < keylen
) goto truncated
;
725 if ((size_t)(end
-cp
) > keylen
) {
726 log_warn(LD_PROTOCOL
,
727 "Signature is %d bytes too long on service descriptor.",
728 (int)((size_t)(end
-cp
) - keylen
));
731 note_crypto_pk_op(REND_CLIENT
);
732 if (crypto_pk_public_checksig_digest(result
->pk
,
733 (char*)str
,cp
-str
, /* data */
734 (char*)cp
,end
-cp
/* signature*/
736 log_warn(LD_PROTOCOL
, "Bad signature on service descriptor.");
742 log_warn(LD_PROTOCOL
, "Truncated service descriptor.");
744 rend_service_descriptor_free(result
);
748 /** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
749 * base32 encoded. NUL-terminates out. (We use this string to
750 * identify services in directory requests and .onion URLs.)
753 rend_get_service_id(crypto_pk_t
*pk
, char *out
)
755 char buf
[DIGEST_LEN
];
757 if (crypto_pk_get_digest(pk
, buf
) < 0)
759 base32_encode(out
, REND_SERVICE_ID_LEN_BASE32
+1, buf
, REND_SERVICE_ID_LEN
);
763 /* ==== Rendezvous service descriptor cache. */
765 /** How old do we let hidden service descriptors get before discarding
766 * them as too old? */
767 #define REND_CACHE_MAX_AGE (2*24*60*60)
768 /** How wrong do we assume our clock may be when checking whether hidden
769 * services are too old or too new? */
770 #define REND_CACHE_MAX_SKEW (24*60*60)
772 /** Map from service id (as generated by rend_get_service_id) to
773 * rend_cache_entry_t. */
774 static strmap_t
*rend_cache
= NULL
;
776 /** Map from descriptor id to rend_cache_entry_t; only for hidden service
778 static digestmap_t
*rend_cache_v2_dir
= NULL
;
780 /** Initializes the service descriptor cache.
783 rend_cache_init(void)
785 rend_cache
= strmap_new();
786 rend_cache_v2_dir
= digestmap_new();
789 /** Helper: free storage held by a single service descriptor cache entry. */
791 rend_cache_entry_free(rend_cache_entry_t
*e
)
795 rend_service_descriptor_free(e
->parsed
);
800 /** Helper: deallocate a rend_cache_entry_t. (Used with strmap_free(), which
801 * requires a function pointer whose argument is void*). */
803 rend_cache_entry_free_(void *p
)
805 rend_cache_entry_free(p
);
808 /** Free all storage held by the service descriptor cache. */
810 rend_cache_free_all(void)
812 strmap_free(rend_cache
, rend_cache_entry_free_
);
813 digestmap_free(rend_cache_v2_dir
, rend_cache_entry_free_
);
815 rend_cache_v2_dir
= NULL
;
818 /** Removes all old entries from the service descriptor cache.
821 rend_cache_clean(time_t now
)
826 rend_cache_entry_t
*ent
;
827 time_t cutoff
= now
- REND_CACHE_MAX_AGE
- REND_CACHE_MAX_SKEW
;
828 for (iter
= strmap_iter_init(rend_cache
); !strmap_iter_done(iter
); ) {
829 strmap_iter_get(iter
, &key
, &val
);
830 ent
= (rend_cache_entry_t
*)val
;
831 if (ent
->parsed
->timestamp
< cutoff
) {
832 iter
= strmap_iter_next_rmv(rend_cache
, iter
);
833 rend_cache_entry_free(ent
);
835 iter
= strmap_iter_next(rend_cache
, iter
);
840 /** Remove ALL entries from the rendezvous service descriptor cache.
843 rend_cache_purge(void)
846 log_info(LD_REND
, "Purging client/v0-HS-authority HS descriptor cache");
847 strmap_free(rend_cache
, rend_cache_entry_free_
);
849 rend_cache
= strmap_new();
852 /** Remove all old v2 descriptors and those for which this hidden service
853 * directory is not responsible for any more. */
855 rend_cache_clean_v2_descs_as_dir(time_t now
)
857 digestmap_iter_t
*iter
;
858 time_t cutoff
= now
- REND_CACHE_MAX_AGE
- REND_CACHE_MAX_SKEW
;
859 for (iter
= digestmap_iter_init(rend_cache_v2_dir
);
860 !digestmap_iter_done(iter
); ) {
863 rend_cache_entry_t
*ent
;
864 digestmap_iter_get(iter
, &key
, &val
);
866 if (ent
->parsed
->timestamp
< cutoff
||
867 !hid_serv_responsible_for_desc_id(key
)) {
868 char key_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
869 base32_encode(key_base32
, sizeof(key_base32
), key
, DIGEST_LEN
);
870 log_info(LD_REND
, "Removing descriptor with ID '%s' from cache",
871 safe_str_client(key_base32
));
872 iter
= digestmap_iter_next_rmv(rend_cache_v2_dir
, iter
);
873 rend_cache_entry_free(ent
);
875 iter
= digestmap_iter_next(rend_cache_v2_dir
, iter
);
880 /** Determines whether <b>a</b> is in the interval of <b>b</b> (excluded) and
881 * <b>c</b> (included) in a circular digest ring; returns 1 if this is the
882 * case, and 0 otherwise.
885 rend_id_is_in_interval(const char *a
, const char *b
, const char *c
)
892 /* There are five cases in which a is outside the interval ]b,c]: */
893 a_b
= tor_memcmp(a
,b
,DIGEST_LEN
);
895 return 0; /* 1. a == b (b is excluded) */
896 b_c
= tor_memcmp(b
,c
,DIGEST_LEN
);
898 return 0; /* 2. b == c (interval is empty) */
899 else if (a_b
<= 0 && b_c
< 0)
900 return 0; /* 3. a b c */
901 c_a
= tor_memcmp(c
,a
,DIGEST_LEN
);
902 if (c_a
< 0 && a_b
<= 0)
903 return 0; /* 4. c a b */
904 else if (b_c
< 0 && c_a
< 0)
905 return 0; /* 5. b c a */
907 /* In the other cases (a c b; b a c; c b a), a is inside the interval. */
911 /** Return true iff <b>query</b> is a syntactically valid service ID (as
912 * generated by rend_get_service_id). */
914 rend_valid_service_id(const char *query
)
916 if (strlen(query
) != REND_SERVICE_ID_LEN_BASE32
)
919 if (strspn(query
, BASE32_CHARS
) != REND_SERVICE_ID_LEN_BASE32
)
925 /** If we have a cached rend_cache_entry_t for the service ID <b>query</b>
926 * with <b>version</b>, set *<b>e</b> to that entry and return 1.
927 * Else return 0. If <b>version</b> is nonnegative, only return an entry
928 * in that descriptor format version. Otherwise (if <b>version</b> is
929 * negative), return the most recent format we have.
932 rend_cache_lookup_entry(const char *query
, int version
, rend_cache_entry_t
**e
)
934 char key
[REND_SERVICE_ID_LEN_BASE32
+2]; /* <version><query>\0 */
935 tor_assert(rend_cache
);
936 if (!rend_valid_service_id(query
))
940 tor_snprintf(key
, sizeof(key
), "2%s", query
);
941 *e
= strmap_get_lc(rend_cache
, key
);
943 if (!*e
&& version
!= 2) {
944 tor_snprintf(key
, sizeof(key
), "0%s", query
);
945 *e
= strmap_get_lc(rend_cache
, key
);
949 tor_assert((*e
)->parsed
&& (*e
)->parsed
->intro_nodes
);
950 /* XXX023 hack for now, to return "not found" if there are no intro
951 * points remaining. See bug 997. */
952 if (! rend_client_any_intro_points_usable(*e
))
957 /** <b>query</b> is a base32'ed service id. If it's malformed, return -1.
959 * - If it is found, point *desc to it, and write its length into
960 * *desc_len, and return 1.
961 * - If it is not found, return 0.
962 * Note: calls to rend_cache_clean or rend_cache_store may invalidate
966 rend_cache_lookup_desc(const char *query
, int version
, const char **desc
,
969 rend_cache_entry_t
*e
;
971 r
= rend_cache_lookup_entry(query
,version
,&e
);
972 if (r
<= 0) return r
;
978 /** Lookup the v2 service descriptor with base32-encoded <b>desc_id</b> and
979 * copy the pointer to it to *<b>desc</b>. Return 1 on success, 0 on
980 * well-formed-but-not-found, and -1 on failure.
983 rend_cache_lookup_v2_desc_as_dir(const char *desc_id
, const char **desc
)
985 rend_cache_entry_t
*e
;
986 char desc_id_digest
[DIGEST_LEN
];
987 tor_assert(rend_cache_v2_dir
);
988 if (base32_decode(desc_id_digest
, DIGEST_LEN
,
989 desc_id
, REND_DESC_ID_V2_LEN_BASE32
) < 0) {
990 log_fn(LOG_PROTOCOL_WARN
, LD_REND
,
991 "Rejecting v2 rendezvous descriptor request -- descriptor ID "
992 "contains illegal characters: %s",
996 /* Lookup descriptor and return. */
997 e
= digestmap_get(rend_cache_v2_dir
, desc_id_digest
);
1005 /** Parse *desc, calculate its service id, and store it in the cache.
1006 * If we have a newer v0 descriptor with the same ID, ignore this one.
1007 * If we have an older descriptor with the same ID, replace it.
1008 * If we are acting as client due to the published flag and have any v2
1009 * descriptor with the same ID, reject this one in order to not get
1010 * confused with having both versions for the same service.
1012 * Return -2 if it's malformed or otherwise rejected; return -1 if we
1013 * already have a v2 descriptor here; return 0 if it's the same or older
1014 * than one we've already got; return 1 if it's novel.
1016 * The published flag tells us if we store the descriptor
1017 * in our role as directory (1) or if we cache it as client (0).
1019 * If <b>service_id</b> is non-NULL and the descriptor is not for that
1020 * service ID, reject it. <b>service_id</b> must be specified if and
1021 * only if <b>published</b> is 0 (we fetched this descriptor).
1024 rend_cache_store(const char *desc
, size_t desc_len
, int published
,
1025 const char *service_id
)
1027 rend_cache_entry_t
*e
;
1028 rend_service_descriptor_t
*parsed
;
1029 char query
[REND_SERVICE_ID_LEN_BASE32
+1];
1030 char key
[REND_SERVICE_ID_LEN_BASE32
+2]; /* 0<query>\0 */
1032 tor_assert(rend_cache
);
1033 parsed
= rend_parse_service_descriptor(desc
,desc_len
);
1035 log_warn(LD_PROTOCOL
,"Couldn't parse service descriptor.");
1038 if (rend_get_service_id(parsed
->pk
, query
)<0) {
1039 log_warn(LD_BUG
,"Couldn't compute service ID.");
1040 rend_service_descriptor_free(parsed
);
1043 if ((service_id
!= NULL
) && strcmp(query
, service_id
)) {
1044 log_warn(LD_REND
, "Received service descriptor for service ID %s; "
1045 "expected descriptor for service ID %s.",
1046 query
, safe_str(service_id
));
1047 rend_service_descriptor_free(parsed
);
1051 if (parsed
->timestamp
< now
-REND_CACHE_MAX_AGE
-REND_CACHE_MAX_SKEW
) {
1052 log_fn(LOG_PROTOCOL_WARN
, LD_REND
,
1053 "Service descriptor %s is too old.",
1054 safe_str_client(query
));
1055 rend_service_descriptor_free(parsed
);
1058 if (parsed
->timestamp
> now
+REND_CACHE_MAX_SKEW
) {
1059 log_fn(LOG_PROTOCOL_WARN
, LD_REND
,
1060 "Service descriptor %s is too far in the future.",
1061 safe_str_client(query
));
1062 rend_service_descriptor_free(parsed
);
1065 /* Do we have a v2 descriptor and fetched this descriptor as a client? */
1066 tor_snprintf(key
, sizeof(key
), "2%s", query
);
1067 if (!published
&& strmap_get_lc(rend_cache
, key
)) {
1068 log_info(LD_REND
, "We already have a v2 descriptor for service %s.",
1069 safe_str_client(query
));
1070 rend_service_descriptor_free(parsed
);
1073 tor_snprintf(key
, sizeof(key
), "0%s", query
);
1074 e
= (rend_cache_entry_t
*) strmap_get_lc(rend_cache
, key
);
1075 if (e
&& e
->parsed
->timestamp
> parsed
->timestamp
) {
1076 log_info(LD_REND
,"We already have a newer service descriptor %s with the "
1077 "same ID and version.",
1078 safe_str_client(query
));
1079 rend_service_descriptor_free(parsed
);
1082 if (e
&& e
->len
== desc_len
&& tor_memeq(desc
,e
->desc
,desc_len
)) {
1083 log_info(LD_REND
,"We already have this service descriptor %s.",
1084 safe_str_client(query
));
1085 e
->received
= time(NULL
);
1086 rend_service_descriptor_free(parsed
);
1090 e
= tor_malloc_zero(sizeof(rend_cache_entry_t
));
1091 strmap_set_lc(rend_cache
, key
, e
);
1093 rend_service_descriptor_free(e
->parsed
);
1096 e
->received
= time(NULL
);
1099 e
->desc
= tor_malloc(desc_len
);
1100 memcpy(e
->desc
, desc
, desc_len
);
1102 log_debug(LD_REND
,"Successfully stored rend desc '%s', len %d.",
1103 safe_str_client(query
), (int)desc_len
);
1107 /** Parse the v2 service descriptor(s) in <b>desc</b> and store it/them to the
1108 * local rend cache. Don't attempt to decrypt the included list of introduction
1109 * points (as we don't have a descriptor cookie for it).
1111 * If we have a newer descriptor with the same ID, ignore this one.
1112 * If we have an older descriptor with the same ID, replace it.
1113 * Return -2 if we are not acting as hidden service directory;
1114 * return -1 if the descriptor(s) were not parsable; return 0 if all
1115 * descriptors are the same or older than those we've already got;
1116 * return a positive number for the number of novel stored descriptors.
1119 rend_cache_store_v2_desc_as_dir(const char *desc
)
1121 rend_service_descriptor_t
*parsed
;
1122 char desc_id
[DIGEST_LEN
];
1123 char *intro_content
;
1125 size_t encoded_size
;
1126 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
1127 int number_parsed
= 0, number_stored
= 0;
1128 const char *current_desc
= desc
;
1129 const char *next_desc
;
1130 rend_cache_entry_t
*e
;
1131 time_t now
= time(NULL
);
1132 tor_assert(rend_cache_v2_dir
);
1134 if (!hid_serv_acting_as_directory()) {
1135 /* Cannot store descs, because we are (currently) not acting as
1136 * hidden service directory. */
1137 log_info(LD_REND
, "Cannot store descs: Not acting as hs dir");
1140 while (rend_parse_v2_service_descriptor(&parsed
, desc_id
, &intro_content
,
1141 &intro_size
, &encoded_size
,
1142 &next_desc
, current_desc
) >= 0) {
1144 /* We don't care about the introduction points. */
1145 tor_free(intro_content
);
1146 /* For pretty log statements. */
1147 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
1148 desc_id
, DIGEST_LEN
);
1149 /* Is desc ID in the range that we are (directly or indirectly) responsible
1151 if (!hid_serv_responsible_for_desc_id(desc_id
)) {
1152 log_info(LD_REND
, "Service descriptor with desc ID %s is not in "
1153 "interval that we are responsible for.",
1154 safe_str_client(desc_id_base32
));
1157 /* Is descriptor too old? */
1158 if (parsed
->timestamp
< now
- REND_CACHE_MAX_AGE
-REND_CACHE_MAX_SKEW
) {
1159 log_info(LD_REND
, "Service descriptor with desc ID %s is too old.",
1160 safe_str(desc_id_base32
));
1163 /* Is descriptor too far in the future? */
1164 if (parsed
->timestamp
> now
+ REND_CACHE_MAX_SKEW
) {
1165 log_info(LD_REND
, "Service descriptor with desc ID %s is too far in the "
1167 safe_str(desc_id_base32
));
1170 /* Do we already have a newer descriptor? */
1171 e
= digestmap_get(rend_cache_v2_dir
, desc_id
);
1172 if (e
&& e
->parsed
->timestamp
> parsed
->timestamp
) {
1173 log_info(LD_REND
, "We already have a newer service descriptor with the "
1174 "same desc ID %s and version.",
1175 safe_str(desc_id_base32
));
1178 /* Do we already have this descriptor? */
1179 if (e
&& !strcmp(desc
, e
->desc
)) {
1180 log_info(LD_REND
, "We already have this service descriptor with desc "
1181 "ID %s.", safe_str(desc_id_base32
));
1182 e
->received
= time(NULL
);
1185 /* Store received descriptor. */
1187 e
= tor_malloc_zero(sizeof(rend_cache_entry_t
));
1188 digestmap_set(rend_cache_v2_dir
, desc_id
, e
);
1190 rend_service_descriptor_free(e
->parsed
);
1193 e
->received
= time(NULL
);
1195 e
->desc
= tor_strndup(current_desc
, encoded_size
);
1196 e
->len
= encoded_size
;
1197 log_info(LD_REND
, "Successfully stored service descriptor with desc ID "
1199 safe_str(desc_id_base32
), (int)encoded_size
);
1203 rend_service_descriptor_free(parsed
);
1205 /* advance to next descriptor, if available. */
1206 current_desc
= next_desc
;
1207 /* check if there is a next descriptor. */
1208 if (!current_desc
||
1209 strcmpstart(current_desc
, "rendezvous-service-descriptor "))
1212 if (!number_parsed
) {
1213 log_info(LD_REND
, "Could not parse any descriptor.");
1216 log_info(LD_REND
, "Parsed %d and added %d descriptor%s.",
1217 number_parsed
, number_stored
, number_stored
!= 1 ? "s" : "");
1218 return number_stored
;
1221 /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
1222 * of introduction points with <b>descriptor_cookie</b> (which may also be
1223 * <b>NULL</b> if decryption is not necessary), and store the descriptor to
1224 * the local cache under its version and service id.
1226 * If we have a newer v2 descriptor with the same ID, ignore this one.
1227 * If we have an older descriptor with the same ID, replace it.
1228 * If we have any v0 descriptor with the same ID, reject this one in order
1229 * to not get confused with having both versions for the same service.
1230 * If the descriptor's service ID does not match
1231 * <b>rend_query</b>-\>onion_address, reject it.
1232 * Return -2 if it's malformed or otherwise rejected; return -1 if we
1233 * already have a v0 descriptor here; return 0 if it's the same or older
1234 * than one we've already got; return 1 if it's novel.
1237 rend_cache_store_v2_desc_as_client(const char *desc
,
1238 const rend_data_t
*rend_query
)
1240 /*XXXX this seems to have a bit of duplicate code with
1241 * rend_cache_store_v2_desc_as_dir(). Fix that. */
1242 /* Though having similar elements, both functions were separated on
1244 * - dirs don't care about encoded/encrypted introduction points, clients
1246 * - dirs store descriptors in a separate cache by descriptor ID, whereas
1247 * clients store them by service ID; both caches are different data
1248 * structures and have different access methods.
1249 * - dirs store a descriptor only if they are responsible for its ID,
1250 * clients do so in every way (because they have requested it before).
1251 * - dirs can process multiple concatenated descriptors which is required
1252 * for replication, whereas clients only accept a single descriptor.
1253 * Thus, combining both methods would result in a lot of if statements
1254 * which probably would not improve, but worsen code readability. -KL */
1255 rend_service_descriptor_t
*parsed
= NULL
;
1256 char desc_id
[DIGEST_LEN
];
1257 char *intro_content
= NULL
;
1259 size_t encoded_size
;
1260 const char *next_desc
;
1261 time_t now
= time(NULL
);
1262 char key
[REND_SERVICE_ID_LEN_BASE32
+2];
1263 char service_id
[REND_SERVICE_ID_LEN_BASE32
+1];
1264 rend_cache_entry_t
*e
;
1266 tor_assert(rend_cache
);
1268 /* Parse the descriptor. */
1269 if (rend_parse_v2_service_descriptor(&parsed
, desc_id
, &intro_content
,
1270 &intro_size
, &encoded_size
,
1271 &next_desc
, desc
) < 0) {
1272 log_warn(LD_REND
, "Could not parse descriptor.");
1276 /* Compute service ID from public key. */
1277 if (rend_get_service_id(parsed
->pk
, service_id
)<0) {
1278 log_warn(LD_REND
, "Couldn't compute service ID.");
1282 if (strcmp(rend_query
->onion_address
, service_id
)) {
1283 log_warn(LD_REND
, "Received service descriptor for service ID %s; "
1284 "expected descriptor for service ID %s.",
1285 service_id
, safe_str(rend_query
->onion_address
));
1289 /* Decode/decrypt introduction points. */
1290 if (intro_content
) {
1291 if (rend_query
->auth_type
!= REND_NO_AUTH
&&
1292 !tor_mem_is_zero(rend_query
->descriptor_cookie
,
1293 sizeof(rend_query
->descriptor_cookie
))) {
1294 char *ipos_decrypted
= NULL
;
1295 size_t ipos_decrypted_size
;
1296 if (rend_decrypt_introduction_points(&ipos_decrypted
,
1297 &ipos_decrypted_size
,
1298 rend_query
->descriptor_cookie
,
1301 log_warn(LD_REND
, "Failed to decrypt introduction points. We are "
1302 "probably unable to parse the encoded introduction points.");
1304 /* Replace encrypted with decrypted introduction points. */
1305 log_info(LD_REND
, "Successfully decrypted introduction points.");
1306 tor_free(intro_content
);
1307 intro_content
= ipos_decrypted
;
1308 intro_size
= ipos_decrypted_size
;
1311 if (rend_parse_introduction_points(parsed
, intro_content
,
1313 log_warn(LD_REND
, "Failed to parse introduction points. Either the "
1314 "service has published a corrupt descriptor or you have "
1315 "provided invalid authorization data.");
1320 log_info(LD_REND
, "Descriptor does not contain any introduction points.");
1321 parsed
->intro_nodes
= smartlist_new();
1323 /* We don't need the encoded/encrypted introduction points any longer. */
1324 tor_free(intro_content
);
1325 /* Is descriptor too old? */
1326 if (parsed
->timestamp
< now
- REND_CACHE_MAX_AGE
-REND_CACHE_MAX_SKEW
) {
1327 log_warn(LD_REND
, "Service descriptor with service ID %s is too old.",
1328 safe_str_client(service_id
));
1332 /* Is descriptor too far in the future? */
1333 if (parsed
->timestamp
> now
+ REND_CACHE_MAX_SKEW
) {
1334 log_warn(LD_REND
, "Service descriptor with service ID %s is too far in "
1335 "the future.", safe_str_client(service_id
));
1339 /* Do we have a v0 descriptor? */
1340 tor_snprintf(key
, sizeof(key
), "0%s", service_id
);
1341 if (strmap_get_lc(rend_cache
, key
)) {
1342 log_info(LD_REND
, "We already have a v0 descriptor for service ID %s.",
1343 safe_str_client(service_id
));
1347 /* Do we already have a newer descriptor? */
1348 tor_snprintf(key
, sizeof(key
), "2%s", service_id
);
1349 e
= (rend_cache_entry_t
*) strmap_get_lc(rend_cache
, key
);
1350 if (e
&& e
->parsed
->timestamp
> parsed
->timestamp
) {
1351 log_info(LD_REND
, "We already have a newer service descriptor for "
1352 "service ID %s with the same desc ID and version.",
1353 safe_str_client(service_id
));
1357 /* Do we already have this descriptor? */
1358 if (e
&& !strcmp(desc
, e
->desc
)) {
1359 log_info(LD_REND
,"We already have this service descriptor %s.",
1360 safe_str_client(service_id
));
1361 e
->received
= time(NULL
);
1366 e
= tor_malloc_zero(sizeof(rend_cache_entry_t
));
1367 strmap_set_lc(rend_cache
, key
, e
);
1369 rend_service_descriptor_free(e
->parsed
);
1372 e
->received
= time(NULL
);
1374 e
->desc
= tor_malloc_zero(encoded_size
+ 1);
1375 strlcpy(e
->desc
, desc
, encoded_size
+ 1);
1376 e
->len
= encoded_size
;
1377 log_debug(LD_REND
,"Successfully stored rend desc '%s', len %d.",
1378 safe_str_client(service_id
), (int)encoded_size
);
1382 rend_service_descriptor_free(parsed
);
1383 tor_free(intro_content
);
1387 /** Called when we get a rendezvous-related relay cell on circuit
1388 * <b>circ</b>. Dispatch on rendezvous relay command. */
1390 rend_process_relay_cell(circuit_t
*circ
, const crypt_path_t
*layer_hint
,
1391 int command
, size_t length
,
1392 const uint8_t *payload
)
1394 or_circuit_t
*or_circ
= NULL
;
1395 origin_circuit_t
*origin_circ
= NULL
;
1397 if (CIRCUIT_IS_ORIGIN(circ
)) {
1398 origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
1399 if (!layer_hint
|| layer_hint
!= origin_circ
->cpath
->prev
) {
1400 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1401 "Relay cell (rend purpose %d) from wrong hop on origin circ",
1406 or_circ
= TO_OR_CIRCUIT(circ
);
1410 case RELAY_COMMAND_ESTABLISH_INTRO
:
1412 r
= rend_mid_establish_intro(or_circ
,payload
,length
);
1414 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS
:
1416 r
= rend_mid_establish_rendezvous(or_circ
,payload
,length
);
1418 case RELAY_COMMAND_INTRODUCE1
:
1420 r
= rend_mid_introduce(or_circ
,payload
,length
);
1422 case RELAY_COMMAND_INTRODUCE2
:
1424 r
= rend_service_introduce(origin_circ
,payload
,length
);
1426 case RELAY_COMMAND_INTRODUCE_ACK
:
1428 r
= rend_client_introduction_acked(origin_circ
,payload
,length
);
1430 case RELAY_COMMAND_RENDEZVOUS1
:
1432 r
= rend_mid_rendezvous(or_circ
,payload
,length
);
1434 case RELAY_COMMAND_RENDEZVOUS2
:
1436 r
= rend_client_receive_rendezvous(origin_circ
,payload
,length
);
1438 case RELAY_COMMAND_INTRO_ESTABLISHED
:
1440 r
= rend_service_intro_established(origin_circ
,payload
,length
);
1442 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
:
1444 r
= rend_client_rendezvous_acked(origin_circ
,payload
,length
);
1447 tor_fragile_assert();
1451 log_info(LD_PROTOCOL
, "Dropping cell (type %d) for wrong circuit type.",
1455 /** Allocate and return a new rend_data_t with the same
1456 * contents as <b>query</b>. */
1458 rend_data_dup(const rend_data_t
*data
)
1461 return tor_memdup(data
, sizeof(rend_data_t
));