Tor 0.4.9.1-alpha-dev
parsecommon.h
Go to the documentation of this file.
1/* Copyright (c) 2016-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file parsecommon.h
6 * \brief Header file for parsecommon.c
7 **/
8
9#ifndef TOR_PARSECOMMON_H
10#define TOR_PARSECOMMON_H
11
12#include <stddef.h>
13
14struct smartlist_t;
15struct crypto_pk_t;
16struct memarea_t;
17
18/** Enumeration of possible token types. The ones starting with K_ correspond
19* to directory 'keywords'. A_ is for an annotation, R or C is related to
20* hidden services, ERR_ is an error in the tokenizing process, EOF_ is an
21* end-of-file marker, and NIL_ is used to encode not-a-token.
22*/
23typedef enum {
24 K_ACCEPT = 0,
25 K_ACCEPT6,
26 K_DIRECTORY_SIGNATURE,
27 K_RECOMMENDED_SOFTWARE,
28 K_REJECT,
29 K_REJECT6,
30 K_ROUTER,
31 K_SIGNED_DIRECTORY,
32 K_SIGNING_KEY,
33 K_ONION_KEY,
34 K_ONION_KEY_NTOR,
35 K_ROUTER_SIGNATURE,
36 K_PUBLISHED,
37 K_RUNNING_ROUTERS,
38 K_ROUTER_STATUS,
39 K_PLATFORM,
40 K_PROTO,
41 K_OPT,
42 K_BANDWIDTH,
43 K_CONTACT,
44 K_NETWORK_STATUS,
45 K_UPTIME,
46 K_DIR_SIGNING_KEY,
47 K_FAMILY,
48 K_FINGERPRINT,
49 K_HIBERNATING,
50 K_READ_HISTORY,
51 K_WRITE_HISTORY,
52 K_NETWORK_STATUS_VERSION,
53 K_DIR_SOURCE,
54 K_DIR_OPTIONS,
55 K_CLIENT_VERSIONS,
56 K_SERVER_VERSIONS,
57 K_RECOMMENDED_CLIENT_PROTOCOLS,
58 K_RECOMMENDED_RELAY_PROTOCOLS,
59 K_REQUIRED_CLIENT_PROTOCOLS,
60 K_REQUIRED_RELAY_PROTOCOLS,
61 K_OR_ADDRESS,
62 K_ID,
63 K_P,
64 K_P6,
65 K_R,
66 K_A,
67 K_S,
68 K_V,
69 K_W,
70 K_M,
71 K_EXTRA_INFO,
72 K_EXTRA_INFO_DIGEST,
73 K_CACHES_EXTRA_INFO,
74 K_HIDDEN_SERVICE_DIR,
75 K_ALLOW_SINGLE_HOP_EXITS,
76 K_IPV6_POLICY,
77 K_ROUTER_SIG_ED25519,
78 K_IDENTITY_ED25519,
79 K_MASTER_KEY_ED25519,
80 K_ONION_KEY_CROSSCERT,
81 K_NTOR_ONION_KEY_CROSSCERT,
82
83 K_DIRREQ_END,
84 K_DIRREQ_V2_IPS,
85 K_DIRREQ_V3_IPS,
86 K_DIRREQ_V2_REQS,
87 K_DIRREQ_V3_REQS,
88 K_DIRREQ_V2_SHARE,
89 K_DIRREQ_V3_SHARE,
90 K_DIRREQ_V2_RESP,
91 K_DIRREQ_V3_RESP,
92 K_DIRREQ_V2_DIR,
93 K_DIRREQ_V3_DIR,
94 K_DIRREQ_V2_TUN,
95 K_DIRREQ_V3_TUN,
96 K_ENTRY_END,
97 K_ENTRY_IPS,
98 K_CELL_END,
99 K_CELL_PROCESSED,
100 K_CELL_QUEUED,
101 K_CELL_TIME,
102 K_CELL_CIRCS,
103 K_EXIT_END,
104 K_EXIT_WRITTEN,
105 K_EXIT_READ,
106 K_EXIT_OPENED,
107
108 K_DIR_KEY_CERTIFICATE_VERSION,
109 K_DIR_IDENTITY_KEY,
110 K_DIR_KEY_PUBLISHED,
111 K_DIR_KEY_EXPIRES,
112 K_DIR_KEY_CERTIFICATION,
113 K_DIR_KEY_CROSSCERT,
114 K_DIR_ADDRESS,
115 K_DIR_TUNNELLED,
116
117 K_VOTE_STATUS,
118 K_VALID_AFTER,
119 K_FRESH_UNTIL,
120 K_VALID_UNTIL,
121 K_VOTING_DELAY,
122
123 K_KNOWN_FLAGS,
124 K_PARAMS,
125 K_BW_WEIGHTS,
126 K_VOTE_DIGEST,
127 K_CONSENSUS_DIGEST,
128 K_ADDITIONAL_DIGEST,
129 K_ADDITIONAL_SIGNATURE,
130 K_CONSENSUS_METHODS,
131 K_CONSENSUS_METHOD,
132 K_LEGACY_DIR_KEY,
133 K_DIRECTORY_FOOTER,
134 K_SIGNING_CERT_ED,
135 K_SR_FLAG,
136 K_COMMIT,
137 K_PREVIOUS_SRV,
138 K_CURRENT_SRV,
139 K_PACKAGE,
140
141 A_PURPOSE,
142 A_LAST_LISTED,
143 A_UNKNOWN_,
144
145 R_RENDEZVOUS_SERVICE_DESCRIPTOR,
146 R_VERSION,
147 R_PERMANENT_KEY,
148 R_SECRET_ID_PART,
149 R_PUBLICATION_TIME,
150 R_PROTOCOL_VERSIONS,
151 R_INTRODUCTION_POINTS,
152 R_SIGNATURE,
153
154 R_HS_DESCRIPTOR, /* From version 3, this MUST be generic to all future
155 descriptor versions thus making it R_. */
156 R3_DESC_LIFETIME,
157 R3_DESC_SIGNING_CERT,
158 R3_REVISION_COUNTER,
159 R3_SUPERENCRYPTED,
160 R3_SIGNATURE,
161 R3_CREATE2_FORMATS,
162 R3_INTRO_AUTH_REQUIRED,
163 R3_SINGLE_ONION_SERVICE,
164 R3_INTRODUCTION_POINT,
165 R3_INTRO_ONION_KEY,
166 R3_INTRO_AUTH_KEY,
167 R3_INTRO_ENC_KEY,
168 R3_INTRO_ENC_KEY_CERT,
169 R3_INTRO_LEGACY_KEY,
170 R3_INTRO_LEGACY_KEY_CERT,
171 R3_DESC_AUTH_TYPE,
172 R3_DESC_AUTH_KEY,
173 R3_DESC_AUTH_CLIENT,
174 R3_ENCRYPTED,
175 R3_FLOW_CONTROL,
176 R3_POW_PARAMS,
177
178 R_IPO_IDENTIFIER,
179 R_IPO_IP_ADDRESS,
180 R_IPO_ONION_PORT,
181 R_IPO_ONION_KEY,
182 R_IPO_SERVICE_KEY,
183
184 C_CLIENT_NAME,
185 C_DESCRIPTOR_COOKIE,
186 C_CLIENT_KEY,
187
188 ERR_,
189 EOF_,
190 NIL_
192
193/** Structure to hold a single directory token.
194 *
195 * We parse a directory by breaking it into "tokens", each consisting
196 * of a keyword, a line full of arguments, and a binary object. The
197 * arguments and object are both optional, depending on the keyword
198 * type.
199 *
200 * This structure is only allocated in memareas; do not allocate it on
201 * the heap, or token_clear() won't work.
202 */
203typedef struct directory_token_t {
204 directory_keyword tp; /**< Type of the token. */
205 int n_args:30; /**< Number of elements in args */
206 char **args; /**< Array of arguments from keyword line. */
207
208 char *object_type; /**< -----BEGIN [object_type]-----*/
209 size_t object_size; /**< Bytes in object_body */
210 char *object_body; /**< Contents of object, base64-decoded. */
211
212 struct crypto_pk_t *key; /**< For public keys only. Heap-allocated. */
213
214 char *error; /**< For ERR_ tokens only. */
216
217/** We use a table of rules to decide how to parse each token type. */
218
219/** Rules for whether the keyword needs an object. */
220typedef enum {
221 NO_OBJ, /**< No object, ever. */
222 NEED_OBJ, /**< Object is required. */
223 OPT_KEY_1024, /**< If object is present, it must be a 1024 bit public key */
224 NEED_KEY_1024, /**< Object is required, and must be a 1024 bit public key */
225 NEED_KEY, /**< Object is required, and must be a public key. */
226 OBJ_OK, /**< Object is optional. */
227} obj_syntax;
228
229#define AT_START 1
230#define AT_END 2
231
232#define TS_ANNOTATIONS_OK 1
233#define TS_NOCHECK 2
234#define TS_NO_NEW_ANNOTATIONS 4
235
236/**
237 * @name macros for defining token rules
238 *
239 * Helper macros to define token tables. 's' is a string, 't' is a
240 * directory_keyword, 'a' is a trio of argument multiplicities, and 'o' is an
241 * object syntax.
242 */
243/**@{*/
244
245/** Appears to indicate the end of a table. */
246#define END_OF_TABLE { NULL, NIL_, 0,0,0, NO_OBJ, 0, INT_MAX, 0, 0 }
247/** An item with no restrictions: used for obsolete document types */
248#define T(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0, 0 }
249/** An item with no restrictions on multiplicity or location. */
250#define T0N(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0, 0 }
251/** An item that must appear exactly once */
252#define T1(s,t,a,o) { s, t, a, o, 1, 1, 0, 0 }
253/** An item that must appear exactly once, at the start of the document */
254#define T1_START(s,t,a,o) { s, t, a, o, 1, 1, AT_START, 0 }
255/** An item that must appear exactly once, at the end of the document */
256#define T1_END(s,t,a,o) { s, t, a, o, 1, 1, AT_END, 0 }
257/** An item that must appear one or more times */
258#define T1N(s,t,a,o) { s, t, a, o, 1, INT_MAX, 0, 0 }
259/** An item that must appear no more than once */
260#define T01(s,t,a,o) { s, t, a, o, 0, 1, 0, 0 }
261/** An annotation that must appear no more than once */
262#define A01(s,t,a,o) { s, t, a, o, 0, 1, 0, 1 }
263
264/** Argument multiplicity: any number of arguments. */
265#define ARGS 0,INT_MAX,0
266/** Argument multiplicity: no arguments. */
267#define NO_ARGS 0,0,0
268/** Argument multiplicity: concatenate all arguments. */
269#define CONCAT_ARGS 1,1,1
270/** Argument multiplicity: at least <b>n</b> arguments. */
271#define GE(n) n,INT_MAX,0
272/** Argument multiplicity: exactly <b>n</b> arguments. */
273#define EQ(n) n,n,0
274/**@}*/
275
276/** Determines the parsing rules for a single token type. */
277typedef struct token_rule_t {
278 /** The string value of the keyword identifying the type of item. */
279 const char *t;
280 /** The corresponding directory_keyword enum. */
282 /** Minimum number of arguments for this item */
284 /** Maximum number of arguments for this item */
286 /** If true, we concatenate all arguments for this item into a single
287 * string. */
289 /** Requirements on object syntax for this item. */
291 /** Lowest number of times this item may appear in a document. */
293 /** Highest number of times this item may appear in a document. */
295 /** One or more of AT_START/AT_END to limit where the item may appear in a
296 * document. */
297 int pos;
298 /** True iff this token is an annotation. */
301
303
304int tokenize_string(struct memarea_t *area,
305 const char *start, const char *end,
306 struct smartlist_t *out,
307 const token_rule_t *table,
308 int flags);
310 const char **s,
311 const char *eos,
312 const token_rule_t *table);
313
315 directory_keyword keyword,
316 const char *keyword_str);
317
318#define find_by_keyword(s, keyword) \
319 find_by_keyword_((s), (keyword), #keyword)
320
322 directory_keyword keyword);
323struct smartlist_t * find_all_by_keyword(const struct smartlist_t *s,
325
326#endif /* !defined(TOR_PARSECOMMON_H) */
smartlist_t * find_all_by_keyword(const smartlist_t *s, directory_keyword k)
Definition: parsecommon.c:462
directory_token_t * find_opt_by_keyword(const smartlist_t *s, directory_keyword keyword)
Definition: parsecommon.c:451
void token_clear(directory_token_t *tok)
Definition: parsecommon.c:41
directory_token_t * get_next_token(struct memarea_t *area, const char **s, const char *eos, const token_rule_t *table)
Definition: parsecommon.c:271
int tokenize_string(struct memarea_t *area, const char *start, const char *end, struct smartlist_t *out, const token_rule_t *table, int flags)
Definition: parsecommon.c:53
directory_token_t * find_by_keyword_(struct smartlist_t *s, directory_keyword keyword, const char *keyword_str)
Definition: parsecommon.c:435
obj_syntax
Definition: parsecommon.h:220
@ NEED_KEY
Definition: parsecommon.h:225
@ OPT_KEY_1024
Definition: parsecommon.h:223
@ OBJ_OK
Definition: parsecommon.h:226
@ NO_OBJ
Definition: parsecommon.h:221
@ NEED_OBJ
Definition: parsecommon.h:222
@ NEED_KEY_1024
Definition: parsecommon.h:224
directory_keyword
Definition: parsecommon.h:23
directory_keyword tp
Definition: parsecommon.h:204
struct crypto_pk_t * key
Definition: parsecommon.h:212
obj_syntax os
Definition: parsecommon.h:290
directory_keyword v
Definition: parsecommon.h:281
const char * t
Definition: parsecommon.h:279