parsecommon.h 8.6 KB

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