parsecommon.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_AUTH_KEY,
  153. R3_INTRO_ENC_KEY,
  154. R3_INTRO_ENC_KEY_CERT,
  155. R3_INTRO_LEGACY_KEY,
  156. R3_INTRO_LEGACY_KEY_CERT,
  157. R3_DESC_AUTH_TYPE,
  158. R3_DESC_AUTH_KEY,
  159. R3_DESC_AUTH_CLIENT,
  160. R3_ENCRYPTED,
  161. R_IPO_IDENTIFIER,
  162. R_IPO_IP_ADDRESS,
  163. R_IPO_ONION_PORT,
  164. R_IPO_ONION_KEY,
  165. R_IPO_SERVICE_KEY,
  166. C_CLIENT_NAME,
  167. C_DESCRIPTOR_COOKIE,
  168. C_CLIENT_KEY,
  169. ERR_,
  170. EOF_,
  171. NIL_
  172. } directory_keyword;
  173. /** Structure to hold a single directory token.
  174. *
  175. * We parse a directory by breaking it into "tokens", each consisting
  176. * of a keyword, a line full of arguments, and a binary object. The
  177. * arguments and object are both optional, depending on the keyword
  178. * type.
  179. *
  180. * This structure is only allocated in memareas; do not allocate it on
  181. * the heap, or token_clear() won't work.
  182. */
  183. typedef struct directory_token_t {
  184. directory_keyword tp; /**< Type of the token. */
  185. int n_args:30; /**< Number of elements in args */
  186. char **args; /**< Array of arguments from keyword line. */
  187. char *object_type; /**< -----BEGIN [object_type]-----*/
  188. size_t object_size; /**< Bytes in object_body */
  189. char *object_body; /**< Contents of object, base64-decoded. */
  190. crypto_pk_t *key; /**< For public keys only. Heap-allocated. */
  191. char *error; /**< For ERR_ tokens only. */
  192. } directory_token_t;
  193. /** We use a table of rules to decide how to parse each token type. */
  194. /** Rules for whether the keyword needs an object. */
  195. typedef enum {
  196. NO_OBJ, /**< No object, ever. */
  197. NEED_OBJ, /**< Object is required. */
  198. NEED_SKEY_1024,/**< Object is required, and must be a 1024 bit private key */
  199. NEED_KEY_1024, /**< Object is required, and must be a 1024 bit public key */
  200. NEED_KEY, /**< Object is required, and must be a public key. */
  201. OBJ_OK, /**< Object is optional. */
  202. } obj_syntax;
  203. #define AT_START 1
  204. #define AT_END 2
  205. #define TS_ANNOTATIONS_OK 1
  206. #define TS_NOCHECK 2
  207. #define TS_NO_NEW_ANNOTATIONS 4
  208. /**
  209. * @name macros for defining token rules
  210. *
  211. * Helper macros to define token tables. 's' is a string, 't' is a
  212. * directory_keyword, 'a' is a trio of argument multiplicities, and 'o' is an
  213. * object syntax.
  214. */
  215. /**@{*/
  216. /** Appears to indicate the end of a table. */
  217. #define END_OF_TABLE { NULL, NIL_, 0,0,0, NO_OBJ, 0, INT_MAX, 0, 0 }
  218. /** An item with no restrictions: used for obsolete document types */
  219. #define T(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0, 0 }
  220. /** An item with no restrictions on multiplicity or location. */
  221. #define T0N(s,t,a,o) { s, t, a, o, 0, INT_MAX, 0, 0 }
  222. /** An item that must appear exactly once */
  223. #define T1(s,t,a,o) { s, t, a, o, 1, 1, 0, 0 }
  224. /** An item that must appear exactly once, at the start of the document */
  225. #define T1_START(s,t,a,o) { s, t, a, o, 1, 1, AT_START, 0 }
  226. /** An item that must appear exactly once, at the end of the document */
  227. #define T1_END(s,t,a,o) { s, t, a, o, 1, 1, AT_END, 0 }
  228. /** An item that must appear one or more times */
  229. #define T1N(s,t,a,o) { s, t, a, o, 1, INT_MAX, 0, 0 }
  230. /** An item that must appear no more than once */
  231. #define T01(s,t,a,o) { s, t, a, o, 0, 1, 0, 0 }
  232. /** An annotation that must appear no more than once */
  233. #define A01(s,t,a,o) { s, t, a, o, 0, 1, 0, 1 }
  234. /** Argument multiplicity: any number of arguments. */
  235. #define ARGS 0,INT_MAX,0
  236. /** Argument multiplicity: no arguments. */
  237. #define NO_ARGS 0,0,0
  238. /** Argument multiplicity: concatenate all arguments. */
  239. #define CONCAT_ARGS 1,1,1
  240. /** Argument multiplicity: at least <b>n</b> arguments. */
  241. #define GE(n) n,INT_MAX,0
  242. /** Argument multiplicity: exactly <b>n</b> arguments. */
  243. #define EQ(n) n,n,0
  244. /**@}*/
  245. /** Determines the parsing rules for a single token type. */
  246. typedef struct token_rule_t {
  247. /** The string value of the keyword identifying the type of item. */
  248. const char *t;
  249. /** The corresponding directory_keyword enum. */
  250. directory_keyword v;
  251. /** Minimum number of arguments for this item */
  252. int min_args;
  253. /** Maximum number of arguments for this item */
  254. int max_args;
  255. /** If true, we concatenate all arguments for this item into a single
  256. * string. */
  257. int concat_args;
  258. /** Requirements on object syntax for this item. */
  259. obj_syntax os;
  260. /** Lowest number of times this item may appear in a document. */
  261. int min_cnt;
  262. /** Highest number of times this item may appear in a document. */
  263. int max_cnt;
  264. /** One or more of AT_START/AT_END to limit where the item may appear in a
  265. * document. */
  266. int pos;
  267. /** True iff this token is an annotation. */
  268. int is_annotation;
  269. } token_rule_t;
  270. void token_clear(directory_token_t *tok);
  271. int tokenize_string(memarea_t *area,
  272. const char *start, const char *end,
  273. smartlist_t *out,
  274. token_rule_t *table,
  275. int flags);
  276. directory_token_t *get_next_token(memarea_t *area,
  277. const char **s,
  278. const char *eos,
  279. token_rule_t *table);
  280. directory_token_t *find_by_keyword_(smartlist_t *s,
  281. directory_keyword keyword,
  282. const char *keyword_str);
  283. #define find_by_keyword(s, keyword) \
  284. find_by_keyword_((s), (keyword), #keyword)
  285. directory_token_t *find_opt_by_keyword(smartlist_t *s,
  286. directory_keyword keyword);
  287. smartlist_t * find_all_by_keyword(smartlist_t *s, directory_keyword k);
  288. #endif /* TOR_PARSECOMMON_H */