Tor  0.4.6.0-alpha-dev
microdesc_parse.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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file microdesc_parse.c
9  * \brief Code to parse and validate microdescriptors.
10  **/
11 
12 #include "core/or/or.h"
13 
14 #include "app/config/config.h"
15 #include "core/or/policies.h"
22 #include "feature/relay/router.h"
26 #include "lib/memarea/memarea.h"
27 
29 
30 /** List of tokens recognized in microdescriptors */
31 // clang-format off
33  T1_START("onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024),
34  T1("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
35  T0N("id", K_ID, GE(2), NO_OBJ ),
36  T0N("a", K_A, GE(1), NO_OBJ ),
37  T01("family", K_FAMILY, CONCAT_ARGS, NO_OBJ ),
38  T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
39  T01("p6", K_P6, CONCAT_ARGS, NO_OBJ ),
40  A01("@last-listed", A_LAST_LISTED, CONCAT_ARGS, NO_OBJ ),
42 };
43 // clang-format on
44 
45 /** Assuming that s starts with a microdesc, return the start of the
46  * *NEXT* one. Return NULL on "not found." */
47 static const char *
48 find_start_of_next_microdesc(const char *s, const char *eos)
49 {
50  int started_with_annotations;
51  s = eat_whitespace_eos(s, eos);
52  if (!s)
53  return NULL;
54 
55 #define CHECK_LENGTH() STMT_BEGIN \
56  if (eos - s < 32) \
57  return NULL; \
58  STMT_END
59 
60 #define NEXT_LINE() STMT_BEGIN \
61  s = memchr(s, '\n', eos-s); \
62  if (!s || eos - s <= 1) \
63  return NULL; \
64  s++; \
65  STMT_END
66 
67  CHECK_LENGTH();
68 
69  started_with_annotations = (*s == '@');
70 
71  if (started_with_annotations) {
72  /* Start by advancing to the first non-annotation line. */
73  while (*s == '@')
74  NEXT_LINE();
75  }
76  CHECK_LENGTH();
77 
78  /* Now we should be pointed at an onion-key line. If we are, then skip
79  * it. */
80  if (!strcmpstart(s, "onion-key"))
81  NEXT_LINE();
82 
83  /* Okay, now we're pointed at the first line of the microdescriptor which is
84  not an annotation or onion-key. The next line that _is_ an annotation or
85  onion-key is the start of the next microdescriptor. */
86  while (eos - s > 32) {
87  if (*s == '@' || !strcmpstart(s, "onion-key"))
88  return s;
89  NEXT_LINE();
90  }
91  return NULL;
92 
93 #undef CHECK_LENGTH
94 #undef NEXT_LINE
95 }
96 
97 static inline int
98 policy_is_reject_star_or_null(struct short_policy_t *policy)
99 {
100  return !policy || short_policy_is_reject_star(policy);
101 }
102 
103 /**
104  * Return a human-readable description of a given saved_location_t.
105  * Never returns NULL.
106  **/
107 static const char *
109 {
110  const char *location;
111  switch (where) {
112  case SAVED_NOWHERE:
113  location = "download or generated string";
114  break;
115  case SAVED_IN_CACHE:
116  location = "cache";
117  break;
118  case SAVED_IN_JOURNAL:
119  location = "journal";
120  break;
121  default:
122  location = "unknown location";
123  break;
124  }
125  return location;
126 }
127 
128 /**
129  * Given a microdescriptor stored in <b>where</b> which starts at <b>s</b>,
130  * which ends at <b>start_of_next_microdescriptor</b>, and which is located
131  * within a larger document beginning at <b>start</b>: Fill in the body,
132  * bodylen, bodylen, saved_location, off, and digest fields of <b>md</b> as
133  * appropriate.
134  *
135  * The body field will be an alias within <b>s</b> if <b>saved_location</b>
136  * is SAVED_IN_CACHE, and will be copied into body and nul-terminated
137  * otherwise.
138  **/
139 static int
141  const char *start,
142  const char *s, const char *start_of_next_microdesc,
143  saved_location_t where)
144 {
145  const bool copy_body = (where != SAVED_IN_CACHE);
146 
147  const char *cp = tor_memstr(s, start_of_next_microdesc-s, "onion-key");
148 
149  const bool no_onion_key = (cp == NULL);
150  if (no_onion_key) {
151  cp = s; /* So that we have *some* junk to put in the body */
152  }
153 
154  md->bodylen = start_of_next_microdesc - cp;
155  md->saved_location = where;
156  if (copy_body)
157  md->body = tor_memdup_nulterm(cp, md->bodylen);
158  else
159  md->body = (char*)cp;
160  md->off = cp - start;
161 
162  crypto_digest256(md->digest, md->body, md->bodylen, DIGEST_SHA256);
163 
164  return no_onion_key ? -1 : 0;
165 }
166 
167 /**
168  * Parse a microdescriptor which begins at <b>s</b> and ends at
169  * <b>start_of_next_microdesc</b>. Store its fields into <b>md</b>. Use
170  * <b>where</b> for generating log information. If <b>allow_annotations</b>
171  * is true, then one or more annotations may precede the microdescriptor body
172  * proper. Use <b>area</b> for memory management, clearing it when done.
173  *
174  * On success, return 0; otherwise return -1.
175  **/
176 static int
178  memarea_t *area,
179  const char *s, const char *start_of_next_microdesc,
180  int allow_annotations,
181  saved_location_t where)
182 {
183  smartlist_t *tokens = smartlist_new();
184  int rv = -1;
185  int flags = allow_annotations ? TS_ANNOTATIONS_OK : 0;
186  directory_token_t *tok;
187 
188  if (tokenize_string(area, s, start_of_next_microdesc, tokens,
189  microdesc_token_table, flags)) {
190  log_warn(LD_DIR, "Unparseable microdescriptor found in %s",
191  saved_location_to_string(where));
192  goto err;
193  }
194 
195  if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
196  if (parse_iso_time(tok->args[0], &md->last_listed)) {
197  log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
198  goto err;
199  }
200  }
201 
202  tok = find_by_keyword(tokens, K_ONION_KEY);
203  if (!crypto_pk_public_exponent_ok(tok->key)) {
204  log_warn(LD_DIR,
205  "Relay's onion key had invalid exponent.");
206  goto err;
207  }
208  md->onion_pkey = tor_memdup(tok->object_body, tok->object_size);
209  md->onion_pkey_len = tok->object_size;
210  crypto_pk_free(tok->key);
211 
212  if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
214  tor_assert(tok->n_args >= 1);
215  if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
216  log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
217  goto err;
218  }
220  tor_memdup(&k, sizeof(curve25519_public_key_t));
221  }
222 
223  smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
224  if (id_lines) {
226  tor_assert(t->n_args >= 2);
227  if (!strcmp(t->args[0], "ed25519")) {
228  if (md->ed25519_identity_pkey) {
229  log_warn(LD_DIR, "Extra ed25519 key in microdesc");
230  smartlist_free(id_lines);
231  goto err;
232  }
234  if (ed25519_public_from_base64(&k, t->args[1])<0) {
235  log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
236  smartlist_free(id_lines);
237  goto err;
238  }
239  md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
240  }
241  } SMARTLIST_FOREACH_END(t);
242  smartlist_free(id_lines);
243  }
244 
245  {
246  smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
247  if (a_lines) {
248  find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
249  smartlist_free(a_lines);
250  }
251  }
252 
253  if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
254  md->family = nodefamily_parse(tok->args[0],
255  NULL,
256  NF_WARN_MALFORMED);
257  }
258 
259  if ((tok = find_opt_by_keyword(tokens, K_P))) {
260  md->exit_policy = parse_short_policy(tok->args[0]);
261  }
262  if ((tok = find_opt_by_keyword(tokens, K_P6))) {
264  }
265 
266  if (policy_is_reject_star_or_null(md->exit_policy) &&
267  policy_is_reject_star_or_null(md->ipv6_exit_policy)) {
268  md->policy_is_reject_star = 1;
269  }
270 
271  rv = 0;
272  err:
273 
275  memarea_clear(area);
276  smartlist_free(tokens);
277 
278  return rv;
279 }
280 
281 /** Parse as many microdescriptors as are found from the string starting at
282  * <b>s</b> and ending at <b>eos</b>. If allow_annotations is set, read any
283  * annotations we recognize and ignore ones we don't.
284  *
285  * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
286  * descriptor in the body field of each microdesc_t.
287  *
288  * Return all newly parsed microdescriptors in a newly allocated
289  * smartlist_t. If <b>invalid_disgests_out</b> is provided, add a SHA256
290  * microdesc digest to it for every microdesc that we found to be badly
291  * formed. (This may cause duplicates) */
292 smartlist_t *
293 microdescs_parse_from_string(const char *s, const char *eos,
294  int allow_annotations,
295  saved_location_t where,
296  smartlist_t *invalid_digests_out)
297 {
298  smartlist_t *result;
299  microdesc_t *md = NULL;
300  memarea_t *area;
301  const char *start = s;
302  const char *start_of_next_microdesc;
303 
304  if (!eos)
305  eos = s + strlen(s);
306 
307  s = eat_whitespace_eos(s, eos);
308  area = memarea_new();
309  result = smartlist_new();
310 
311  while (s < eos) {
312  bool okay = false;
313 
314  start_of_next_microdesc = find_start_of_next_microdesc(s, eos);
315  if (!start_of_next_microdesc)
316  start_of_next_microdesc = eos;
317 
318  md = tor_malloc_zero(sizeof(microdesc_t));
319  uint8_t md_digest[DIGEST256_LEN];
320  {
321  const bool body_not_found =
322  microdesc_extract_body(md, start, s,
323  start_of_next_microdesc,
324  where) < 0;
325 
326  memcpy(md_digest, md->digest, DIGEST256_LEN);
327  if (body_not_found) {
328  log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Malformed or truncated descriptor");
329  goto next;
330  }
331  }
332 
333  if (microdesc_parse_fields(md, area, s, start_of_next_microdesc,
334  allow_annotations, where) == 0) {
335  smartlist_add(result, md);
336  md = NULL; // prevent free
337  okay = true;
338  }
339 
340  next:
341  if (! okay && invalid_digests_out) {
342  smartlist_add(invalid_digests_out,
343  tor_memdup(md_digest, DIGEST256_LEN));
344  }
345  microdesc_free(md);
346  md = NULL;
347  s = start_of_next_microdesc;
348  }
349 
350  memarea_drop_all(area);
351 
352  return result;
353 }
log_fn
#define log_fn(severity, domain, args,...)
Definition: log.h:283
microdesc_t
Definition: microdesc_st.h:27
T1_START
#define T1_START(s, t, a, o)
Definition: parsecommon.h:252
microdesc_parse.h
Header file for microdesc_parse.c.
short_policy_t
Definition: policies.h:57
NEED_KEY_1024
@ NEED_KEY_1024
Definition: parsecommon.h:222
microdesc_t::digest
char digest[DIGEST256_LEN]
Definition: microdesc_st.h:62
END_OF_TABLE
#define END_OF_TABLE
Definition: parsecommon.h:244
tokenize_string
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
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
microdesc.h
Header file for microdesc.c.
router.h
Header file for router.c.
memarea.h
Header for memarea.c.
T0N
#define T0N(s, t, a, o)
Definition: parsecommon.h:248
directory_token_t
Definition: parsecommon.h:201
NO_OBJ
@ NO_OBJ
Definition: parsecommon.h:219
memarea_drop_all
#define memarea_drop_all(area)
Definition: memarea.h:22
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
token_clear
void token_clear(directory_token_t *tok)
Definition: parsecommon.c:41
directory_token_t::args
char ** args
Definition: parsecommon.h:204
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
microdesc_t::off
off_t off
Definition: microdesc_st.h:50
parse_iso_time
int parse_iso_time(const char *cp, time_t *t)
Definition: time_fmt.c:392
microdesc_t::body
char * body
Definition: microdesc_st.h:58
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
microdesc_t::last_listed
time_t last_listed
Definition: microdesc_st.h:36
find_all_by_keyword
smartlist_t * find_all_by_keyword(const smartlist_t *s, directory_keyword k)
Definition: parsecommon.c:468
T01
#define T01(s, t, a, o)
Definition: parsecommon.h:258
directory_token_t::n_args
int n_args
Definition: parsecommon.h:203
microdesc_t::onion_pkey
char * onion_pkey
Definition: microdesc_st.h:70
memarea_clear
void memarea_clear(memarea_t *area)
Definition: memarea.c:178
microdescs_parse_from_string
smartlist_t * microdescs_parse_from_string(const char *s, const char *eos, int allow_annotations, saved_location_t where, smartlist_t *invalid_digests_out)
Definition: microdesc_parse.c:293
crypto_format.h
Header for crypto_format.c.
NO_ARGS
#define NO_ARGS
Definition: parsecommon.h:265
curve25519_public_from_base64
int curve25519_public_from_base64(curve25519_public_key_t *pkey, const char *input)
Definition: crypto_format.c:170
T1
#define T1(s, t, a, o)
Definition: parsecommon.h:250
memarea_new
memarea_t * memarea_new(void)
Definition: memarea.c:153
microdesc_t::ed25519_identity_pkey
struct ed25519_public_key_t * ed25519_identity_pkey
Definition: microdesc_st.h:77
microdesc_t::exit_policy
struct short_policy_t * exit_policy
Definition: microdesc_st.h:85
ed25519_public_key_t
Definition: crypto_ed25519.h:23
microdesc_t::ipv6_orport
uint16_t ipv6_orport
Definition: microdesc_st.h:81
strcmpstart
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:215
SAVED_IN_JOURNAL
@ SAVED_IN_JOURNAL
Definition: or.h:761
microdesc_st.h
Microdescriptor structure.
memarea_t
Definition: memarea.c:119
directory_token_t::object_body
char * object_body
Definition: parsecommon.h:208
microdesc_t::family
struct nodefamily_t * family
Definition: microdesc_st.h:83
crypto_curve25519.h
Header for crypto_curve25519.c.
microdesc_t::onion_pkey_len
size_t onion_pkey_len
Definition: microdesc_st.h:72
DIGEST256_LEN
#define DIGEST256_LEN
Definition: digest_sizes.h:23
CONCAT_ARGS
#define CONCAT_ARGS
Definition: parsecommon.h:267
saved_location_t
saved_location_t
Definition: or.h:747
short_policy_is_reject_star
int short_policy_is_reject_star(const short_policy_t *policy)
Definition: policies.c:2870
nodefamily.h
Header file for nodefamily.c.
SAVED_IN_CACHE
@ SAVED_IN_CACHE
Definition: or.h:754
parse_short_policy
short_policy_t * parse_short_policy(const char *summary)
Definition: policies.c:2685
directory_token_t::object_size
size_t object_size
Definition: parsecommon.h:207
curve25519_public_key_t
Definition: crypto_curve25519.h:24
microdesc_t::onion_curve25519_pkey
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: microdesc_st.h:75
crypto_pk_public_exponent_ok
int crypto_pk_public_exponent_ok(const crypto_pk_t *env)
Definition: crypto_rsa_nss.c:261
microdesc_token_table
static token_rule_t microdesc_token_table[]
Definition: microdesc_parse.c:32
nickname.h
Header file for nickname.c.
parsecommon.h
Header file for parsecommon.c.
find_start_of_next_microdesc
static const char * find_start_of_next_microdesc(const char *s, const char *eos)
Definition: microdesc_parse.c:48
microdesc_t::ipv6_addr
tor_addr_t ipv6_addr
Definition: microdesc_st.h:79
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
find_single_ipv6_orport
int find_single_ipv6_orport(const smartlist_t *list, tor_addr_t *addr_out, uint16_t *port_out)
Definition: routerparse.c:340
find_opt_by_keyword
directory_token_t * find_opt_by_keyword(const smartlist_t *s, directory_keyword keyword)
Definition: parsecommon.c:457
policies.h
Header file for policies.c.
ed25519_public_from_base64
int ed25519_public_from_base64(ed25519_public_key_t *pkey, const char *input)
Definition: crypto_format.c:212
microdesc_extract_body
static int microdesc_extract_body(microdesc_t *md, const char *start, const char *s, const char *start_of_next_microdesc, saved_location_t where)
Definition: microdesc_parse.c:140
config.h
Header file for config.c.
crypto_digest256
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
Definition: crypto_digest_nss.c:121
microdesc_t::saved_location
saved_location_bitfield_t saved_location
Definition: microdesc_st.h:38
token_rule_t
Definition: parsecommon.h:275
microdesc_t::policy_is_reject_star
unsigned int policy_is_reject_star
Definition: microdesc_st.h:44
microdesc_t::ipv6_exit_policy
struct short_policy_t * ipv6_exit_policy
Definition: microdesc_st.h:87
microdesc_t::bodylen
size_t bodylen
Definition: microdesc_st.h:60
saved_location_to_string
static const char * saved_location_to_string(saved_location_t where)
Definition: microdesc_parse.c:108
GE
#define GE(n)
Definition: parsecommon.h:269
routerparse.h
Header file for routerparse.c.
LD_DIR
#define LD_DIR
Definition: log.h:88
microdesc_parse_fields
static int microdesc_parse_fields(microdesc_t *md, memarea_t *area, const char *s, const char *start_of_next_microdesc, int allow_annotations, saved_location_t where)
Definition: microdesc_parse.c:177
A01
#define A01(s, t, a, o)
Definition: parsecommon.h:260
eat_whitespace_eos
const char * eat_whitespace_eos(const char *s, const char *eos)
Definition: util_string.c:304
crypto_ed25519.h
Header for crypto_ed25519.c.
SAVED_NOWHERE
@ SAVED_NOWHERE
Definition: or.h:750
smartlist_t
Definition: smartlist_core.h:26
directory_token_t::key
struct crypto_pk_t * key
Definition: parsecommon.h:210
or.h
Master header file for Tor-specific functionality.