protover.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file protover.c
  5. * \brief Versioning information for different pieces of the Tor protocol.
  6. *
  7. * Starting in version 0.2.9.3-alpha, Tor places separate version numbers on
  8. * each of the different components of its protocol. Relays use these numbers
  9. * to advertise what versions of the protocols they can support, and clients
  10. * use them to find what they can ask a given relay to do. Authorities vote
  11. * on the supported protocol versions for each relay, and also vote on the
  12. * which protocols you should have to support in order to be on the Tor
  13. * network. All Tor instances use these required/recommended protocol versions
  14. * to tell what level of support for recent protocols each relay has, and
  15. * to decide whether they should be running given their current protocols.
  16. *
  17. * The main advantage of these protocol versions numbers over using Tor
  18. * version numbers is that they allow different implementations of the Tor
  19. * protocols to develop independently, without having to claim compatibility
  20. * with specific versions of Tor.
  21. **/
  22. #define PROTOVER_PRIVATE
  23. #include "core/or/or.h"
  24. #include "core/or/protover.h"
  25. #include "core/or/versions.h"
  26. #include "lib/tls/tortls.h"
  27. #ifndef HAVE_RUST
  28. static const smartlist_t *get_supported_protocol_list(void);
  29. static int protocol_list_contains(const smartlist_t *protos,
  30. protocol_type_t pr, uint32_t ver);
  31. /** Mapping between protocol type string and protocol type. */
  32. /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
  33. static const struct {
  34. protocol_type_t protover_type;
  35. const char *name;
  36. /* If you add a new protocol here, you probably also want to add
  37. * parsing for it in routerstatus_parse_entry_from_string() so that
  38. * it is set in routerstatus_t */
  39. } PROTOCOL_NAMES[] = {
  40. { PRT_LINK, "Link" },
  41. { PRT_LINKAUTH, "LinkAuth" },
  42. { PRT_RELAY, "Relay" },
  43. { PRT_DIRCACHE, "DirCache" },
  44. { PRT_HSDIR, "HSDir" },
  45. { PRT_HSINTRO, "HSIntro" },
  46. { PRT_HSREND, "HSRend" },
  47. { PRT_DESC, "Desc" },
  48. { PRT_MICRODESC, "Microdesc"},
  49. { PRT_PADDING, "Padding"},
  50. { PRT_CONS, "Cons" }
  51. };
  52. #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
  53. /* Maximum allowed length of any single subprotocol name. */
  54. // C_RUST_COUPLED: src/rust/protover/protover.rs
  55. // `MAX_PROTOCOL_NAME_LENGTH`
  56. static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
  57. /**
  58. * Given a protocol_type_t, return the corresponding string used in
  59. * descriptors.
  60. */
  61. STATIC const char *
  62. protocol_type_to_str(protocol_type_t pr)
  63. {
  64. unsigned i;
  65. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  66. if (PROTOCOL_NAMES[i].protover_type == pr)
  67. return PROTOCOL_NAMES[i].name;
  68. }
  69. /* LCOV_EXCL_START */
  70. tor_assert_nonfatal_unreached_once();
  71. return "UNKNOWN";
  72. /* LCOV_EXCL_STOP */
  73. }
  74. /**
  75. * Given a string, find the corresponding protocol type and store it in
  76. * <b>pr_out</b>. Return 0 on success, -1 on failure.
  77. */
  78. STATIC int
  79. str_to_protocol_type(const char *s, protocol_type_t *pr_out)
  80. {
  81. if (BUG(!pr_out))
  82. return -1;
  83. unsigned i;
  84. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  85. if (0 == strcmp(s, PROTOCOL_NAMES[i].name)) {
  86. *pr_out = PROTOCOL_NAMES[i].protover_type;
  87. return 0;
  88. }
  89. }
  90. return -1;
  91. }
  92. /**
  93. * Release all space held by a single proto_entry_t structure
  94. */
  95. STATIC void
  96. proto_entry_free_(proto_entry_t *entry)
  97. {
  98. if (!entry)
  99. return;
  100. tor_free(entry->name);
  101. SMARTLIST_FOREACH(entry->ranges, proto_range_t *, r, tor_free(r));
  102. smartlist_free(entry->ranges);
  103. tor_free(entry);
  104. }
  105. /** The largest possible protocol version. */
  106. #define MAX_PROTOCOL_VERSION (UINT32_MAX-1)
  107. /**
  108. * Given a string <b>s</b> and optional end-of-string pointer
  109. * <b>end_of_range</b>, parse the protocol range and store it in
  110. * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
  111. * U-U, where U is an unsigned 32-bit integer.
  112. */
  113. static int
  114. parse_version_range(const char *s, const char *end_of_range,
  115. uint32_t *low_out, uint32_t *high_out)
  116. {
  117. uint32_t low, high;
  118. char *next = NULL;
  119. int ok;
  120. tor_assert(high_out);
  121. tor_assert(low_out);
  122. if (BUG(!end_of_range))
  123. end_of_range = s + strlen(s); // LCOV_EXCL_LINE
  124. /* A range must start with a digit. */
  125. if (!TOR_ISDIGIT(*s)) {
  126. goto error;
  127. }
  128. /* Note that this wouldn't be safe if we didn't know that eventually,
  129. * we'd hit a NUL */
  130. low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
  131. if (!ok)
  132. goto error;
  133. if (next > end_of_range)
  134. goto error;
  135. if (next == end_of_range) {
  136. high = low;
  137. goto done;
  138. }
  139. if (*next != '-')
  140. goto error;
  141. s = next+1;
  142. /* ibid */
  143. if (!TOR_ISDIGIT(*s)) {
  144. goto error;
  145. }
  146. high = (uint32_t) tor_parse_ulong(s, 10, 0,
  147. MAX_PROTOCOL_VERSION, &ok, &next);
  148. if (!ok)
  149. goto error;
  150. if (next != end_of_range)
  151. goto error;
  152. if (low > high)
  153. goto error;
  154. done:
  155. *high_out = high;
  156. *low_out = low;
  157. return 0;
  158. error:
  159. return -1;
  160. }
  161. static int
  162. is_valid_keyword(const char *s, size_t n)
  163. {
  164. for (size_t i = 0; i < n; i++) {
  165. if (!TOR_ISALNUM(s[i]) && s[i] != '-')
  166. return 0;
  167. }
  168. return 1;
  169. }
  170. /** Parse a single protocol entry from <b>s</b> up to an optional
  171. * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
  172. * on error.
  173. *
  174. * A protocol entry has a keyword, an = sign, and zero or more ranges. */
  175. static proto_entry_t *
  176. parse_single_entry(const char *s, const char *end_of_entry)
  177. {
  178. proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
  179. const char *equals;
  180. out->ranges = smartlist_new();
  181. if (BUG (!end_of_entry))
  182. end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
  183. /* There must be an =. */
  184. equals = memchr(s, '=', end_of_entry - s);
  185. if (!equals)
  186. goto error;
  187. /* The name must be nonempty */
  188. if (equals == s)
  189. goto error;
  190. /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
  191. if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
  192. log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
  193. "protocol name. This is possibly an attack or a bug, unless "
  194. "the Tor network truly supports protocol names larger than "
  195. "%ud characters. The offending string was: %s",
  196. MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
  197. goto error;
  198. }
  199. /* The name must contain only alphanumeric characters and hyphens. */
  200. if (!is_valid_keyword(s, equals-s))
  201. goto error;
  202. out->name = tor_strndup(s, equals-s);
  203. tor_assert(equals < end_of_entry);
  204. s = equals + 1;
  205. while (s < end_of_entry) {
  206. const char *comma = memchr(s, ',', end_of_entry-s);
  207. proto_range_t *range = tor_malloc_zero(sizeof(proto_range_t));
  208. if (! comma)
  209. comma = end_of_entry;
  210. smartlist_add(out->ranges, range);
  211. if (parse_version_range(s, comma, &range->low, &range->high) < 0) {
  212. goto error;
  213. }
  214. s = comma;
  215. while (*s == ',' && s < end_of_entry)
  216. ++s;
  217. }
  218. return out;
  219. error:
  220. proto_entry_free(out);
  221. return NULL;
  222. }
  223. /**
  224. * Parse the protocol list from <b>s</b> and return it as a smartlist of
  225. * proto_entry_t
  226. */
  227. STATIC smartlist_t *
  228. parse_protocol_list(const char *s)
  229. {
  230. smartlist_t *entries = smartlist_new();
  231. while (*s) {
  232. /* Find the next space or the NUL. */
  233. const char *end_of_entry = strchr(s, ' ');
  234. proto_entry_t *entry;
  235. if (!end_of_entry)
  236. end_of_entry = s + strlen(s);
  237. entry = parse_single_entry(s, end_of_entry);
  238. if (! entry)
  239. goto error;
  240. smartlist_add(entries, entry);
  241. s = end_of_entry;
  242. while (*s == ' ')
  243. ++s;
  244. }
  245. return entries;
  246. error:
  247. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  248. smartlist_free(entries);
  249. return NULL;
  250. }
  251. /**
  252. * Return true if the unparsed protover in <b>s</b> would contain a protocol
  253. * name longer than MAX_PROTOCOL_NAME_LENGTH, and false otherwise.
  254. */
  255. bool
  256. protover_contains_long_protocol_names(const char *s)
  257. {
  258. smartlist_t *list = parse_protocol_list(s);
  259. if (!list)
  260. return true; /* yes, has a dangerous name */
  261. SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
  262. smartlist_free(list);
  263. return false; /* no, looks fine */
  264. }
  265. /**
  266. * Given a protocol type and version number, return true iff we know
  267. * how to speak that protocol.
  268. */
  269. int
  270. protover_is_supported_here(protocol_type_t pr, uint32_t ver)
  271. {
  272. const smartlist_t *ours = get_supported_protocol_list();
  273. return protocol_list_contains(ours, pr, ver);
  274. }
  275. /**
  276. * Return true iff "list" encodes a protocol list that includes support for
  277. * the indicated protocol and version.
  278. */
  279. int
  280. protocol_list_supports_protocol(const char *list, protocol_type_t tp,
  281. uint32_t version)
  282. {
  283. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  284. * up in profiles, we should memoize it.
  285. */
  286. smartlist_t *protocols = parse_protocol_list(list);
  287. if (!protocols) {
  288. return 0;
  289. }
  290. int contains = protocol_list_contains(protocols, tp, version);
  291. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  292. smartlist_free(protocols);
  293. return contains;
  294. }
  295. /**
  296. * Return true iff "list" encodes a protocol list that includes support for
  297. * the indicated protocol and version, or some later version.
  298. */
  299. int
  300. protocol_list_supports_protocol_or_later(const char *list,
  301. protocol_type_t tp,
  302. uint32_t version)
  303. {
  304. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  305. * up in profiles, we should memoize it.
  306. */
  307. smartlist_t *protocols = parse_protocol_list(list);
  308. if (!protocols) {
  309. return 0;
  310. }
  311. const char *pr_name = protocol_type_to_str(tp);
  312. int contains = 0;
  313. SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
  314. if (strcasecmp(proto->name, pr_name))
  315. continue;
  316. SMARTLIST_FOREACH_BEGIN(proto->ranges, const proto_range_t *, range) {
  317. if (range->high >= version) {
  318. contains = 1;
  319. goto found;
  320. }
  321. } SMARTLIST_FOREACH_END(range);
  322. } SMARTLIST_FOREACH_END(proto);
  323. found:
  324. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  325. smartlist_free(protocols);
  326. return contains;
  327. }
  328. /** Return the canonical string containing the list of protocols
  329. * that we support. */
  330. /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
  331. const char *
  332. protover_get_supported_protocols(void)
  333. {
  334. return
  335. "Cons=1-2 "
  336. "Desc=1-2 "
  337. "DirCache=1-2 "
  338. "HSDir=1-2 "
  339. "HSIntro=3-4 "
  340. "HSRend=1-2 "
  341. "Link=1-5 "
  342. #ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
  343. "LinkAuth=1,3 "
  344. #else
  345. "LinkAuth=3 "
  346. #endif
  347. "Microdesc=1-2 "
  348. "Relay=1-2 "
  349. "Padding=1";
  350. }
  351. /** The protocols from protover_get_supported_protocols(), as parsed into a
  352. * list of proto_entry_t values. Access this via
  353. * get_supported_protocol_list. */
  354. static smartlist_t *supported_protocol_list = NULL;
  355. /** Return a pointer to a smartlist of proto_entry_t for the protocols
  356. * we support. */
  357. static const smartlist_t *
  358. get_supported_protocol_list(void)
  359. {
  360. if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
  361. supported_protocol_list =
  362. parse_protocol_list(protover_get_supported_protocols());
  363. }
  364. return supported_protocol_list;
  365. }
  366. /**
  367. * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
  368. * as one or more newly allocated strings.
  369. */
  370. static void
  371. proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
  372. {
  373. smartlist_add_asprintf(chunks, "%s=", entry->name);
  374. SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
  375. const char *comma = "";
  376. if (range_sl_idx != 0)
  377. comma = ",";
  378. if (range->low == range->high) {
  379. smartlist_add_asprintf(chunks, "%s%lu",
  380. comma, (unsigned long)range->low);
  381. } else {
  382. smartlist_add_asprintf(chunks, "%s%lu-%lu",
  383. comma, (unsigned long)range->low,
  384. (unsigned long)range->high);
  385. }
  386. } SMARTLIST_FOREACH_END(range);
  387. }
  388. /** Given a list of space-separated proto_entry_t items,
  389. * encode it into a newly allocated space-separated string. */
  390. STATIC char *
  391. encode_protocol_list(const smartlist_t *sl)
  392. {
  393. const char *separator = "";
  394. smartlist_t *chunks = smartlist_new();
  395. SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
  396. smartlist_add_strdup(chunks, separator);
  397. proto_entry_encode_into(chunks, ent);
  398. separator = " ";
  399. } SMARTLIST_FOREACH_END(ent);
  400. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  401. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  402. smartlist_free(chunks);
  403. return result;
  404. }
  405. /* We treat any protocol list with more than this many subprotocols in it
  406. * as a DoS attempt. */
  407. /// C_RUST_COUPLED: src/rust/protover/protover.rs
  408. /// `MAX_PROTOCOLS_TO_EXPAND`
  409. static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
  410. /** Voting helper: Given a list of proto_entry_t, return a newly allocated
  411. * smartlist of newly allocated strings, one for each included protocol
  412. * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
  413. * 'Foo=7'.)
  414. *
  415. * Do not list any protocol version more than once.
  416. *
  417. * Return NULL if the list would be too big.
  418. */
  419. static smartlist_t *
  420. expand_protocol_list(const smartlist_t *protos)
  421. {
  422. smartlist_t *expanded = smartlist_new();
  423. if (!protos)
  424. return expanded;
  425. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  426. const char *name = ent->name;
  427. if (strlen(name) > MAX_PROTOCOL_NAME_LENGTH) {
  428. log_warn(LD_NET, "When expanding a protocol entry, I got a very large "
  429. "protocol name. This is possibly an attack or a bug, unless "
  430. "the Tor network truly supports protocol names larger than "
  431. "%ud characters. The offending string was: %s",
  432. MAX_PROTOCOL_NAME_LENGTH, escaped(name));
  433. continue;
  434. }
  435. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  436. uint32_t u;
  437. for (u = range->low; u <= range->high; ++u) {
  438. smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
  439. if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
  440. goto too_many;
  441. }
  442. } SMARTLIST_FOREACH_END(range);
  443. } SMARTLIST_FOREACH_END(ent);
  444. smartlist_sort_strings(expanded);
  445. smartlist_uniq_strings(expanded); // This makes voting work. do not remove
  446. return expanded;
  447. too_many:
  448. SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
  449. smartlist_free(expanded);
  450. return NULL;
  451. }
  452. /** Voting helper: compare two singleton proto_entry_t items by version
  453. * alone. (A singleton item is one with a single range entry where
  454. * low==high.) */
  455. static int
  456. cmp_single_ent_by_version(const void **a_, const void **b_)
  457. {
  458. const proto_entry_t *ent_a = *a_;
  459. const proto_entry_t *ent_b = *b_;
  460. tor_assert(smartlist_len(ent_a->ranges) == 1);
  461. tor_assert(smartlist_len(ent_b->ranges) == 1);
  462. const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
  463. const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
  464. tor_assert(a->low == a->high);
  465. tor_assert(b->low == b->high);
  466. if (a->low < b->low) {
  467. return -1;
  468. } else if (a->low == b->low) {
  469. return 0;
  470. } else {
  471. return 1;
  472. }
  473. }
  474. /** Voting helper: Given a list of singleton protocol strings (of the form
  475. * Foo=7), return a canonical listing of all the protocol versions listed,
  476. * with as few ranges as possible, with protocol versions sorted lexically and
  477. * versions sorted in numerically increasing order, using as few range entries
  478. * as possible.
  479. **/
  480. static char *
  481. contract_protocol_list(const smartlist_t *proto_strings)
  482. {
  483. if (smartlist_len(proto_strings) == 0) {
  484. return tor_strdup("");
  485. }
  486. // map from name to list of single-version entries
  487. strmap_t *entry_lists_by_name = strmap_new();
  488. // list of protocol names
  489. smartlist_t *all_names = smartlist_new();
  490. // list of strings for the output we're building
  491. smartlist_t *chunks = smartlist_new();
  492. // Parse each item and stick it entry_lists_by_name. Build
  493. // 'all_names' at the same time.
  494. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  495. if (BUG(!s))
  496. continue;// LCOV_EXCL_LINE
  497. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  498. if (BUG(!ent))
  499. continue; // LCOV_EXCL_LINE
  500. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  501. if (!lst) {
  502. smartlist_add(all_names, ent->name);
  503. lst = smartlist_new();
  504. strmap_set(entry_lists_by_name, ent->name, lst);
  505. }
  506. smartlist_add(lst, ent);
  507. } SMARTLIST_FOREACH_END(s);
  508. // We want to output the protocols sorted by their name.
  509. smartlist_sort_strings(all_names);
  510. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  511. const int first_entry = (name_sl_idx == 0);
  512. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  513. tor_assert(lst);
  514. // Sort every entry with this name by version. They are
  515. // singletons, so there can't be overlap.
  516. smartlist_sort(lst, cmp_single_ent_by_version);
  517. if (! first_entry)
  518. smartlist_add_strdup(chunks, " ");
  519. /* We're going to construct this entry from the ranges. */
  520. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  521. entry->ranges = smartlist_new();
  522. entry->name = tor_strdup(name);
  523. // Now, find all the ranges of versions start..end where
  524. // all of start, start+1, start+2, ..end are included.
  525. int start_of_cur_series = 0;
  526. while (start_of_cur_series < smartlist_len(lst)) {
  527. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  528. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  529. const uint32_t ver_low = range->low;
  530. uint32_t ver_high = ver_low;
  531. int idx;
  532. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  533. ent = smartlist_get(lst, idx);
  534. range = smartlist_get(ent->ranges, 0);
  535. if (range->low != ver_high + 1)
  536. break;
  537. ver_high += 1;
  538. }
  539. // Now idx is either off the end of the list, or the first sequence
  540. // break in the list.
  541. start_of_cur_series = idx;
  542. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  543. new_range->low = ver_low;
  544. new_range->high = ver_high;
  545. smartlist_add(entry->ranges, new_range);
  546. }
  547. proto_entry_encode_into(chunks, entry);
  548. proto_entry_free(entry);
  549. } SMARTLIST_FOREACH_END(name);
  550. // Build the result...
  551. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  552. // And free all the stuff we allocated.
  553. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  554. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  555. tor_assert(lst);
  556. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  557. smartlist_free(lst);
  558. } SMARTLIST_FOREACH_END(name);
  559. strmap_free(entry_lists_by_name, NULL);
  560. smartlist_free(all_names);
  561. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  562. smartlist_free(chunks);
  563. return result;
  564. }
  565. /**
  566. * Protocol voting implementation.
  567. *
  568. * Given a list of strings describing protocol versions, return a newly
  569. * allocated string encoding all of the protocols that are listed by at
  570. * least <b>threshold</b> of the inputs.
  571. *
  572. * The string is minimal and sorted according to the rules of
  573. * contract_protocol_list above.
  574. */
  575. char *
  576. protover_compute_vote(const smartlist_t *list_of_proto_strings,
  577. int threshold)
  578. {
  579. if (smartlist_len(list_of_proto_strings) == 0) {
  580. return tor_strdup("");
  581. }
  582. smartlist_t *all_entries = smartlist_new();
  583. // First, parse the inputs and break them into singleton entries.
  584. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  585. smartlist_t *unexpanded = parse_protocol_list(vote);
  586. if (! unexpanded) {
  587. log_warn(LD_NET, "I failed with parsing a protocol list from "
  588. "an authority. The offending string was: %s",
  589. escaped(vote));
  590. continue;
  591. }
  592. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  593. if (this_vote == NULL) {
  594. log_warn(LD_NET, "When expanding a protocol list from an authority, I "
  595. "got too many protocols. This is possibly an attack or a bug, "
  596. "unless the Tor network truly has expanded to support over %d "
  597. "different subprotocol versions. The offending string was: %s",
  598. MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
  599. } else {
  600. smartlist_add_all(all_entries, this_vote);
  601. smartlist_free(this_vote);
  602. }
  603. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  604. smartlist_free(unexpanded);
  605. } SMARTLIST_FOREACH_END(vote);
  606. if (smartlist_len(all_entries) == 0) {
  607. smartlist_free(all_entries);
  608. return tor_strdup("");
  609. }
  610. // Now sort the singleton entries
  611. smartlist_sort_strings(all_entries);
  612. // Now find all the strings that appear at least 'threshold' times.
  613. smartlist_t *include_entries = smartlist_new();
  614. const char *cur_entry = smartlist_get(all_entries, 0);
  615. int n_times = 0;
  616. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  617. if (!strcmp(ent, cur_entry)) {
  618. n_times++;
  619. } else {
  620. if (n_times >= threshold && cur_entry)
  621. smartlist_add(include_entries, (void*)cur_entry);
  622. cur_entry = ent;
  623. n_times = 1 ;
  624. }
  625. } SMARTLIST_FOREACH_END(ent);
  626. if (n_times >= threshold && cur_entry)
  627. smartlist_add(include_entries, (void*)cur_entry);
  628. // Finally, compress that list.
  629. char *result = contract_protocol_list(include_entries);
  630. smartlist_free(include_entries);
  631. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  632. smartlist_free(all_entries);
  633. return result;
  634. }
  635. /** Return true if every protocol version described in the string <b>s</b> is
  636. * one that we support, and false otherwise. If <b>missing_out</b> is
  637. * provided, set it to the list of protocols we do not support.
  638. *
  639. * NOTE: This is quadratic, but we don't do it much: only a few times per
  640. * consensus. Checking signatures should be way more expensive than this
  641. * ever would be.
  642. **/
  643. int
  644. protover_all_supported(const char *s, char **missing_out)
  645. {
  646. int all_supported = 1;
  647. smartlist_t *missing_some;
  648. smartlist_t *missing_completely;
  649. smartlist_t *missing_all;
  650. if (!s) {
  651. return 1;
  652. }
  653. smartlist_t *entries = parse_protocol_list(s);
  654. if (BUG(entries == NULL)) {
  655. log_warn(LD_NET, "Received an unparseable protocol list %s"
  656. " from the consensus", escaped(s));
  657. return 1;
  658. }
  659. missing_some = smartlist_new();
  660. missing_completely = smartlist_new();
  661. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  662. protocol_type_t tp;
  663. if (str_to_protocol_type(ent->name, &tp) < 0) {
  664. if (smartlist_len(ent->ranges)) {
  665. goto unsupported;
  666. }
  667. continue;
  668. }
  669. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  670. proto_entry_t *unsupported = tor_malloc_zero(sizeof(proto_entry_t));
  671. proto_range_t *versions = tor_malloc_zero(sizeof(proto_range_t));
  672. uint32_t i;
  673. unsupported->name = tor_strdup(ent->name);
  674. unsupported->ranges = smartlist_new();
  675. for (i = range->low; i <= range->high; ++i) {
  676. if (!protover_is_supported_here(tp, i)) {
  677. if (versions->low == 0 && versions->high == 0) {
  678. versions->low = i;
  679. /* Pre-emptively add the high now, just in case we're in a single
  680. * version range (e.g. "Link=999"). */
  681. versions->high = i;
  682. }
  683. /* If the last one to be unsupported is one less than the current
  684. * one, we're in a continuous range, so set the high field. */
  685. if ((versions->high && versions->high == i - 1) ||
  686. /* Similarly, if the last high wasn't set and we're currently
  687. * one higher than the low, add current index as the highest
  688. * known high. */
  689. (!versions->high && versions->low == i - 1)) {
  690. versions->high = i;
  691. continue;
  692. }
  693. } else {
  694. /* If we hit a supported version, and we previously had a range,
  695. * we've hit a non-continuity. Copy the previous range and add it to
  696. * the unsupported->ranges list and zero-out the previous range for
  697. * the next iteration. */
  698. if (versions->low != 0 && versions->high != 0) {
  699. proto_range_t *versions_to_add = tor_malloc(sizeof(proto_range_t));
  700. versions_to_add->low = versions->low;
  701. versions_to_add->high = versions->high;
  702. smartlist_add(unsupported->ranges, versions_to_add);
  703. versions->low = 0;
  704. versions->high = 0;
  705. }
  706. }
  707. }
  708. /* Once we've run out of versions to check, see if we had any unsupported
  709. * ones and, if so, add them to unsupported->ranges. */
  710. if (versions->low != 0 && versions->high != 0) {
  711. smartlist_add(unsupported->ranges, versions);
  712. }
  713. /* Finally, if we had something unsupported, add it to the list of
  714. * missing_some things and mark that there was something missing. */
  715. if (smartlist_len(unsupported->ranges) != 0) {
  716. smartlist_add(missing_some, (void*) unsupported);
  717. all_supported = 0;
  718. } else {
  719. proto_entry_free(unsupported);
  720. tor_free(versions);
  721. }
  722. } SMARTLIST_FOREACH_END(range);
  723. continue;
  724. unsupported:
  725. all_supported = 0;
  726. smartlist_add(missing_completely, (void*) ent);
  727. } SMARTLIST_FOREACH_END(ent);
  728. /* We keep the two smartlists separate so that we can free the proto_entry_t
  729. * we created and put in missing_some, so here we add them together to build
  730. * the string. */
  731. missing_all = smartlist_new();
  732. smartlist_add_all(missing_all, missing_some);
  733. smartlist_add_all(missing_all, missing_completely);
  734. if (missing_out && !all_supported) {
  735. tor_assert(smartlist_len(missing_all) != 0);
  736. *missing_out = encode_protocol_list(missing_all);
  737. }
  738. SMARTLIST_FOREACH(missing_some, proto_entry_t *, ent, proto_entry_free(ent));
  739. smartlist_free(missing_some);
  740. smartlist_free(missing_completely);
  741. smartlist_free(missing_all);
  742. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  743. smartlist_free(entries);
  744. return all_supported;
  745. }
  746. /** Helper: Given a list of proto_entry_t, return true iff
  747. * <b>pr</b>=<b>ver</b> is included in that list. */
  748. static int
  749. protocol_list_contains(const smartlist_t *protos,
  750. protocol_type_t pr, uint32_t ver)
  751. {
  752. if (BUG(protos == NULL)) {
  753. return 0; // LCOV_EXCL_LINE
  754. }
  755. const char *pr_name = protocol_type_to_str(pr);
  756. if (BUG(pr_name == NULL)) {
  757. return 0; // LCOV_EXCL_LINE
  758. }
  759. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  760. if (strcasecmp(ent->name, pr_name))
  761. continue;
  762. /* name matches; check the ranges */
  763. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  764. if (ver >= range->low && ver <= range->high)
  765. return 1;
  766. } SMARTLIST_FOREACH_END(range);
  767. } SMARTLIST_FOREACH_END(ent);
  768. return 0;
  769. }
  770. /** Return a string describing the protocols supported by tor version
  771. * <b>version</b>, or an empty string if we cannot tell.
  772. *
  773. * Note that this is only used to infer protocols for Tor versions that
  774. * can't declare their own.
  775. **/
  776. /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
  777. const char *
  778. protover_compute_for_old_tor(const char *version)
  779. {
  780. if (version == NULL) {
  781. /* No known version; guess the oldest series that is still supported. */
  782. version = "0.2.5.15";
  783. }
  784. if (tor_version_as_new_as(version,
  785. FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
  786. return "";
  787. } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
  788. /* 0.2.9.1-alpha HSRend=2 */
  789. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
  790. "Link=1-4 LinkAuth=1 "
  791. "Microdesc=1-2 Relay=1-2";
  792. } else if (tor_version_as_new_as(version, "0.2.7.5")) {
  793. /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
  794. * ed25519 support. We'll call them present only in "stable" 027,
  795. * though. */
  796. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  797. "Link=1-4 LinkAuth=1 "
  798. "Microdesc=1-2 Relay=1-2";
  799. } else if (tor_version_as_new_as(version, "0.2.4.19")) {
  800. /* No currently supported Tor server versions are older than this, or
  801. * lack these protocols. */
  802. return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  803. "Link=1-4 LinkAuth=1 "
  804. "Microdesc=1 Relay=1-2";
  805. } else {
  806. /* Cannot infer protocols. */
  807. return "";
  808. }
  809. }
  810. /**
  811. * Release all storage held by static fields in protover.c
  812. */
  813. void
  814. protover_free_all(void)
  815. {
  816. if (supported_protocol_list) {
  817. smartlist_t *entries = supported_protocol_list;
  818. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  819. smartlist_free(entries);
  820. supported_protocol_list = NULL;
  821. }
  822. }
  823. #endif /* !defined(HAVE_RUST) */