Tor 0.4.9.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-2021, 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"
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." */
47static const char *
48find_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
97static inline int
98policy_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 **/
107static 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 **/
139static 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 **/
176static 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;
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",
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);
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) */
293microdescs_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}
Header file for config.c.
Header for crypto_curve25519.c.
int curve25519_public_from_base64(curve25519_public_key_t *pkey, const char *input)
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
Header for crypto_ed25519.c.
int ed25519_public_from_base64(ed25519_public_key_t *pkey, const char *input)
Header for crypto_format.c.
int crypto_pk_public_exponent_ok(const crypto_pk_t *env)
#define DIGEST256_LEN
Definition: digest_sizes.h:23
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_DIR
Definition: log.h:88
void memarea_clear(memarea_t *area)
Definition: memarea.c:178
memarea_t * memarea_new(void)
Definition: memarea.c:153
Header for memarea.c.
#define memarea_drop_all(area)
Definition: memarea.h:22
Header file for microdesc.c.
smartlist_t * microdescs_parse_from_string(const char *s, const char *eos, int allow_annotations, saved_location_t where, smartlist_t *invalid_digests_out)
static int microdesc_extract_body(microdesc_t *md, const char *start, const char *s, const char *start_of_next_microdesc, saved_location_t where)
static const char * saved_location_to_string(saved_location_t where)
static const char * find_start_of_next_microdesc(const char *s, const char *eos)
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)
static token_rule_t microdesc_token_table[]
Header file for microdesc_parse.c.
Microdescriptor structure.
Header file for nickname.c.
Header file for nodefamily.c.
Master header file for Tor-specific functionality.
saved_location_t
Definition: or.h:623
@ SAVED_IN_JOURNAL
Definition: or.h:637
@ SAVED_NOWHERE
Definition: or.h:626
@ SAVED_IN_CACHE
Definition: or.h:630
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:451
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:440
Header file for parsecommon.c.
#define T0N(s, t, a, o)
Definition: parsecommon.h:249
#define T01(s, t, a, o)
Definition: parsecommon.h:259
#define NO_ARGS
Definition: parsecommon.h:266
#define A01(s, t, a, o)
Definition: parsecommon.h:261
@ NO_OBJ
Definition: parsecommon.h:221
@ NEED_KEY_1024
Definition: parsecommon.h:223
#define T1_START(s, t, a, o)
Definition: parsecommon.h:253
#define GE(n)
Definition: parsecommon.h:270
#define CONCAT_ARGS
Definition: parsecommon.h:268
#define T1(s, t, a, o)
Definition: parsecommon.h:251
#define END_OF_TABLE
Definition: parsecommon.h:245
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.
Header file for router.c.
int find_single_ipv6_orport(const smartlist_t *list, tor_addr_t *addr_out, uint16_t *port_out)
Definition: routerparse.c:340
Header file for routerparse.c.
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)
struct crypto_pk_t * key
Definition: parsecommon.h:212
uint16_t ipv6_orport
Definition: microdesc_st.h:81
char * onion_pkey
Definition: microdesc_st.h:70
char * body
Definition: microdesc_st.h:58
struct short_policy_t * exit_policy
Definition: microdesc_st.h:85
tor_addr_t ipv6_addr
Definition: microdesc_st.h:79
size_t onion_pkey_len
Definition: microdesc_st.h:72
saved_location_bitfield_t saved_location
Definition: microdesc_st.h:38
time_t last_listed
Definition: microdesc_st.h:36
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: microdesc_st.h:75
char digest[DIGEST256_LEN]
Definition: microdesc_st.h:62
unsigned int policy_is_reject_star
Definition: microdesc_st.h:44
size_t bodylen
Definition: microdesc_st.h:60
struct nodefamily_t * family
Definition: microdesc_st.h:83
struct ed25519_public_key_t * ed25519_identity_pkey
Definition: microdesc_st.h:77
struct short_policy_t * ipv6_exit_policy
Definition: microdesc_st.h:87
int parse_iso_time(const char *cp, time_t *t)
Definition: time_fmt.c:423
#define tor_assert(expr)
Definition: util_bug.h:103
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