routerparse.c 45 KB

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