parsecommon.h 8.2 KB

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