parsecommon.h 8.7 KB

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