Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
routerparse.c
Go to the documentation of this file.
1/* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file routerparse.c
9 * \brief Code to parse and validate router descriptors, consenus directories,
10 * and similar objects.
11 *
12 * The objects parsed by this module use a common text-based metaformat,
13 * documented in dir-spec.txt in torspec.git. This module is itself divided
14 * into two major kinds of function: code to handle the metaformat, and code
15 * to convert from particular instances of the metaformat into the
16 * objects that Tor uses.
17 *
18 * The generic parsing code works by calling a table-based tokenizer on the
19 * input string. Each token corresponds to a single line with a token, plus
20 * optional arguments on that line, plus an optional base-64 encoded object
21 * after that line. Each token has a definition in a table of token_rule_t
22 * entries that describes how many arguments it can take, whether it takes an
23 * object, how many times it may appear, whether it must appear first, and so
24 * on.
25 *
26 * The tokenizer function tokenize_string() converts its string input into a
27 * smartlist full of instances of directory_token_t, according to a provided
28 * table of token_rule_t.
29 *
30 * The generic parts of this module additionally include functions for
31 * finding the start and end of signed information inside a signed object, and
32 * computing the digest that will be signed.
33 *
34 * There are also functions for saving objects to disk that have caused
35 * parsing to fail.
36 *
37 * The specific parts of this module describe conversions between
38 * particular lists of directory_token_t and particular objects. The
39 * kinds of objects that can be parsed here are:
40 * <ul>
41 * <li>router descriptors (managed from routerlist.c)
42 * <li>extra-info documents (managed from routerlist.c)
43 * <li>microdescriptors (managed from microdesc.c)
44 * <li>vote and consensus networkstatus documents, and the routerstatus_t
45 * objects that they comprise (managed from networkstatus.c)
46 * <li>detached-signature objects used by authorities for gathering
47 * signatures on the networkstatus consensus (managed from dirvote.c)
48 * <li>authority key certificates (managed from routerlist.c)
49 * <li>hidden service descriptors (managed from rendcommon.c and rendcache.c)
50 * </ul>
51 **/
52
53#define ROUTERDESC_TOKEN_TABLE_PRIVATE
54#define ROUTERPARSE_PRIVATE
55
56#include "core/or/or.h"
57#include "app/config/config.h"
58#include "core/or/policies.h"
59#include "core/or/versions.h"
74#include "lib/memarea/memarea.h"
75#include "lib/sandbox/sandbox.h"
76
81
82/****************************************************************************/
83
84/** List of tokens recognized in router descriptors */
85// clang-format off
87 T0N("reject", K_REJECT, ARGS, NO_OBJ ),
88 T0N("accept", K_ACCEPT, ARGS, NO_OBJ ),
89 T0N("reject6", K_REJECT6, ARGS, NO_OBJ ),
90 T0N("accept6", K_ACCEPT6, ARGS, NO_OBJ ),
91 T1_START( "router", K_ROUTER, GE(5), NO_OBJ ),
92 T01("ipv6-policy", K_IPV6_POLICY, CONCAT_ARGS, NO_OBJ),
93 T1( "signing-key", K_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
94 T01("onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024 ),
95 T1("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
96 T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
97 T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
98 T01("uptime", K_UPTIME, GE(1), NO_OBJ ),
99 T01("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
100 T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ ),
101 T01("platform", K_PLATFORM, CONCAT_ARGS, NO_OBJ ),
102 T1("proto", K_PROTO, CONCAT_ARGS, NO_OBJ ),
103 T01("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
104 T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
105 T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
106 T01("extra-info-digest", K_EXTRA_INFO_DIGEST, GE(1), NO_OBJ ),
107 T01("hidden-service-dir", K_HIDDEN_SERVICE_DIR, NO_ARGS, NO_OBJ ),
108 T1("identity-ed25519", K_IDENTITY_ED25519, NO_ARGS, NEED_OBJ ),
109 T1("master-key-ed25519", K_MASTER_KEY_ED25519, GE(1), NO_OBJ ),
110 T1("router-sig-ed25519", K_ROUTER_SIG_ED25519, GE(1), NO_OBJ ),
111 T01("onion-key-crosscert", K_ONION_KEY_CROSSCERT, NO_ARGS, NEED_OBJ ),
112 T1("ntor-onion-key-crosscert", K_NTOR_ONION_KEY_CROSSCERT,
113 EQ(1), NEED_OBJ ),
114
115 T01("allow-single-hop-exits",K_ALLOW_SINGLE_HOP_EXITS, NO_ARGS, NO_OBJ ),
116
117 T01("family", K_FAMILY, ARGS, NO_OBJ ),
118 T0N("family-cert", K_FAMILY_CERT, ARGS, NEED_OBJ ),
119 T01("caches-extra-info", K_CACHES_EXTRA_INFO, NO_ARGS, NO_OBJ ),
120 T0N("or-address", K_OR_ADDRESS, GE(1), NO_OBJ ),
121
122 T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
123 T1( "bandwidth", K_BANDWIDTH, GE(3), NO_OBJ ),
124 A01("@purpose", A_PURPOSE, GE(1), NO_OBJ ),
125 T01("tunnelled-dir-server",K_DIR_TUNNELLED, NO_ARGS, NO_OBJ ),
126
128};
129// clang-format on
130
131/** List of tokens recognized in extra-info documents. */
132// clang-format off
134 T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
135 T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
136 T1("identity-ed25519", K_IDENTITY_ED25519, NO_ARGS, NEED_OBJ ),
137 T1("router-sig-ed25519", K_ROUTER_SIG_ED25519, GE(1), NO_OBJ ),
138 T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
139 T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
140 T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
141 T01("dirreq-stats-end", K_DIRREQ_END, ARGS, NO_OBJ ),
142 T01("dirreq-v2-ips", K_DIRREQ_V2_IPS, ARGS, NO_OBJ ),
143 T01("dirreq-v3-ips", K_DIRREQ_V3_IPS, ARGS, NO_OBJ ),
144 T01("dirreq-v2-reqs", K_DIRREQ_V2_REQS, ARGS, NO_OBJ ),
145 T01("dirreq-v3-reqs", K_DIRREQ_V3_REQS, ARGS, NO_OBJ ),
146 T01("dirreq-v2-share", K_DIRREQ_V2_SHARE, ARGS, NO_OBJ ),
147 T01("dirreq-v3-share", K_DIRREQ_V3_SHARE, ARGS, NO_OBJ ),
148 T01("dirreq-v2-resp", K_DIRREQ_V2_RESP, ARGS, NO_OBJ ),
149 T01("dirreq-v3-resp", K_DIRREQ_V3_RESP, ARGS, NO_OBJ ),
150 T01("dirreq-v2-direct-dl", K_DIRREQ_V2_DIR, ARGS, NO_OBJ ),
151 T01("dirreq-v3-direct-dl", K_DIRREQ_V3_DIR, ARGS, NO_OBJ ),
152 T01("dirreq-v2-tunneled-dl", K_DIRREQ_V2_TUN, ARGS, NO_OBJ ),
153 T01("dirreq-v3-tunneled-dl", K_DIRREQ_V3_TUN, ARGS, NO_OBJ ),
154 T01("entry-stats-end", K_ENTRY_END, ARGS, NO_OBJ ),
155 T01("entry-ips", K_ENTRY_IPS, ARGS, NO_OBJ ),
156 T01("cell-stats-end", K_CELL_END, ARGS, NO_OBJ ),
157 T01("cell-processed-cells", K_CELL_PROCESSED, ARGS, NO_OBJ ),
158 T01("cell-queued-cells", K_CELL_QUEUED, ARGS, NO_OBJ ),
159 T01("cell-time-in-queue", K_CELL_TIME, ARGS, NO_OBJ ),
160 T01("cell-circuits-per-decile", K_CELL_CIRCS, ARGS, NO_OBJ ),
161 T01("exit-stats-end", K_EXIT_END, ARGS, NO_OBJ ),
162 T01("exit-kibibytes-written", K_EXIT_WRITTEN, ARGS, NO_OBJ ),
163 T01("exit-kibibytes-read", K_EXIT_READ, ARGS, NO_OBJ ),
164 T01("exit-streams-opened", K_EXIT_OPENED, ARGS, NO_OBJ ),
165
166 T1_START( "extra-info", K_EXTRA_INFO, GE(2), NO_OBJ ),
167
169};
170// clang-format on
171
172#undef T
173
174/* static function prototypes */
177static int check_family_certs(const smartlist_t *family_cert_tokens,
178 const ed25519_public_key_t *identity_key,
179 smartlist_t **family_ids_out,
180 time_t *family_expiration_out);
181
182/** Set <b>digest</b> to the SHA-1 digest of the hash of the first router in
183 * <b>s</b>. Return 0 on success, -1 on failure.
184 */
185int
186router_get_router_hash(const char *s, size_t s_len, char *digest)
187{
188 return router_get_hash_impl(s, s_len, digest,
189 "router ","\nrouter-signature", '\n',
190 DIGEST_SHA1);
191}
192
193/** Set <b>digest</b> to the SHA-1 digest of the hash of the <b>s_len</b>-byte
194 * extrainfo string at <b>s</b>. Return 0 on success, -1 on failure. */
195int
196router_get_extrainfo_hash(const char *s, size_t s_len, char *digest)
197{
198 return router_get_hash_impl(s, s_len, digest, "extra-info",
199 "\nrouter-signature",'\n', DIGEST_SHA1);
200}
201
202/** Helper: move *<b>s_ptr</b> ahead to the next router, the next extra-info,
203 * or to the first of the annotations proceeding the next router or
204 * extra-info---whichever comes first. Set <b>is_extrainfo_out</b> to true if
205 * we found an extrainfo, or false if found a router. Do not scan beyond
206 * <b>eos</b>. Return -1 if we found nothing; 0 if we found something. */
207static int
209 const char *eos,
210 int *is_extrainfo_out)
211{
212 const char *annotations = NULL;
213 const char *s = *s_ptr;
214
215 s = eat_whitespace_eos(s, eos);
216
217 while (s < eos-32) { /* 32 gives enough room for a the first keyword. */
218 /* We're at the start of a line. */
219 tor_assert(*s != '\n');
220
221 if (*s == '@' && !annotations) {
222 annotations = s;
223 } else if (*s == 'r' && !strcmpstart(s, "router ")) {
224 *s_ptr = annotations ? annotations : s;
225 *is_extrainfo_out = 0;
226 return 0;
227 } else if (*s == 'e' && !strcmpstart(s, "extra-info ")) {
228 *s_ptr = annotations ? annotations : s;
229 *is_extrainfo_out = 1;
230 return 0;
231 }
232
233 if (!(s = memchr(s+1, '\n', eos-(s+1))))
234 break;
235 s = eat_whitespace_eos(s, eos);
236 }
237 return -1;
238}
239
240/** Given a string *<b>s</b> containing a concatenated sequence of router
241 * descriptors (or extra-info documents if <b>want_extrainfo</b> is set),
242 * parses them and stores the result in <b>dest</b>. All routers are marked
243 * running and valid. Advances *s to a point immediately following the last
244 * router entry. Ignore any trailing router entries that are not complete.
245 *
246 * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
247 * descriptor in the signed_descriptor_body field of each routerinfo_t. If it
248 * isn't SAVED_NOWHERE, remember the offset of each descriptor.
249 *
250 * Returns 0 on success and -1 on failure. Adds a digest to
251 * <b>invalid_digests_out</b> for every entry that was unparseable or
252 * invalid. (This may cause duplicate entries.)
253 */
254int
255router_parse_list_from_string(const char **s, const char *eos,
256 smartlist_t *dest,
257 saved_location_t saved_location,
258 int want_extrainfo,
259 int allow_annotations,
260 const char *prepend_annotations,
261 smartlist_t *invalid_digests_out)
262{
263 routerinfo_t *router;
264 extrainfo_t *extrainfo;
265 signed_descriptor_t *signed_desc = NULL;
266 void *elt;
267 const char *end, *start;
268 int have_extrainfo;
269
270 tor_assert(s);
271 tor_assert(*s);
272 tor_assert(dest);
273
274 start = *s;
275 if (!eos)
276 eos = *s + strlen(*s);
277
278 tor_assert(eos >= *s);
279
280 while (1) {
281 char raw_digest[DIGEST_LEN];
282 int have_raw_digest = 0;
283 int dl_again = 0;
284 if (find_start_of_next_router_or_extrainfo(s, eos, &have_extrainfo) < 0)
285 break;
286
287 end = tor_memstr(*s, eos-*s, "\nrouter-signature");
288 if (end)
289 end = tor_memstr(end, eos-end, "\n-----END SIGNATURE-----\n");
290 if (end)
291 end += strlen("\n-----END SIGNATURE-----\n");
292
293 if (!end)
294 break;
295
296 elt = NULL;
297
298 if (have_extrainfo && want_extrainfo) {
300 have_raw_digest = router_get_extrainfo_hash(*s, end-*s, raw_digest) == 0;
301 extrainfo = extrainfo_parse_entry_from_string(*s, end,
302 saved_location != SAVED_IN_CACHE,
303 rl->identity_map, &dl_again);
304 if (extrainfo) {
305 signed_desc = &extrainfo->cache_info;
306 elt = extrainfo;
307 }
308 } else if (!have_extrainfo && !want_extrainfo) {
309 have_raw_digest = router_get_router_hash(*s, end-*s, raw_digest) == 0;
310 router = router_parse_entry_from_string(*s, end,
311 saved_location != SAVED_IN_CACHE,
312 allow_annotations,
313 prepend_annotations, &dl_again);
314 if (router) {
315 log_debug(LD_DIR, "Read router '%s', purpose '%s'",
316 router_describe(router),
318 signed_desc = &router->cache_info;
319 elt = router;
320 }
321 }
322 if (! elt && ! dl_again && have_raw_digest && invalid_digests_out) {
323 smartlist_add(invalid_digests_out, tor_memdup(raw_digest, DIGEST_LEN));
324 }
325 if (!elt) {
326 *s = end;
327 continue;
328 }
329 if (saved_location != SAVED_NOWHERE) {
330 tor_assert(signed_desc);
331 signed_desc->saved_location = saved_location;
332 signed_desc->saved_offset = *s - start;
333 }
334 *s = end;
335 smartlist_add(dest, elt);
336 }
337
338 return 0;
339}
340
341/** Try to find an IPv6 OR port in <b>list</b> of directory_token_t's
342 * with at least one argument (use GE(1) in setup). If found, store
343 * address and port number to <b>addr_out</b> and
344 * <b>port_out</b>. Return number of OR ports found. */
345int
347 tor_addr_t *addr_out,
348 uint16_t *port_out)
349{
350 int ret = 0;
351 tor_assert(list != NULL);
352 tor_assert(addr_out != NULL);
353 tor_assert(port_out != NULL);
354
356 tor_addr_t a;
357 maskbits_t bits;
358 uint16_t port_min, port_max;
359 tor_assert(t->n_args >= 1);
360 /* XXXX Prop186 the full spec allows much more than this. */
361 if (tor_addr_parse_mask_ports(t->args[0], 0,
362 &a, &bits, &port_min,
363 &port_max) == AF_INET6 &&
364 bits == 128 &&
365 port_min == port_max) {
366 /* Okay, this is one we can understand. Use it and ignore
367 any potential more addresses in list. */
368 tor_addr_copy(addr_out, &a);
369 *port_out = port_min;
370 ret = 1;
371 break;
372 }
373 } SMARTLIST_FOREACH_END(t);
374
375 return ret;
376}
377
378/** Helper function: reads a single router entry from *<b>s</b> ...
379 * *<b>end</b>. Mallocs a new router and returns it if all goes well, else
380 * returns NULL. If <b>cache_copy</b> is true, duplicate the contents of
381 * s through end into the signed_descriptor_body of the resulting
382 * routerinfo_t.
383 *
384 * If <b>end</b> is NULL, <b>s</b> must be properly NUL-terminated.
385 *
386 * If <b>allow_annotations</b>, it's okay to encounter annotations in <b>s</b>
387 * before the router; if it's false, reject the router if it's annotated. If
388 * <b>prepend_annotations</b> is set, it should contain some annotations:
389 * append them to the front of the router before parsing it, and keep them
390 * around when caching the router.
391 *
392 * Only one of allow_annotations and prepend_annotations may be set.
393 *
394 * If <b>can_dl_again_out</b> is provided, set *<b>can_dl_again_out</b> to 1
395 * if it's okay to try to download a descriptor with this same digest again,
396 * and 0 if it isn't. (It might not be okay to download it again if part of
397 * the part covered by the digest is invalid.)
398 */
400router_parse_entry_from_string(const char *s, const char *end,
401 int cache_copy, int allow_annotations,
402 const char *prepend_annotations,
403 int *can_dl_again_out)
404{
405 routerinfo_t *router = NULL;
406 char digest[128];
407 smartlist_t *tokens = NULL, *exit_policy_tokens = NULL;
409 struct in_addr in;
410 const char *start_of_annotations, *cp, *s_dup = s;
411 size_t prepend_len = prepend_annotations ? strlen(prepend_annotations) : 0;
412 int ok = 1;
413 memarea_t *area = NULL;
414 tor_cert_t *ntor_cc_cert = NULL;
415 /* Do not set this to '1' until we have parsed everything that we intend to
416 * parse that's covered by the hash. */
417 int can_dl_again = 0;
418 crypto_pk_t *rsa_pubkey = NULL;
419
420 tor_assert(!allow_annotations || !prepend_annotations);
421
422 if (!end) {
423 end = s + strlen(s);
424 }
425
426 /* point 'end' to a point immediately after the final newline. */
427 while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
428 --end;
429
430 area = memarea_new();
431 tokens = smartlist_new();
432 if (prepend_annotations) {
433 if (tokenize_string(area,prepend_annotations,NULL,tokens,
434 routerdesc_token_table,TS_NOCHECK)) {
435 log_warn(LD_DIR, "Error tokenizing router descriptor (annotations).");
436 goto err;
437 }
438 }
439
440 start_of_annotations = s;
441 cp = tor_memstr(s, end-s, "\nrouter ");
442 if (!cp) {
443 if (end-s < 7 || strcmpstart(s, "router ")) {
444 log_warn(LD_DIR, "No router keyword found.");
445 goto err;
446 }
447 } else {
448 s = cp+1;
449 }
450
451 if (start_of_annotations != s) { /* We have annotations */
452 if (allow_annotations) {
453 if (tokenize_string(area,start_of_annotations,s,tokens,
454 routerdesc_token_table,TS_NOCHECK)) {
455 log_warn(LD_DIR, "Error tokenizing router descriptor (annotations).");
456 goto err;
457 }
458 } else {
459 log_warn(LD_DIR, "Found unexpected annotations on router descriptor not "
460 "loaded from disk. Dropping it.");
461 goto err;
462 }
463 }
464
465 if (!tor_memstr(s, end-s, "\nproto ")) {
466 log_debug(LD_DIR, "Found an obsolete router descriptor. "
467 "Rejecting quietly.");
468 goto err;
469 }
470
471 if (router_get_router_hash(s, end - s, digest) < 0) {
472 log_warn(LD_DIR, "Couldn't compute router hash.");
473 goto err;
474 }
475 {
476 int flags = 0;
477 if (allow_annotations)
478 flags |= TS_ANNOTATIONS_OK;
479 if (prepend_annotations)
480 flags |= TS_ANNOTATIONS_OK|TS_NO_NEW_ANNOTATIONS;
481
482 if (tokenize_string(area,s,end,tokens,routerdesc_token_table, flags)) {
483 log_warn(LD_DIR, "Error tokenizing router descriptor.");
484 goto err;
485 }
486 }
487
488 if (smartlist_len(tokens) < 2) {
489 log_warn(LD_DIR, "Impossibly short router descriptor.");
490 goto err;
491 }
492
493 tok = find_by_keyword(tokens, K_ROUTER);
494 const int router_token_pos = smartlist_pos(tokens, tok);
495 tor_assert(tok->n_args >= 5);
496
497 router = tor_malloc_zero(sizeof(routerinfo_t));
498 router->cert_expiration_time = TIME_MAX;
499 router->cache_info.routerlist_index = -1;
500 router->cache_info.annotations_len = s-start_of_annotations + prepend_len;
501 router->cache_info.signed_descriptor_len = end-s;
502 if (cache_copy) {
503 size_t len = router->cache_info.signed_descriptor_len +
504 router->cache_info.annotations_len;
505 char *signed_body =
506 router->cache_info.signed_descriptor_body = tor_malloc(len+1);
507 if (prepend_annotations) {
508 memcpy(signed_body, prepend_annotations, prepend_len);
509 signed_body += prepend_len;
510 }
511 /* This assertion will always succeed.
512 * len == signed_desc_len + annotations_len
513 * == end-s + s-start_of_annotations + prepend_len
514 * == end-start_of_annotations + prepend_len
515 * We already wrote prepend_len bytes into the buffer; now we're
516 * writing end-start_of_annotations -NM. */
517 tor_assert(signed_body+(end-start_of_annotations) ==
518 router->cache_info.signed_descriptor_body+len);
519 memcpy(signed_body, start_of_annotations, end-start_of_annotations);
520 router->cache_info.signed_descriptor_body[len] = '\0';
521 tor_assert(strlen(router->cache_info.signed_descriptor_body) == len);
522 }
523 memcpy(router->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
524
525 router->nickname = tor_strdup(tok->args[0]);
526 if (!is_legal_nickname(router->nickname)) {
527 log_warn(LD_DIR,"Router nickname is invalid");
528 goto err;
529 }
530 if (!tor_inet_aton(tok->args[1], &in)) {
531 log_warn(LD_DIR,"Router address is not an IP address.");
532 goto err;
533 }
534 tor_addr_from_in(&router->ipv4_addr, &in);
535
536 router->ipv4_orport =
537 (uint16_t) tor_parse_long(tok->args[2],10,0,65535,&ok,NULL);
538 if (!ok) {
539 log_warn(LD_DIR,"Invalid OR port %s", escaped(tok->args[2]));
540 goto err;
541 }
542 router->ipv4_dirport =
543 (uint16_t) tor_parse_long(tok->args[4],10,0,65535,&ok,NULL);
544 if (!ok) {
545 log_warn(LD_DIR,"Invalid dir port %s", escaped(tok->args[4]));
546 goto err;
547 }
548
549 tok = find_by_keyword(tokens, K_BANDWIDTH);
550 tor_assert(tok->n_args >= 3);
551 router->bandwidthrate = (int)
552 tor_parse_long(tok->args[0],10,1,INT_MAX,&ok,NULL);
553
554 if (!ok) {
555 log_warn(LD_DIR, "bandwidthrate %s unreadable or 0. Failing.",
556 escaped(tok->args[0]));
557 goto err;
558 }
559 router->bandwidthburst =
560 (int) tor_parse_long(tok->args[1],10,0,INT_MAX,&ok,NULL);
561 if (!ok) {
562 log_warn(LD_DIR, "Invalid bandwidthburst %s", escaped(tok->args[1]));
563 goto err;
564 }
565 router->bandwidthcapacity = (int)
566 tor_parse_long(tok->args[2],10,0,INT_MAX,&ok,NULL);
567 if (!ok) {
568 log_warn(LD_DIR, "Invalid bandwidthcapacity %s", escaped(tok->args[1]));
569 goto err;
570 }
571
572 if ((tok = find_opt_by_keyword(tokens, A_PURPOSE))) {
573 tor_assert(tok->n_args);
574 router->purpose = router_purpose_from_string(tok->args[0]);
575 if (router->purpose == ROUTER_PURPOSE_UNKNOWN) {
576 goto err;
577 }
578 } else {
580 }
581 router->cache_info.send_unencrypted =
582 (router->purpose == ROUTER_PURPOSE_GENERAL) ? 1 : 0;
583
584 if ((tok = find_opt_by_keyword(tokens, K_UPTIME))) {
585 tor_assert(tok->n_args >= 1);
586 router->uptime = tor_parse_long(tok->args[0],10,0,LONG_MAX,&ok,NULL);
587 if (!ok) {
588 log_warn(LD_DIR, "Invalid uptime %s", escaped(tok->args[0]));
589 goto err;
590 }
591 }
592
593 if ((tok = find_opt_by_keyword(tokens, K_HIBERNATING))) {
594 tor_assert(tok->n_args >= 1);
595 router->is_hibernating
596 = (tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL) != 0);
597 }
598
599 tok = find_by_keyword(tokens, K_PUBLISHED);
600 tor_assert(tok->n_args == 1);
601 if (parse_iso_time(tok->args[0], &router->cache_info.published_on) < 0)
602 goto err;
603
604 tok = find_opt_by_keyword(tokens, K_ONION_KEY);
605 if (tok) {
607 log_warn(LD_DIR,
608 "Relay's onion key had invalid exponent.");
609 goto err;
610 }
611 router->tap_onion_pkey = tor_memdup(tok->object_body, tok->object_size);
612 router->tap_onion_pkey_len = tok->object_size;
613 crypto_pk_free(tok->key);
614 }
615
616 if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
618 tor_assert(tok->n_args >= 1);
619 if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
620 log_warn(LD_DIR, "Bogus ntor-onion-key in routerinfo");
621 goto err;
622 }
623 router->onion_curve25519_pkey =
624 tor_memdup(&k, sizeof(curve25519_public_key_t));
625 }
626
627 tok = find_by_keyword(tokens, K_SIGNING_KEY);
628 router->identity_pkey = tok->key;
629 tok->key = NULL; /* Prevent free */
631 router->cache_info.identity_digest)) {
632 log_warn(LD_DIR, "Couldn't calculate key digest"); goto err;
633 }
634
635 {
636 directory_token_t *ed_sig_tok, *ed_cert_tok, *cc_tap_tok, *cc_ntor_tok,
637 *master_key_tok;
638 ed_sig_tok = find_by_keyword(tokens, K_ROUTER_SIG_ED25519);
639 ed_cert_tok = find_by_keyword(tokens, K_IDENTITY_ED25519);
640 master_key_tok = find_by_keyword(tokens, K_MASTER_KEY_ED25519);
641 cc_ntor_tok = find_by_keyword(tokens, K_NTOR_ONION_KEY_CROSSCERT);
642 /* This, and only this, is optional. */
643 cc_tap_tok = find_opt_by_keyword(tokens, K_ONION_KEY_CROSSCERT);
644
645 if (bool_neq(cc_tap_tok==NULL, router->tap_onion_pkey==NULL)) {
646 log_warn(LD_DIR, "Router descriptor had only one of (onion-key, "
647 "onion-key-crosscert).");
648 goto err;
649 }
650
651 IF_BUG_ONCE(! (ed_sig_tok && ed_cert_tok&& cc_ntor_tok &&master_key_tok)) {
652 goto err;
653 }
654
655 tor_cert_t *cert;
656 {
657 /* Parse the identity certificate */
658 cert = tor_cert_parse(
659 (const uint8_t*)ed_cert_tok->object_body,
660 ed_cert_tok->object_size);
661 if (! cert) {
662 log_warn(LD_DIR, "Couldn't parse ed25519 cert");
663 goto err;
664 }
665 /* makes sure it gets freed. */
666 router->cache_info.signing_key_cert = cert;
667
668 if (cert->cert_type != CERT_TYPE_ID_SIGNING ||
669 ! cert->signing_key_included) {
670 log_warn(LD_DIR, "Invalid form for ed25519 cert");
671 goto err;
672 }
673 }
674
675 if (cc_tap_tok) {
676 rsa_pubkey = router_get_rsa_onion_pkey(router->tap_onion_pkey,
677 router->tap_onion_pkey_len);
678 if (rsa_pubkey == NULL) {
679 log_warn(LD_DIR, "No pubkey for TAP cross-verification.");
680 goto err;
681 }
682 if (strcmp(cc_tap_tok->object_type, "CROSSCERT")) {
683 log_warn(LD_DIR, "Wrong object type on onion-key-crosscert "
684 "in descriptor");
685 goto err;
686 }
688 (const uint8_t*)cc_tap_tok->object_body,
689 (int)cc_tap_tok->object_size,
690 rsa_pubkey,
691 &cert->signing_key,
692 (const uint8_t*)router->cache_info.identity_digest)<0) {
693 log_warn(LD_DIR, "Incorrect TAP cross-verification");
694 goto err;
695 }
696 }
697
698 {
699 tor_assert(ed_sig_tok && ed_cert_tok && cc_ntor_tok);
700 const int ed_cert_token_pos = smartlist_pos(tokens, ed_cert_tok);
701 if (ed_cert_token_pos == -1 || router_token_pos == -1 ||
702 (ed_cert_token_pos != router_token_pos + 1 &&
703 ed_cert_token_pos != router_token_pos - 1)) {
704 log_warn(LD_DIR, "Ed25519 certificate in wrong position");
705 goto err;
706 }
707 if (ed_sig_tok != smartlist_get(tokens, smartlist_len(tokens)-2)) {
708 log_warn(LD_DIR, "Ed25519 signature in wrong position");
709 goto err;
710 }
711 if (strcmp(ed_cert_tok->object_type, "ED25519 CERT")) {
712 log_warn(LD_DIR, "Wrong object type on identity-ed25519 "
713 "in descriptor");
714 goto err;
715 }
716 if (strcmp(cc_ntor_tok->object_type, "ED25519 CERT")) {
717 log_warn(LD_DIR, "Wrong object type on ntor-onion-key-crosscert "
718 "in descriptor");
719 goto err;
720 }
721 if (strcmp(cc_ntor_tok->args[0], "0") &&
722 strcmp(cc_ntor_tok->args[0], "1")) {
723 log_warn(LD_DIR, "Bad sign bit on ntor-onion-key-crosscert");
724 goto err;
725 }
726 int ntor_cc_sign_bit = !strcmp(cc_ntor_tok->args[0], "1");
727 uint8_t d256[DIGEST256_LEN];
728 const char *signed_start, *signed_end;
729
730 if (master_key_tok) {
731 /* This token is optional, but if it's present, it must match
732 * the signature in the signing cert, or supplant it. */
733 tor_assert(master_key_tok->n_args >= 1);
735 if (ed25519_public_from_base64(&pkey, master_key_tok->args[0])<0) {
736 log_warn(LD_DIR, "Can't parse ed25519 master key");
737 goto err;
738 }
739
740 if (fast_memneq(&cert->signing_key.pubkey,
741 pkey.pubkey, ED25519_PUBKEY_LEN)) {
742 log_warn(LD_DIR, "Ed25519 master key does not match "
743 "key in certificate");
744 goto err;
745 }
746 }
747 ntor_cc_cert = tor_cert_parse((const uint8_t*)cc_ntor_tok->object_body,
748 cc_ntor_tok->object_size);
749 if (!ntor_cc_cert) {
750 log_warn(LD_DIR, "Couldn't parse ntor-onion-key-crosscert cert");
751 goto err;
752 }
753 if (ntor_cc_cert->cert_type != CERT_TYPE_ONION_ID ||
754 ! ed25519_pubkey_eq(&ntor_cc_cert->signed_key, &cert->signing_key)) {
755 log_warn(LD_DIR, "Invalid contents for ntor-onion-key-crosscert cert");
756 goto err;
757 }
758
759 ed25519_public_key_t ntor_cc_pk;
761 router->onion_curve25519_pkey,
762 ntor_cc_sign_bit)<0) {
763 log_warn(LD_DIR, "Error converting onion key to ed25519");
764 goto err;
765 }
766
767 if (router_get_hash_impl_helper(s, end-s, "router ",
768 "\nrouter-sig-ed25519",
769 ' ', LOG_WARN,
770 &signed_start, &signed_end) < 0) {
771 log_warn(LD_DIR, "Can't find ed25519-signed portion of descriptor");
772 goto err;
773 }
774 crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
775 crypto_digest_add_bytes(d, ED_DESC_SIGNATURE_PREFIX,
776 strlen(ED_DESC_SIGNATURE_PREFIX));
777 crypto_digest_add_bytes(d, signed_start, signed_end-signed_start);
778
779 crypto_digest_get_digest(d, (char*)d256, sizeof(d256));
781
782 ed25519_checkable_t check[3];
783 int check_ok[3];
784 time_t expires = TIME_MAX;
785 if (tor_cert_get_checkable_sig(&check[0], cert, NULL, &expires) < 0) {
786 log_err(LD_BUG, "Couldn't create 'checkable' for cert.");
787 goto err;
788 }
789 if (tor_cert_get_checkable_sig(&check[1],
790 ntor_cc_cert, &ntor_cc_pk, &expires) < 0) {
791 log_err(LD_BUG, "Couldn't create 'checkable' for ntor_cc_cert.");
792 goto err;
793 }
794
795 if (ed25519_signature_from_base64(&check[2].signature,
796 ed_sig_tok->args[0])<0) {
797 log_warn(LD_DIR, "Couldn't decode ed25519 signature");
798 goto err;
799 }
800 check[2].pubkey = &cert->signed_key;
801 check[2].msg = d256;
802 check[2].len = DIGEST256_LEN;
803
804 if (ed25519_checksig_batch(check_ok, check, 3) < 0) {
805 log_warn(LD_DIR, "Incorrect ed25519 signature(s)");
806 goto err;
807 }
808
809 /* We check this before adding it to the routerlist. */
810 router->cert_expiration_time = expires;
811 }
812 }
813
814 if ((tok = find_opt_by_keyword(tokens, K_FINGERPRINT))) {
815 /* If there's a fingerprint line, it must match the identity digest. */
816 char d[DIGEST_LEN];
817 tor_assert(tok->n_args == 1);
818 tor_strstrip(tok->args[0], " ");
820 tok->args[0], strlen(tok->args[0])) != DIGEST_LEN) {
821 log_warn(LD_DIR, "Couldn't decode router fingerprint %s",
822 escaped(tok->args[0]));
823 goto err;
824 }
825 if (tor_memneq(d,router->cache_info.identity_digest, DIGEST_LEN)) {
826 log_warn(LD_DIR, "Fingerprint '%s' does not match identity digest.",
827 tok->args[0]);
828 goto err;
829 }
830 }
831
832 {
833 const char *version = NULL, *protocols = NULL;
834 if ((tok = find_opt_by_keyword(tokens, K_PLATFORM))) {
835 router->platform = tor_strdup(tok->args[0]);
836 version = tok->args[0];
837 }
838
839 if ((tok = find_opt_by_keyword(tokens, K_PROTO))) {
840 router->protocol_list = tor_strdup(tok->args[0]);
841 protocols = tok->args[0];
842 }
843
844 summarize_protover_flags(&router->pv, protocols, version);
845 }
846
847 if ((tok = find_opt_by_keyword(tokens, K_CONTACT))) {
848 router->contact_info = tor_strdup(tok->args[0]);
849 }
850
851 if (find_opt_by_keyword(tokens, K_REJECT6) ||
852 find_opt_by_keyword(tokens, K_ACCEPT6)) {
853 log_warn(LD_DIR, "Rejecting router with reject6/accept6 line: they crash "
854 "older Tors.");
855 goto err;
856 }
857 {
858 smartlist_t *or_addresses = find_all_by_keyword(tokens, K_OR_ADDRESS);
859 if (or_addresses) {
860 find_single_ipv6_orport(or_addresses, &router->ipv6_addr,
861 &router->ipv6_orport);
862 smartlist_free(or_addresses);
863 }
864 }
865 exit_policy_tokens = find_all_exitpolicy(tokens);
866 if (!smartlist_len(exit_policy_tokens)) {
867 log_warn(LD_DIR, "No exit policy tokens in descriptor.");
868 goto err;
869 }
870 SMARTLIST_FOREACH(exit_policy_tokens, directory_token_t *, t,
871 if (router_add_exit_policy(router,t)<0) {
872 log_warn(LD_DIR,"Error in exit policy");
873 goto err;
874 });
876
877 if ((tok = find_opt_by_keyword(tokens, K_IPV6_POLICY)) && tok->n_args) {
878 router->ipv6_exit_policy = parse_short_policy(tok->args[0]);
879 if (! router->ipv6_exit_policy) {
880 log_warn(LD_DIR , "Error in ipv6-policy %s", escaped(tok->args[0]));
881 goto err;
882 }
883 }
884
885 if (policy_is_reject_star(router->exit_policy, AF_INET, 1) &&
886 (!router->ipv6_exit_policy ||
888 router->policy_is_reject_star = 1;
889
890 if ((tok = find_opt_by_keyword(tokens, K_FAMILY)) && tok->n_args) {
891 int i;
892 router->declared_family = smartlist_new();
893 for (i=0;i<tok->n_args;++i) {
894 if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
895 log_warn(LD_DIR, "Illegal nickname %s in family line",
896 escaped(tok->args[i]));
897 goto err;
898 }
899 smartlist_add_strdup(router->declared_family, tok->args[i]);
900 }
901 }
902
903 {
904 smartlist_t *family_cert_toks = find_all_by_keyword(tokens, K_FAMILY_CERT);
905 time_t family_expiration = TIME_MAX;
906 int r = 0;
907 if (family_cert_toks) {
908 r = check_family_certs(family_cert_toks,
909 &router->cache_info.signing_key_cert->signing_key,
910 &router->family_ids,
911 &family_expiration);
912 smartlist_free(family_cert_toks);
913 }
914 if (r<0)
915 goto err;
916 }
917
918 if (find_opt_by_keyword(tokens, K_CACHES_EXTRA_INFO))
919 router->caches_extra_info = 1;
920
921 if (find_opt_by_keyword(tokens, K_ALLOW_SINGLE_HOP_EXITS))
922 router->allow_single_hop_exits = 1;
923
924 if ((tok = find_opt_by_keyword(tokens, K_EXTRA_INFO_DIGEST))) {
925 tor_assert(tok->n_args >= 1);
926 if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
927 if (base16_decode(router->cache_info.extra_info_digest, DIGEST_LEN,
928 tok->args[0], HEX_DIGEST_LEN) != DIGEST_LEN) {
929 log_warn(LD_DIR,"Invalid extra info digest");
930 }
931 } else {
932 log_warn(LD_DIR, "Invalid extra info digest %s", escaped(tok->args[0]));
933 }
934
935 if (tok->n_args >= 2) {
936 if (digest256_from_base64(router->cache_info.extra_info_digest256,
937 tok->args[1]) < 0) {
938 log_warn(LD_DIR, "Invalid extra info digest256 %s",
939 escaped(tok->args[1]));
940 }
941 }
942 }
943
944 if (find_opt_by_keyword(tokens, K_HIDDEN_SERVICE_DIR)) {
945 router->wants_to_be_hs_dir = 1;
946 }
947
948 /* This router accepts tunnelled directory requests via begindir if it has
949 * an open dirport or it included "tunnelled-dir-server". */
950 if (find_opt_by_keyword(tokens, K_DIR_TUNNELLED) ||
951 router->ipv4_dirport > 0) {
953 }
954
955 tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
956
957 if (!router->ipv4_orport) {
958 log_warn(LD_DIR,"or_port unreadable or 0. Failing.");
959 goto err;
960 }
961
962 /* We've checked everything that's covered by the hash. */
963 can_dl_again = 1;
964 if (check_signature_token(digest, DIGEST_LEN, tok, router->identity_pkey, 0,
965 "router descriptor") < 0)
966 goto err;
967
968 if (!router->platform) {
969 router->platform = tor_strdup("<unknown>");
970 }
971 goto done;
972
973 err:
974 dump_desc(s_dup, "router descriptor");
975 routerinfo_free(router);
976 router = NULL;
977 done:
978 crypto_pk_free(rsa_pubkey);
979 tor_cert_free(ntor_cc_cert);
980 if (tokens) {
982 smartlist_free(tokens);
983 }
984 smartlist_free(exit_policy_tokens);
985 if (area) {
986 DUMP_AREA(area, "routerinfo");
987 memarea_drop_all(area);
988 }
989 if (can_dl_again_out)
990 *can_dl_again_out = can_dl_again;
991 return router;
992}
993
994/** Parse a single extrainfo entry from the string <b>s</b>, ending at
995 * <b>end</b>. (If <b>end</b> is NULL, parse up to the end of <b>s</b>.) If
996 * <b>cache_copy</b> is true, make a copy of the extra-info document in the
997 * cache_info fields of the result. If <b>routermap</b> is provided, use it
998 * as a map from router identity to routerinfo_t when looking up signing keys.
999 *
1000 * If <b>can_dl_again_out</b> is provided, set *<b>can_dl_again_out</b> to 1
1001 * if it's okay to try to download an extrainfo with this same digest again,
1002 * and 0 if it isn't. (It might not be okay to download it again if part of
1003 * the part covered by the digest is invalid.)
1004 */
1006extrainfo_parse_entry_from_string(const char *s, const char *end,
1007 int cache_copy, struct digest_ri_map_t *routermap,
1008 int *can_dl_again_out)
1009{
1010 extrainfo_t *extrainfo = NULL;
1011 char digest[128];
1012 smartlist_t *tokens = NULL;
1013 directory_token_t *tok;
1014 crypto_pk_t *key = NULL;
1015 routerinfo_t *router = NULL;
1016 memarea_t *area = NULL;
1017 const char *s_dup = s;
1018 /* Do not set this to '1' until we have parsed everything that we intend to
1019 * parse that's covered by the hash. */
1020 int can_dl_again = 0;
1021
1022 if (BUG(s == NULL))
1023 return NULL;
1024
1025 if (!end) {
1026 end = s + strlen(s);
1027 }
1028
1029 /* point 'end' to a point immediately after the final newline. */
1030 while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
1031 --end;
1032
1033 if (!tor_memstr(s, end-s, "\nidentity-ed25519")) {
1034 log_debug(LD_DIR, "Found an obsolete extrainfo. Rejecting quietly.");
1035 goto err;
1036 }
1037
1038 if (router_get_extrainfo_hash(s, end-s, digest) < 0) {
1039 log_warn(LD_DIR, "Couldn't compute router hash.");
1040 goto err;
1041 }
1042 tokens = smartlist_new();
1043 area = memarea_new();
1044 if (tokenize_string(area,s,end,tokens,extrainfo_token_table,0)) {
1045 log_warn(LD_DIR, "Error tokenizing extra-info document.");
1046 goto err;
1047 }
1048
1049 if (smartlist_len(tokens) < 2) {
1050 log_warn(LD_DIR, "Impossibly short extra-info document.");
1051 goto err;
1052 }
1053
1054 /* XXXX Accept this in position 1 too, and ed identity in position 0. */
1055 tok = smartlist_get(tokens,0);
1056 if (tok->tp != K_EXTRA_INFO) {
1057 log_warn(LD_DIR,"Entry does not start with \"extra-info\"");
1058 goto err;
1059 }
1060
1061 extrainfo = tor_malloc_zero(sizeof(extrainfo_t));
1062 extrainfo->cache_info.is_extrainfo = 1;
1063 if (cache_copy)
1064 extrainfo->cache_info.signed_descriptor_body = tor_memdup_nulterm(s,end-s);
1065 extrainfo->cache_info.signed_descriptor_len = end-s;
1066 memcpy(extrainfo->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
1067 crypto_digest256((char*)extrainfo->digest256, s, end-s, DIGEST_SHA256);
1068
1069 tor_assert(tok->n_args >= 2);
1070 if (!is_legal_nickname(tok->args[0])) {
1071 log_warn(LD_DIR,"Bad nickname %s on \"extra-info\"",escaped(tok->args[0]));
1072 goto err;
1073 }
1074 strlcpy(extrainfo->nickname, tok->args[0], sizeof(extrainfo->nickname));
1075 if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
1076 base16_decode(extrainfo->cache_info.identity_digest, DIGEST_LEN,
1077 tok->args[1], HEX_DIGEST_LEN) != DIGEST_LEN) {
1078 log_warn(LD_DIR,"Invalid fingerprint %s on \"extra-info\"",
1079 escaped(tok->args[1]));
1080 goto err;
1081 }
1082
1083 tok = find_by_keyword(tokens, K_PUBLISHED);
1084 if (parse_iso_time(tok->args[0], &extrainfo->cache_info.published_on)) {
1085 log_warn(LD_DIR,"Invalid published time %s on \"extra-info\"",
1086 escaped(tok->args[0]));
1087 goto err;
1088 }
1089
1090 {
1091 directory_token_t *ed_sig_tok, *ed_cert_tok;
1092 ed_sig_tok = find_opt_by_keyword(tokens, K_ROUTER_SIG_ED25519);
1093 ed_cert_tok = find_opt_by_keyword(tokens, K_IDENTITY_ED25519);
1094 int n_ed_toks = !!ed_sig_tok + !!ed_cert_tok;
1095 if (n_ed_toks != 0 && n_ed_toks != 2) {
1096 log_warn(LD_DIR, "Router descriptor with only partial ed25519/"
1097 "cross-certification support");
1098 goto err;
1099 }
1100 if (ed_sig_tok) {
1101 tor_assert(ed_cert_tok);
1102 const int ed_cert_token_pos = smartlist_pos(tokens, ed_cert_tok);
1103 if (ed_cert_token_pos != 1) {
1104 /* Accept this in position 0 XXXX */
1105 log_warn(LD_DIR, "Ed25519 certificate in wrong position");
1106 goto err;
1107 }
1108 if (ed_sig_tok != smartlist_get(tokens, smartlist_len(tokens)-2)) {
1109 log_warn(LD_DIR, "Ed25519 signature in wrong position");
1110 goto err;
1111 }
1112 if (strcmp(ed_cert_tok->object_type, "ED25519 CERT")) {
1113 log_warn(LD_DIR, "Wrong object type on identity-ed25519 "
1114 "in descriptor");
1115 goto err;
1116 }
1117
1118 uint8_t d256[DIGEST256_LEN];
1119 const char *signed_start, *signed_end;
1120 tor_cert_t *cert = tor_cert_parse(
1121 (const uint8_t*)ed_cert_tok->object_body,
1122 ed_cert_tok->object_size);
1123 if (! cert) {
1124 log_warn(LD_DIR, "Couldn't parse ed25519 cert");
1125 goto err;
1126 }
1127 /* makes sure it gets freed. */
1128 extrainfo->cache_info.signing_key_cert = cert;
1129
1130 if (cert->cert_type != CERT_TYPE_ID_SIGNING ||
1131 ! cert->signing_key_included) {
1132 log_warn(LD_DIR, "Invalid form for ed25519 cert");
1133 goto err;
1134 }
1135
1136 if (router_get_hash_impl_helper(s, end-s, "extra-info ",
1137 "\nrouter-sig-ed25519",
1138 ' ', LOG_WARN,
1139 &signed_start, &signed_end) < 0) {
1140 log_warn(LD_DIR, "Can't find ed25519-signed portion of extrainfo");
1141 goto err;
1142 }
1143 crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
1144 crypto_digest_add_bytes(d, ED_DESC_SIGNATURE_PREFIX,
1145 strlen(ED_DESC_SIGNATURE_PREFIX));
1146 crypto_digest_add_bytes(d, signed_start, signed_end-signed_start);
1147 crypto_digest_get_digest(d, (char*)d256, sizeof(d256));
1149
1150 ed25519_checkable_t check[2];
1151 int check_ok[2];
1152 if (tor_cert_get_checkable_sig(&check[0], cert, NULL, NULL) < 0) {
1153 log_err(LD_BUG, "Couldn't create 'checkable' for cert.");
1154 goto err;
1155 }
1156
1157 if (ed25519_signature_from_base64(&check[1].signature,
1158 ed_sig_tok->args[0])<0) {
1159 log_warn(LD_DIR, "Couldn't decode ed25519 signature");
1160 goto err;
1161 }
1162 check[1].pubkey = &cert->signed_key;
1163 check[1].msg = d256;
1164 check[1].len = DIGEST256_LEN;
1165
1166 if (ed25519_checksig_batch(check_ok, check, 2) < 0) {
1167 log_warn(LD_DIR, "Incorrect ed25519 signature(s)");
1168 goto err;
1169 }
1170 /* We don't check the certificate expiration time: checking that it
1171 * matches the cert in the router descriptor is adequate. */
1172 }
1173 }
1174
1175 /* We've checked everything that's covered by the hash. */
1176 can_dl_again = 1;
1177
1178 if (routermap &&
1179 (router = digestmap_get((digestmap_t*)routermap,
1180 extrainfo->cache_info.identity_digest))) {
1181 key = router->identity_pkey;
1182 }
1183
1184 tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
1185 if (strcmp(tok->object_type, "SIGNATURE") ||
1186 tok->object_size < 128 || tok->object_size > 512) {
1187 log_warn(LD_DIR, "Bad object type or length on extra-info signature");
1188 goto err;
1189 }
1190
1191 if (key) {
1192 if (check_signature_token(digest, DIGEST_LEN, tok, key, 0,
1193 "extra-info") < 0)
1194 goto err;
1195
1196 if (router)
1197 extrainfo->cache_info.send_unencrypted =
1198 router->cache_info.send_unencrypted;
1199 } else {
1200 extrainfo->pending_sig = tor_memdup(tok->object_body,
1201 tok->object_size);
1202 extrainfo->pending_sig_len = tok->object_size;
1203 }
1204
1205 goto done;
1206 err:
1207 dump_desc(s_dup, "extra-info descriptor");
1208 extrainfo_free(extrainfo);
1209 extrainfo = NULL;
1210 done:
1211 if (tokens) {
1213 smartlist_free(tokens);
1214 }
1215 if (area) {
1216 DUMP_AREA(area, "extrainfo");
1217 memarea_drop_all(area);
1218 }
1219 if (can_dl_again_out)
1220 *can_dl_again_out = can_dl_again;
1221 return extrainfo;
1222}
1223
1224/** Add an exit policy stored in the token <b>tok</b> to the router info in
1225 * <b>router</b>. Return 0 on success, -1 on failure. */
1226static int
1228{
1229 addr_policy_t *newe;
1230 /* Use the standard interpretation of accept/reject *, an IPv4 wildcard. */
1231 newe = router_parse_addr_policy(tok, 0);
1232 if (!newe)
1233 return -1;
1234 if (! router->exit_policy)
1235 router->exit_policy = smartlist_new();
1236
1237 /* Ensure that in descriptors, accept/reject fields are followed by
1238 * IPv4 addresses, and accept6/reject6 fields are followed by
1239 * IPv6 addresses. Unlike torrcs, descriptor exit policies do not permit
1240 * accept/reject followed by IPv6. */
1241 if (((tok->tp == K_ACCEPT6 || tok->tp == K_REJECT6) &&
1242 tor_addr_family(&newe->addr) == AF_INET)
1243 ||
1244 ((tok->tp == K_ACCEPT || tok->tp == K_REJECT) &&
1245 tor_addr_family(&newe->addr) == AF_INET6)) {
1246 /* There's nothing the user can do about other relays' descriptors,
1247 * so we don't provide usage advice here. */
1248 log_warn(LD_DIR, "Mismatch between field type and address type in exit "
1249 "policy '%s'. Discarding entire router descriptor.",
1250 tok->n_args == 1 ? tok->args[0] : "");
1251 addr_policy_free(newe);
1252 return -1;
1253 }
1254
1255 smartlist_add(router->exit_policy, newe);
1256
1257 return 0;
1258}
1259
1260/** Return a newly allocated smartlist of all accept or reject tokens in
1261 * <b>s</b>.
1262 */
1263static smartlist_t *
1265{
1266 smartlist_t *out = smartlist_new();
1268 if (t->tp == K_ACCEPT || t->tp == K_ACCEPT6 ||
1269 t->tp == K_REJECT || t->tp == K_REJECT6)
1270 smartlist_add(out,t));
1271 return out;
1272}
1273
1274/**
1275 * Parse and validate a single `FAMILY_CERT` token's object.
1276 *
1277 * Arguments are as for `check_family_certs()`.
1278 */
1279STATIC int
1280check_one_family_cert(const uint8_t *cert_body,
1281 size_t cert_body_size,
1282 const ed25519_public_key_t *identity_key,
1283 char **family_id_out,
1284 time_t *family_expiration_out)
1285{
1286 tor_cert_t *cert = NULL;
1287 int r = -1;
1288
1289 cert = tor_cert_parse(cert_body, cert_body_size);
1290
1291 if (! cert)
1292 goto done;
1293 if (cert->cert_type != CERT_TYPE_FAMILY_V_IDENTITY) {
1294 log_warn(LD_DIR, "Wrong cert type in family certificate.");
1295 goto done;
1296 }
1297 if (! cert->signing_key_included) {
1298 log_warn(LD_DIR, "Missing family key in family certificate.");
1299 goto done;
1300 }
1301 if (! ed25519_pubkey_eq(&cert->signed_key, identity_key)) {
1302 log_warn(LD_DIR, "Key mismatch in family certificate.");
1303 goto done;
1304 }
1305
1306 time_t valid_until = cert->valid_until;
1307
1308 /* We're using NULL for the key, since the cert has the signing key included.
1309 * We're using 0 for "now", since we're going to extract the expiration
1310 * separately.
1311 */
1312 if (tor_cert_checksig(cert, NULL, 0) < 0) {
1313 log_warn(LD_DIR, "Invalid signature in family certificate");
1314 goto done;
1315 }
1316
1317 /* At this point we know that the cert is valid.
1318 * We extract the expiration time and the signing key. */
1319 *family_expiration_out = valid_until;
1320
1321 char buf[ED25519_BASE64_LEN+1];
1323 tor_asprintf(family_id_out, "ed25519:%s", buf);
1324
1325 r = 0;
1326 done:
1327 tor_cert_free(cert);
1328 return r;
1329}
1330
1331/**
1332 * Given a list of `FAMILY_CERT` tokens, and a relay's ed25519 `identity_key`,
1333 * validate the family certificates in all the tokens, and convert them into
1334 * family IDs in a newly allocated `family_ids_out` list.
1335 * Set `family_expiration_out` to the earliest time at which any certificate
1336 * in the list expires.
1337 * Return 0 on success, and -1 on failure.
1338 */
1339static int
1340check_family_certs(const smartlist_t *family_cert_tokens,
1341 const ed25519_public_key_t *identity_key,
1342 smartlist_t **family_ids_out,
1343 time_t *family_expiration_out)
1344{
1345 if (BUG(!identity_key) ||
1346 BUG(!family_ids_out) ||
1347 BUG(!family_expiration_out))
1348 return -1;
1349
1350 *family_expiration_out = TIME_MAX;
1351
1352 if (family_cert_tokens == NULL || smartlist_len(family_cert_tokens) == 0) {
1353 *family_ids_out = NULL;
1354 return 0;
1355 }
1356
1357 *family_ids_out = smartlist_new();
1358 SMARTLIST_FOREACH_BEGIN(family_cert_tokens, directory_token_t *, tok) {
1359 if (BUG(tok->object_body == NULL))
1360 goto err;
1361
1362 char *this_id = NULL;
1363 time_t this_expiration = TIME_MAX;
1364 if (check_one_family_cert((const uint8_t*)tok->object_body,
1365 tok->object_size,
1366 identity_key,
1367 &this_id, &this_expiration) < 0)
1368 goto err;
1369 smartlist_add(*family_ids_out, this_id);
1370 *family_expiration_out = MIN(*family_expiration_out, this_expiration);
1371 } SMARTLIST_FOREACH_END(tok);
1372
1373 smartlist_sort_strings(*family_ids_out);
1374 smartlist_uniq_strings(*family_ids_out);
1375
1376 return 0;
1377 err:
1378 SMARTLIST_FOREACH(*family_ids_out, char *, cp, tor_free(cp));
1379 smartlist_free(*family_ids_out);
1380 return -1;
1381}
1382
1383/** Called on startup; right now we just handle scanning the unparseable
1384 * descriptor dumps, but hang anything else we might need to do in the
1385 * future here as well.
1386 */
1387void
1389{
1390 /*
1391 * Check both if the sandbox is active and whether it's configured; no
1392 * point in loading all that if we won't be able to use it after the
1393 * sandbox becomes active.
1394 */
1395 if (!(sandbox_is_active() || get_options()->Sandbox)) {
1397 }
1398}
1399
1400/** Clean up all data structures used by routerparse.c at exit */
1401void
1403{
1405}
Address policy structures.
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
int tor_addr_parse_mask_ports(const char *s, unsigned flags, tor_addr_t *addr_out, maskbits_t *maskbits_out, uint16_t *port_min_out, uint16_t *port_max_out)
Definition: address.c:543
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
#define tor_addr_from_in(dest, in)
Definition: address.h:331
uint8_t maskbits_t
Definition: address.h:62
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
const or_options_t * get_options(void)
Definition: config.c:947
Header file for config.c.
Header for crypto_curve25519.c.
int curve25519_public_from_base64(curve25519_public_key_t *pkey, const char *input)
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
#define crypto_digest_free(d)
crypto_digest_t * crypto_digest256_new(digest_algorithm_t algorithm)
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
int ed25519_checksig_batch(int *okay_out, const ed25519_checkable_t *checkable, int n_checkable)
int ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey, const curve25519_public_key_t *pubkey_in, int signbit)
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
Header for crypto_ed25519.c.
int ed25519_signature_from_base64(ed25519_signature_t *sig, const char *input)
void ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
int ed25519_public_from_base64(ed25519_public_key_t *pkey, const char *input)
int digest256_from_base64(char *digest, const char *d64)
Header for crypto_format.c.
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
int crypto_pk_public_exponent_ok(const crypto_pk_t *env)
const char * router_describe(const routerinfo_t *ri)
Definition: describe.c:137
Header file for describe.c.
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
#define fast_memneq(a, b, c)
Definition: di_ops.h:42
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define DIGEST256_LEN
Definition: digest_sizes.h:23
const char * escaped(const char *s)
Definition: escape.c:126
A relay's extra-info structure.
int tor_inet_aton(const char *str, struct in_addr *addr)
Definition: inaddr.c:40
#define LD_BUG
Definition: log.h:86
#define LD_DIR
Definition: log.h:88
#define LOG_WARN
Definition: log.h:53
#define bool_neq(a, b)
Definition: logic.h:18
#define tor_free(p)
Definition: malloc.h:56
memarea_t * memarea_new(void)
Definition: memarea.c:153
Header for memarea.c.
#define memarea_drop_all(area)
Definition: memarea.h:22
int is_legal_nickname(const char *s)
Definition: nickname.c:19
int is_legal_nickname_or_hexdigest(const char *s)
Definition: nickname.c:31
Header file for nickname.c.
Master header file for Tor-specific functionality.
saved_location_t
Definition: or.h:711
@ SAVED_NOWHERE
Definition: or.h:714
@ SAVED_IN_CACHE
Definition: or.h:718
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: parse_int.c:59
void token_clear(directory_token_t *tok)
Definition: parsecommon.c:41
smartlist_t * find_all_by_keyword(const smartlist_t *s, directory_keyword k)
Definition: parsecommon.c:462
int tokenize_string(memarea_t *area, const char *start, const char *end, smartlist_t *out, const token_rule_t *table, int flags)
Definition: parsecommon.c:53
directory_token_t * find_opt_by_keyword(const smartlist_t *s, directory_keyword keyword)
Definition: parsecommon.c:451
Header file for parsecommon.c.
#define T0N(s, t, a, o)
Definition: parsecommon.h:252
#define T01(s, t, a, o)
Definition: parsecommon.h:262
#define NO_ARGS
Definition: parsecommon.h:269
#define A01(s, t, a, o)
Definition: parsecommon.h:264
#define T1_END(s, t, a, o)
Definition: parsecommon.h:258
@ OBJ_OK
Definition: parsecommon.h:228
@ NO_OBJ
Definition: parsecommon.h:223
@ NEED_OBJ
Definition: parsecommon.h:224
@ NEED_KEY_1024
Definition: parsecommon.h:226
#define T1_START(s, t, a, o)
Definition: parsecommon.h:256
#define GE(n)
Definition: parsecommon.h:273
#define CONCAT_ARGS
Definition: parsecommon.h:271
#define T1(s, t, a, o)
Definition: parsecommon.h:254
#define EQ(n)
Definition: parsecommon.h:275
#define END_OF_TABLE
Definition: parsecommon.h:248
#define ARGS
Definition: parsecommon.h:267
void policy_expand_private(smartlist_t **policy)
Definition: policies.c:110
int policy_is_reject_star(const smartlist_t *policy, sa_family_t family, int default_reject)
Definition: policies.c:2264
short_policy_t * parse_short_policy(const char *summary)
Definition: policies.c:2705
int short_policy_is_reject_star(const short_policy_t *policy)
Definition: policies.c:2890
Header file for policies.c.
addr_policy_t * router_parse_addr_policy(directory_token_t *tok, unsigned fmt_flags)
Definition: policy_parse.c:136
Header file for policy_parse.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
Header file for router.c.
const char * router_purpose_to_string(uint8_t p)
Definition: routerinfo.c:98
uint8_t router_purpose_from_string(const char *s)
Definition: routerinfo.c:113
Header file for routerinfo.c.
Router descriptor structure.
#define ROUTER_PURPOSE_UNKNOWN
#define ROUTER_PURPOSE_GENERAL
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:897
Header file for routerlist.c.
Router descriptor list structure.
int router_parse_list_from_string(const char **s, const char *eos, smartlist_t *dest, saved_location_t saved_location, int want_extrainfo, int allow_annotations, const char *prepend_annotations, smartlist_t *invalid_digests_out)
Definition: routerparse.c:255
routerinfo_t * router_parse_entry_from_string(const char *s, const char *end, int cache_copy, int allow_annotations, const char *prepend_annotations, int *can_dl_again_out)
Definition: routerparse.c:400
static int check_family_certs(const smartlist_t *family_cert_tokens, const ed25519_public_key_t *identity_key, smartlist_t **family_ids_out, time_t *family_expiration_out)
Definition: routerparse.c:1340
int router_get_extrainfo_hash(const char *s, size_t s_len, char *digest)
Definition: routerparse.c:196
static smartlist_t * find_all_exitpolicy(smartlist_t *s)
Definition: routerparse.c:1264
STATIC int check_one_family_cert(const uint8_t *cert_body, size_t cert_body_size, const ed25519_public_key_t *identity_key, char **family_id_out, time_t *family_expiration_out)
Definition: routerparse.c:1280
static int find_start_of_next_router_or_extrainfo(const char **s_ptr, const char *eos, int *is_extrainfo_out)
Definition: routerparse.c:208
extrainfo_t * extrainfo_parse_entry_from_string(const char *s, const char *end, int cache_copy, struct digest_ri_map_t *routermap, int *can_dl_again_out)
Definition: routerparse.c:1006
int router_get_router_hash(const char *s, size_t s_len, char *digest)
Definition: routerparse.c:186
void routerparse_free_all(void)
Definition: routerparse.c:1402
void routerparse_init(void)
Definition: routerparse.c:1388
int find_single_ipv6_orport(const smartlist_t *list, tor_addr_t *addr_out, uint16_t *port_out)
Definition: routerparse.c:346
const token_rule_t routerdesc_token_table[]
Definition: routerparse.c:86
static int router_add_exit_policy(routerinfo_t *router, directory_token_t *tok)
Definition: routerparse.c:1227
static token_rule_t extrainfo_token_table[]
Definition: routerparse.c:133
Header file for routerparse.c.
int sandbox_is_active(void)
Definition: sandbox.c:2348
Header file for sandbox.c.
int check_signature_token(const char *digest, ssize_t digest_len, directory_token_t *tok, crypto_pk_t *pkey, int flags, const char *doctype)
Definition: sigcommon.c:148
int router_get_hash_impl_helper(const char *s, size_t s_len, const char *start_str, const char *end_str, char end_c, int log_severity, const char **start_out, const char **end_out)
Definition: sigcommon.c:27
int router_get_hash_impl(const char *s, size_t s_len, char *digest, const char *start_str, const char *end_str, char end_c, digest_algorithm_t alg)
Definition: sigcommon.c:74
Header file for sigcommon.c.
int smartlist_pos(const smartlist_t *sl, const void *element)
Definition: smartlist.c:119
void smartlist_uniq_strings(smartlist_t *sl)
Definition: smartlist.c:574
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
tor_addr_t addr
directory_keyword tp
Definition: parsecommon.h:206
struct crypto_pk_t * key
Definition: parsecommon.h:214
char * pending_sig
Definition: extrainfo_st.h:29
uint8_t digest256[DIGEST256_LEN]
Definition: extrainfo_st.h:21
char nickname[MAX_NICKNAME_LEN+1]
Definition: extrainfo_st.h:23
size_t pending_sig_len
Definition: extrainfo_st.h:31
unsigned int allow_single_hop_exits
Definition: routerinfo_st.h:80
char * platform
Definition: routerinfo_st.h:52
unsigned int caches_extra_info
Definition: routerinfo_st.h:78
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:31
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:26
protover_summary_flags_t pv
smartlist_t * exit_policy
Definition: routerinfo_st.h:63
smartlist_t * declared_family
Definition: routerinfo_st.h:69
uint32_t bandwidthrate
Definition: routerinfo_st.h:58
crypto_pk_t * identity_pkey
Definition: routerinfo_st.h:45
size_t tap_onion_pkey_len
Definition: routerinfo_st.h:43
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: routerinfo_st.h:47
unsigned int wants_to_be_hs_dir
Definition: routerinfo_st.h:83
struct smartlist_t * family_ids
Definition: routerinfo_st.h:74
unsigned int is_hibernating
Definition: routerinfo_st.h:76
unsigned int policy_is_reject_star
Definition: routerinfo_st.h:85
char * protocol_list
Definition: routerinfo_st.h:54
uint8_t purpose
unsigned int supports_tunnelled_dir_requests
Definition: routerinfo_st.h:94
char * contact_info
Definition: routerinfo_st.h:75
uint32_t bandwidthcapacity
Definition: routerinfo_st.h:62
time_t cert_expiration_time
Definition: routerinfo_st.h:50
uint32_t bandwidthburst
Definition: routerinfo_st.h:60
char * tap_onion_pkey
Definition: routerinfo_st.h:41
char * nickname
Definition: routerinfo_st.h:23
struct short_policy_t * ipv6_exit_policy
Definition: routerinfo_st.h:67
struct digest_ri_map_t * identity_map
Definition: routerlist_st.h:20
char signed_descriptor_digest[DIGEST_LEN]
char extra_info_digest[DIGEST_LEN]
char identity_digest[DIGEST_LEN]
struct tor_cert_st * signing_key_cert
char extra_info_digest256[DIGEST256_LEN]
saved_location_t saved_location
ed25519_public_key_t signing_key
Definition: torcert.h:36
uint8_t cert_type
Definition: torcert.h:46
time_t valid_until
Definition: torcert.h:38
ed25519_public_key_t signed_key
Definition: torcert.h:33
unsigned signing_key_included
Definition: torcert.h:48
#define STATIC
Definition: testsupport.h:32
int parse_iso_time(const char *cp, time_t *t)
Definition: time_fmt.c:423
int tor_cert_checksig(tor_cert_t *cert, const ed25519_public_key_t *pubkey, time_t now)
Definition: torcert.c:244
int tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out, const tor_cert_t *cert, const ed25519_public_key_t *pubkey, time_t *expiration_out)
Definition: torcert.c:211
int check_tap_onion_key_crosscert(const uint8_t *crosscert, int crosscert_len, const crypto_pk_t *onion_pkey, const ed25519_public_key_t *master_id_pkey, const uint8_t *rsa_id_digest)
Definition: torcert.c:649
tor_cert_t * tor_cert_parse(const uint8_t *encoded, const size_t len)
Definition: torcert.c:159
Header for torcert.c.
void dump_desc_fifo_cleanup(void)
Definition: unparseable.c:239
void dump_desc(const char *desc, const char *type)
Definition: unparseable.c:496
void dump_desc_init(void)
Definition: unparseable.c:38
Header file for unparseable.c.
#define tor_assert(expr)
Definition: util_bug.h:103
#define IF_BUG_ONCE(cond)
Definition: util_bug.h:254
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:217
const char * eat_whitespace_eos(const char *s, const char *eos)
Definition: util_string.c:306
void tor_strstrip(char *s, const char *strip)
Definition: util_string.c:113
void summarize_protover_flags(protover_summary_flags_t *out, const char *protocols, const char *version)
Definition: versions.c:514
Header file for versions.c.
#define ED25519_BASE64_LEN
Definition: x25519_sizes.h:43
#define ED25519_PUBKEY_LEN
Definition: x25519_sizes.h:27