routerparse.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file routerparse.c
  8. * \brief Code to parse and validate router descriptors, consenus directories,
  9. * and similar objects.
  10. *
  11. * The objects parsed by this module use a common text-based metaformat,
  12. * documented in dir-spec.txt in torspec.git. This module is itself divided
  13. * into two major kinds of function: code to handle the metaformat, and code
  14. * to convert from particular instances of the metaformat into the
  15. * objects that Tor uses.
  16. *
  17. * The generic parsing code works by calling a table-based tokenizer on the
  18. * input string. Each token corresponds to a single line with a token, plus
  19. * optional arguments on that line, plus an optional base-64 encoded object
  20. * after that line. Each token has a definition in a table of token_rule_t
  21. * entries that describes how many arguments it can take, whether it takes an
  22. * object, how many times it may appear, whether it must appear first, and so
  23. * on.
  24. *
  25. * The tokenizer function tokenize_string() converts its string input into a
  26. * smartlist full of instances of directory_token_t, according to a provided
  27. * table of token_rule_t.
  28. *
  29. * The generic parts of this module additionally include functions for
  30. * finding the start and end of signed information inside a signed object, and
  31. * computing the digest that will be signed.
  32. *
  33. * There are also functions for saving objects to disk that have caused
  34. * parsing to fail.
  35. *
  36. * The specific parts of this module describe conversions between
  37. * particular lists of directory_token_t and particular objects. The
  38. * kinds of objects that can be parsed here are:
  39. * <ul>
  40. * <li>router descriptors (managed from routerlist.c)
  41. * <li>extra-info documents (managed from routerlist.c)
  42. * <li>microdescriptors (managed from microdesc.c)
  43. * <li>vote and consensus networkstatus documents, and the routerstatus_t
  44. * objects that they comprise (managed from networkstatus.c)
  45. * <li>detached-signature objects used by authorities for gathering
  46. * signatures on the networkstatus consensus (managed from dirvote.c)
  47. * <li>authority key certificates (managed from routerlist.c)
  48. * <li>hidden service descriptors (managed from rendcommon.c and rendcache.c)
  49. * </ul>
  50. **/
  51. #define EXPOSE_ROUTERDESC_TOKEN_TABLE
  52. #include "core/or/or.h"
  53. #include "app/config/config.h"
  54. #include "core/or/policies.h"
  55. #include "core/or/versions.h"
  56. #include "feature/dirparse/parsecommon.h"
  57. #include "feature/dirparse/policy_parse.h"
  58. #include "feature/dirparse/routerparse.h"
  59. #include "feature/dirparse/sigcommon.h"
  60. #include "feature/dirparse/unparseable.h"
  61. #include "feature/nodelist/describe.h"
  62. #include "feature/nodelist/nickname.h"
  63. #include "feature/nodelist/routerinfo.h"
  64. #include "feature/nodelist/routerlist.h"
  65. #include "feature/nodelist/torcert.h"
  66. #include "feature/relay/router.h"
  67. #include "lib/crypt_ops/crypto_curve25519.h"
  68. #include "lib/crypt_ops/crypto_ed25519.h"
  69. #include "lib/crypt_ops/crypto_format.h"
  70. #include "lib/memarea/memarea.h"
  71. #include "lib/sandbox/sandbox.h"
  72. #include "core/or/addr_policy_st.h"
  73. #include "feature/nodelist/extrainfo_st.h"
  74. #include "feature/nodelist/routerinfo_st.h"
  75. #include "feature/nodelist/routerlist_st.h"
  76. /****************************************************************************/
  77. /** List of tokens recognized in router descriptors */
  78. const token_rule_t routerdesc_token_table[] = {
  79. T0N("reject", K_REJECT, ARGS, NO_OBJ ),
  80. T0N("accept", K_ACCEPT, ARGS, NO_OBJ ),
  81. T0N("reject6", K_REJECT6, ARGS, NO_OBJ ),
  82. T0N("accept6", K_ACCEPT6, ARGS, NO_OBJ ),
  83. T1_START( "router", K_ROUTER, GE(5), NO_OBJ ),
  84. T01("ipv6-policy", K_IPV6_POLICY, CONCAT_ARGS, NO_OBJ),
  85. T1( "signing-key", K_SIGNING_KEY, NO_ARGS, NEED_KEY_1024 ),
  86. T1( "onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024 ),
  87. T01("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
  88. T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
  89. T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
  90. T01("uptime", K_UPTIME, GE(1), NO_OBJ ),
  91. T01("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
  92. T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ ),
  93. T01("platform", K_PLATFORM, CONCAT_ARGS, NO_OBJ ),
  94. T01("proto", K_PROTO, CONCAT_ARGS, NO_OBJ ),
  95. T01("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
  96. T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
  97. T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
  98. T01("extra-info-digest", K_EXTRA_INFO_DIGEST, GE(1), NO_OBJ ),
  99. T01("hidden-service-dir", K_HIDDEN_SERVICE_DIR, NO_ARGS, NO_OBJ ),
  100. T01("identity-ed25519", K_IDENTITY_ED25519, NO_ARGS, NEED_OBJ ),
  101. T01("master-key-ed25519", K_MASTER_KEY_ED25519, GE(1), NO_OBJ ),
  102. T01("router-sig-ed25519", K_ROUTER_SIG_ED25519, GE(1), NO_OBJ ),
  103. T01("onion-key-crosscert", K_ONION_KEY_CROSSCERT, NO_ARGS, NEED_OBJ ),
  104. T01("ntor-onion-key-crosscert", K_NTOR_ONION_KEY_CROSSCERT,
  105. EQ(1), NEED_OBJ ),
  106. T01("allow-single-hop-exits",K_ALLOW_SINGLE_HOP_EXITS, NO_ARGS, NO_OBJ ),
  107. T01("family", K_FAMILY, ARGS, NO_OBJ ),
  108. T01("caches-extra-info", K_CACHES_EXTRA_INFO, NO_ARGS, NO_OBJ ),
  109. T0N("or-address", K_OR_ADDRESS, GE(1), NO_OBJ ),
  110. T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
  111. T1( "bandwidth", K_BANDWIDTH, GE(3), NO_OBJ ),
  112. A01("@purpose", A_PURPOSE, GE(1), NO_OBJ ),
  113. T01("tunnelled-dir-server",K_DIR_TUNNELLED, NO_ARGS, NO_OBJ ),
  114. END_OF_TABLE
  115. };
  116. /** List of tokens recognized in extra-info documents. */
  117. static token_rule_t extrainfo_token_table[] = {
  118. T1_END( "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ ),
  119. T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
  120. T01("identity-ed25519", K_IDENTITY_ED25519, NO_ARGS, NEED_OBJ ),
  121. T01("router-sig-ed25519", K_ROUTER_SIG_ED25519, GE(1), NO_OBJ ),
  122. T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
  123. T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ),
  124. T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ),
  125. T01("dirreq-stats-end", K_DIRREQ_END, ARGS, NO_OBJ ),
  126. T01("dirreq-v2-ips", K_DIRREQ_V2_IPS, ARGS, NO_OBJ ),
  127. T01("dirreq-v3-ips", K_DIRREQ_V3_IPS, ARGS, NO_OBJ ),
  128. T01("dirreq-v2-reqs", K_DIRREQ_V2_REQS, ARGS, NO_OBJ ),
  129. T01("dirreq-v3-reqs", K_DIRREQ_V3_REQS, ARGS, NO_OBJ ),
  130. T01("dirreq-v2-share", K_DIRREQ_V2_SHARE, ARGS, NO_OBJ ),
  131. T01("dirreq-v3-share", K_DIRREQ_V3_SHARE, ARGS, NO_OBJ ),
  132. T01("dirreq-v2-resp", K_DIRREQ_V2_RESP, ARGS, NO_OBJ ),
  133. T01("dirreq-v3-resp", K_DIRREQ_V3_RESP, ARGS, NO_OBJ ),
  134. T01("dirreq-v2-direct-dl", K_DIRREQ_V2_DIR, ARGS, NO_OBJ ),
  135. T01("dirreq-v3-direct-dl", K_DIRREQ_V3_DIR, ARGS, NO_OBJ ),
  136. T01("dirreq-v2-tunneled-dl", K_DIRREQ_V2_TUN, ARGS, NO_OBJ ),
  137. T01("dirreq-v3-tunneled-dl", K_DIRREQ_V3_TUN, ARGS, NO_OBJ ),
  138. T01("entry-stats-end", K_ENTRY_END, ARGS, NO_OBJ ),
  139. T01("entry-ips", K_ENTRY_IPS, ARGS, NO_OBJ ),
  140. T01("cell-stats-end", K_CELL_END, ARGS, NO_OBJ ),
  141. T01("cell-processed-cells", K_CELL_PROCESSED, ARGS, NO_OBJ ),
  142. T01("cell-queued-cells", K_CELL_QUEUED, ARGS, NO_OBJ ),
  143. T01("cell-time-in-queue", K_CELL_TIME, ARGS, NO_OBJ ),
  144. T01("cell-circuits-per-decile", K_CELL_CIRCS, ARGS, NO_OBJ ),
  145. T01("exit-stats-end", K_EXIT_END, ARGS, NO_OBJ ),
  146. T01("exit-kibibytes-written", K_EXIT_WRITTEN, ARGS, NO_OBJ ),
  147. T01("exit-kibibytes-read", K_EXIT_READ, ARGS, NO_OBJ ),
  148. T01("exit-streams-opened", K_EXIT_OPENED, ARGS, NO_OBJ ),
  149. T1_START( "extra-info", K_EXTRA_INFO, GE(2), NO_OBJ ),
  150. END_OF_TABLE
  151. };
  152. #undef T
  153. /* static function prototypes */
  154. static int router_add_exit_policy(routerinfo_t *router,directory_token_t *tok);
  155. static smartlist_t *find_all_exitpolicy(smartlist_t *s);
  156. /** Set <b>digest</b> to the SHA-1 digest of the hash of the first router in
  157. * <b>s</b>. Return 0 on success, -1 on failure.
  158. */
  159. int
  160. router_get_router_hash(const char *s, size_t s_len, char *digest)
  161. {
  162. return router_get_hash_impl(s, s_len, digest,
  163. "router ","\nrouter-signature", '\n',
  164. DIGEST_SHA1);
  165. }
  166. /** Set <b>digest</b> to the SHA-1 digest of the hash of the <b>s_len</b>-byte
  167. * extrainfo string at <b>s</b>. Return 0 on success, -1 on failure. */
  168. int
  169. router_get_extrainfo_hash(const char *s, size_t s_len, char *digest)
  170. {
  171. return router_get_hash_impl(s, s_len, digest, "extra-info",
  172. "\nrouter-signature",'\n', DIGEST_SHA1);
  173. }
  174. /** Helper: move *<b>s_ptr</b> ahead to the next router, the next extra-info,
  175. * or to the first of the annotations proceeding the next router or
  176. * extra-info---whichever comes first. Set <b>is_extrainfo_out</b> to true if
  177. * we found an extrainfo, or false if found a router. Do not scan beyond
  178. * <b>eos</b>. Return -1 if we found nothing; 0 if we found something. */
  179. static int
  180. find_start_of_next_router_or_extrainfo(const char **s_ptr,
  181. const char *eos,
  182. int *is_extrainfo_out)
  183. {
  184. const char *annotations = NULL;
  185. const char *s = *s_ptr;
  186. s = eat_whitespace_eos(s, eos);
  187. while (s < eos-32) { /* 32 gives enough room for a the first keyword. */
  188. /* We're at the start of a line. */
  189. tor_assert(*s != '\n');
  190. if (*s == '@' && !annotations) {
  191. annotations = s;
  192. } else if (*s == 'r' && !strcmpstart(s, "router ")) {
  193. *s_ptr = annotations ? annotations : s;
  194. *is_extrainfo_out = 0;
  195. return 0;
  196. } else if (*s == 'e' && !strcmpstart(s, "extra-info ")) {
  197. *s_ptr = annotations ? annotations : s;
  198. *is_extrainfo_out = 1;
  199. return 0;
  200. }
  201. if (!(s = memchr(s+1, '\n', eos-(s+1))))
  202. break;
  203. s = eat_whitespace_eos(s, eos);
  204. }
  205. return -1;
  206. }
  207. /** Given a string *<b>s</b> containing a concatenated sequence of router
  208. * descriptors (or extra-info documents if <b>want_extrainfo</b> is set),
  209. * parses them and stores the result in <b>dest</b>. All routers are marked
  210. * running and valid. Advances *s to a point immediately following the last
  211. * router entry. Ignore any trailing router entries that are not complete.
  212. *
  213. * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
  214. * descriptor in the signed_descriptor_body field of each routerinfo_t. If it
  215. * isn't SAVED_NOWHERE, remember the offset of each descriptor.
  216. *
  217. * Returns 0 on success and -1 on failure. Adds a digest to
  218. * <b>invalid_digests_out</b> for every entry that was unparseable or
  219. * invalid. (This may cause duplicate entries.)
  220. */
  221. int
  222. router_parse_list_from_string(const char **s, const char *eos,
  223. smartlist_t *dest,
  224. saved_location_t saved_location,
  225. int want_extrainfo,
  226. int allow_annotations,
  227. const char *prepend_annotations,
  228. smartlist_t *invalid_digests_out)
  229. {
  230. routerinfo_t *router;
  231. extrainfo_t *extrainfo;
  232. signed_descriptor_t *signed_desc = NULL;
  233. void *elt;
  234. const char *end, *start;
  235. int have_extrainfo;
  236. tor_assert(s);
  237. tor_assert(*s);
  238. tor_assert(dest);
  239. start = *s;
  240. if (!eos)
  241. eos = *s + strlen(*s);
  242. tor_assert(eos >= *s);
  243. while (1) {
  244. char raw_digest[DIGEST_LEN];
  245. int have_raw_digest = 0;
  246. int dl_again = 0;
  247. if (find_start_of_next_router_or_extrainfo(s, eos, &have_extrainfo) < 0)
  248. break;
  249. end = tor_memstr(*s, eos-*s, "\nrouter-signature");
  250. if (end)
  251. end = tor_memstr(end, eos-end, "\n-----END SIGNATURE-----\n");
  252. if (end)
  253. end += strlen("\n-----END SIGNATURE-----\n");
  254. if (!end)
  255. break;
  256. elt = NULL;
  257. if (have_extrainfo && want_extrainfo) {
  258. routerlist_t *rl = router_get_routerlist();
  259. have_raw_digest = router_get_extrainfo_hash(*s, end-*s, raw_digest) == 0;
  260. extrainfo = extrainfo_parse_entry_from_string(*s, end,
  261. saved_location != SAVED_IN_CACHE,
  262. rl->identity_map, &dl_again);
  263. if (extrainfo) {
  264. signed_desc = &extrainfo->cache_info;
  265. elt = extrainfo;
  266. }
  267. } else if (!have_extrainfo && !want_extrainfo) {
  268. have_raw_digest = router_get_router_hash(*s, end-*s, raw_digest) == 0;
  269. router = router_parse_entry_from_string(*s, end,
  270. saved_location != SAVED_IN_CACHE,
  271. allow_annotations,
  272. prepend_annotations, &dl_again);
  273. if (router) {
  274. log_debug(LD_DIR, "Read router '%s', purpose '%s'",
  275. router_describe(router),
  276. router_purpose_to_string(router->purpose));
  277. signed_desc = &router->cache_info;
  278. elt = router;
  279. }
  280. }
  281. if (! elt && ! dl_again && have_raw_digest && invalid_digests_out) {
  282. smartlist_add(invalid_digests_out, tor_memdup(raw_digest, DIGEST_LEN));
  283. }
  284. if (!elt) {
  285. *s = end;
  286. continue;
  287. }
  288. if (saved_location != SAVED_NOWHERE) {
  289. tor_assert(signed_desc);
  290. signed_desc->saved_location = saved_location;
  291. signed_desc->saved_offset = *s - start;
  292. }
  293. *s = end;
  294. smartlist_add(dest, elt);
  295. }
  296. return 0;
  297. }
  298. /** Try to find an IPv6 OR port in <b>list</b> of directory_token_t's
  299. * with at least one argument (use GE(1) in setup). If found, store
  300. * address and port number to <b>addr_out</b> and
  301. * <b>port_out</b>. Return number of OR ports found. */
  302. int
  303. find_single_ipv6_orport(const smartlist_t *list,
  304. tor_addr_t *addr_out,
  305. uint16_t *port_out)
  306. {
  307. int ret = 0;
  308. tor_assert(list != NULL);
  309. tor_assert(addr_out != NULL);
  310. tor_assert(port_out != NULL);
  311. SMARTLIST_FOREACH_BEGIN(list, directory_token_t *, t) {
  312. tor_addr_t a;
  313. maskbits_t bits;
  314. uint16_t port_min, port_max;
  315. tor_assert(t->n_args >= 1);
  316. /* XXXX Prop186 the full spec allows much more than this. */
  317. if (tor_addr_parse_mask_ports(t->args[0], 0,
  318. &a, &bits, &port_min,
  319. &port_max) == AF_INET6 &&
  320. bits == 128 &&
  321. port_min == port_max) {
  322. /* Okay, this is one we can understand. Use it and ignore
  323. any potential more addresses in list. */
  324. tor_addr_copy(addr_out, &a);
  325. *port_out = port_min;
  326. ret = 1;
  327. break;
  328. }
  329. } SMARTLIST_FOREACH_END(t);
  330. return ret;
  331. }
  332. /** Helper function: reads a single router entry from *<b>s</b> ...
  333. * *<b>end</b>. Mallocs a new router and returns it if all goes well, else
  334. * returns NULL. If <b>cache_copy</b> is true, duplicate the contents of
  335. * s through end into the signed_descriptor_body of the resulting
  336. * routerinfo_t.
  337. *
  338. * If <b>end</b> is NULL, <b>s</b> must be properly NUL-terminated.
  339. *
  340. * If <b>allow_annotations</b>, it's okay to encounter annotations in <b>s</b>
  341. * before the router; if it's false, reject the router if it's annotated. If
  342. * <b>prepend_annotations</b> is set, it should contain some annotations:
  343. * append them to the front of the router before parsing it, and keep them
  344. * around when caching the router.
  345. *
  346. * Only one of allow_annotations and prepend_annotations may be set.
  347. *
  348. * If <b>can_dl_again_out</b> is provided, set *<b>can_dl_again_out</b> to 1
  349. * if it's okay to try to download a descriptor with this same digest again,
  350. * and 0 if it isn't. (It might not be okay to download it again if part of
  351. * the part covered by the digest is invalid.)
  352. */
  353. routerinfo_t *
  354. router_parse_entry_from_string(const char *s, const char *end,
  355. int cache_copy, int allow_annotations,
  356. const char *prepend_annotations,
  357. int *can_dl_again_out)
  358. {
  359. routerinfo_t *router = NULL;
  360. char digest[128];
  361. smartlist_t *tokens = NULL, *exit_policy_tokens = NULL;
  362. directory_token_t *tok;
  363. struct in_addr in;
  364. const char *start_of_annotations, *cp, *s_dup = s;
  365. size_t prepend_len = prepend_annotations ? strlen(prepend_annotations) : 0;
  366. int ok = 1;
  367. memarea_t *area = NULL;
  368. tor_cert_t *ntor_cc_cert = NULL;
  369. /* Do not set this to '1' until we have parsed everything that we intend to
  370. * parse that's covered by the hash. */
  371. int can_dl_again = 0;
  372. crypto_pk_t *rsa_pubkey = NULL;
  373. tor_assert(!allow_annotations || !prepend_annotations);
  374. if (!end) {
  375. end = s + strlen(s);
  376. }
  377. /* point 'end' to a point immediately after the final newline. */
  378. while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
  379. --end;
  380. area = memarea_new();
  381. tokens = smartlist_new();
  382. if (prepend_annotations) {
  383. if (tokenize_string(area,prepend_annotations,NULL,tokens,
  384. routerdesc_token_table,TS_NOCHECK)) {
  385. log_warn(LD_DIR, "Error tokenizing router descriptor (annotations).");
  386. goto err;
  387. }
  388. }
  389. start_of_annotations = s;
  390. cp = tor_memstr(s, end-s, "\nrouter ");
  391. if (!cp) {
  392. if (end-s < 7 || strcmpstart(s, "router ")) {
  393. log_warn(LD_DIR, "No router keyword found.");
  394. goto err;
  395. }
  396. } else {
  397. s = cp+1;
  398. }
  399. if (start_of_annotations != s) { /* We have annotations */
  400. if (allow_annotations) {
  401. if (tokenize_string(area,start_of_annotations,s,tokens,
  402. routerdesc_token_table,TS_NOCHECK)) {
  403. log_warn(LD_DIR, "Error tokenizing router descriptor (annotations).");
  404. goto err;
  405. }
  406. } else {
  407. log_warn(LD_DIR, "Found unexpected annotations on router descriptor not "
  408. "loaded from disk. Dropping it.");
  409. goto err;
  410. }
  411. }
  412. if (router_get_router_hash(s, end - s, digest) < 0) {
  413. log_warn(LD_DIR, "Couldn't compute router hash.");
  414. goto err;
  415. }
  416. {
  417. int flags = 0;
  418. if (allow_annotations)
  419. flags |= TS_ANNOTATIONS_OK;
  420. if (prepend_annotations)
  421. flags |= TS_ANNOTATIONS_OK|TS_NO_NEW_ANNOTATIONS;
  422. if (tokenize_string(area,s,end,tokens,routerdesc_token_table, flags)) {
  423. log_warn(LD_DIR, "Error tokenizing router descriptor.");
  424. goto err;
  425. }
  426. }
  427. if (smartlist_len(tokens) < 2) {
  428. log_warn(LD_DIR, "Impossibly short router descriptor.");
  429. goto err;
  430. }
  431. tok = find_by_keyword(tokens, K_ROUTER);
  432. const int router_token_pos = smartlist_pos(tokens, tok);
  433. tor_assert(tok->n_args >= 5);
  434. router = tor_malloc_zero(sizeof(routerinfo_t));
  435. router->cert_expiration_time = TIME_MAX;
  436. router->cache_info.routerlist_index = -1;
  437. router->cache_info.annotations_len = s-start_of_annotations + prepend_len;
  438. router->cache_info.signed_descriptor_len = end-s;
  439. if (cache_copy) {
  440. size_t len = router->cache_info.signed_descriptor_len +
  441. router->cache_info.annotations_len;
  442. char *signed_body =
  443. router->cache_info.signed_descriptor_body = tor_malloc(len+1);
  444. if (prepend_annotations) {
  445. memcpy(signed_body, prepend_annotations, prepend_len);
  446. signed_body += prepend_len;
  447. }
  448. /* This assertion will always succeed.
  449. * len == signed_desc_len + annotations_len
  450. * == end-s + s-start_of_annotations + prepend_len
  451. * == end-start_of_annotations + prepend_len
  452. * We already wrote prepend_len bytes into the buffer; now we're
  453. * writing end-start_of_annotations -NM. */
  454. tor_assert(signed_body+(end-start_of_annotations) ==
  455. router->cache_info.signed_descriptor_body+len);
  456. memcpy(signed_body, start_of_annotations, end-start_of_annotations);
  457. router->cache_info.signed_descriptor_body[len] = '\0';
  458. tor_assert(strlen(router->cache_info.signed_descriptor_body) == len);
  459. }
  460. memcpy(router->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
  461. router->nickname = tor_strdup(tok->args[0]);
  462. if (!is_legal_nickname(router->nickname)) {
  463. log_warn(LD_DIR,"Router nickname is invalid");
  464. goto err;
  465. }
  466. if (!tor_inet_aton(tok->args[1], &in)) {
  467. log_warn(LD_DIR,"Router address is not an IP address.");
  468. goto err;
  469. }
  470. router->addr = ntohl(in.s_addr);
  471. router->or_port =
  472. (uint16_t) tor_parse_long(tok->args[2],10,0,65535,&ok,NULL);
  473. if (!ok) {
  474. log_warn(LD_DIR,"Invalid OR port %s", escaped(tok->args[2]));
  475. goto err;
  476. }
  477. router->dir_port =
  478. (uint16_t) tor_parse_long(tok->args[4],10,0,65535,&ok,NULL);
  479. if (!ok) {
  480. log_warn(LD_DIR,"Invalid dir port %s", escaped(tok->args[4]));
  481. goto err;
  482. }
  483. tok = find_by_keyword(tokens, K_BANDWIDTH);
  484. tor_assert(tok->n_args >= 3);
  485. router->bandwidthrate = (int)
  486. tor_parse_long(tok->args[0],10,1,INT_MAX,&ok,NULL);
  487. if (!ok) {
  488. log_warn(LD_DIR, "bandwidthrate %s unreadable or 0. Failing.",
  489. escaped(tok->args[0]));
  490. goto err;
  491. }
  492. router->bandwidthburst =
  493. (int) tor_parse_long(tok->args[1],10,0,INT_MAX,&ok,NULL);
  494. if (!ok) {
  495. log_warn(LD_DIR, "Invalid bandwidthburst %s", escaped(tok->args[1]));
  496. goto err;
  497. }
  498. router->bandwidthcapacity = (int)
  499. tor_parse_long(tok->args[2],10,0,INT_MAX,&ok,NULL);
  500. if (!ok) {
  501. log_warn(LD_DIR, "Invalid bandwidthcapacity %s", escaped(tok->args[1]));
  502. goto err;
  503. }
  504. if ((tok = find_opt_by_keyword(tokens, A_PURPOSE))) {
  505. tor_assert(tok->n_args);
  506. router->purpose = router_purpose_from_string(tok->args[0]);
  507. if (router->purpose == ROUTER_PURPOSE_UNKNOWN) {
  508. goto err;
  509. }
  510. } else {
  511. router->purpose = ROUTER_PURPOSE_GENERAL;
  512. }
  513. router->cache_info.send_unencrypted =
  514. (router->purpose == ROUTER_PURPOSE_GENERAL) ? 1 : 0;
  515. if ((tok = find_opt_by_keyword(tokens, K_UPTIME))) {
  516. tor_assert(tok->n_args >= 1);
  517. router->uptime = tor_parse_long(tok->args[0],10,0,LONG_MAX,&ok,NULL);
  518. if (!ok) {
  519. log_warn(LD_DIR, "Invalid uptime %s", escaped(tok->args[0]));
  520. goto err;
  521. }
  522. }
  523. if ((tok = find_opt_by_keyword(tokens, K_HIBERNATING))) {
  524. tor_assert(tok->n_args >= 1);
  525. router->is_hibernating
  526. = (tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL) != 0);
  527. }
  528. tok = find_by_keyword(tokens, K_PUBLISHED);
  529. tor_assert(tok->n_args == 1);
  530. if (parse_iso_time(tok->args[0], &router->cache_info.published_on) < 0)
  531. goto err;
  532. tok = find_by_keyword(tokens, K_ONION_KEY);
  533. if (!crypto_pk_public_exponent_ok(tok->key)) {
  534. log_warn(LD_DIR,
  535. "Relay's onion key had invalid exponent.");
  536. goto err;
  537. }
  538. router->onion_pkey = tor_memdup(tok->object_body, tok->object_size);
  539. router->onion_pkey_len = tok->object_size;
  540. crypto_pk_free(tok->key);
  541. if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
  542. curve25519_public_key_t k;
  543. tor_assert(tok->n_args >= 1);
  544. if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
  545. log_warn(LD_DIR, "Bogus ntor-onion-key in routerinfo");
  546. goto err;
  547. }
  548. router->onion_curve25519_pkey =
  549. tor_memdup(&k, sizeof(curve25519_public_key_t));
  550. }
  551. tok = find_by_keyword(tokens, K_SIGNING_KEY);
  552. router->identity_pkey = tok->key;
  553. tok->key = NULL; /* Prevent free */
  554. if (crypto_pk_get_digest(router->identity_pkey,
  555. router->cache_info.identity_digest)) {
  556. log_warn(LD_DIR, "Couldn't calculate key digest"); goto err;
  557. }
  558. {
  559. directory_token_t *ed_sig_tok, *ed_cert_tok, *cc_tap_tok, *cc_ntor_tok,
  560. *master_key_tok;
  561. ed_sig_tok = find_opt_by_keyword(tokens, K_ROUTER_SIG_ED25519);
  562. ed_cert_tok = find_opt_by_keyword(tokens, K_IDENTITY_ED25519);
  563. master_key_tok = find_opt_by_keyword(tokens, K_MASTER_KEY_ED25519);
  564. cc_tap_tok = find_opt_by_keyword(tokens, K_ONION_KEY_CROSSCERT);
  565. cc_ntor_tok = find_opt_by_keyword(tokens, K_NTOR_ONION_KEY_CROSSCERT);
  566. int n_ed_toks = !!ed_sig_tok + !!ed_cert_tok +
  567. !!cc_tap_tok + !!cc_ntor_tok;
  568. if ((n_ed_toks != 0 && n_ed_toks != 4) ||
  569. (n_ed_toks == 4 && !router->onion_curve25519_pkey)) {
  570. log_warn(LD_DIR, "Router descriptor with only partial ed25519/"
  571. "cross-certification support");
  572. goto err;
  573. }
  574. if (master_key_tok && !ed_sig_tok) {
  575. log_warn(LD_DIR, "Router descriptor has ed25519 master key but no "
  576. "certificate");
  577. goto err;
  578. }
  579. if (ed_sig_tok) {
  580. tor_assert(ed_cert_tok && cc_tap_tok && cc_ntor_tok);
  581. const int ed_cert_token_pos = smartlist_pos(tokens, ed_cert_tok);
  582. if (ed_cert_token_pos == -1 || router_token_pos == -1 ||
  583. (ed_cert_token_pos != router_token_pos + 1 &&
  584. ed_cert_token_pos != router_token_pos - 1)) {
  585. log_warn(LD_DIR, "Ed25519 certificate in wrong position");
  586. goto err;
  587. }
  588. if (ed_sig_tok != smartlist_get(tokens, smartlist_len(tokens)-2)) {
  589. log_warn(LD_DIR, "Ed25519 signature in wrong position");
  590. goto err;
  591. }
  592. if (strcmp(ed_cert_tok->object_type, "ED25519 CERT")) {
  593. log_warn(LD_DIR, "Wrong object type on identity-ed25519 in decriptor");
  594. goto err;
  595. }
  596. if (strcmp(cc_ntor_tok->object_type, "ED25519 CERT")) {
  597. log_warn(LD_DIR, "Wrong object type on ntor-onion-key-crosscert "
  598. "in decriptor");
  599. goto err;
  600. }
  601. if (strcmp(cc_tap_tok->object_type, "CROSSCERT")) {
  602. log_warn(LD_DIR, "Wrong object type on onion-key-crosscert "
  603. "in decriptor");
  604. goto err;
  605. }
  606. if (strcmp(cc_ntor_tok->args[0], "0") &&
  607. strcmp(cc_ntor_tok->args[0], "1")) {
  608. log_warn(LD_DIR, "Bad sign bit on ntor-onion-key-crosscert");
  609. goto err;
  610. }
  611. int ntor_cc_sign_bit = !strcmp(cc_ntor_tok->args[0], "1");
  612. uint8_t d256[DIGEST256_LEN];
  613. const char *signed_start, *signed_end;
  614. tor_cert_t *cert = tor_cert_parse(
  615. (const uint8_t*)ed_cert_tok->object_body,
  616. ed_cert_tok->object_size);
  617. if (! cert) {
  618. log_warn(LD_DIR, "Couldn't parse ed25519 cert");
  619. goto err;
  620. }
  621. /* makes sure it gets freed. */
  622. router->cache_info.signing_key_cert = cert;
  623. if (cert->cert_type != CERT_TYPE_ID_SIGNING ||
  624. ! cert->signing_key_included) {
  625. log_warn(LD_DIR, "Invalid form for ed25519 cert");
  626. goto err;
  627. }
  628. if (master_key_tok) {
  629. /* This token is optional, but if it's present, it must match
  630. * the signature in the signing cert, or supplant it. */
  631. tor_assert(master_key_tok->n_args >= 1);
  632. ed25519_public_key_t pkey;
  633. if (ed25519_public_from_base64(&pkey, master_key_tok->args[0])<0) {
  634. log_warn(LD_DIR, "Can't parse ed25519 master key");
  635. goto err;
  636. }
  637. if (fast_memneq(&cert->signing_key.pubkey,
  638. pkey.pubkey, ED25519_PUBKEY_LEN)) {
  639. log_warn(LD_DIR, "Ed25519 master key does not match "
  640. "key in certificate");
  641. goto err;
  642. }
  643. }
  644. ntor_cc_cert = tor_cert_parse((const uint8_t*)cc_ntor_tok->object_body,
  645. cc_ntor_tok->object_size);
  646. if (!ntor_cc_cert) {
  647. log_warn(LD_DIR, "Couldn't parse ntor-onion-key-crosscert cert");
  648. goto err;
  649. }
  650. if (ntor_cc_cert->cert_type != CERT_TYPE_ONION_ID ||
  651. ! ed25519_pubkey_eq(&ntor_cc_cert->signed_key, &cert->signing_key)) {
  652. log_warn(LD_DIR, "Invalid contents for ntor-onion-key-crosscert cert");
  653. goto err;
  654. }
  655. ed25519_public_key_t ntor_cc_pk;
  656. if (ed25519_public_key_from_curve25519_public_key(&ntor_cc_pk,
  657. router->onion_curve25519_pkey,
  658. ntor_cc_sign_bit)<0) {
  659. log_warn(LD_DIR, "Error converting onion key to ed25519");
  660. goto err;
  661. }
  662. if (router_get_hash_impl_helper(s, end-s, "router ",
  663. "\nrouter-sig-ed25519",
  664. ' ', LOG_WARN,
  665. &signed_start, &signed_end) < 0) {
  666. log_warn(LD_DIR, "Can't find ed25519-signed portion of descriptor");
  667. goto err;
  668. }
  669. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  670. crypto_digest_add_bytes(d, ED_DESC_SIGNATURE_PREFIX,
  671. strlen(ED_DESC_SIGNATURE_PREFIX));
  672. crypto_digest_add_bytes(d, signed_start, signed_end-signed_start);
  673. crypto_digest_get_digest(d, (char*)d256, sizeof(d256));
  674. crypto_digest_free(d);
  675. ed25519_checkable_t check[3];
  676. int check_ok[3];
  677. time_t expires = TIME_MAX;
  678. if (tor_cert_get_checkable_sig(&check[0], cert, NULL, &expires) < 0) {
  679. log_err(LD_BUG, "Couldn't create 'checkable' for cert.");
  680. goto err;
  681. }
  682. if (tor_cert_get_checkable_sig(&check[1],
  683. ntor_cc_cert, &ntor_cc_pk, &expires) < 0) {
  684. log_err(LD_BUG, "Couldn't create 'checkable' for ntor_cc_cert.");
  685. goto err;
  686. }
  687. if (ed25519_signature_from_base64(&check[2].signature,
  688. ed_sig_tok->args[0])<0) {
  689. log_warn(LD_DIR, "Couldn't decode ed25519 signature");
  690. goto err;
  691. }
  692. check[2].pubkey = &cert->signed_key;
  693. check[2].msg = d256;
  694. check[2].len = DIGEST256_LEN;
  695. if (ed25519_checksig_batch(check_ok, check, 3) < 0) {
  696. log_warn(LD_DIR, "Incorrect ed25519 signature(s)");
  697. goto err;
  698. }
  699. rsa_pubkey = router_get_rsa_onion_pkey(router->onion_pkey,
  700. router->onion_pkey_len);
  701. if (check_tap_onion_key_crosscert(
  702. (const uint8_t*)cc_tap_tok->object_body,
  703. (int)cc_tap_tok->object_size,
  704. rsa_pubkey,
  705. &cert->signing_key,
  706. (const uint8_t*)router->cache_info.identity_digest)<0) {
  707. log_warn(LD_DIR, "Incorrect TAP cross-verification");
  708. goto err;
  709. }
  710. /* We check this before adding it to the routerlist. */
  711. router->cert_expiration_time = expires;
  712. }
  713. }
  714. if ((tok = find_opt_by_keyword(tokens, K_FINGERPRINT))) {
  715. /* If there's a fingerprint line, it must match the identity digest. */
  716. char d[DIGEST_LEN];
  717. tor_assert(tok->n_args == 1);
  718. tor_strstrip(tok->args[0], " ");
  719. if (base16_decode(d, DIGEST_LEN,
  720. tok->args[0], strlen(tok->args[0])) != DIGEST_LEN) {
  721. log_warn(LD_DIR, "Couldn't decode router fingerprint %s",
  722. escaped(tok->args[0]));
  723. goto err;
  724. }
  725. if (tor_memneq(d,router->cache_info.identity_digest, DIGEST_LEN)) {
  726. log_warn(LD_DIR, "Fingerprint '%s' does not match identity digest.",
  727. tok->args[0]);
  728. goto err;
  729. }
  730. }
  731. {
  732. const char *version = NULL, *protocols = NULL;
  733. if ((tok = find_opt_by_keyword(tokens, K_PLATFORM))) {
  734. router->platform = tor_strdup(tok->args[0]);
  735. version = tok->args[0];
  736. }
  737. if ((tok = find_opt_by_keyword(tokens, K_PROTO))) {
  738. router->protocol_list = tor_strdup(tok->args[0]);
  739. protocols = tok->args[0];
  740. }
  741. summarize_protover_flags(&router->pv, protocols, version);
  742. }
  743. if ((tok = find_opt_by_keyword(tokens, K_CONTACT))) {
  744. router->contact_info = tor_strdup(tok->args[0]);
  745. }
  746. if (find_opt_by_keyword(tokens, K_REJECT6) ||
  747. find_opt_by_keyword(tokens, K_ACCEPT6)) {
  748. log_warn(LD_DIR, "Rejecting router with reject6/accept6 line: they crash "
  749. "older Tors.");
  750. goto err;
  751. }
  752. {
  753. smartlist_t *or_addresses = find_all_by_keyword(tokens, K_OR_ADDRESS);
  754. if (or_addresses) {
  755. find_single_ipv6_orport(or_addresses, &router->ipv6_addr,
  756. &router->ipv6_orport);
  757. smartlist_free(or_addresses);
  758. }
  759. }
  760. exit_policy_tokens = find_all_exitpolicy(tokens);
  761. if (!smartlist_len(exit_policy_tokens)) {
  762. log_warn(LD_DIR, "No exit policy tokens in descriptor.");
  763. goto err;
  764. }
  765. SMARTLIST_FOREACH(exit_policy_tokens, directory_token_t *, t,
  766. if (router_add_exit_policy(router,t)<0) {
  767. log_warn(LD_DIR,"Error in exit policy");
  768. goto err;
  769. });
  770. policy_expand_private(&router->exit_policy);
  771. if ((tok = find_opt_by_keyword(tokens, K_IPV6_POLICY)) && tok->n_args) {
  772. router->ipv6_exit_policy = parse_short_policy(tok->args[0]);
  773. if (! router->ipv6_exit_policy) {
  774. log_warn(LD_DIR , "Error in ipv6-policy %s", escaped(tok->args[0]));
  775. goto err;
  776. }
  777. }
  778. if (policy_is_reject_star(router->exit_policy, AF_INET, 1) &&
  779. (!router->ipv6_exit_policy ||
  780. short_policy_is_reject_star(router->ipv6_exit_policy)))
  781. router->policy_is_reject_star = 1;
  782. if ((tok = find_opt_by_keyword(tokens, K_FAMILY)) && tok->n_args) {
  783. int i;
  784. router->declared_family = smartlist_new();
  785. for (i=0;i<tok->n_args;++i) {
  786. if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
  787. log_warn(LD_DIR, "Illegal nickname %s in family line",
  788. escaped(tok->args[i]));
  789. goto err;
  790. }
  791. smartlist_add_strdup(router->declared_family, tok->args[i]);
  792. }
  793. }
  794. if (find_opt_by_keyword(tokens, K_CACHES_EXTRA_INFO))
  795. router->caches_extra_info = 1;
  796. if (find_opt_by_keyword(tokens, K_ALLOW_SINGLE_HOP_EXITS))
  797. router->allow_single_hop_exits = 1;
  798. if ((tok = find_opt_by_keyword(tokens, K_EXTRA_INFO_DIGEST))) {
  799. tor_assert(tok->n_args >= 1);
  800. if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
  801. if (base16_decode(router->cache_info.extra_info_digest, DIGEST_LEN,
  802. tok->args[0], HEX_DIGEST_LEN) != DIGEST_LEN) {
  803. log_warn(LD_DIR,"Invalid extra info digest");
  804. }
  805. } else {
  806. log_warn(LD_DIR, "Invalid extra info digest %s", escaped(tok->args[0]));
  807. }
  808. if (tok->n_args >= 2) {
  809. if (digest256_from_base64(router->cache_info.extra_info_digest256,
  810. tok->args[1]) < 0) {
  811. log_warn(LD_DIR, "Invalid extra info digest256 %s",
  812. escaped(tok->args[1]));
  813. }
  814. }
  815. }
  816. if (find_opt_by_keyword(tokens, K_HIDDEN_SERVICE_DIR)) {
  817. router->wants_to_be_hs_dir = 1;
  818. }
  819. /* This router accepts tunnelled directory requests via begindir if it has
  820. * an open dirport or it included "tunnelled-dir-server". */
  821. if (find_opt_by_keyword(tokens, K_DIR_TUNNELLED) || router->dir_port > 0) {
  822. router->supports_tunnelled_dir_requests = 1;
  823. }
  824. tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
  825. if (!router->or_port) {
  826. log_warn(LD_DIR,"or_port unreadable or 0. Failing.");
  827. goto err;
  828. }
  829. /* We've checked everything that's covered by the hash. */
  830. can_dl_again = 1;
  831. if (check_signature_token(digest, DIGEST_LEN, tok, router->identity_pkey, 0,
  832. "router descriptor") < 0)
  833. goto err;
  834. if (!router->platform) {
  835. router->platform = tor_strdup("<unknown>");
  836. }
  837. goto done;
  838. err:
  839. dump_desc(s_dup, "router descriptor");
  840. routerinfo_free(router);
  841. router = NULL;
  842. done:
  843. crypto_pk_free(rsa_pubkey);
  844. tor_cert_free(ntor_cc_cert);
  845. if (tokens) {
  846. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  847. smartlist_free(tokens);
  848. }
  849. smartlist_free(exit_policy_tokens);
  850. if (area) {
  851. DUMP_AREA(area, "routerinfo");
  852. memarea_drop_all(area);
  853. }
  854. if (can_dl_again_out)
  855. *can_dl_again_out = can_dl_again;
  856. return router;
  857. }
  858. /** Parse a single extrainfo entry from the string <b>s</b>, ending at
  859. * <b>end</b>. (If <b>end</b> is NULL, parse up to the end of <b>s</b>.) If
  860. * <b>cache_copy</b> is true, make a copy of the extra-info document in the
  861. * cache_info fields of the result. If <b>routermap</b> is provided, use it
  862. * as a map from router identity to routerinfo_t when looking up signing keys.
  863. *
  864. * If <b>can_dl_again_out</b> is provided, set *<b>can_dl_again_out</b> to 1
  865. * if it's okay to try to download an extrainfo with this same digest again,
  866. * and 0 if it isn't. (It might not be okay to download it again if part of
  867. * the part covered by the digest is invalid.)
  868. */
  869. extrainfo_t *
  870. extrainfo_parse_entry_from_string(const char *s, const char *end,
  871. int cache_copy, struct digest_ri_map_t *routermap,
  872. int *can_dl_again_out)
  873. {
  874. extrainfo_t *extrainfo = NULL;
  875. char digest[128];
  876. smartlist_t *tokens = NULL;
  877. directory_token_t *tok;
  878. crypto_pk_t *key = NULL;
  879. routerinfo_t *router = NULL;
  880. memarea_t *area = NULL;
  881. const char *s_dup = s;
  882. /* Do not set this to '1' until we have parsed everything that we intend to
  883. * parse that's covered by the hash. */
  884. int can_dl_again = 0;
  885. if (BUG(s == NULL))
  886. return NULL;
  887. if (!end) {
  888. end = s + strlen(s);
  889. }
  890. /* point 'end' to a point immediately after the final newline. */
  891. while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
  892. --end;
  893. if (router_get_extrainfo_hash(s, end-s, digest) < 0) {
  894. log_warn(LD_DIR, "Couldn't compute router hash.");
  895. goto err;
  896. }
  897. tokens = smartlist_new();
  898. area = memarea_new();
  899. if (tokenize_string(area,s,end,tokens,extrainfo_token_table,0)) {
  900. log_warn(LD_DIR, "Error tokenizing extra-info document.");
  901. goto err;
  902. }
  903. if (smartlist_len(tokens) < 2) {
  904. log_warn(LD_DIR, "Impossibly short extra-info document.");
  905. goto err;
  906. }
  907. /* XXXX Accept this in position 1 too, and ed identity in position 0. */
  908. tok = smartlist_get(tokens,0);
  909. if (tok->tp != K_EXTRA_INFO) {
  910. log_warn(LD_DIR,"Entry does not start with \"extra-info\"");
  911. goto err;
  912. }
  913. extrainfo = tor_malloc_zero(sizeof(extrainfo_t));
  914. extrainfo->cache_info.is_extrainfo = 1;
  915. if (cache_copy)
  916. extrainfo->cache_info.signed_descriptor_body = tor_memdup_nulterm(s,end-s);
  917. extrainfo->cache_info.signed_descriptor_len = end-s;
  918. memcpy(extrainfo->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
  919. crypto_digest256((char*)extrainfo->digest256, s, end-s, DIGEST_SHA256);
  920. tor_assert(tok->n_args >= 2);
  921. if (!is_legal_nickname(tok->args[0])) {
  922. log_warn(LD_DIR,"Bad nickname %s on \"extra-info\"",escaped(tok->args[0]));
  923. goto err;
  924. }
  925. strlcpy(extrainfo->nickname, tok->args[0], sizeof(extrainfo->nickname));
  926. if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
  927. base16_decode(extrainfo->cache_info.identity_digest, DIGEST_LEN,
  928. tok->args[1], HEX_DIGEST_LEN) != DIGEST_LEN) {
  929. log_warn(LD_DIR,"Invalid fingerprint %s on \"extra-info\"",
  930. escaped(tok->args[1]));
  931. goto err;
  932. }
  933. tok = find_by_keyword(tokens, K_PUBLISHED);
  934. if (parse_iso_time(tok->args[0], &extrainfo->cache_info.published_on)) {
  935. log_warn(LD_DIR,"Invalid published time %s on \"extra-info\"",
  936. escaped(tok->args[0]));
  937. goto err;
  938. }
  939. {
  940. directory_token_t *ed_sig_tok, *ed_cert_tok;
  941. ed_sig_tok = find_opt_by_keyword(tokens, K_ROUTER_SIG_ED25519);
  942. ed_cert_tok = find_opt_by_keyword(tokens, K_IDENTITY_ED25519);
  943. int n_ed_toks = !!ed_sig_tok + !!ed_cert_tok;
  944. if (n_ed_toks != 0 && n_ed_toks != 2) {
  945. log_warn(LD_DIR, "Router descriptor with only partial ed25519/"
  946. "cross-certification support");
  947. goto err;
  948. }
  949. if (ed_sig_tok) {
  950. tor_assert(ed_cert_tok);
  951. const int ed_cert_token_pos = smartlist_pos(tokens, ed_cert_tok);
  952. if (ed_cert_token_pos != 1) {
  953. /* Accept this in position 0 XXXX */
  954. log_warn(LD_DIR, "Ed25519 certificate in wrong position");
  955. goto err;
  956. }
  957. if (ed_sig_tok != smartlist_get(tokens, smartlist_len(tokens)-2)) {
  958. log_warn(LD_DIR, "Ed25519 signature in wrong position");
  959. goto err;
  960. }
  961. if (strcmp(ed_cert_tok->object_type, "ED25519 CERT")) {
  962. log_warn(LD_DIR, "Wrong object type on identity-ed25519 in decriptor");
  963. goto err;
  964. }
  965. uint8_t d256[DIGEST256_LEN];
  966. const char *signed_start, *signed_end;
  967. tor_cert_t *cert = tor_cert_parse(
  968. (const uint8_t*)ed_cert_tok->object_body,
  969. ed_cert_tok->object_size);
  970. if (! cert) {
  971. log_warn(LD_DIR, "Couldn't parse ed25519 cert");
  972. goto err;
  973. }
  974. /* makes sure it gets freed. */
  975. extrainfo->cache_info.signing_key_cert = cert;
  976. if (cert->cert_type != CERT_TYPE_ID_SIGNING ||
  977. ! cert->signing_key_included) {
  978. log_warn(LD_DIR, "Invalid form for ed25519 cert");
  979. goto err;
  980. }
  981. if (router_get_hash_impl_helper(s, end-s, "extra-info ",
  982. "\nrouter-sig-ed25519",
  983. ' ', LOG_WARN,
  984. &signed_start, &signed_end) < 0) {
  985. log_warn(LD_DIR, "Can't find ed25519-signed portion of extrainfo");
  986. goto err;
  987. }
  988. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  989. crypto_digest_add_bytes(d, ED_DESC_SIGNATURE_PREFIX,
  990. strlen(ED_DESC_SIGNATURE_PREFIX));
  991. crypto_digest_add_bytes(d, signed_start, signed_end-signed_start);
  992. crypto_digest_get_digest(d, (char*)d256, sizeof(d256));
  993. crypto_digest_free(d);
  994. ed25519_checkable_t check[2];
  995. int check_ok[2];
  996. if (tor_cert_get_checkable_sig(&check[0], cert, NULL, NULL) < 0) {
  997. log_err(LD_BUG, "Couldn't create 'checkable' for cert.");
  998. goto err;
  999. }
  1000. if (ed25519_signature_from_base64(&check[1].signature,
  1001. ed_sig_tok->args[0])<0) {
  1002. log_warn(LD_DIR, "Couldn't decode ed25519 signature");
  1003. goto err;
  1004. }
  1005. check[1].pubkey = &cert->signed_key;
  1006. check[1].msg = d256;
  1007. check[1].len = DIGEST256_LEN;
  1008. if (ed25519_checksig_batch(check_ok, check, 2) < 0) {
  1009. log_warn(LD_DIR, "Incorrect ed25519 signature(s)");
  1010. goto err;
  1011. }
  1012. /* We don't check the certificate expiration time: checking that it
  1013. * matches the cert in the router descriptor is adequate. */
  1014. }
  1015. }
  1016. /* We've checked everything that's covered by the hash. */
  1017. can_dl_again = 1;
  1018. if (routermap &&
  1019. (router = digestmap_get((digestmap_t*)routermap,
  1020. extrainfo->cache_info.identity_digest))) {
  1021. key = router->identity_pkey;
  1022. }
  1023. tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
  1024. if (strcmp(tok->object_type, "SIGNATURE") ||
  1025. tok->object_size < 128 || tok->object_size > 512) {
  1026. log_warn(LD_DIR, "Bad object type or length on extra-info signature");
  1027. goto err;
  1028. }
  1029. if (key) {
  1030. if (check_signature_token(digest, DIGEST_LEN, tok, key, 0,
  1031. "extra-info") < 0)
  1032. goto err;
  1033. if (router)
  1034. extrainfo->cache_info.send_unencrypted =
  1035. router->cache_info.send_unencrypted;
  1036. } else {
  1037. extrainfo->pending_sig = tor_memdup(tok->object_body,
  1038. tok->object_size);
  1039. extrainfo->pending_sig_len = tok->object_size;
  1040. }
  1041. goto done;
  1042. err:
  1043. dump_desc(s_dup, "extra-info descriptor");
  1044. extrainfo_free(extrainfo);
  1045. extrainfo = NULL;
  1046. done:
  1047. if (tokens) {
  1048. SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
  1049. smartlist_free(tokens);
  1050. }
  1051. if (area) {
  1052. DUMP_AREA(area, "extrainfo");
  1053. memarea_drop_all(area);
  1054. }
  1055. if (can_dl_again_out)
  1056. *can_dl_again_out = can_dl_again;
  1057. return extrainfo;
  1058. }
  1059. /** Add an exit policy stored in the token <b>tok</b> to the router info in
  1060. * <b>router</b>. Return 0 on success, -1 on failure. */
  1061. static int
  1062. router_add_exit_policy(routerinfo_t *router, directory_token_t *tok)
  1063. {
  1064. addr_policy_t *newe;
  1065. /* Use the standard interpretation of accept/reject *, an IPv4 wildcard. */
  1066. newe = router_parse_addr_policy(tok, 0);
  1067. if (!newe)
  1068. return -1;
  1069. if (! router->exit_policy)
  1070. router->exit_policy = smartlist_new();
  1071. /* Ensure that in descriptors, accept/reject fields are followed by
  1072. * IPv4 addresses, and accept6/reject6 fields are followed by
  1073. * IPv6 addresses. Unlike torrcs, descriptor exit policies do not permit
  1074. * accept/reject followed by IPv6. */
  1075. if (((tok->tp == K_ACCEPT6 || tok->tp == K_REJECT6) &&
  1076. tor_addr_family(&newe->addr) == AF_INET)
  1077. ||
  1078. ((tok->tp == K_ACCEPT || tok->tp == K_REJECT) &&
  1079. tor_addr_family(&newe->addr) == AF_INET6)) {
  1080. /* There's nothing the user can do about other relays' descriptors,
  1081. * so we don't provide usage advice here. */
  1082. log_warn(LD_DIR, "Mismatch between field type and address type in exit "
  1083. "policy '%s'. Discarding entire router descriptor.",
  1084. tok->n_args == 1 ? tok->args[0] : "");
  1085. addr_policy_free(newe);
  1086. return -1;
  1087. }
  1088. smartlist_add(router->exit_policy, newe);
  1089. return 0;
  1090. }
  1091. /** Return a newly allocated smartlist of all accept or reject tokens in
  1092. * <b>s</b>.
  1093. */
  1094. static smartlist_t *
  1095. find_all_exitpolicy(smartlist_t *s)
  1096. {
  1097. smartlist_t *out = smartlist_new();
  1098. SMARTLIST_FOREACH(s, directory_token_t *, t,
  1099. if (t->tp == K_ACCEPT || t->tp == K_ACCEPT6 ||
  1100. t->tp == K_REJECT || t->tp == K_REJECT6)
  1101. smartlist_add(out,t));
  1102. return out;
  1103. }
  1104. /** Called on startup; right now we just handle scanning the unparseable
  1105. * descriptor dumps, but hang anything else we might need to do in the
  1106. * future here as well.
  1107. */
  1108. void
  1109. routerparse_init(void)
  1110. {
  1111. /*
  1112. * Check both if the sandbox is active and whether it's configured; no
  1113. * point in loading all that if we won't be able to use it after the
  1114. * sandbox becomes active.
  1115. */
  1116. if (!(sandbox_is_active() || get_options()->Sandbox)) {
  1117. dump_desc_init();
  1118. }
  1119. }
  1120. /** Clean up all data structures used by routerparse.c at exit */
  1121. void
  1122. routerparse_free_all(void)
  1123. {
  1124. dump_desc_fifo_cleanup();
  1125. }