tor-commits
Threads by month
- ----- 2025 -----
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
October 2018
- 17 participants
- 2350 discussions
commit 82f4d3ca75566be89e2d26667691e742cd9b4637
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Sep 30 23:31:59 2018 -0500
Move v2 hs parsing into feature/rend
---
src/core/include.am | 2 +
src/feature/control/control.c | 1 +
src/feature/dirparse/routerparse.c | 579 -----------------------------------
src/feature/dirparse/routerparse.h | 17 --
src/feature/rend/rendcache.c | 1 +
src/feature/rend/rendcommon.c | 1 +
src/feature/rend/rendparse.c | 600 +++++++++++++++++++++++++++++++++++++
src/feature/rend/rendparse.h | 32 ++
src/feature/rend/rendservice.c | 1 +
src/test/fuzz/fuzz_hsdescv2.c | 3 +-
src/test/fuzz/fuzz_iptsv2.c | 4 +-
src/test/test.c | 1 +
12 files changed, 642 insertions(+), 600 deletions(-)
diff --git a/src/core/include.am b/src/core/include.am
index a295e6ca3..febc1d821 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -121,6 +121,7 @@ LIBTOR_APP_A_SOURCES = \
src/feature/rend/rendclient.c \
src/feature/rend/rendcommon.c \
src/feature/rend/rendmid.c \
+ src/feature/rend/rendparse.c \
src/feature/rend/rendservice.c \
src/feature/stats/geoip_stats.c \
src/feature/stats/rephist.c \
@@ -359,6 +360,7 @@ noinst_HEADERS += \
src/feature/rend/rendclient.h \
src/feature/rend/rendcommon.h \
src/feature/rend/rendmid.h \
+ src/feature/rend/rendparse.h \
src/feature/rend/rendservice.h \
src/feature/stats/geoip_stats.h \
src/feature/stats/rephist.h \
diff --git a/src/feature/control/control.c b/src/feature/control/control.c
index 418ce2e7a..7f0032c0b 100644
--- a/src/feature/control/control.c
+++ b/src/feature/control/control.c
@@ -83,6 +83,7 @@
#include "feature/relay/selftest.h"
#include "feature/rend/rendclient.h"
#include "feature/rend/rendcommon.h"
+#include "feature/rend/rendparse.h"
#include "feature/rend/rendservice.h"
#include "feature/stats/geoip_stats.h"
#include "feature/stats/predict_ports.h"
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index be1f75e56..36fd1c13d 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -209,40 +209,6 @@ static token_rule_t rtrstatus_token_table[] = {
END_OF_TABLE
};
-/** List of tokens recognized in rendezvous service descriptors */
-static token_rule_t desc_token_table[] = {
- T1_START("rendezvous-service-descriptor", R_RENDEZVOUS_SERVICE_DESCRIPTOR,
- EQ(1), NO_OBJ),
- T1("version", R_VERSION, EQ(1), NO_OBJ),
- T1("permanent-key", R_PERMANENT_KEY, NO_ARGS, NEED_KEY_1024),
- T1("secret-id-part", R_SECRET_ID_PART, EQ(1), NO_OBJ),
- T1("publication-time", R_PUBLICATION_TIME, CONCAT_ARGS, NO_OBJ),
- T1("protocol-versions", R_PROTOCOL_VERSIONS, EQ(1), NO_OBJ),
- T01("introduction-points", R_INTRODUCTION_POINTS, NO_ARGS, NEED_OBJ),
- T1_END("signature", R_SIGNATURE, NO_ARGS, NEED_OBJ),
- END_OF_TABLE
-};
-
-/** List of tokens recognized in the (encrypted) list of introduction points of
- * rendezvous service descriptors */
-static token_rule_t ipo_token_table[] = {
- T1_START("introduction-point", R_IPO_IDENTIFIER, EQ(1), NO_OBJ),
- T1("ip-address", R_IPO_IP_ADDRESS, EQ(1), NO_OBJ),
- T1("onion-port", R_IPO_ONION_PORT, EQ(1), NO_OBJ),
- T1("onion-key", R_IPO_ONION_KEY, NO_ARGS, NEED_KEY_1024),
- T1("service-key", R_IPO_SERVICE_KEY, NO_ARGS, NEED_KEY_1024),
- END_OF_TABLE
-};
-
-/** List of tokens recognized in the (possibly encrypted) list of introduction
- * points of rendezvous service descriptors */
-static token_rule_t client_keys_token_table[] = {
- T1_START("client-name", C_CLIENT_NAME, CONCAT_ARGS, NO_OBJ),
- T1("descriptor-cookie", C_DESCRIPTOR_COOKIE, EQ(1), NO_OBJ),
- T01("client-key", C_CLIENT_KEY, NO_ARGS, NEED_SKEY_1024),
- END_OF_TABLE
-};
-
/** List of tokens recognized in V3 networkstatus votes. */
static token_rule_t networkstatus_token_table[] = {
T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
@@ -366,7 +332,6 @@ static addr_policy_t *router_parse_addr_policy_private(directory_token_t *tok);
static smartlist_t *find_all_exitpolicy(smartlist_t *s);
-
/** Set <b>digest</b> to the SHA-1 digest of the hash of the directory in
* <b>s</b>. Return 0 on success, -1 on failure.
*/
@@ -3702,550 +3667,6 @@ microdescs_parse_from_string(const char *s, const char *eos,
return result;
}
-/** Parse and validate the ASCII-encoded v2 descriptor in <b>desc</b>,
- * write the parsed descriptor to the newly allocated *<b>parsed_out</b>, the
- * binary descriptor ID of length DIGEST_LEN to <b>desc_id_out</b>, the
- * encrypted introduction points to the newly allocated
- * *<b>intro_points_encrypted_out</b>, their encrypted size to
- * *<b>intro_points_encrypted_size_out</b>, the size of the encoded descriptor
- * to *<b>encoded_size_out</b>, and a pointer to the possibly next
- * descriptor to *<b>next_out</b>; return 0 for success (including validation)
- * and -1 for failure.
- *
- * If <b>as_hsdir</b> is 1, we're parsing this as an HSDir, and we should
- * be strict about time formats.
- */
-int
-rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
- char *desc_id_out,
- char **intro_points_encrypted_out,
- size_t *intro_points_encrypted_size_out,
- size_t *encoded_size_out,
- const char **next_out, const char *desc,
- int as_hsdir)
-{
- rend_service_descriptor_t *result =
- tor_malloc_zero(sizeof(rend_service_descriptor_t));
- char desc_hash[DIGEST_LEN];
- const char *eos;
- smartlist_t *tokens = smartlist_new();
- directory_token_t *tok;
- char secret_id_part[DIGEST_LEN];
- int i, version, num_ok=1;
- smartlist_t *versions;
- char public_key_hash[DIGEST_LEN];
- char test_desc_id[DIGEST_LEN];
- memarea_t *area = NULL;
- const int strict_time_fmt = as_hsdir;
-
- tor_assert(desc);
- /* Check if desc starts correctly. */
- if (strcmpstart(desc, "rendezvous-service-descriptor ")) {
- log_info(LD_REND, "Descriptor does not start correctly.");
- goto err;
- }
- /* Compute descriptor hash for later validation. */
- if (router_get_hash_impl(desc, strlen(desc), desc_hash,
- "rendezvous-service-descriptor ",
- "\nsignature", '\n', DIGEST_SHA1) < 0) {
- log_warn(LD_REND, "Couldn't compute descriptor hash.");
- goto err;
- }
- /* Determine end of string. */
- eos = strstr(desc, "\nrendezvous-service-descriptor ");
- if (!eos)
- eos = desc + strlen(desc);
- else
- eos = eos + 1;
- /* Check length. */
- if (eos-desc > REND_DESC_MAX_SIZE) {
- /* XXXX+ If we are parsing this descriptor as a server, this
- * should be a protocol warning. */
- log_warn(LD_REND, "Descriptor length is %d which exceeds "
- "maximum rendezvous descriptor size of %d bytes.",
- (int)(eos-desc), REND_DESC_MAX_SIZE);
- goto err;
- }
- /* Tokenize descriptor. */
- area = memarea_new();
- if (tokenize_string(area, desc, eos, tokens, desc_token_table, 0)) {
- log_warn(LD_REND, "Error tokenizing descriptor.");
- goto err;
- }
- /* Set next to next descriptor, if available. */
- *next_out = eos;
- /* Set length of encoded descriptor. */
- *encoded_size_out = eos - desc;
- /* Check min allowed length of token list. */
- if (smartlist_len(tokens) < 7) {
- log_warn(LD_REND, "Impossibly short descriptor.");
- goto err;
- }
- /* Parse base32-encoded descriptor ID. */
- tok = find_by_keyword(tokens, R_RENDEZVOUS_SERVICE_DESCRIPTOR);
- tor_assert(tok == smartlist_get(tokens, 0));
- tor_assert(tok->n_args == 1);
- if (!rend_valid_descriptor_id(tok->args[0])) {
- log_warn(LD_REND, "Invalid descriptor ID: '%s'", tok->args[0]);
- goto err;
- }
- if (base32_decode(desc_id_out, DIGEST_LEN,
- tok->args[0], REND_DESC_ID_V2_LEN_BASE32) < 0) {
- log_warn(LD_REND, "Descriptor ID contains illegal characters: %s",
- tok->args[0]);
- goto err;
- }
- /* Parse descriptor version. */
- tok = find_by_keyword(tokens, R_VERSION);
- tor_assert(tok->n_args == 1);
- result->version =
- (int) tor_parse_long(tok->args[0], 10, 0, INT_MAX, &num_ok, NULL);
- if (result->version != 2 || !num_ok) {
- /* If it's <2, it shouldn't be under this format. If the number
- * is greater than 2, we bumped it because we broke backward
- * compatibility. See how version numbers in our other formats
- * work. */
- log_warn(LD_REND, "Unrecognized descriptor version: %s",
- escaped(tok->args[0]));
- goto err;
- }
- /* Parse public key. */
- tok = find_by_keyword(tokens, R_PERMANENT_KEY);
- result->pk = tok->key;
- tok->key = NULL; /* Prevent free */
- /* Parse secret ID part. */
- tok = find_by_keyword(tokens, R_SECRET_ID_PART);
- tor_assert(tok->n_args == 1);
- if (strlen(tok->args[0]) != REND_SECRET_ID_PART_LEN_BASE32 ||
- strspn(tok->args[0], BASE32_CHARS) != REND_SECRET_ID_PART_LEN_BASE32) {
- log_warn(LD_REND, "Invalid secret ID part: '%s'", tok->args[0]);
- goto err;
- }
- if (base32_decode(secret_id_part, DIGEST_LEN, tok->args[0], 32) < 0) {
- log_warn(LD_REND, "Secret ID part contains illegal characters: %s",
- tok->args[0]);
- goto err;
- }
- /* Parse publication time -- up-to-date check is done when storing the
- * descriptor. */
- tok = find_by_keyword(tokens, R_PUBLICATION_TIME);
- tor_assert(tok->n_args == 1);
- if (parse_iso_time_(tok->args[0], &result->timestamp,
- strict_time_fmt, 0) < 0) {
- log_warn(LD_REND, "Invalid publication time: '%s'", tok->args[0]);
- goto err;
- }
- /* Parse protocol versions. */
- tok = find_by_keyword(tokens, R_PROTOCOL_VERSIONS);
- tor_assert(tok->n_args == 1);
- versions = smartlist_new();
- smartlist_split_string(versions, tok->args[0], ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- for (i = 0; i < smartlist_len(versions); i++) {
- version = (int) tor_parse_long(smartlist_get(versions, i),
- 10, 0, INT_MAX, &num_ok, NULL);
- if (!num_ok) /* It's a string; let's ignore it. */
- continue;
- if (version >= REND_PROTOCOL_VERSION_BITMASK_WIDTH)
- /* Avoid undefined left-shift behaviour. */
- continue;
- result->protocols |= 1 << version;
- }
- SMARTLIST_FOREACH(versions, char *, cp, tor_free(cp));
- smartlist_free(versions);
- /* Parse encrypted introduction points. Don't verify. */
- tok = find_opt_by_keyword(tokens, R_INTRODUCTION_POINTS);
- if (tok) {
- if (strcmp(tok->object_type, "MESSAGE")) {
- log_warn(LD_DIR, "Bad object type: introduction points should be of "
- "type MESSAGE");
- goto err;
- }
- *intro_points_encrypted_out = tor_memdup(tok->object_body,
- tok->object_size);
- *intro_points_encrypted_size_out = tok->object_size;
- } else {
- *intro_points_encrypted_out = NULL;
- *intro_points_encrypted_size_out = 0;
- }
- /* Parse and verify signature. */
- tok = find_by_keyword(tokens, R_SIGNATURE);
- if (check_signature_token(desc_hash, DIGEST_LEN, tok, result->pk, 0,
- "v2 rendezvous service descriptor") < 0)
- goto err;
- /* Verify that descriptor ID belongs to public key and secret ID part. */
- if (crypto_pk_get_digest(result->pk, public_key_hash) < 0) {
- log_warn(LD_REND, "Unable to compute rend descriptor public key digest");
- goto err;
- }
- rend_get_descriptor_id_bytes(test_desc_id, public_key_hash,
- secret_id_part);
- if (tor_memneq(desc_id_out, test_desc_id, DIGEST_LEN)) {
- log_warn(LD_REND, "Parsed descriptor ID does not match "
- "computed descriptor ID.");
- goto err;
- }
- goto done;
- err:
- rend_service_descriptor_free(result);
- result = NULL;
- done:
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- }
- if (area)
- memarea_drop_all(area);
- *parsed_out = result;
- if (result)
- return 0;
- return -1;
-}
-
-/** Decrypt the encrypted introduction points in <b>ipos_encrypted</b> of
- * length <b>ipos_encrypted_size</b> using <b>descriptor_cookie</b> and
- * write the result to a newly allocated string that is pointed to by
- * <b>ipos_decrypted</b> and its length to <b>ipos_decrypted_size</b>.
- * Return 0 if decryption was successful and -1 otherwise. */
-int
-rend_decrypt_introduction_points(char **ipos_decrypted,
- size_t *ipos_decrypted_size,
- const char *descriptor_cookie,
- const char *ipos_encrypted,
- size_t ipos_encrypted_size)
-{
- tor_assert(ipos_encrypted);
- tor_assert(descriptor_cookie);
- if (ipos_encrypted_size < 2) {
- log_warn(LD_REND, "Size of encrypted introduction points is too "
- "small.");
- return -1;
- }
- if (ipos_encrypted[0] == (int)REND_BASIC_AUTH) {
- char iv[CIPHER_IV_LEN], client_id[REND_BASIC_AUTH_CLIENT_ID_LEN],
- session_key[CIPHER_KEY_LEN], *dec;
- int declen, client_blocks;
- size_t pos = 0, len, client_entries_len;
- crypto_digest_t *digest;
- crypto_cipher_t *cipher;
- client_blocks = (int) ipos_encrypted[1];
- client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
- REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
- if (ipos_encrypted_size < 2 + client_entries_len + CIPHER_IV_LEN + 1) {
- log_warn(LD_REND, "Size of encrypted introduction points is too "
- "small.");
- return -1;
- }
- memcpy(iv, ipos_encrypted + 2 + client_entries_len, CIPHER_IV_LEN);
- digest = crypto_digest_new();
- crypto_digest_add_bytes(digest, descriptor_cookie, REND_DESC_COOKIE_LEN);
- crypto_digest_add_bytes(digest, iv, CIPHER_IV_LEN);
- crypto_digest_get_digest(digest, client_id,
- REND_BASIC_AUTH_CLIENT_ID_LEN);
- crypto_digest_free(digest);
- for (pos = 2; pos < 2 + client_entries_len;
- pos += REND_BASIC_AUTH_CLIENT_ENTRY_LEN) {
- if (tor_memeq(ipos_encrypted + pos, client_id,
- REND_BASIC_AUTH_CLIENT_ID_LEN)) {
- /* Attempt to decrypt introduction points. */
- cipher = crypto_cipher_new(descriptor_cookie);
- if (crypto_cipher_decrypt(cipher, session_key, ipos_encrypted
- + pos + REND_BASIC_AUTH_CLIENT_ID_LEN,
- CIPHER_KEY_LEN) < 0) {
- log_warn(LD_REND, "Could not decrypt session key for client.");
- crypto_cipher_free(cipher);
- return -1;
- }
- crypto_cipher_free(cipher);
-
- len = ipos_encrypted_size - 2 - client_entries_len - CIPHER_IV_LEN;
- dec = tor_malloc_zero(len + 1);
- declen = crypto_cipher_decrypt_with_iv(session_key, dec, len,
- ipos_encrypted + 2 + client_entries_len,
- ipos_encrypted_size - 2 - client_entries_len);
-
- if (declen < 0) {
- log_warn(LD_REND, "Could not decrypt introduction point string.");
- tor_free(dec);
- return -1;
- }
- if (fast_memcmpstart(dec, declen, "introduction-point ")) {
- log_warn(LD_REND, "Decrypted introduction points don't "
- "look like we could parse them.");
- tor_free(dec);
- continue;
- }
- *ipos_decrypted = dec;
- *ipos_decrypted_size = declen;
- return 0;
- }
- }
- log_warn(LD_REND, "Could not decrypt introduction points. Please "
- "check your authorization for this service!");
- return -1;
- } else if (ipos_encrypted[0] == (int)REND_STEALTH_AUTH) {
- char *dec;
- int declen;
- if (ipos_encrypted_size < CIPHER_IV_LEN + 2) {
- log_warn(LD_REND, "Size of encrypted introduction points is too "
- "small.");
- return -1;
- }
- dec = tor_malloc_zero(ipos_encrypted_size - CIPHER_IV_LEN - 1 + 1);
-
- declen = crypto_cipher_decrypt_with_iv(descriptor_cookie, dec,
- ipos_encrypted_size -
- CIPHER_IV_LEN - 1,
- ipos_encrypted + 1,
- ipos_encrypted_size - 1);
-
- if (declen < 0) {
- log_warn(LD_REND, "Decrypting introduction points failed!");
- tor_free(dec);
- return -1;
- }
- *ipos_decrypted = dec;
- *ipos_decrypted_size = declen;
- return 0;
- } else {
- log_warn(LD_REND, "Unknown authorization type number: %d",
- ipos_encrypted[0]);
- return -1;
- }
-}
-
-/** Parse the encoded introduction points in <b>intro_points_encoded</b> of
- * length <b>intro_points_encoded_size</b> and write the result to the
- * descriptor in <b>parsed</b>; return the number of successfully parsed
- * introduction points or -1 in case of a failure. */
-int
-rend_parse_introduction_points(rend_service_descriptor_t *parsed,
- const char *intro_points_encoded,
- size_t intro_points_encoded_size)
-{
- const char *current_ipo, *end_of_intro_points;
- smartlist_t *tokens = NULL;
- directory_token_t *tok;
- rend_intro_point_t *intro;
- extend_info_t *info;
- int result, num_ok=1;
- memarea_t *area = NULL;
- tor_assert(parsed);
- /** Function may only be invoked once. */
- tor_assert(!parsed->intro_nodes);
- if (!intro_points_encoded || intro_points_encoded_size == 0) {
- log_warn(LD_REND, "Empty or zero size introduction point list");
- goto err;
- }
- /* Consider one intro point after the other. */
- current_ipo = intro_points_encoded;
- end_of_intro_points = intro_points_encoded + intro_points_encoded_size;
- tokens = smartlist_new();
- parsed->intro_nodes = smartlist_new();
- area = memarea_new();
-
- while (!fast_memcmpstart(current_ipo, end_of_intro_points-current_ipo,
- "introduction-point ")) {
- /* Determine end of string. */
- const char *eos = tor_memstr(current_ipo, end_of_intro_points-current_ipo,
- "\nintroduction-point ");
- if (!eos)
- eos = end_of_intro_points;
- else
- eos = eos+1;
- tor_assert(eos <= intro_points_encoded+intro_points_encoded_size);
- /* Free tokens and clear token list. */
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_clear(tokens);
- memarea_clear(area);
- /* Tokenize string. */
- if (tokenize_string(area, current_ipo, eos, tokens, ipo_token_table, 0)) {
- log_warn(LD_REND, "Error tokenizing introduction point");
- goto err;
- }
- /* Advance to next introduction point, if available. */
- current_ipo = eos;
- /* Check minimum allowed length of introduction point. */
- if (smartlist_len(tokens) < 5) {
- log_warn(LD_REND, "Impossibly short introduction point.");
- goto err;
- }
- /* Allocate new intro point and extend info. */
- intro = tor_malloc_zero(sizeof(rend_intro_point_t));
- info = intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
- /* Parse identifier. */
- tok = find_by_keyword(tokens, R_IPO_IDENTIFIER);
- if (base32_decode(info->identity_digest, DIGEST_LEN,
- tok->args[0], REND_INTRO_POINT_ID_LEN_BASE32) < 0) {
- log_warn(LD_REND, "Identity digest contains illegal characters: %s",
- tok->args[0]);
- rend_intro_point_free(intro);
- goto err;
- }
- /* Write identifier to nickname. */
- info->nickname[0] = '$';
- base16_encode(info->nickname + 1, sizeof(info->nickname) - 1,
- info->identity_digest, DIGEST_LEN);
- /* Parse IP address. */
- tok = find_by_keyword(tokens, R_IPO_IP_ADDRESS);
- if (tor_addr_parse(&info->addr, tok->args[0])<0) {
- log_warn(LD_REND, "Could not parse introduction point address.");
- rend_intro_point_free(intro);
- goto err;
- }
- if (tor_addr_family(&info->addr) != AF_INET) {
- log_warn(LD_REND, "Introduction point address was not ipv4.");
- rend_intro_point_free(intro);
- goto err;
- }
-
- /* Parse onion port. */
- tok = find_by_keyword(tokens, R_IPO_ONION_PORT);
- info->port = (uint16_t) tor_parse_long(tok->args[0],10,1,65535,
- &num_ok,NULL);
- if (!info->port || !num_ok) {
- log_warn(LD_REND, "Introduction point onion port %s is invalid",
- escaped(tok->args[0]));
- rend_intro_point_free(intro);
- goto err;
- }
- /* Parse onion key. */
- tok = find_by_keyword(tokens, R_IPO_ONION_KEY);
- if (!crypto_pk_public_exponent_ok(tok->key)) {
- log_warn(LD_REND,
- "Introduction point's onion key had invalid exponent.");
- rend_intro_point_free(intro);
- goto err;
- }
- info->onion_key = tok->key;
- tok->key = NULL; /* Prevent free */
- /* Parse service key. */
- tok = find_by_keyword(tokens, R_IPO_SERVICE_KEY);
- if (!crypto_pk_public_exponent_ok(tok->key)) {
- log_warn(LD_REND,
- "Introduction point key had invalid exponent.");
- rend_intro_point_free(intro);
- goto err;
- }
- intro->intro_key = tok->key;
- tok->key = NULL; /* Prevent free */
- /* Add extend info to list of introduction points. */
- smartlist_add(parsed->intro_nodes, intro);
- }
- result = smartlist_len(parsed->intro_nodes);
- goto done;
-
- err:
- result = -1;
-
- done:
- /* Free tokens and clear token list. */
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- }
- if (area)
- memarea_drop_all(area);
-
- return result;
-}
-
-/** Parse the content of a client_key file in <b>ckstr</b> and add
- * rend_authorized_client_t's for each parsed client to
- * <b>parsed_clients</b>. Return the number of parsed clients as result
- * or -1 for failure. */
-int
-rend_parse_client_keys(strmap_t *parsed_clients, const char *ckstr)
-{
- int result = -1;
- smartlist_t *tokens;
- directory_token_t *tok;
- const char *current_entry = NULL;
- memarea_t *area = NULL;
- char *err_msg = NULL;
- if (!ckstr || strlen(ckstr) == 0)
- return -1;
- tokens = smartlist_new();
- /* Begin parsing with first entry, skipping comments or whitespace at the
- * beginning. */
- area = memarea_new();
- current_entry = eat_whitespace(ckstr);
- while (!strcmpstart(current_entry, "client-name ")) {
- rend_authorized_client_t *parsed_entry;
- /* Determine end of string. */
- const char *eos = strstr(current_entry, "\nclient-name ");
- if (!eos)
- eos = current_entry + strlen(current_entry);
- else
- eos = eos + 1;
- /* Free tokens and clear token list. */
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_clear(tokens);
- memarea_clear(area);
- /* Tokenize string. */
- if (tokenize_string(area, current_entry, eos, tokens,
- client_keys_token_table, 0)) {
- log_warn(LD_REND, "Error tokenizing client keys file.");
- goto err;
- }
- /* Advance to next entry, if available. */
- current_entry = eos;
- /* Check minimum allowed length of token list. */
- if (smartlist_len(tokens) < 2) {
- log_warn(LD_REND, "Impossibly short client key entry.");
- goto err;
- }
- /* Parse client name. */
- tok = find_by_keyword(tokens, C_CLIENT_NAME);
- tor_assert(tok == smartlist_get(tokens, 0));
- tor_assert(tok->n_args == 1);
-
- if (!rend_valid_client_name(tok->args[0])) {
- log_warn(LD_CONFIG, "Illegal client name: %s. (Length must be "
- "between 1 and %d, and valid characters are "
- "[A-Za-z0-9+-_].)", tok->args[0], REND_CLIENTNAME_MAX_LEN);
- goto err;
- }
- /* Check if client name is duplicate. */
- if (strmap_get(parsed_clients, tok->args[0])) {
- log_warn(LD_CONFIG, "HiddenServiceAuthorizeClient contains a "
- "duplicate client name: '%s'. Ignoring.", tok->args[0]);
- goto err;
- }
- parsed_entry = tor_malloc_zero(sizeof(rend_authorized_client_t));
- parsed_entry->client_name = tor_strdup(tok->args[0]);
- strmap_set(parsed_clients, parsed_entry->client_name, parsed_entry);
- /* Parse client key. */
- tok = find_opt_by_keyword(tokens, C_CLIENT_KEY);
- if (tok) {
- parsed_entry->client_key = tok->key;
- tok->key = NULL; /* Prevent free */
- }
-
- /* Parse descriptor cookie. */
- tok = find_by_keyword(tokens, C_DESCRIPTOR_COOKIE);
- tor_assert(tok->n_args == 1);
- if (rend_auth_decode_cookie(tok->args[0], parsed_entry->descriptor_cookie,
- NULL, &err_msg) < 0) {
- tor_assert(err_msg);
- log_warn(LD_REND, "%s", err_msg);
- tor_free(err_msg);
- goto err;
- }
- }
- result = strmap_size(parsed_clients);
- goto done;
- err:
- result = -1;
- done:
- /* Free tokens and clear token list. */
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- if (area)
- memarea_drop_all(area);
- return result;
-}
-
/** Called on startup; right now we just handle scanning the unparseable
* descriptor dumps, but hang anything else we might need to do in the
* future here as well.
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index 4aba62653..b8858e561 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -63,23 +63,6 @@ smartlist_t *microdescs_parse_from_string(const char *s, const char *eos,
saved_location_t where,
smartlist_t *invalid_digests_out);
-int rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
- char *desc_id_out,
- char **intro_points_encrypted_out,
- size_t *intro_points_encrypted_size_out,
- size_t *encoded_size_out,
- const char **next_out, const char *desc,
- int as_hsdir);
-int rend_decrypt_introduction_points(char **ipos_decrypted,
- size_t *ipos_decrypted_size,
- const char *descriptor_cookie,
- const char *ipos_encrypted,
- size_t ipos_encrypted_size);
-int rend_parse_introduction_points(rend_service_descriptor_t *parsed,
- const char *intro_points_encoded,
- size_t intro_points_encoded_size);
-int rend_parse_client_keys(strmap_t *parsed_clients, const char *str);
-
void routerparse_init(void);
void routerparse_free_all(void);
diff --git a/src/feature/rend/rendcache.c b/src/feature/rend/rendcache.c
index 9197a53bd..4c8afbfca 100644
--- a/src/feature/rend/rendcache.c
+++ b/src/feature/rend/rendcache.c
@@ -14,6 +14,7 @@
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/routerparse.h"
#include "feature/rend/rendcommon.h"
+#include "feature/rend/rendparse.h"
#include "core/or/extend_info_st.h"
#include "feature/rend/rend_intro_point_st.h"
diff --git a/src/feature/rend/rendcommon.c b/src/feature/rend/rendcommon.c
index 2c28437b2..458b92f51 100644
--- a/src/feature/rend/rendcommon.c
+++ b/src/feature/rend/rendcommon.c
@@ -25,6 +25,7 @@
#include "feature/rend/rendclient.h"
#include "feature/rend/rendcommon.h"
#include "feature/rend/rendmid.h"
+#include "feature/rend/rendparse.h"
#include "feature/rend/rendservice.h"
#include "feature/stats/rephist.h"
#include "feature/hs_common/replaycache.h"
diff --git a/src/feature/rend/rendparse.c b/src/feature/rend/rendparse.c
new file mode 100644
index 000000000..e2378e340
--- /dev/null
+++ b/src/feature/rend/rendparse.c
@@ -0,0 +1,600 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file rendparse.c
+ * \brief Code to parse and validate v2 hidden service descriptors.
+ **/
+
+#include "core/or/or.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/sigcommon.h"
+#include "feature/rend/rendcommon.h"
+#include "feature/rend/rendparse.h"
+#include "lib/memarea/memarea.h"
+
+#include "core/or/extend_info_st.h"
+#include "feature/rend/rend_authorized_client_st.h"
+#include "feature/rend/rend_intro_point_st.h"
+#include "feature/rend/rend_service_descriptor_st.h"
+
+/** List of tokens recognized in rendezvous service descriptors */
+static token_rule_t desc_token_table[] = {
+ T1_START("rendezvous-service-descriptor", R_RENDEZVOUS_SERVICE_DESCRIPTOR,
+ EQ(1), NO_OBJ),
+ T1("version", R_VERSION, EQ(1), NO_OBJ),
+ T1("permanent-key", R_PERMANENT_KEY, NO_ARGS, NEED_KEY_1024),
+ T1("secret-id-part", R_SECRET_ID_PART, EQ(1), NO_OBJ),
+ T1("publication-time", R_PUBLICATION_TIME, CONCAT_ARGS, NO_OBJ),
+ T1("protocol-versions", R_PROTOCOL_VERSIONS, EQ(1), NO_OBJ),
+ T01("introduction-points", R_INTRODUCTION_POINTS, NO_ARGS, NEED_OBJ),
+ T1_END("signature", R_SIGNATURE, NO_ARGS, NEED_OBJ),
+ END_OF_TABLE
+};
+
+/** List of tokens recognized in the (encrypted) list of introduction points of
+ * rendezvous service descriptors */
+static token_rule_t ipo_token_table[] = {
+ T1_START("introduction-point", R_IPO_IDENTIFIER, EQ(1), NO_OBJ),
+ T1("ip-address", R_IPO_IP_ADDRESS, EQ(1), NO_OBJ),
+ T1("onion-port", R_IPO_ONION_PORT, EQ(1), NO_OBJ),
+ T1("onion-key", R_IPO_ONION_KEY, NO_ARGS, NEED_KEY_1024),
+ T1("service-key", R_IPO_SERVICE_KEY, NO_ARGS, NEED_KEY_1024),
+ END_OF_TABLE
+};
+
+/** List of tokens recognized in the (possibly encrypted) list of introduction
+ * points of rendezvous service descriptors */
+static token_rule_t client_keys_token_table[] = {
+ T1_START("client-name", C_CLIENT_NAME, CONCAT_ARGS, NO_OBJ),
+ T1("descriptor-cookie", C_DESCRIPTOR_COOKIE, EQ(1), NO_OBJ),
+ T01("client-key", C_CLIENT_KEY, NO_ARGS, NEED_SKEY_1024),
+ END_OF_TABLE
+};
+
+/** Parse and validate the ASCII-encoded v2 descriptor in <b>desc</b>,
+ * write the parsed descriptor to the newly allocated *<b>parsed_out</b>, the
+ * binary descriptor ID of length DIGEST_LEN to <b>desc_id_out</b>, the
+ * encrypted introduction points to the newly allocated
+ * *<b>intro_points_encrypted_out</b>, their encrypted size to
+ * *<b>intro_points_encrypted_size_out</b>, the size of the encoded descriptor
+ * to *<b>encoded_size_out</b>, and a pointer to the possibly next
+ * descriptor to *<b>next_out</b>; return 0 for success (including validation)
+ * and -1 for failure.
+ *
+ * If <b>as_hsdir</b> is 1, we're parsing this as an HSDir, and we should
+ * be strict about time formats.
+ */
+int
+rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
+ char *desc_id_out,
+ char **intro_points_encrypted_out,
+ size_t *intro_points_encrypted_size_out,
+ size_t *encoded_size_out,
+ const char **next_out, const char *desc,
+ int as_hsdir)
+{
+ rend_service_descriptor_t *result =
+ tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ char desc_hash[DIGEST_LEN];
+ const char *eos;
+ smartlist_t *tokens = smartlist_new();
+ directory_token_t *tok;
+ char secret_id_part[DIGEST_LEN];
+ int i, version, num_ok=1;
+ smartlist_t *versions;
+ char public_key_hash[DIGEST_LEN];
+ char test_desc_id[DIGEST_LEN];
+ memarea_t *area = NULL;
+ const int strict_time_fmt = as_hsdir;
+
+ tor_assert(desc);
+ /* Check if desc starts correctly. */
+ if (strcmpstart(desc, "rendezvous-service-descriptor ")) {
+ log_info(LD_REND, "Descriptor does not start correctly.");
+ goto err;
+ }
+ /* Compute descriptor hash for later validation. */
+ if (router_get_hash_impl(desc, strlen(desc), desc_hash,
+ "rendezvous-service-descriptor ",
+ "\nsignature", '\n', DIGEST_SHA1) < 0) {
+ log_warn(LD_REND, "Couldn't compute descriptor hash.");
+ goto err;
+ }
+ /* Determine end of string. */
+ eos = strstr(desc, "\nrendezvous-service-descriptor ");
+ if (!eos)
+ eos = desc + strlen(desc);
+ else
+ eos = eos + 1;
+ /* Check length. */
+ if (eos-desc > REND_DESC_MAX_SIZE) {
+ /* XXXX+ If we are parsing this descriptor as a server, this
+ * should be a protocol warning. */
+ log_warn(LD_REND, "Descriptor length is %d which exceeds "
+ "maximum rendezvous descriptor size of %d bytes.",
+ (int)(eos-desc), REND_DESC_MAX_SIZE);
+ goto err;
+ }
+ /* Tokenize descriptor. */
+ area = memarea_new();
+ if (tokenize_string(area, desc, eos, tokens, desc_token_table, 0)) {
+ log_warn(LD_REND, "Error tokenizing descriptor.");
+ goto err;
+ }
+ /* Set next to next descriptor, if available. */
+ *next_out = eos;
+ /* Set length of encoded descriptor. */
+ *encoded_size_out = eos - desc;
+ /* Check min allowed length of token list. */
+ if (smartlist_len(tokens) < 7) {
+ log_warn(LD_REND, "Impossibly short descriptor.");
+ goto err;
+ }
+ /* Parse base32-encoded descriptor ID. */
+ tok = find_by_keyword(tokens, R_RENDEZVOUS_SERVICE_DESCRIPTOR);
+ tor_assert(tok == smartlist_get(tokens, 0));
+ tor_assert(tok->n_args == 1);
+ if (!rend_valid_descriptor_id(tok->args[0])) {
+ log_warn(LD_REND, "Invalid descriptor ID: '%s'", tok->args[0]);
+ goto err;
+ }
+ if (base32_decode(desc_id_out, DIGEST_LEN,
+ tok->args[0], REND_DESC_ID_V2_LEN_BASE32) < 0) {
+ log_warn(LD_REND, "Descriptor ID contains illegal characters: %s",
+ tok->args[0]);
+ goto err;
+ }
+ /* Parse descriptor version. */
+ tok = find_by_keyword(tokens, R_VERSION);
+ tor_assert(tok->n_args == 1);
+ result->version =
+ (int) tor_parse_long(tok->args[0], 10, 0, INT_MAX, &num_ok, NULL);
+ if (result->version != 2 || !num_ok) {
+ /* If it's <2, it shouldn't be under this format. If the number
+ * is greater than 2, we bumped it because we broke backward
+ * compatibility. See how version numbers in our other formats
+ * work. */
+ log_warn(LD_REND, "Unrecognized descriptor version: %s",
+ escaped(tok->args[0]));
+ goto err;
+ }
+ /* Parse public key. */
+ tok = find_by_keyword(tokens, R_PERMANENT_KEY);
+ result->pk = tok->key;
+ tok->key = NULL; /* Prevent free */
+ /* Parse secret ID part. */
+ tok = find_by_keyword(tokens, R_SECRET_ID_PART);
+ tor_assert(tok->n_args == 1);
+ if (strlen(tok->args[0]) != REND_SECRET_ID_PART_LEN_BASE32 ||
+ strspn(tok->args[0], BASE32_CHARS) != REND_SECRET_ID_PART_LEN_BASE32) {
+ log_warn(LD_REND, "Invalid secret ID part: '%s'", tok->args[0]);
+ goto err;
+ }
+ if (base32_decode(secret_id_part, DIGEST_LEN, tok->args[0], 32) < 0) {
+ log_warn(LD_REND, "Secret ID part contains illegal characters: %s",
+ tok->args[0]);
+ goto err;
+ }
+ /* Parse publication time -- up-to-date check is done when storing the
+ * descriptor. */
+ tok = find_by_keyword(tokens, R_PUBLICATION_TIME);
+ tor_assert(tok->n_args == 1);
+ if (parse_iso_time_(tok->args[0], &result->timestamp,
+ strict_time_fmt, 0) < 0) {
+ log_warn(LD_REND, "Invalid publication time: '%s'", tok->args[0]);
+ goto err;
+ }
+ /* Parse protocol versions. */
+ tok = find_by_keyword(tokens, R_PROTOCOL_VERSIONS);
+ tor_assert(tok->n_args == 1);
+ versions = smartlist_new();
+ smartlist_split_string(versions, tok->args[0], ",",
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
+ for (i = 0; i < smartlist_len(versions); i++) {
+ version = (int) tor_parse_long(smartlist_get(versions, i),
+ 10, 0, INT_MAX, &num_ok, NULL);
+ if (!num_ok) /* It's a string; let's ignore it. */
+ continue;
+ if (version >= REND_PROTOCOL_VERSION_BITMASK_WIDTH)
+ /* Avoid undefined left-shift behaviour. */
+ continue;
+ result->protocols |= 1 << version;
+ }
+ SMARTLIST_FOREACH(versions, char *, cp, tor_free(cp));
+ smartlist_free(versions);
+ /* Parse encrypted introduction points. Don't verify. */
+ tok = find_opt_by_keyword(tokens, R_INTRODUCTION_POINTS);
+ if (tok) {
+ if (strcmp(tok->object_type, "MESSAGE")) {
+ log_warn(LD_DIR, "Bad object type: introduction points should be of "
+ "type MESSAGE");
+ goto err;
+ }
+ *intro_points_encrypted_out = tor_memdup(tok->object_body,
+ tok->object_size);
+ *intro_points_encrypted_size_out = tok->object_size;
+ } else {
+ *intro_points_encrypted_out = NULL;
+ *intro_points_encrypted_size_out = 0;
+ }
+ /* Parse and verify signature. */
+ tok = find_by_keyword(tokens, R_SIGNATURE);
+ if (check_signature_token(desc_hash, DIGEST_LEN, tok, result->pk, 0,
+ "v2 rendezvous service descriptor") < 0)
+ goto err;
+ /* Verify that descriptor ID belongs to public key and secret ID part. */
+ if (crypto_pk_get_digest(result->pk, public_key_hash) < 0) {
+ log_warn(LD_REND, "Unable to compute rend descriptor public key digest");
+ goto err;
+ }
+ rend_get_descriptor_id_bytes(test_desc_id, public_key_hash,
+ secret_id_part);
+ if (tor_memneq(desc_id_out, test_desc_id, DIGEST_LEN)) {
+ log_warn(LD_REND, "Parsed descriptor ID does not match "
+ "computed descriptor ID.");
+ goto err;
+ }
+ goto done;
+ err:
+ rend_service_descriptor_free(result);
+ result = NULL;
+ done:
+ if (tokens) {
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ }
+ if (area)
+ memarea_drop_all(area);
+ *parsed_out = result;
+ if (result)
+ return 0;
+ return -1;
+}
+
+/** Decrypt the encrypted introduction points in <b>ipos_encrypted</b> of
+ * length <b>ipos_encrypted_size</b> using <b>descriptor_cookie</b> and
+ * write the result to a newly allocated string that is pointed to by
+ * <b>ipos_decrypted</b> and its length to <b>ipos_decrypted_size</b>.
+ * Return 0 if decryption was successful and -1 otherwise. */
+int
+rend_decrypt_introduction_points(char **ipos_decrypted,
+ size_t *ipos_decrypted_size,
+ const char *descriptor_cookie,
+ const char *ipos_encrypted,
+ size_t ipos_encrypted_size)
+{
+ tor_assert(ipos_encrypted);
+ tor_assert(descriptor_cookie);
+ if (ipos_encrypted_size < 2) {
+ log_warn(LD_REND, "Size of encrypted introduction points is too "
+ "small.");
+ return -1;
+ }
+ if (ipos_encrypted[0] == (int)REND_BASIC_AUTH) {
+ char iv[CIPHER_IV_LEN], client_id[REND_BASIC_AUTH_CLIENT_ID_LEN],
+ session_key[CIPHER_KEY_LEN], *dec;
+ int declen, client_blocks;
+ size_t pos = 0, len, client_entries_len;
+ crypto_digest_t *digest;
+ crypto_cipher_t *cipher;
+ client_blocks = (int) ipos_encrypted[1];
+ client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
+ REND_BASIC_AUTH_CLIENT_ENTRY_LEN;
+ if (ipos_encrypted_size < 2 + client_entries_len + CIPHER_IV_LEN + 1) {
+ log_warn(LD_REND, "Size of encrypted introduction points is too "
+ "small.");
+ return -1;
+ }
+ memcpy(iv, ipos_encrypted + 2 + client_entries_len, CIPHER_IV_LEN);
+ digest = crypto_digest_new();
+ crypto_digest_add_bytes(digest, descriptor_cookie, REND_DESC_COOKIE_LEN);
+ crypto_digest_add_bytes(digest, iv, CIPHER_IV_LEN);
+ crypto_digest_get_digest(digest, client_id,
+ REND_BASIC_AUTH_CLIENT_ID_LEN);
+ crypto_digest_free(digest);
+ for (pos = 2; pos < 2 + client_entries_len;
+ pos += REND_BASIC_AUTH_CLIENT_ENTRY_LEN) {
+ if (tor_memeq(ipos_encrypted + pos, client_id,
+ REND_BASIC_AUTH_CLIENT_ID_LEN)) {
+ /* Attempt to decrypt introduction points. */
+ cipher = crypto_cipher_new(descriptor_cookie);
+ if (crypto_cipher_decrypt(cipher, session_key, ipos_encrypted
+ + pos + REND_BASIC_AUTH_CLIENT_ID_LEN,
+ CIPHER_KEY_LEN) < 0) {
+ log_warn(LD_REND, "Could not decrypt session key for client.");
+ crypto_cipher_free(cipher);
+ return -1;
+ }
+ crypto_cipher_free(cipher);
+
+ len = ipos_encrypted_size - 2 - client_entries_len - CIPHER_IV_LEN;
+ dec = tor_malloc_zero(len + 1);
+ declen = crypto_cipher_decrypt_with_iv(session_key, dec, len,
+ ipos_encrypted + 2 + client_entries_len,
+ ipos_encrypted_size - 2 - client_entries_len);
+
+ if (declen < 0) {
+ log_warn(LD_REND, "Could not decrypt introduction point string.");
+ tor_free(dec);
+ return -1;
+ }
+ if (fast_memcmpstart(dec, declen, "introduction-point ")) {
+ log_warn(LD_REND, "Decrypted introduction points don't "
+ "look like we could parse them.");
+ tor_free(dec);
+ continue;
+ }
+ *ipos_decrypted = dec;
+ *ipos_decrypted_size = declen;
+ return 0;
+ }
+ }
+ log_warn(LD_REND, "Could not decrypt introduction points. Please "
+ "check your authorization for this service!");
+ return -1;
+ } else if (ipos_encrypted[0] == (int)REND_STEALTH_AUTH) {
+ char *dec;
+ int declen;
+ if (ipos_encrypted_size < CIPHER_IV_LEN + 2) {
+ log_warn(LD_REND, "Size of encrypted introduction points is too "
+ "small.");
+ return -1;
+ }
+ dec = tor_malloc_zero(ipos_encrypted_size - CIPHER_IV_LEN - 1 + 1);
+
+ declen = crypto_cipher_decrypt_with_iv(descriptor_cookie, dec,
+ ipos_encrypted_size -
+ CIPHER_IV_LEN - 1,
+ ipos_encrypted + 1,
+ ipos_encrypted_size - 1);
+
+ if (declen < 0) {
+ log_warn(LD_REND, "Decrypting introduction points failed!");
+ tor_free(dec);
+ return -1;
+ }
+ *ipos_decrypted = dec;
+ *ipos_decrypted_size = declen;
+ return 0;
+ } else {
+ log_warn(LD_REND, "Unknown authorization type number: %d",
+ ipos_encrypted[0]);
+ return -1;
+ }
+}
+
+/** Parse the encoded introduction points in <b>intro_points_encoded</b> of
+ * length <b>intro_points_encoded_size</b> and write the result to the
+ * descriptor in <b>parsed</b>; return the number of successfully parsed
+ * introduction points or -1 in case of a failure. */
+int
+rend_parse_introduction_points(rend_service_descriptor_t *parsed,
+ const char *intro_points_encoded,
+ size_t intro_points_encoded_size)
+{
+ const char *current_ipo, *end_of_intro_points;
+ smartlist_t *tokens = NULL;
+ directory_token_t *tok;
+ rend_intro_point_t *intro;
+ extend_info_t *info;
+ int result, num_ok=1;
+ memarea_t *area = NULL;
+ tor_assert(parsed);
+ /** Function may only be invoked once. */
+ tor_assert(!parsed->intro_nodes);
+ if (!intro_points_encoded || intro_points_encoded_size == 0) {
+ log_warn(LD_REND, "Empty or zero size introduction point list");
+ goto err;
+ }
+ /* Consider one intro point after the other. */
+ current_ipo = intro_points_encoded;
+ end_of_intro_points = intro_points_encoded + intro_points_encoded_size;
+ tokens = smartlist_new();
+ parsed->intro_nodes = smartlist_new();
+ area = memarea_new();
+
+ while (!fast_memcmpstart(current_ipo, end_of_intro_points-current_ipo,
+ "introduction-point ")) {
+ /* Determine end of string. */
+ const char *eos = tor_memstr(current_ipo, end_of_intro_points-current_ipo,
+ "\nintroduction-point ");
+ if (!eos)
+ eos = end_of_intro_points;
+ else
+ eos = eos+1;
+ tor_assert(eos <= intro_points_encoded+intro_points_encoded_size);
+ /* Free tokens and clear token list. */
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_clear(tokens);
+ memarea_clear(area);
+ /* Tokenize string. */
+ if (tokenize_string(area, current_ipo, eos, tokens, ipo_token_table, 0)) {
+ log_warn(LD_REND, "Error tokenizing introduction point");
+ goto err;
+ }
+ /* Advance to next introduction point, if available. */
+ current_ipo = eos;
+ /* Check minimum allowed length of introduction point. */
+ if (smartlist_len(tokens) < 5) {
+ log_warn(LD_REND, "Impossibly short introduction point.");
+ goto err;
+ }
+ /* Allocate new intro point and extend info. */
+ intro = tor_malloc_zero(sizeof(rend_intro_point_t));
+ info = intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
+ /* Parse identifier. */
+ tok = find_by_keyword(tokens, R_IPO_IDENTIFIER);
+ if (base32_decode(info->identity_digest, DIGEST_LEN,
+ tok->args[0], REND_INTRO_POINT_ID_LEN_BASE32) < 0) {
+ log_warn(LD_REND, "Identity digest contains illegal characters: %s",
+ tok->args[0]);
+ rend_intro_point_free(intro);
+ goto err;
+ }
+ /* Write identifier to nickname. */
+ info->nickname[0] = '$';
+ base16_encode(info->nickname + 1, sizeof(info->nickname) - 1,
+ info->identity_digest, DIGEST_LEN);
+ /* Parse IP address. */
+ tok = find_by_keyword(tokens, R_IPO_IP_ADDRESS);
+ if (tor_addr_parse(&info->addr, tok->args[0])<0) {
+ log_warn(LD_REND, "Could not parse introduction point address.");
+ rend_intro_point_free(intro);
+ goto err;
+ }
+ if (tor_addr_family(&info->addr) != AF_INET) {
+ log_warn(LD_REND, "Introduction point address was not ipv4.");
+ rend_intro_point_free(intro);
+ goto err;
+ }
+
+ /* Parse onion port. */
+ tok = find_by_keyword(tokens, R_IPO_ONION_PORT);
+ info->port = (uint16_t) tor_parse_long(tok->args[0],10,1,65535,
+ &num_ok,NULL);
+ if (!info->port || !num_ok) {
+ log_warn(LD_REND, "Introduction point onion port %s is invalid",
+ escaped(tok->args[0]));
+ rend_intro_point_free(intro);
+ goto err;
+ }
+ /* Parse onion key. */
+ tok = find_by_keyword(tokens, R_IPO_ONION_KEY);
+ if (!crypto_pk_public_exponent_ok(tok->key)) {
+ log_warn(LD_REND,
+ "Introduction point's onion key had invalid exponent.");
+ rend_intro_point_free(intro);
+ goto err;
+ }
+ info->onion_key = tok->key;
+ tok->key = NULL; /* Prevent free */
+ /* Parse service key. */
+ tok = find_by_keyword(tokens, R_IPO_SERVICE_KEY);
+ if (!crypto_pk_public_exponent_ok(tok->key)) {
+ log_warn(LD_REND,
+ "Introduction point key had invalid exponent.");
+ rend_intro_point_free(intro);
+ goto err;
+ }
+ intro->intro_key = tok->key;
+ tok->key = NULL; /* Prevent free */
+ /* Add extend info to list of introduction points. */
+ smartlist_add(parsed->intro_nodes, intro);
+ }
+ result = smartlist_len(parsed->intro_nodes);
+ goto done;
+
+ err:
+ result = -1;
+
+ done:
+ /* Free tokens and clear token list. */
+ if (tokens) {
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ }
+ if (area)
+ memarea_drop_all(area);
+
+ return result;
+}
+
+/** Parse the content of a client_key file in <b>ckstr</b> and add
+ * rend_authorized_client_t's for each parsed client to
+ * <b>parsed_clients</b>. Return the number of parsed clients as result
+ * or -1 for failure. */
+int
+rend_parse_client_keys(strmap_t *parsed_clients, const char *ckstr)
+{
+ int result = -1;
+ smartlist_t *tokens;
+ directory_token_t *tok;
+ const char *current_entry = NULL;
+ memarea_t *area = NULL;
+ char *err_msg = NULL;
+ if (!ckstr || strlen(ckstr) == 0)
+ return -1;
+ tokens = smartlist_new();
+ /* Begin parsing with first entry, skipping comments or whitespace at the
+ * beginning. */
+ area = memarea_new();
+ current_entry = eat_whitespace(ckstr);
+ while (!strcmpstart(current_entry, "client-name ")) {
+ rend_authorized_client_t *parsed_entry;
+ /* Determine end of string. */
+ const char *eos = strstr(current_entry, "\nclient-name ");
+ if (!eos)
+ eos = current_entry + strlen(current_entry);
+ else
+ eos = eos + 1;
+ /* Free tokens and clear token list. */
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_clear(tokens);
+ memarea_clear(area);
+ /* Tokenize string. */
+ if (tokenize_string(area, current_entry, eos, tokens,
+ client_keys_token_table, 0)) {
+ log_warn(LD_REND, "Error tokenizing client keys file.");
+ goto err;
+ }
+ /* Advance to next entry, if available. */
+ current_entry = eos;
+ /* Check minimum allowed length of token list. */
+ if (smartlist_len(tokens) < 2) {
+ log_warn(LD_REND, "Impossibly short client key entry.");
+ goto err;
+ }
+ /* Parse client name. */
+ tok = find_by_keyword(tokens, C_CLIENT_NAME);
+ tor_assert(tok == smartlist_get(tokens, 0));
+ tor_assert(tok->n_args == 1);
+
+ if (!rend_valid_client_name(tok->args[0])) {
+ log_warn(LD_CONFIG, "Illegal client name: %s. (Length must be "
+ "between 1 and %d, and valid characters are "
+ "[A-Za-z0-9+-_].)", tok->args[0], REND_CLIENTNAME_MAX_LEN);
+ goto err;
+ }
+ /* Check if client name is duplicate. */
+ if (strmap_get(parsed_clients, tok->args[0])) {
+ log_warn(LD_CONFIG, "HiddenServiceAuthorizeClient contains a "
+ "duplicate client name: '%s'. Ignoring.", tok->args[0]);
+ goto err;
+ }
+ parsed_entry = tor_malloc_zero(sizeof(rend_authorized_client_t));
+ parsed_entry->client_name = tor_strdup(tok->args[0]);
+ strmap_set(parsed_clients, parsed_entry->client_name, parsed_entry);
+ /* Parse client key. */
+ tok = find_opt_by_keyword(tokens, C_CLIENT_KEY);
+ if (tok) {
+ parsed_entry->client_key = tok->key;
+ tok->key = NULL; /* Prevent free */
+ }
+
+ /* Parse descriptor cookie. */
+ tok = find_by_keyword(tokens, C_DESCRIPTOR_COOKIE);
+ tor_assert(tok->n_args == 1);
+ if (rend_auth_decode_cookie(tok->args[0], parsed_entry->descriptor_cookie,
+ NULL, &err_msg) < 0) {
+ tor_assert(err_msg);
+ log_warn(LD_REND, "%s", err_msg);
+ tor_free(err_msg);
+ goto err;
+ }
+ }
+ result = strmap_size(parsed_clients);
+ goto done;
+ err:
+ result = -1;
+ done:
+ /* Free tokens and clear token list. */
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ if (area)
+ memarea_drop_all(area);
+ return result;
+}
diff --git a/src/feature/rend/rendparse.h b/src/feature/rend/rendparse.h
new file mode 100644
index 000000000..337d3e3b1
--- /dev/null
+++ b/src/feature/rend/rendparse.h
@@ -0,0 +1,32 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file rend_parse.h
+ * \brief Header file for rend_parse.c.
+ **/
+
+#ifndef TOR_REND_PARSE_H
+#define TOR_REND_PARSE_H
+
+int rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
+ char *desc_id_out,
+ char **intro_points_encrypted_out,
+ size_t *intro_points_encrypted_size_out,
+ size_t *encoded_size_out,
+ const char **next_out, const char *desc,
+ int as_hsdir);
+int rend_decrypt_introduction_points(char **ipos_decrypted,
+ size_t *ipos_decrypted_size,
+ const char *descriptor_cookie,
+ const char *ipos_encrypted,
+ size_t ipos_encrypted_size);
+int rend_parse_introduction_points(rend_service_descriptor_t *parsed,
+ const char *intro_points_encoded,
+ size_t intro_points_encoded_size);
+int rend_parse_client_keys(strmap_t *parsed_clients, const char *str);
+
+#endif
diff --git a/src/feature/rend/rendservice.c b/src/feature/rend/rendservice.c
index bcd123f37..e70694b78 100644
--- a/src/feature/rend/rendservice.c
+++ b/src/feature/rend/rendservice.c
@@ -35,6 +35,7 @@
#include "feature/nodelist/routerset.h"
#include "feature/rend/rendclient.h"
#include "feature/rend/rendcommon.h"
+#include "feature/rend/rendparse.h"
#include "feature/rend/rendservice.h"
#include "feature/stats/predict_ports.h"
#include "lib/crypt_ops/crypto_dh.h"
diff --git a/src/test/fuzz/fuzz_hsdescv2.c b/src/test/fuzz/fuzz_hsdescv2.c
index 1963114ca..667b58b3a 100644
--- a/src/test/fuzz/fuzz_hsdescv2.c
+++ b/src/test/fuzz/fuzz_hsdescv2.c
@@ -1,10 +1,9 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
#include "core/or/or.h"
-#include "feature/dirparse/routerparse.h"
#include "feature/dirparse/unparseable.h"
#include "feature/rend/rendcommon.h"
+#include "feature/rend/rendparse.h"
#include "lib/crypt_ops/crypto_ed25519.h"
#include "test/fuzz/fuzzing.h"
diff --git a/src/test/fuzz/fuzz_iptsv2.c b/src/test/fuzz/fuzz_iptsv2.c
index c2147c8ba..265677eeb 100644
--- a/src/test/fuzz/fuzz_iptsv2.c
+++ b/src/test/fuzz/fuzz_iptsv2.c
@@ -1,10 +1,10 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
+
#include "core/or/or.h"
-#include "feature/dirparse/routerparse.h"
#include "feature/dirparse/unparseable.h"
#include "feature/rend/rendcommon.h"
+#include "feature/rend/rendparse.h"
#include "lib/crypt_ops/crypto_ed25519.h"
#include "feature/rend/rend_service_descriptor_st.h"
diff --git a/src/test/test.c b/src/test/test.c
index 2b2f536e2..2198de797 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -45,6 +45,7 @@
#include "core/or/connection_edge.h"
#include "feature/rend/rendcommon.h"
#include "feature/rend/rendcache.h"
+#include "feature/rend/rendparse.h"
#include "test/test.h"
#include "core/mainloop/mainloop.h"
#include "lib/memarea/memarea.h"
1
0

[tor/master] Split the authority-cert and signature/hash code from routerparse
by nickm@torproject.org 12 Oct '18
by nickm@torproject.org 12 Oct '18
12 Oct '18
commit 430ca38f706be08e0df0c1cf025786cb25e46172
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Sep 30 23:08:25 2018 -0500
Split the authority-cert and signature/hash code from routerparse
---
src/core/include.am | 7 +
src/feature/dirauth/dirvote.c | 1 +
src/feature/dirparse/authcert_members.i | 13 +
src/feature/dirparse/authcert_parse.c | 207 +++++++++++++
src/feature/dirparse/authcert_parse.h | 18 ++
src/feature/dirparse/routerparse.c | 498 +-------------------------------
src/feature/dirparse/routerparse.h | 17 +-
src/feature/dirparse/sigcommon.c | 185 ++++++++++++
src/feature/dirparse/sigcommon.h | 48 +++
src/feature/dirparse/signing.c | 98 +++++++
src/feature/dirparse/signing.h | 23 ++
src/feature/dirparse/unparseable.h | 12 +
src/feature/nodelist/authcert.c | 2 +-
src/feature/relay/router.c | 2 +
src/feature/rend/rendcommon.c | 1 +
src/test/fuzz/fuzz_consensus.c | 3 +-
src/test/fuzz/fuzz_descriptor.c | 3 +-
src/test/fuzz/fuzz_extrainfo.c | 3 +-
src/test/test_dir.c | 1 +
src/test/test_dir_common.c | 1 +
src/test/test_dir_handle_get.c | 1 +
src/test/test_routerlist.c | 1 +
src/test/test_shared_random.c | 1 +
23 files changed, 631 insertions(+), 515 deletions(-)
diff --git a/src/core/include.am b/src/core/include.am
index c1f63c751..a295e6ca3 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -73,8 +73,11 @@ LIBTOR_APP_A_SOURCES = \
src/feature/dircommon/directory.c \
src/feature/dircommon/fp_pair.c \
src/feature/dircommon/voting_schedule.c \
+ src/feature/dirparse/authcert_parse.c \
src/feature/dirparse/parsecommon.c \
src/feature/dirparse/routerparse.c \
+ src/feature/dirparse/sigcommon.c \
+ src/feature/dirparse/signing.c \
src/feature/dirparse/unparseable.c \
src/feature/hibernate/hibernate.c \
src/feature/hs/hs_cache.c \
@@ -288,8 +291,12 @@ noinst_HEADERS += \
src/feature/dircommon/fp_pair.h \
src/feature/dircommon/vote_timing_st.h \
src/feature/dircommon/voting_schedule.h \
+ src/feature/dirparse/authcert_members.i \
+ src/feature/dirparse/authcert_parse.h \
src/feature/dirparse/parsecommon.h \
src/feature/dirparse/routerparse.h \
+ src/feature/dirparse/sigcommon.h \
+ src/feature/dirparse/signing.h \
src/feature/dirparse/unparseable.h \
src/feature/hibernate/hibernate.h \
src/feature/hs/hs_cache.h \
diff --git a/src/feature/dirauth/dirvote.c b/src/feature/dirauth/dirvote.c
index badb6dc4e..f5546abd4 100644
--- a/src/feature/dirauth/dirvote.c
+++ b/src/feature/dirauth/dirvote.c
@@ -28,6 +28,7 @@
#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/signing.h"
#include "feature/nodelist/fmt_routerstatus.h"
#include "feature/client/entrynodes.h" /* needed for guardfraction methods */
#include "feature/nodelist/torcert.h"
diff --git a/src/feature/dirparse/authcert_members.i b/src/feature/dirparse/authcert_members.i
new file mode 100644
index 000000000..08cffca97
--- /dev/null
+++ b/src/feature/dirparse/authcert_members.i
@@ -0,0 +1,13 @@
+/*
+ * List of tokens common to V3 authority certificates and V3 consensuses.
+ */
+ T1("dir-key-certificate-version", K_DIR_KEY_CERTIFICATE_VERSION,
+ GE(1), NO_OBJ ),
+ T1("dir-identity-key", K_DIR_IDENTITY_KEY, NO_ARGS, NEED_KEY ),
+ T1("dir-key-published",K_DIR_KEY_PUBLISHED, CONCAT_ARGS, NO_OBJ),
+ T1("dir-key-expires", K_DIR_KEY_EXPIRES, CONCAT_ARGS, NO_OBJ),
+ T1("dir-signing-key", K_DIR_SIGNING_KEY, NO_ARGS, NEED_KEY ),
+ T1("dir-key-crosscert", K_DIR_KEY_CROSSCERT, NO_ARGS, NEED_OBJ ),
+ T1("dir-key-certification", K_DIR_KEY_CERTIFICATION,
+ NO_ARGS, NEED_OBJ),
+ T01("dir-address", K_DIR_ADDRESS, GE(1), NO_OBJ),
diff --git a/src/feature/dirparse/authcert_parse.c b/src/feature/dirparse/authcert_parse.c
new file mode 100644
index 000000000..2ba46bb8f
--- /dev/null
+++ b/src/feature/dirparse/authcert_parse.c
@@ -0,0 +1,207 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "core/or/or.h"
+#include "feature/dirparse/authcert_parse.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/sigcommon.h"
+#include "feature/dirparse/unparseable.h"
+#include "feature/nodelist/authcert.h"
+#include "lib/memarea/memarea.h"
+
+#include "feature/nodelist/authority_cert_st.h"
+
+/** List of tokens recognized in V3 authority certificates. */
+static token_rule_t dir_key_certificate_table[] = {
+#include "feature/dirparse/authcert_members.i"
+ T1("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
+ END_OF_TABLE
+};
+
+/** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to
+ * the first character after the certificate. */
+authority_cert_t *
+authority_cert_parse_from_string(const char *s, const char **end_of_string)
+{
+ /** Reject any certificate at least this big; it is probably an overflow, an
+ * attack, a bug, or some other nonsense. */
+#define MAX_CERT_SIZE (128*1024)
+
+ authority_cert_t *cert = NULL, *old_cert;
+ smartlist_t *tokens = NULL;
+ char digest[DIGEST_LEN];
+ directory_token_t *tok;
+ char fp_declared[DIGEST_LEN];
+ char *eos;
+ size_t len;
+ int found;
+ memarea_t *area = NULL;
+ const char *s_dup = s;
+
+ s = eat_whitespace(s);
+ eos = strstr(s, "\ndir-key-certification");
+ if (! eos) {
+ log_warn(LD_DIR, "No signature found on key certificate");
+ return NULL;
+ }
+ eos = strstr(eos, "\n-----END SIGNATURE-----\n");
+ if (! eos) {
+ log_warn(LD_DIR, "No end-of-signature found on key certificate");
+ return NULL;
+ }
+ eos = strchr(eos+2, '\n');
+ tor_assert(eos);
+ ++eos;
+ len = eos - s;
+
+ if (len > MAX_CERT_SIZE) {
+ log_warn(LD_DIR, "Certificate is far too big (at %lu bytes long); "
+ "rejecting", (unsigned long)len);
+ return NULL;
+ }
+
+ tokens = smartlist_new();
+ area = memarea_new();
+ if (tokenize_string(area,s, eos, tokens, dir_key_certificate_table, 0) < 0) {
+ log_warn(LD_DIR, "Error tokenizing key certificate");
+ goto err;
+ }
+ if (router_get_hash_impl(s, strlen(s), digest, "dir-key-certificate-version",
+ "\ndir-key-certification", '\n', DIGEST_SHA1) < 0)
+ goto err;
+ tok = smartlist_get(tokens, 0);
+ if (tok->tp != K_DIR_KEY_CERTIFICATE_VERSION || strcmp(tok->args[0], "3")) {
+ log_warn(LD_DIR,
+ "Key certificate does not begin with a recognized version (3).");
+ goto err;
+ }
+
+ cert = tor_malloc_zero(sizeof(authority_cert_t));
+ memcpy(cert->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
+
+ tok = find_by_keyword(tokens, K_DIR_SIGNING_KEY);
+ tor_assert(tok->key);
+ cert->signing_key = tok->key;
+ tok->key = NULL;
+ if (crypto_pk_get_digest(cert->signing_key, cert->signing_key_digest))
+ goto err;
+
+ tok = find_by_keyword(tokens, K_DIR_IDENTITY_KEY);
+ tor_assert(tok->key);
+ cert->identity_key = tok->key;
+ tok->key = NULL;
+
+ tok = find_by_keyword(tokens, K_FINGERPRINT);
+ tor_assert(tok->n_args);
+ if (base16_decode(fp_declared, DIGEST_LEN, tok->args[0],
+ strlen(tok->args[0])) != DIGEST_LEN) {
+ log_warn(LD_DIR, "Couldn't decode key certificate fingerprint %s",
+ escaped(tok->args[0]));
+ goto err;
+ }
+
+ if (crypto_pk_get_digest(cert->identity_key,
+ cert->cache_info.identity_digest))
+ goto err;
+
+ if (tor_memneq(cert->cache_info.identity_digest, fp_declared, DIGEST_LEN)) {
+ log_warn(LD_DIR, "Digest of certificate key didn't match declared "
+ "fingerprint");
+ goto err;
+ }
+
+ tok = find_opt_by_keyword(tokens, K_DIR_ADDRESS);
+ if (tok) {
+ struct in_addr in;
+ char *address = NULL;
+ tor_assert(tok->n_args);
+ /* XXX++ use some tor_addr parse function below instead. -RD */
+ if (tor_addr_port_split(LOG_WARN, tok->args[0], &address,
+ &cert->dir_port) < 0 ||
+ tor_inet_aton(address, &in) == 0) {
+ log_warn(LD_DIR, "Couldn't parse dir-address in certificate");
+ tor_free(address);
+ goto err;
+ }
+ cert->addr = ntohl(in.s_addr);
+ tor_free(address);
+ }
+
+ tok = find_by_keyword(tokens, K_DIR_KEY_PUBLISHED);
+ if (parse_iso_time(tok->args[0], &cert->cache_info.published_on) < 0) {
+ goto err;
+ }
+ tok = find_by_keyword(tokens, K_DIR_KEY_EXPIRES);
+ if (parse_iso_time(tok->args[0], &cert->expires) < 0) {
+ goto err;
+ }
+
+ tok = smartlist_get(tokens, smartlist_len(tokens)-1);
+ if (tok->tp != K_DIR_KEY_CERTIFICATION) {
+ log_warn(LD_DIR, "Certificate didn't end with dir-key-certification.");
+ goto err;
+ }
+
+ /* If we already have this cert, don't bother checking the signature. */
+ old_cert = authority_cert_get_by_digests(
+ cert->cache_info.identity_digest,
+ cert->signing_key_digest);
+ found = 0;
+ if (old_cert) {
+ /* XXXX We could just compare signed_descriptor_digest, but that wouldn't
+ * buy us much. */
+ if (old_cert->cache_info.signed_descriptor_len == len &&
+ old_cert->cache_info.signed_descriptor_body &&
+ tor_memeq(s, old_cert->cache_info.signed_descriptor_body, len)) {
+ log_debug(LD_DIR, "We already checked the signature on this "
+ "certificate; no need to do so again.");
+ found = 1;
+ }
+ }
+ if (!found) {
+ if (check_signature_token(digest, DIGEST_LEN, tok, cert->identity_key, 0,
+ "key certificate")) {
+ goto err;
+ }
+
+ tok = find_by_keyword(tokens, K_DIR_KEY_CROSSCERT);
+ if (check_signature_token(cert->cache_info.identity_digest,
+ DIGEST_LEN,
+ tok,
+ cert->signing_key,
+ CST_NO_CHECK_OBJTYPE,
+ "key cross-certification")) {
+ goto err;
+ }
+ }
+
+ cert->cache_info.signed_descriptor_len = len;
+ cert->cache_info.signed_descriptor_body = tor_malloc(len+1);
+ memcpy(cert->cache_info.signed_descriptor_body, s, len);
+ cert->cache_info.signed_descriptor_body[len] = 0;
+ cert->cache_info.saved_location = SAVED_NOWHERE;
+
+ if (end_of_string) {
+ *end_of_string = eat_whitespace(eos);
+ }
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ if (area) {
+ DUMP_AREA(area, "authority cert");
+ memarea_drop_all(area);
+ }
+ return cert;
+ err:
+ dump_desc(s_dup, "authority cert");
+ authority_cert_free(cert);
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ if (area) {
+ DUMP_AREA(area, "authority cert");
+ memarea_drop_all(area);
+ }
+ return NULL;
+}
diff --git a/src/feature/dirparse/authcert_parse.h b/src/feature/dirparse/authcert_parse.h
new file mode 100644
index 000000000..f63525e04
--- /dev/null
+++ b/src/feature/dirparse/authcert_parse.h
@@ -0,0 +1,18 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file authcert_parse.h
+ * \brief Header file for authcert_parse.c.
+ **/
+
+#ifndef TOR_AUTHCERT_PARSE_H
+#define TOR_AUTHCERT_PARSE_H
+
+authority_cert_t *authority_cert_parse_from_string(const char *s,
+ const char **end_of_string);
+
+#endif /* !defined(TOR_AUTHCERT_PARSE_H) */
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index 5ff5fdf23..be1f75e56 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -48,9 +48,6 @@
* <li>authority key certificates (managed from routerlist.c)
* <li>hidden service descriptors (managed from rendcommon.c and rendcache.c)
* </ul>
- *
- * For no terribly good reason, the functions to <i>generate</i> signatures on
- * the above directory objects are also in this module.
**/
#define ROUTERPARSE_PRIVATE
@@ -82,6 +79,8 @@
#include "lib/crypt_ops/crypto_util.h"
#include "lib/memarea/memarea.h"
#include "lib/sandbox/sandbox.h"
+#include "feature/dirparse/authcert_parse.h"
+#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/dirauth/dirvote.h"
@@ -210,26 +209,6 @@ static token_rule_t rtrstatus_token_table[] = {
END_OF_TABLE
};
-/** List of tokens common to V3 authority certificates and V3 consensuses. */
-#define CERTIFICATE_MEMBERS \
- T1("dir-key-certificate-version", K_DIR_KEY_CERTIFICATE_VERSION, \
- GE(1), NO_OBJ ), \
- T1("dir-identity-key", K_DIR_IDENTITY_KEY, NO_ARGS, NEED_KEY ),\
- T1("dir-key-published",K_DIR_KEY_PUBLISHED, CONCAT_ARGS, NO_OBJ), \
- T1("dir-key-expires", K_DIR_KEY_EXPIRES, CONCAT_ARGS, NO_OBJ), \
- T1("dir-signing-key", K_DIR_SIGNING_KEY, NO_ARGS, NEED_KEY ),\
- T1("dir-key-crosscert", K_DIR_KEY_CROSSCERT, NO_ARGS, NEED_OBJ ),\
- T1("dir-key-certification", K_DIR_KEY_CERTIFICATION, \
- NO_ARGS, NEED_OBJ), \
- T01("dir-address", K_DIR_ADDRESS, GE(1), NO_OBJ),
-
-/** List of tokens recognized in V3 authority certificates. */
-static token_rule_t dir_key_certificate_table[] = {
- CERTIFICATE_MEMBERS
- T1("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
- END_OF_TABLE
-};
-
/** List of tokens recognized in rendezvous service descriptors */
static token_rule_t desc_token_table[] = {
T1_START("rendezvous-service-descriptor", R_RENDEZVOUS_SERVICE_DESCRIPTOR,
@@ -292,7 +271,7 @@ static token_rule_t networkstatus_token_table[] = {
T01("required-relay-protocols", K_REQUIRED_RELAY_PROTOCOLS,
CONCAT_ARGS, NO_OBJ ),
- CERTIFICATE_MEMBERS
+#include "feature/dirparse/authcert_members.i"
T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
T1( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
@@ -385,41 +364,8 @@ static addr_policy_t *router_parse_addr_policy(directory_token_t *tok,
unsigned fmt_flags);
static addr_policy_t *router_parse_addr_policy_private(directory_token_t *tok);
-static int router_get_hash_impl_helper(const char *s, size_t s_len,
- const char *start_str,
- const char *end_str, char end_c,
- int log_severity,
- const char **start_out, const char **end_out);
-static int router_get_hash_impl(const char *s, size_t s_len, char *digest,
- const char *start_str, const char *end_str,
- char end_char,
- digest_algorithm_t alg);
-static int router_get_hashes_impl(const char *s, size_t s_len,
- common_digests_t *digests,
- const char *start_str, const char *end_str,
- char end_char);
static smartlist_t *find_all_exitpolicy(smartlist_t *s);
-#define CST_NO_CHECK_OBJTYPE (1<<0)
-static int check_signature_token(const char *digest,
- ssize_t digest_len,
- directory_token_t *tok,
- crypto_pk_t *pkey,
- int flags,
- const char *doctype);
-
-#undef DEBUG_AREA_ALLOC
-
-#ifdef DEBUG_AREA_ALLOC
-#define DUMP_AREA(a,name) STMT_BEGIN \
- size_t alloc=0, used=0; \
- memarea_get_stats((a),&alloc,&used); \
- log_debug(LD_MM, "Area for %s has %lu allocated; using %lu.", \
- name, (unsigned long)alloc, (unsigned long)used); \
- STMT_END
-#else /* !(defined(DEBUG_AREA_ALLOC)) */
-#define DUMP_AREA(a,name) STMT_NIL
-#endif /* defined(DEBUG_AREA_ALLOC) */
/** Set <b>digest</b> to the SHA-1 digest of the hash of the directory in
* <b>s</b>. Return 0 on success, -1 on failure.
@@ -498,149 +444,6 @@ router_get_extrainfo_hash(const char *s, size_t s_len, char *digest)
"\nrouter-signature",'\n', DIGEST_SHA1);
}
-/** Helper: used to generate signatures for routers, directories and
- * network-status objects. Given a <b>digest_len</b>-byte digest in
- * <b>digest</b> and a secret <b>private_key</b>, generate an PKCS1-padded
- * signature, BASE64-encode it, surround it with -----BEGIN/END----- pairs,
- * and return the new signature on success or NULL on failure.
- */
-char *
-router_get_dirobj_signature(const char *digest,
- size_t digest_len,
- const crypto_pk_t *private_key)
-{
- char *signature;
- size_t i, keysize;
- int siglen;
- char *buf = NULL;
- size_t buf_len;
- /* overestimate of BEGIN/END lines total len. */
-#define BEGIN_END_OVERHEAD_LEN 64
-
- keysize = crypto_pk_keysize(private_key);
- signature = tor_malloc(keysize);
- siglen = crypto_pk_private_sign(private_key, signature, keysize,
- digest, digest_len);
- if (siglen < 0) {
- log_warn(LD_BUG,"Couldn't sign digest.");
- goto err;
- }
-
- /* The *2 here is a ridiculous overestimate of base-64 overhead. */
- buf_len = (siglen * 2) + BEGIN_END_OVERHEAD_LEN;
- buf = tor_malloc(buf_len);
-
- if (strlcpy(buf, "-----BEGIN SIGNATURE-----\n", buf_len) >= buf_len)
- goto truncated;
-
- i = strlen(buf);
- if (base64_encode(buf+i, buf_len-i, signature, siglen,
- BASE64_ENCODE_MULTILINE) < 0) {
- log_warn(LD_BUG,"couldn't base64-encode signature");
- goto err;
- }
-
- if (strlcat(buf, "-----END SIGNATURE-----\n", buf_len) >= buf_len)
- goto truncated;
-
- tor_free(signature);
- return buf;
-
- truncated:
- log_warn(LD_BUG,"tried to exceed string length.");
- err:
- tor_free(signature);
- tor_free(buf);
- return NULL;
-}
-
-/** Helper: used to generate signatures for routers, directories and
- * network-status objects. Given a digest in <b>digest</b> and a secret
- * <b>private_key</b>, generate a PKCS1-padded signature, BASE64-encode it,
- * surround it with -----BEGIN/END----- pairs, and write it to the
- * <b>buf_len</b>-byte buffer at <b>buf</b>. Return 0 on success, -1 on
- * failure.
- */
-int
-router_append_dirobj_signature(char *buf, size_t buf_len, const char *digest,
- size_t digest_len, crypto_pk_t *private_key)
-{
- size_t sig_len, s_len;
- char *sig = router_get_dirobj_signature(digest, digest_len, private_key);
- if (!sig) {
- log_warn(LD_BUG, "No signature generated");
- return -1;
- }
- sig_len = strlen(sig);
- s_len = strlen(buf);
- if (sig_len + s_len + 1 > buf_len) {
- log_warn(LD_BUG, "Not enough room for signature");
- tor_free(sig);
- return -1;
- }
- memcpy(buf+s_len, sig, sig_len+1);
- tor_free(sig);
- return 0;
-}
-
-MOCK_IMPL(STATIC int,
-signed_digest_equals, (const uint8_t *d1, const uint8_t *d2, size_t len))
-{
- return tor_memeq(d1, d2, len);
-}
-
-/** Check whether the object body of the token in <b>tok</b> has a good
- * signature for <b>digest</b> using key <b>pkey</b>.
- * If <b>CST_NO_CHECK_OBJTYPE</b> is set, do not check
- * the object type of the signature object. Use <b>doctype</b> as the type of
- * the document when generating log messages. Return 0 on success, negative
- * on failure.
- */
-static int
-check_signature_token(const char *digest,
- ssize_t digest_len,
- directory_token_t *tok,
- crypto_pk_t *pkey,
- int flags,
- const char *doctype)
-{
- char *signed_digest;
- size_t keysize;
- const int check_objtype = ! (flags & CST_NO_CHECK_OBJTYPE);
-
- tor_assert(pkey);
- tor_assert(tok);
- tor_assert(digest);
- tor_assert(doctype);
-
- if (check_objtype) {
- if (strcmp(tok->object_type, "SIGNATURE")) {
- log_warn(LD_DIR, "Bad object type on %s signature", doctype);
- return -1;
- }
- }
-
- keysize = crypto_pk_keysize(pkey);
- signed_digest = tor_malloc(keysize);
- if (crypto_pk_public_checksig(pkey, signed_digest, keysize,
- tok->object_body, tok->object_size)
- < digest_len) {
- log_warn(LD_DIR, "Error reading %s: invalid signature.", doctype);
- tor_free(signed_digest);
- return -1;
- }
- // log_debug(LD_DIR,"Signed %s hash starts %s", doctype,
- // hex_str(signed_digest,4));
- if (! signed_digest_equals((const uint8_t *)digest,
- (const uint8_t *)signed_digest, digest_len)) {
- log_warn(LD_DIR, "Error reading %s: signature does not match.", doctype);
- tor_free(signed_digest);
- return -1;
- }
- tor_free(signed_digest);
- return 0;
-}
-
/** Helper: move *<b>s_ptr</b> ahead to the next router, the next extra-info,
* or to the first of the annotations proceeding the next router or
* extra-info---whichever comes first. Set <b>is_extrainfo_out</b> to true if
@@ -1649,191 +1452,6 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
return extrainfo;
}
-/** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to
- * the first character after the certificate. */
-authority_cert_t *
-authority_cert_parse_from_string(const char *s, const char **end_of_string)
-{
- /** Reject any certificate at least this big; it is probably an overflow, an
- * attack, a bug, or some other nonsense. */
-#define MAX_CERT_SIZE (128*1024)
-
- authority_cert_t *cert = NULL, *old_cert;
- smartlist_t *tokens = NULL;
- char digest[DIGEST_LEN];
- directory_token_t *tok;
- char fp_declared[DIGEST_LEN];
- char *eos;
- size_t len;
- int found;
- memarea_t *area = NULL;
- const char *s_dup = s;
-
- s = eat_whitespace(s);
- eos = strstr(s, "\ndir-key-certification");
- if (! eos) {
- log_warn(LD_DIR, "No signature found on key certificate");
- return NULL;
- }
- eos = strstr(eos, "\n-----END SIGNATURE-----\n");
- if (! eos) {
- log_warn(LD_DIR, "No end-of-signature found on key certificate");
- return NULL;
- }
- eos = strchr(eos+2, '\n');
- tor_assert(eos);
- ++eos;
- len = eos - s;
-
- if (len > MAX_CERT_SIZE) {
- log_warn(LD_DIR, "Certificate is far too big (at %lu bytes long); "
- "rejecting", (unsigned long)len);
- return NULL;
- }
-
- tokens = smartlist_new();
- area = memarea_new();
- if (tokenize_string(area,s, eos, tokens, dir_key_certificate_table, 0) < 0) {
- log_warn(LD_DIR, "Error tokenizing key certificate");
- goto err;
- }
- if (router_get_hash_impl(s, strlen(s), digest, "dir-key-certificate-version",
- "\ndir-key-certification", '\n', DIGEST_SHA1) < 0)
- goto err;
- tok = smartlist_get(tokens, 0);
- if (tok->tp != K_DIR_KEY_CERTIFICATE_VERSION || strcmp(tok->args[0], "3")) {
- log_warn(LD_DIR,
- "Key certificate does not begin with a recognized version (3).");
- goto err;
- }
-
- cert = tor_malloc_zero(sizeof(authority_cert_t));
- memcpy(cert->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
-
- tok = find_by_keyword(tokens, K_DIR_SIGNING_KEY);
- tor_assert(tok->key);
- cert->signing_key = tok->key;
- tok->key = NULL;
- if (crypto_pk_get_digest(cert->signing_key, cert->signing_key_digest))
- goto err;
-
- tok = find_by_keyword(tokens, K_DIR_IDENTITY_KEY);
- tor_assert(tok->key);
- cert->identity_key = tok->key;
- tok->key = NULL;
-
- tok = find_by_keyword(tokens, K_FINGERPRINT);
- tor_assert(tok->n_args);
- if (base16_decode(fp_declared, DIGEST_LEN, tok->args[0],
- strlen(tok->args[0])) != DIGEST_LEN) {
- log_warn(LD_DIR, "Couldn't decode key certificate fingerprint %s",
- escaped(tok->args[0]));
- goto err;
- }
-
- if (crypto_pk_get_digest(cert->identity_key,
- cert->cache_info.identity_digest))
- goto err;
-
- if (tor_memneq(cert->cache_info.identity_digest, fp_declared, DIGEST_LEN)) {
- log_warn(LD_DIR, "Digest of certificate key didn't match declared "
- "fingerprint");
- goto err;
- }
-
- tok = find_opt_by_keyword(tokens, K_DIR_ADDRESS);
- if (tok) {
- struct in_addr in;
- char *address = NULL;
- tor_assert(tok->n_args);
- /* XXX++ use some tor_addr parse function below instead. -RD */
- if (tor_addr_port_split(LOG_WARN, tok->args[0], &address,
- &cert->dir_port) < 0 ||
- tor_inet_aton(address, &in) == 0) {
- log_warn(LD_DIR, "Couldn't parse dir-address in certificate");
- tor_free(address);
- goto err;
- }
- cert->addr = ntohl(in.s_addr);
- tor_free(address);
- }
-
- tok = find_by_keyword(tokens, K_DIR_KEY_PUBLISHED);
- if (parse_iso_time(tok->args[0], &cert->cache_info.published_on) < 0) {
- goto err;
- }
- tok = find_by_keyword(tokens, K_DIR_KEY_EXPIRES);
- if (parse_iso_time(tok->args[0], &cert->expires) < 0) {
- goto err;
- }
-
- tok = smartlist_get(tokens, smartlist_len(tokens)-1);
- if (tok->tp != K_DIR_KEY_CERTIFICATION) {
- log_warn(LD_DIR, "Certificate didn't end with dir-key-certification.");
- goto err;
- }
-
- /* If we already have this cert, don't bother checking the signature. */
- old_cert = authority_cert_get_by_digests(
- cert->cache_info.identity_digest,
- cert->signing_key_digest);
- found = 0;
- if (old_cert) {
- /* XXXX We could just compare signed_descriptor_digest, but that wouldn't
- * buy us much. */
- if (old_cert->cache_info.signed_descriptor_len == len &&
- old_cert->cache_info.signed_descriptor_body &&
- tor_memeq(s, old_cert->cache_info.signed_descriptor_body, len)) {
- log_debug(LD_DIR, "We already checked the signature on this "
- "certificate; no need to do so again.");
- found = 1;
- }
- }
- if (!found) {
- if (check_signature_token(digest, DIGEST_LEN, tok, cert->identity_key, 0,
- "key certificate")) {
- goto err;
- }
-
- tok = find_by_keyword(tokens, K_DIR_KEY_CROSSCERT);
- if (check_signature_token(cert->cache_info.identity_digest,
- DIGEST_LEN,
- tok,
- cert->signing_key,
- CST_NO_CHECK_OBJTYPE,
- "key cross-certification")) {
- goto err;
- }
- }
-
- cert->cache_info.signed_descriptor_len = len;
- cert->cache_info.signed_descriptor_body = tor_malloc(len+1);
- memcpy(cert->cache_info.signed_descriptor_body, s, len);
- cert->cache_info.signed_descriptor_body[len] = 0;
- cert->cache_info.saved_location = SAVED_NOWHERE;
-
- if (end_of_string) {
- *end_of_string = eat_whitespace(eos);
- }
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- if (area) {
- DUMP_AREA(area, "authority cert");
- memarea_drop_all(area);
- }
- return cert;
- err:
- dump_desc(s_dup, "authority cert");
- authority_cert_free(cert);
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- if (area) {
- DUMP_AREA(area, "authority cert");
- memarea_drop_all(area);
- }
- return NULL;
-}
-
/** Helper: given a string <b>s</b>, return the start of the next router-status
* object (starting with "r " at the start of a line). If none is found,
* return the start of the directory footer, or the next directory signature.
@@ -3857,116 +3475,6 @@ find_all_exitpolicy(smartlist_t *s)
return out;
}
-/** Helper function for <b>router_get_hash_impl</b>: given <b>s</b>,
- * <b>s_len</b>, <b>start_str</b>, <b>end_str</b>, and <b>end_c</b> with the
- * same semantics as in that function, set *<b>start_out</b> (inclusive) and
- * *<b>end_out</b> (exclusive) to the boundaries of the string to be hashed.
- *
- * Return 0 on success and -1 on failure.
- */
-static int
-router_get_hash_impl_helper(const char *s, size_t s_len,
- const char *start_str,
- const char *end_str, char end_c,
- int log_severity,
- const char **start_out, const char **end_out)
-{
- const char *start, *end;
- start = tor_memstr(s, s_len, start_str);
- if (!start) {
- log_fn(log_severity,LD_DIR,
- "couldn't find start of hashed material \"%s\"",start_str);
- return -1;
- }
- if (start != s && *(start-1) != '\n') {
- log_fn(log_severity,LD_DIR,
- "first occurrence of \"%s\" is not at the start of a line",
- start_str);
- return -1;
- }
- end = tor_memstr(start+strlen(start_str),
- s_len - (start-s) - strlen(start_str), end_str);
- if (!end) {
- log_fn(log_severity,LD_DIR,
- "couldn't find end of hashed material \"%s\"",end_str);
- return -1;
- }
- end = memchr(end+strlen(end_str), end_c, s_len - (end-s) - strlen(end_str));
- if (!end) {
- log_fn(log_severity,LD_DIR,
- "couldn't find EOL");
- return -1;
- }
- ++end;
-
- *start_out = start;
- *end_out = end;
- return 0;
-}
-
-/** Compute the digest of the substring of <b>s</b> taken from the first
- * occurrence of <b>start_str</b> through the first instance of c after the
- * first subsequent occurrence of <b>end_str</b>; store the 20-byte or 32-byte
- * result in <b>digest</b>; return 0 on success.
- *
- * If no such substring exists, return -1.
- */
-static int
-router_get_hash_impl(const char *s, size_t s_len, char *digest,
- const char *start_str,
- const char *end_str, char end_c,
- digest_algorithm_t alg)
-{
- const char *start=NULL, *end=NULL;
- if (router_get_hash_impl_helper(s,s_len,start_str,end_str,end_c,LOG_WARN,
- &start,&end)<0)
- return -1;
-
- return router_compute_hash_final(digest, start, end-start, alg);
-}
-
-/** Compute the digest of the <b>len</b>-byte directory object at
- * <b>start</b>, using <b>alg</b>. Store the result in <b>digest</b>, which
- * must be long enough to hold it. */
-MOCK_IMPL(STATIC int,
-router_compute_hash_final,(char *digest,
- const char *start, size_t len,
- digest_algorithm_t alg))
-{
- if (alg == DIGEST_SHA1) {
- if (crypto_digest(digest, start, len) < 0) {
- log_warn(LD_BUG,"couldn't compute digest");
- return -1;
- }
- } else {
- if (crypto_digest256(digest, start, len, alg) < 0) {
- log_warn(LD_BUG,"couldn't compute digest");
- return -1;
- }
- }
-
- return 0;
-}
-
-/** As router_get_hash_impl, but compute all hashes. */
-static int
-router_get_hashes_impl(const char *s, size_t s_len, common_digests_t *digests,
- const char *start_str,
- const char *end_str, char end_c)
-{
- const char *start=NULL, *end=NULL;
- if (router_get_hash_impl_helper(s,s_len,start_str,end_str,end_c,LOG_WARN,
- &start,&end)<0)
- return -1;
-
- if (crypto_common_digests(digests, start, end-start)) {
- log_warn(LD_BUG,"couldn't compute digests");
- return -1;
- }
-
- return 0;
-}
-
/** Assuming that s starts with a microdesc, return the start of the
* *NEXT* one. Return NULL on "not found." */
static const char *
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index 6f1af4016..4aba62653 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -26,14 +26,7 @@ int router_get_networkstatus_v3_signed_boundaries(const char *s,
int router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
const char *s);
int router_get_extrainfo_hash(const char *s, size_t s_len, char *digest);
-#define DIROBJ_MAX_SIG_LEN 256
-char *router_get_dirobj_signature(const char *digest,
- size_t digest_len,
- const crypto_pk_t *private_key);
-int router_append_dirobj_signature(char *buf, size_t buf_len,
- const char *digest,
- size_t digest_len,
- crypto_pk_t *private_key);
+
int router_parse_list_from_string(const char **s, const char *eos,
smartlist_t *dest,
saved_location_t saved_location,
@@ -70,8 +63,6 @@ smartlist_t *microdescs_parse_from_string(const char *s, const char *eos,
saved_location_t where,
smartlist_t *invalid_digests_out);
-authority_cert_t *authority_cert_parse_from_string(const char *s,
- const char **end_of_string);
int rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
char *desc_id_out,
char **intro_points_encrypted_out,
@@ -105,12 +96,6 @@ STATIC routerstatus_t *routerstatus_parse_entry_from_string(
vote_routerstatus_t *vote_rs,
int consensus_method,
consensus_flavor_t flav);
-MOCK_DECL(STATIC int, router_compute_hash_final,(char *digest,
- const char *start, size_t len,
- digest_algorithm_t alg));
-MOCK_DECL(STATIC int, signed_digest_equals,
- (const uint8_t *d1, const uint8_t *d2, size_t len));
-
STATIC void summarize_protover_flags(protover_summary_flags_t *out,
const char *protocols,
const char *version);
diff --git a/src/feature/dirparse/sigcommon.c b/src/feature/dirparse/sigcommon.c
new file mode 100644
index 000000000..28e6ff56e
--- /dev/null
+++ b/src/feature/dirparse/sigcommon.c
@@ -0,0 +1,185 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file sigcommon.c
+ * \brief Shared hashing, signing, and signature-checking code for directory
+ * objects.
+ **/
+
+#define SIGCOMMON_PRIVATE
+
+#include "core/or/or.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/sigcommon.h"
+
+/** Helper function for <b>router_get_hash_impl</b>: given <b>s</b>,
+ * <b>s_len</b>, <b>start_str</b>, <b>end_str</b>, and <b>end_c</b> with the
+ * same semantics as in that function, set *<b>start_out</b> (inclusive) and
+ * *<b>end_out</b> (exclusive) to the boundaries of the string to be hashed.
+ *
+ * Return 0 on success and -1 on failure.
+ */
+int
+router_get_hash_impl_helper(const char *s, size_t s_len,
+ const char *start_str,
+ const char *end_str, char end_c,
+ int log_severity,
+ const char **start_out, const char **end_out)
+{
+ const char *start, *end;
+ start = tor_memstr(s, s_len, start_str);
+ if (!start) {
+ log_fn(log_severity,LD_DIR,
+ "couldn't find start of hashed material \"%s\"",start_str);
+ return -1;
+ }
+ if (start != s && *(start-1) != '\n') {
+ log_fn(log_severity,LD_DIR,
+ "first occurrence of \"%s\" is not at the start of a line",
+ start_str);
+ return -1;
+ }
+ end = tor_memstr(start+strlen(start_str),
+ s_len - (start-s) - strlen(start_str), end_str);
+ if (!end) {
+ log_fn(log_severity,LD_DIR,
+ "couldn't find end of hashed material \"%s\"",end_str);
+ return -1;
+ }
+ end = memchr(end+strlen(end_str), end_c, s_len - (end-s) - strlen(end_str));
+ if (!end) {
+ log_fn(log_severity,LD_DIR,
+ "couldn't find EOL");
+ return -1;
+ }
+ ++end;
+
+ *start_out = start;
+ *end_out = end;
+ return 0;
+}
+
+/** Compute the digest of the substring of <b>s</b> taken from the first
+ * occurrence of <b>start_str</b> through the first instance of c after the
+ * first subsequent occurrence of <b>end_str</b>; store the 20-byte or 32-byte
+ * result in <b>digest</b>; return 0 on success.
+ *
+ * If no such substring exists, return -1.
+ */
+int
+router_get_hash_impl(const char *s, size_t s_len, char *digest,
+ const char *start_str,
+ const char *end_str, char end_c,
+ digest_algorithm_t alg)
+{
+ const char *start=NULL, *end=NULL;
+ if (router_get_hash_impl_helper(s,s_len,start_str,end_str,end_c,LOG_WARN,
+ &start,&end)<0)
+ return -1;
+
+ return router_compute_hash_final(digest, start, end-start, alg);
+}
+
+/** Compute the digest of the <b>len</b>-byte directory object at
+ * <b>start</b>, using <b>alg</b>. Store the result in <b>digest</b>, which
+ * must be long enough to hold it. */
+MOCK_IMPL(STATIC int,
+router_compute_hash_final,(char *digest,
+ const char *start, size_t len,
+ digest_algorithm_t alg))
+{
+ if (alg == DIGEST_SHA1) {
+ if (crypto_digest(digest, start, len) < 0) {
+ log_warn(LD_BUG,"couldn't compute digest");
+ return -1;
+ }
+ } else {
+ if (crypto_digest256(digest, start, len, alg) < 0) {
+ log_warn(LD_BUG,"couldn't compute digest");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/** As router_get_hash_impl, but compute all hashes. */
+int
+router_get_hashes_impl(const char *s, size_t s_len, common_digests_t *digests,
+ const char *start_str,
+ const char *end_str, char end_c)
+{
+ const char *start=NULL, *end=NULL;
+ if (router_get_hash_impl_helper(s,s_len,start_str,end_str,end_c,LOG_WARN,
+ &start,&end)<0)
+ return -1;
+
+ if (crypto_common_digests(digests, start, end-start)) {
+ log_warn(LD_BUG,"couldn't compute digests");
+ return -1;
+ }
+
+ return 0;
+}
+
+MOCK_IMPL(STATIC int,
+signed_digest_equals, (const uint8_t *d1, const uint8_t *d2, size_t len))
+{
+ return tor_memeq(d1, d2, len);
+}
+
+/** Check whether the object body of the token in <b>tok</b> has a good
+ * signature for <b>digest</b> using key <b>pkey</b>.
+ * If <b>CST_NO_CHECK_OBJTYPE</b> is set, do not check
+ * the object type of the signature object. Use <b>doctype</b> as the type of
+ * the document when generating log messages. Return 0 on success, negative
+ * on failure.
+ */
+int
+check_signature_token(const char *digest,
+ ssize_t digest_len,
+ directory_token_t *tok,
+ crypto_pk_t *pkey,
+ int flags,
+ const char *doctype)
+{
+ char *signed_digest;
+ size_t keysize;
+ const int check_objtype = ! (flags & CST_NO_CHECK_OBJTYPE);
+
+ tor_assert(pkey);
+ tor_assert(tok);
+ tor_assert(digest);
+ tor_assert(doctype);
+
+ if (check_objtype) {
+ if (strcmp(tok->object_type, "SIGNATURE")) {
+ log_warn(LD_DIR, "Bad object type on %s signature", doctype);
+ return -1;
+ }
+ }
+
+ keysize = crypto_pk_keysize(pkey);
+ signed_digest = tor_malloc(keysize);
+ if (crypto_pk_public_checksig(pkey, signed_digest, keysize,
+ tok->object_body, tok->object_size)
+ < digest_len) {
+ log_warn(LD_DIR, "Error reading %s: invalid signature.", doctype);
+ tor_free(signed_digest);
+ return -1;
+ }
+ // log_debug(LD_DIR,"Signed %s hash starts %s", doctype,
+ // hex_str(signed_digest,4));
+ if (! signed_digest_equals((const uint8_t *)digest,
+ (const uint8_t *)signed_digest, digest_len)) {
+ log_warn(LD_DIR, "Error reading %s: signature does not match.", doctype);
+ tor_free(signed_digest);
+ return -1;
+ }
+ tor_free(signed_digest);
+ return 0;
+}
diff --git a/src/feature/dirparse/sigcommon.h b/src/feature/dirparse/sigcommon.h
new file mode 100644
index 000000000..5f25817cd
--- /dev/null
+++ b/src/feature/dirparse/sigcommon.h
@@ -0,0 +1,48 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file sigcommon.h
+ * \brief Header file for sigcommon.c.
+ **/
+
+#ifndef TOR_SIGCOMMON_H
+#define TOR_SIGCOMMON_H
+
+/* TODO: Rename all of these functions */
+int router_get_hash_impl(const char *s, size_t s_len, char *digest,
+ const char *start_str, const char *end_str,
+ char end_char,
+ digest_algorithm_t alg);
+
+#define CST_NO_CHECK_OBJTYPE (1<<0)
+struct directory_token_t;
+int check_signature_token(const char *digest,
+ ssize_t digest_len,
+ struct directory_token_t *tok,
+ crypto_pk_t *pkey,
+ int flags,
+ const char *doctype);
+
+int router_get_hash_impl_helper(const char *s, size_t s_len,
+ const char *start_str,
+ const char *end_str, char end_c,
+ int log_severity,
+ const char **start_out, const char **end_out);
+int router_get_hashes_impl(const char *s, size_t s_len,
+ common_digests_t *digests,
+ const char *start_str, const char *end_str,
+ char end_char);
+
+#ifdef SIGCOMMON_PRIVATE
+MOCK_DECL(STATIC int, signed_digest_equals,
+ (const uint8_t *d1, const uint8_t *d2, size_t len));
+MOCK_DECL(STATIC int, router_compute_hash_final,(char *digest,
+ const char *start, size_t len,
+ digest_algorithm_t alg));
+#endif
+
+#endif /* !defined(TOR_SIGCOMMON_H) */
diff --git a/src/feature/dirparse/signing.c b/src/feature/dirparse/signing.c
new file mode 100644
index 000000000..8d6a40605
--- /dev/null
+++ b/src/feature/dirparse/signing.c
@@ -0,0 +1,98 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file signing.c
+ * \brief Code to sign directory objects.
+ **/
+
+#include "core/or/or.h"
+#include "feature/dirparse/signing.h"
+
+/** Helper: used to generate signatures for routers, directories and
+ * network-status objects. Given a <b>digest_len</b>-byte digest in
+ * <b>digest</b> and a secret <b>private_key</b>, generate an PKCS1-padded
+ * signature, BASE64-encode it, surround it with -----BEGIN/END----- pairs,
+ * and return the new signature on success or NULL on failure.
+ */
+char *
+router_get_dirobj_signature(const char *digest,
+ size_t digest_len,
+ const crypto_pk_t *private_key)
+{
+ char *signature;
+ size_t i, keysize;
+ int siglen;
+ char *buf = NULL;
+ size_t buf_len;
+ /* overestimate of BEGIN/END lines total len. */
+#define BEGIN_END_OVERHEAD_LEN 64
+
+ keysize = crypto_pk_keysize(private_key);
+ signature = tor_malloc(keysize);
+ siglen = crypto_pk_private_sign(private_key, signature, keysize,
+ digest, digest_len);
+ if (siglen < 0) {
+ log_warn(LD_BUG,"Couldn't sign digest.");
+ goto err;
+ }
+
+ /* The *2 here is a ridiculous overestimate of base-64 overhead. */
+ buf_len = (siglen * 2) + BEGIN_END_OVERHEAD_LEN;
+ buf = tor_malloc(buf_len);
+
+ if (strlcpy(buf, "-----BEGIN SIGNATURE-----\n", buf_len) >= buf_len)
+ goto truncated;
+
+ i = strlen(buf);
+ if (base64_encode(buf+i, buf_len-i, signature, siglen,
+ BASE64_ENCODE_MULTILINE) < 0) {
+ log_warn(LD_BUG,"couldn't base64-encode signature");
+ goto err;
+ }
+
+ if (strlcat(buf, "-----END SIGNATURE-----\n", buf_len) >= buf_len)
+ goto truncated;
+
+ tor_free(signature);
+ return buf;
+
+ truncated:
+ log_warn(LD_BUG,"tried to exceed string length.");
+ err:
+ tor_free(signature);
+ tor_free(buf);
+ return NULL;
+}
+
+/** Helper: used to generate signatures for routers, directories and
+ * network-status objects. Given a digest in <b>digest</b> and a secret
+ * <b>private_key</b>, generate a PKCS1-padded signature, BASE64-encode it,
+ * surround it with -----BEGIN/END----- pairs, and write it to the
+ * <b>buf_len</b>-byte buffer at <b>buf</b>. Return 0 on success, -1 on
+ * failure.
+ */
+int
+router_append_dirobj_signature(char *buf, size_t buf_len, const char *digest,
+ size_t digest_len, crypto_pk_t *private_key)
+{
+ size_t sig_len, s_len;
+ char *sig = router_get_dirobj_signature(digest, digest_len, private_key);
+ if (!sig) {
+ log_warn(LD_BUG, "No signature generated");
+ return -1;
+ }
+ sig_len = strlen(sig);
+ s_len = strlen(buf);
+ if (sig_len + s_len + 1 > buf_len) {
+ log_warn(LD_BUG, "Not enough room for signature");
+ tor_free(sig);
+ return -1;
+ }
+ memcpy(buf+s_len, sig, sig_len+1);
+ tor_free(sig);
+ return 0;
+}
diff --git a/src/feature/dirparse/signing.h b/src/feature/dirparse/signing.h
new file mode 100644
index 000000000..2b547a185
--- /dev/null
+++ b/src/feature/dirparse/signing.h
@@ -0,0 +1,23 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file signing.h
+ * \brief Header file for signing.c.
+ **/
+
+#ifndef TOR_SIGNING_H
+#define TOR_SIGNING_H
+
+#define DIROBJ_MAX_SIG_LEN 256
+char *router_get_dirobj_signature(const char *digest,
+ size_t digest_len,
+ const crypto_pk_t *private_key);
+int router_append_dirobj_signature(char *buf, size_t buf_len,
+ const char *digest,
+ size_t digest_len,
+ crypto_pk_t *private_key);
+#endif
diff --git a/src/feature/dirparse/unparseable.h b/src/feature/dirparse/unparseable.h
index 831ab6777..2e48c6a9a 100644
--- a/src/feature/dirparse/unparseable.h
+++ b/src/feature/dirparse/unparseable.h
@@ -18,6 +18,18 @@ MOCK_DECL(void,dump_desc,(const char *desc, const char *type));
void dump_desc_fifo_cleanup(void);
void dump_desc_init(void);
+#undef DEBUG_AREA_ALLOC
+#ifdef DEBUG_AREA_ALLOC
+#define DUMP_AREA(a,name) STMT_BEGIN \
+ size_t alloc=0, used=0; \
+ memarea_get_stats((a),&alloc,&used); \
+ log_debug(LD_MM, "Area for %s has %lu allocated; using %lu.", \
+ name, (unsigned long)alloc, (unsigned long)used); \
+ STMT_END
+#else /* !(defined(DEBUG_AREA_ALLOC)) */
+#define DUMP_AREA(a,name) STMT_NIL
+#endif /* defined(DEBUG_AREA_ALLOC) */
+
#ifdef UNPARSEABLE_PRIVATE
/*
diff --git a/src/feature/nodelist/authcert.c b/src/feature/nodelist/authcert.c
index 1d2377016..b111422d0 100644
--- a/src/feature/nodelist/authcert.c
+++ b/src/feature/nodelist/authcert.c
@@ -28,7 +28,7 @@
#include "feature/dirclient/dlstatus.h"
#include "feature/dircommon/directory.h"
#include "feature/dircommon/fp_pair.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/authcert_parse.h"
#include "feature/nodelist/authcert.h"
#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/networkstatus.h"
diff --git a/src/feature/relay/router.c b/src/feature/relay/router.c
index 318cb41ec..4afcddc67 100644
--- a/src/feature/relay/router.c
+++ b/src/feature/relay/router.c
@@ -21,7 +21,9 @@
#include "feature/dircache/dirserv.h"
#include "feature/dirclient/dirclient.h"
#include "feature/dircommon/directory.h"
+#include "feature/dirparse/authcert_parse.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/signing.h"
#include "feature/hibernate/hibernate.h"
#include "feature/keymgt/loadkey.h"
#include "feature/nodelist/authcert.h"
diff --git a/src/feature/rend/rendcommon.c b/src/feature/rend/rendcommon.c
index 57d753fe4..2c28437b2 100644
--- a/src/feature/rend/rendcommon.c
+++ b/src/feature/rend/rendcommon.c
@@ -31,6 +31,7 @@
#include "feature/relay/router.h"
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/signing.h"
#include "core/or/cpath_build_state_st.h"
#include "core/or/crypt_path_st.h"
diff --git a/src/test/fuzz/fuzz_consensus.c b/src/test/fuzz/fuzz_consensus.c
index 1b3f01986..64507c67e 100644
--- a/src/test/fuzz/fuzz_consensus.c
+++ b/src/test/fuzz/fuzz_consensus.c
@@ -1,8 +1,9 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
+#define SIGCOMMON_PRIVATE
#include "core/or/or.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/networkstatus.h"
#include "lib/crypt_ops/crypto_ed25519.h"
diff --git a/src/test/fuzz/fuzz_descriptor.c b/src/test/fuzz/fuzz_descriptor.c
index 8087e1639..342011371 100644
--- a/src/test/fuzz/fuzz_descriptor.c
+++ b/src/test/fuzz/fuzz_descriptor.c
@@ -1,8 +1,9 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
+#define SIGCOMMON_PRIVATE
#include "core/or/or.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/routerlist.h"
#include "feature/nodelist/torcert.h"
diff --git a/src/test/fuzz/fuzz_extrainfo.c b/src/test/fuzz/fuzz_extrainfo.c
index 3ec2baf1e..da0fe8083 100644
--- a/src/test/fuzz/fuzz_extrainfo.c
+++ b/src/test/fuzz/fuzz_extrainfo.c
@@ -1,8 +1,9 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
+#define SIGCOMMON_PRIVATE
#include "core/or/or.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/routerlist.h"
#include "feature/relay/routerkeys.h"
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index 9c189ba6b..23a1d1ba4 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -52,6 +52,7 @@
#include "feature/nodelist/nickname.h"
#include "feature/nodelist/node_select.h"
#include "feature/nodelist/routerlist.h"
+#include "feature/dirparse/authcert_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/routerset.h"
diff --git a/src/test/test_dir_common.c b/src/test/test_dir_common.c
index e65e2b011..5a9659ad4 100644
--- a/src/test/test_dir_common.c
+++ b/src/test/test_dir_common.c
@@ -10,6 +10,7 @@
#include "feature/dirauth/dirvote.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
+#include "feature/dirparse/authcert_parse.h"
#include "test/test_dir_common.h"
#include "feature/dircommon/voting_schedule.h"
diff --git a/src/test/test_dir_handle_get.c b/src/test/test_dir_handle_get.c
index 7deed61e7..7cae45f13 100644
--- a/src/test/test_dir_handle_get.c
+++ b/src/test/test_dir_handle_get.c
@@ -29,6 +29,7 @@
#include "test/test_helpers.h"
#include "feature/nodelist/nodelist.h"
#include "feature/client/entrynodes.h"
+#include "feature/dirparse/authcert_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/nodelist/networkstatus.h"
#include "core/proto/proto_http.h"
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index a76f5c37d..d4606c227 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -33,6 +33,7 @@
#include "feature/nodelist/node_select.h"
#include "feature/nodelist/routerlist.h"
#include "feature/nodelist/routerset.h"
+#include "feature/dirparse/authcert_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/dirauth/shared_random.h"
#include "app/config/statefile.h"
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index 4dc017f33..397a4ceb5 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -19,6 +19,7 @@
#include "feature/relay/routerkeys.h"
#include "feature/nodelist/authcert.h"
#include "feature/nodelist/dirlist.h"
+#include "feature/dirparse/authcert_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/hs_common/shared_random_client.h"
#include "feature/dircommon/voting_schedule.h"
1
0
commit a77b2e984e0781215a03c205c69441b30749ed9b
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Sep 30 23:37:43 2018 -0500
Remove router_get_dir_hash as unused.
---
src/feature/dirparse/routerparse.c | 11 -----------
src/feature/dirparse/routerparse.h | 1 -
2 files changed, 12 deletions(-)
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index 36fd1c13d..b439b79de 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -332,17 +332,6 @@ static addr_policy_t *router_parse_addr_policy_private(directory_token_t *tok);
static smartlist_t *find_all_exitpolicy(smartlist_t *s);
-/** Set <b>digest</b> to the SHA-1 digest of the hash of the directory in
- * <b>s</b>. Return 0 on success, -1 on failure.
- */
-int
-router_get_dir_hash(const char *s, char *digest)
-{
- return router_get_hash_impl(s, strlen(s), digest,
- "signed-directory","\ndirectory-signature",'\n',
- DIGEST_SHA1);
-}
-
/** Set <b>digest</b> to the SHA-1 digest of the hash of the first router in
* <b>s</b>. Return 0 on success, -1 on failure.
*/
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index b8858e561..e6ab751fa 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -17,7 +17,6 @@
enum networkstatus_type_t;
int router_get_router_hash(const char *s, size_t s_len, char *digest);
-int router_get_dir_hash(const char *s, char *digest);
int router_get_networkstatus_v3_hashes(const char *s,
common_digests_t *digests);
int router_get_networkstatus_v3_signed_boundaries(const char *s,
1
0
commit 31008317620c26ec9925592e1f867ef03a1125c4
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Sep 30 23:42:21 2018 -0500
Remove dump_distinct_digest_count()
It was disabled-by-default for ages, and it no longer compiles. I
think it's safe to call it obsolete.
---
src/app/main/main.c | 1 -
src/feature/dirparse/routerparse.c | 30 ------------------------------
src/feature/dirparse/routerparse.h | 1 -
3 files changed, 32 deletions(-)
diff --git a/src/app/main/main.c b/src/app/main/main.c
index 2e3efadc0..ae87add67 100644
--- a/src/app/main/main.c
+++ b/src/app/main/main.c
@@ -424,7 +424,6 @@ dumpstats(int severity)
rep_hist_dump_stats(now,severity);
rend_service_dump_stats(severity);
- dump_distinct_digest_count(severity);
}
/** Called by exit() as we shut down the process.
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index b439b79de..05013ef7f 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -537,31 +537,6 @@ router_parse_list_from_string(const char **s, const char *eos,
return 0;
}
-/* For debugging: define to count every descriptor digest we've seen so we
- * know if we need to try harder to avoid duplicate verifies. */
-#undef COUNT_DISTINCT_DIGESTS
-
-#ifdef COUNT_DISTINCT_DIGESTS
-static digestmap_t *verified_digests = NULL;
-#endif
-
-/** Log the total count of the number of distinct router digests we've ever
- * verified. When compared to the number of times we've verified routerdesc
- * signatures <i>in toto</i>, this will tell us if we're doing too much
- * multiple-verification. */
-void
-dump_distinct_digest_count(int severity)
-{
-#ifdef COUNT_DISTINCT_DIGESTS
- if (!verified_digests)
- verified_digests = digestmap_new();
- tor_log(severity, LD_GENERAL, "%d *distinct* router digests verified",
- digestmap_size(verified_digests));
-#else /* !(defined(COUNT_DISTINCT_DIGESTS)) */
- (void)severity; /* suppress "unused parameter" warning */
-#endif /* defined(COUNT_DISTINCT_DIGESTS) */
-}
-
/** Try to find an IPv6 OR port in <b>list</b> of directory_token_t's
* with at least one argument (use GE(1) in setup). If found, store
* address and port number to <b>addr_out</b> and
@@ -1139,11 +1114,6 @@ router_parse_entry_from_string(const char *s, const char *end,
}
tok = find_by_keyword(tokens, K_ROUTER_SIGNATURE);
-#ifdef COUNT_DISTINCT_DIGESTS
- if (!verified_digests)
- verified_digests = digestmap_new();
- digestmap_set(verified_digests, signed_digest, (void*)(uintptr_t)1);
-#endif
if (!router->or_port) {
log_warn(LD_DIR,"or_port unreadable or 0. Failing.");
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index e6ab751fa..7b514afbb 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -47,7 +47,6 @@ MOCK_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
(const char *s, int assume_action, int *malformed_list));
void assert_addr_policy_ok(smartlist_t *t);
-void dump_distinct_digest_count(int severity);
int compare_vote_routerstatus_entries(const void **_a, const void **_b);
int networkstatus_verify_bw_weights(networkstatus_t *ns, int);
1
0
commit aff5bf5464172bbbe549e147ebd4ba809a8b252c
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Sep 30 23:45:17 2018 -0500
Remove addr_policy_assert_ok() as unused
---
src/feature/dirparse/routerparse.c | 12 ------------
src/feature/dirparse/routerparse.h | 2 --
2 files changed, 14 deletions(-)
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index 05013ef7f..f661042fe 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -3373,18 +3373,6 @@ router_parse_addr_policy_private(directory_token_t *tok)
return addr_policy_get_canonical_entry(&result);
}
-/** Log and exit if <b>t</b> is malformed */
-void
-assert_addr_policy_ok(smartlist_t *lst)
-{
- if (!lst) return;
- SMARTLIST_FOREACH(lst, addr_policy_t *, t, {
- tor_assert(t->policy_type == ADDR_POLICY_REJECT ||
- t->policy_type == ADDR_POLICY_ACCEPT);
- tor_assert(t->prt_min <= t->prt_max);
- });
-}
-
/** Return a newly allocated smartlist of all accept or reject tokens in
* <b>s</b>.
*/
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index 7b514afbb..f90ad08a9 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -46,8 +46,6 @@ extrainfo_t *extrainfo_parse_entry_from_string(const char *s, const char *end,
MOCK_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
(const char *s, int assume_action, int *malformed_list));
-void assert_addr_policy_ok(smartlist_t *t);
-
int compare_vote_routerstatus_entries(const void **_a, const void **_b);
int networkstatus_verify_bw_weights(networkstatus_t *ns, int);
networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
1
0

12 Oct '18
commit cd23903427844efb23048941af62643e5e0163cb
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Mon Oct 1 00:33:23 2018 -0500
Pull detached-signatures code into dirauth.
---
src/core/include.am | 2 +
src/feature/dirauth/dirvote.c | 1 +
src/feature/dirauth/dsigs_parse.c | 282 +++++++++++++++++++++++++++++++++++
src/feature/dirauth/dsigs_parse.h | 22 +++
src/feature/dirparse/routerparse.c | 243 ------------------------------
src/feature/dirparse/routerparse.h | 2 -
src/feature/nodelist/networkstatus.c | 19 ---
src/feature/nodelist/networkstatus.h | 3 -
src/test/test_dir.c | 1 +
9 files changed, 308 insertions(+), 267 deletions(-)
diff --git a/src/core/include.am b/src/core/include.am
index 89b12e958..6815f327f 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -132,6 +132,7 @@ LIBTOR_APP_A_SOURCES = \
# the separation is only in the code location.
LIBTOR_APP_A_SOURCES += \
src/feature/dirauth/bwauth.c \
+ src/feature/dirauth/dsigs_parse.c \
src/feature/dirauth/guardfraction.c \
src/feature/dirauth/reachability.c \
src/feature/dirauth/recommend_pkg.c \
@@ -268,6 +269,7 @@ noinst_HEADERS += \
src/feature/dirauth/bwauth.h \
src/feature/dirauth/dircollate.h \
src/feature/dirauth/dirvote.h \
+ src/feature/dirauth/dsigs_parse.h \
src/feature/dirauth/guardfraction.h \
src/feature/dirauth/keypin.h \
src/feature/dirauth/ns_detached_signatures_st.h \
diff --git a/src/feature/dirauth/dirvote.c b/src/feature/dirauth/dirvote.c
index 27a85de8a..0322c9cae 100644
--- a/src/feature/dirauth/dirvote.c
+++ b/src/feature/dirauth/dirvote.c
@@ -7,6 +7,7 @@
#include "core/or/or.h"
#include "app/config/config.h"
#include "feature/dirauth/dircollate.h"
+#include "feature/dirauth/dsigs_parse.h"
#include "feature/dirauth/recommend_pkg.h"
#include "feature/dirauth/voteflags.h"
#include "feature/dircommon/directory.h"
diff --git a/src/feature/dirauth/dsigs_parse.c b/src/feature/dirauth/dsigs_parse.c
new file mode 100644
index 000000000..b0c407567
--- /dev/null
+++ b/src/feature/dirauth/dsigs_parse.c
@@ -0,0 +1,282 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file dsigs_parse.h
+ * \brief Code to parse and validate detached-signature objects
+ **/
+
+#include "core/or/or.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/unparseable.h"
+#include "feature/nodelist/networkstatus.h"
+#include "lib/memarea/memarea.h"
+
+#include "feature/dirauth/dsigs_parse.h"
+#include "feature/dirauth/ns_detached_signatures_st.h"
+#include "feature/nodelist/document_signature_st.h"
+
+/** List of tokens recognized in detached networkstatus signature documents. */
+static token_rule_t networkstatus_detached_signature_token_table[] = {
+ T1_START("consensus-digest", K_CONSENSUS_DIGEST, GE(1), NO_OBJ ),
+ T("additional-digest", K_ADDITIONAL_DIGEST,GE(3), NO_OBJ ),
+ T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
+ T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
+ T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
+ T("additional-signature", K_ADDITIONAL_SIGNATURE, GE(4), NEED_OBJ ),
+ T1N("directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
+ END_OF_TABLE
+};
+
+/** Return the common_digests_t that holds the digests of the
+ * <b>flavor_name</b>-flavored networkstatus according to the detached
+ * signatures document <b>sigs</b>, allocating a new common_digests_t as
+ * needed. */
+static common_digests_t *
+detached_get_digests(ns_detached_signatures_t *sigs, const char *flavor_name)
+{
+ common_digests_t *d = strmap_get(sigs->digests, flavor_name);
+ if (!d) {
+ d = tor_malloc_zero(sizeof(common_digests_t));
+ strmap_set(sigs->digests, flavor_name, d);
+ }
+ return d;
+}
+
+/** Return the list of signatures of the <b>flavor_name</b>-flavored
+ * networkstatus according to the detached signatures document <b>sigs</b>,
+ * allocating a new common_digests_t as needed. */
+static smartlist_t *
+detached_get_signatures(ns_detached_signatures_t *sigs,
+ const char *flavor_name)
+{
+ smartlist_t *sl = strmap_get(sigs->signatures, flavor_name);
+ if (!sl) {
+ sl = smartlist_new();
+ strmap_set(sigs->signatures, flavor_name, sl);
+ }
+ return sl;
+}
+
+/** Parse a detached v3 networkstatus signature document between <b>s</b> and
+ * <b>eos</b> and return the result. Return -1 on failure. */
+ns_detached_signatures_t *
+networkstatus_parse_detached_signatures(const char *s, const char *eos)
+{
+ /* XXXX there is too much duplicate shared between this function and
+ * networkstatus_parse_vote_from_string(). */
+ directory_token_t *tok;
+ memarea_t *area = NULL;
+ common_digests_t *digests;
+
+ smartlist_t *tokens = smartlist_new();
+ ns_detached_signatures_t *sigs =
+ tor_malloc_zero(sizeof(ns_detached_signatures_t));
+ sigs->digests = strmap_new();
+ sigs->signatures = strmap_new();
+
+ if (!eos)
+ eos = s + strlen(s);
+
+ area = memarea_new();
+ if (tokenize_string(area,s, eos, tokens,
+ networkstatus_detached_signature_token_table, 0)) {
+ log_warn(LD_DIR, "Error tokenizing detached networkstatus signatures");
+ goto err;
+ }
+
+ /* Grab all the digest-like tokens. */
+ SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, _tok) {
+ const char *algname;
+ digest_algorithm_t alg;
+ const char *flavor;
+ const char *hexdigest;
+ size_t expected_length, digest_length;
+
+ tok = _tok;
+
+ if (tok->tp == K_CONSENSUS_DIGEST) {
+ algname = "sha1";
+ alg = DIGEST_SHA1;
+ flavor = "ns";
+ hexdigest = tok->args[0];
+ } else if (tok->tp == K_ADDITIONAL_DIGEST) {
+ int a = crypto_digest_algorithm_parse_name(tok->args[1]);
+ if (a<0) {
+ log_warn(LD_DIR, "Unrecognized algorithm name %s", tok->args[0]);
+ continue;
+ }
+ alg = (digest_algorithm_t) a;
+ flavor = tok->args[0];
+ algname = tok->args[1];
+ hexdigest = tok->args[2];
+ } else {
+ continue;
+ }
+
+ digest_length = crypto_digest_algorithm_get_length(alg);
+ expected_length = digest_length * 2; /* hex encoding */
+
+ if (strlen(hexdigest) != expected_length) {
+ log_warn(LD_DIR, "Wrong length on consensus-digest in detached "
+ "networkstatus signatures");
+ goto err;
+ }
+ digests = detached_get_digests(sigs, flavor);
+ tor_assert(digests);
+ if (!tor_mem_is_zero(digests->d[alg], digest_length)) {
+ log_warn(LD_DIR, "Multiple digests for %s with %s on detached "
+ "signatures document", flavor, algname);
+ continue;
+ }
+ if (base16_decode(digests->d[alg], digest_length,
+ hexdigest, strlen(hexdigest)) != (int) digest_length) {
+ log_warn(LD_DIR, "Bad encoding on consensus-digest in detached "
+ "networkstatus signatures");
+ goto err;
+ }
+ } SMARTLIST_FOREACH_END(_tok);
+
+ tok = find_by_keyword(tokens, K_VALID_AFTER);
+ if (parse_iso_time(tok->args[0], &sigs->valid_after)) {
+ log_warn(LD_DIR, "Bad valid-after in detached networkstatus signatures");
+ goto err;
+ }
+
+ tok = find_by_keyword(tokens, K_FRESH_UNTIL);
+ if (parse_iso_time(tok->args[0], &sigs->fresh_until)) {
+ log_warn(LD_DIR, "Bad fresh-until in detached networkstatus signatures");
+ goto err;
+ }
+
+ tok = find_by_keyword(tokens, K_VALID_UNTIL);
+ if (parse_iso_time(tok->args[0], &sigs->valid_until)) {
+ log_warn(LD_DIR, "Bad valid-until in detached networkstatus signatures");
+ goto err;
+ }
+
+ SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, _tok) {
+ const char *id_hexdigest;
+ const char *sk_hexdigest;
+ const char *algname;
+ const char *flavor;
+ digest_algorithm_t alg;
+
+ char id_digest[DIGEST_LEN];
+ char sk_digest[DIGEST_LEN];
+ smartlist_t *siglist;
+ document_signature_t *sig;
+ int is_duplicate;
+
+ tok = _tok;
+ if (tok->tp == K_DIRECTORY_SIGNATURE) {
+ tor_assert(tok->n_args >= 2);
+ flavor = "ns";
+ algname = "sha1";
+ id_hexdigest = tok->args[0];
+ sk_hexdigest = tok->args[1];
+ } else if (tok->tp == K_ADDITIONAL_SIGNATURE) {
+ tor_assert(tok->n_args >= 4);
+ flavor = tok->args[0];
+ algname = tok->args[1];
+ id_hexdigest = tok->args[2];
+ sk_hexdigest = tok->args[3];
+ } else {
+ continue;
+ }
+
+ {
+ int a = crypto_digest_algorithm_parse_name(algname);
+ if (a<0) {
+ log_warn(LD_DIR, "Unrecognized algorithm name %s", algname);
+ continue;
+ }
+ alg = (digest_algorithm_t) a;
+ }
+
+ if (!tok->object_type ||
+ strcmp(tok->object_type, "SIGNATURE") ||
+ tok->object_size < 128 || tok->object_size > 512) {
+ log_warn(LD_DIR, "Bad object type or length on directory-signature");
+ goto err;
+ }
+
+ if (strlen(id_hexdigest) != HEX_DIGEST_LEN ||
+ base16_decode(id_digest, sizeof(id_digest),
+ id_hexdigest, HEX_DIGEST_LEN) != sizeof(id_digest)) {
+ log_warn(LD_DIR, "Error decoding declared identity %s in "
+ "network-status vote.", escaped(id_hexdigest));
+ goto err;
+ }
+ if (strlen(sk_hexdigest) != HEX_DIGEST_LEN ||
+ base16_decode(sk_digest, sizeof(sk_digest),
+ sk_hexdigest, HEX_DIGEST_LEN) != sizeof(sk_digest)) {
+ log_warn(LD_DIR, "Error decoding declared signing key digest %s in "
+ "network-status vote.", escaped(sk_hexdigest));
+ goto err;
+ }
+
+ siglist = detached_get_signatures(sigs, flavor);
+ is_duplicate = 0;
+ SMARTLIST_FOREACH(siglist, document_signature_t *, dsig, {
+ if (dsig->alg == alg &&
+ tor_memeq(id_digest, dsig->identity_digest, DIGEST_LEN) &&
+ tor_memeq(sk_digest, dsig->signing_key_digest, DIGEST_LEN)) {
+ is_duplicate = 1;
+ }
+ });
+ if (is_duplicate) {
+ log_warn(LD_DIR, "Two signatures with identical keys and algorithm "
+ "found.");
+ continue;
+ }
+
+ sig = tor_malloc_zero(sizeof(document_signature_t));
+ sig->alg = alg;
+ memcpy(sig->identity_digest, id_digest, DIGEST_LEN);
+ memcpy(sig->signing_key_digest, sk_digest, DIGEST_LEN);
+ if (tok->object_size >= INT_MAX || tok->object_size >= SIZE_T_CEILING) {
+ tor_free(sig);
+ goto err;
+ }
+ sig->signature = tor_memdup(tok->object_body, tok->object_size);
+ sig->signature_len = (int) tok->object_size;
+
+ smartlist_add(siglist, sig);
+ } SMARTLIST_FOREACH_END(_tok);
+
+ goto done;
+ err:
+ ns_detached_signatures_free(sigs);
+ sigs = NULL;
+ done:
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ if (area) {
+ DUMP_AREA(area, "detached signatures");
+ memarea_drop_all(area);
+ }
+ return sigs;
+}
+
+/** Release all storage held in <b>s</b>. */
+void
+ns_detached_signatures_free_(ns_detached_signatures_t *s)
+{
+ if (!s)
+ return;
+ if (s->signatures) {
+ STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
+ SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
+ document_signature_free(sig));
+ smartlist_free(sigs);
+ } STRMAP_FOREACH_END;
+ strmap_free(s->signatures, NULL);
+ strmap_free(s->digests, tor_free_);
+ }
+
+ tor_free(s);
+}
diff --git a/src/feature/dirauth/dsigs_parse.h b/src/feature/dirauth/dsigs_parse.h
new file mode 100644
index 000000000..ffb2ac036
--- /dev/null
+++ b/src/feature/dirauth/dsigs_parse.h
@@ -0,0 +1,22 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file dsigs_parse.h
+ * \brief Header file for dsigs_parse.c.
+ **/
+
+#ifndef TOR_DSIGS_PARSE_H
+#define TOR_DSIGS_PARSE_H
+
+ns_detached_signatures_t *networkstatus_parse_detached_signatures(
+ const char *s, const char *eos);
+
+void ns_detached_signatures_free_(ns_detached_signatures_t *s);
+#define ns_detached_signatures_free(s) \
+ FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
+
+#endif
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index 599f2147c..daeb67c91 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -297,18 +297,6 @@ static token_rule_t networkstatus_vote_footer_token_table[] = {
END_OF_TABLE
};
-/** List of tokens recognized in detached networkstatus signature documents. */
-static token_rule_t networkstatus_detached_signature_token_table[] = {
- T1_START("consensus-digest", K_CONSENSUS_DIGEST, GE(1), NO_OBJ ),
- T("additional-digest", K_ADDITIONAL_DIGEST,GE(3), NO_OBJ ),
- T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
- T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
- T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
- T("additional-signature", K_ADDITIONAL_SIGNATURE, GE(4), NEED_OBJ ),
- T1N("directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
- END_OF_TABLE
-};
-
#undef T
/* static function prototypes */
@@ -2901,237 +2889,6 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
return ns;
}
-/** Return the common_digests_t that holds the digests of the
- * <b>flavor_name</b>-flavored networkstatus according to the detached
- * signatures document <b>sigs</b>, allocating a new common_digests_t as
- * needed. */
-static common_digests_t *
-detached_get_digests(ns_detached_signatures_t *sigs, const char *flavor_name)
-{
- common_digests_t *d = strmap_get(sigs->digests, flavor_name);
- if (!d) {
- d = tor_malloc_zero(sizeof(common_digests_t));
- strmap_set(sigs->digests, flavor_name, d);
- }
- return d;
-}
-
-/** Return the list of signatures of the <b>flavor_name</b>-flavored
- * networkstatus according to the detached signatures document <b>sigs</b>,
- * allocating a new common_digests_t as needed. */
-static smartlist_t *
-detached_get_signatures(ns_detached_signatures_t *sigs,
- const char *flavor_name)
-{
- smartlist_t *sl = strmap_get(sigs->signatures, flavor_name);
- if (!sl) {
- sl = smartlist_new();
- strmap_set(sigs->signatures, flavor_name, sl);
- }
- return sl;
-}
-
-/** Parse a detached v3 networkstatus signature document between <b>s</b> and
- * <b>eos</b> and return the result. Return -1 on failure. */
-ns_detached_signatures_t *
-networkstatus_parse_detached_signatures(const char *s, const char *eos)
-{
- /* XXXX there is too much duplicate shared between this function and
- * networkstatus_parse_vote_from_string(). */
- directory_token_t *tok;
- memarea_t *area = NULL;
- common_digests_t *digests;
-
- smartlist_t *tokens = smartlist_new();
- ns_detached_signatures_t *sigs =
- tor_malloc_zero(sizeof(ns_detached_signatures_t));
- sigs->digests = strmap_new();
- sigs->signatures = strmap_new();
-
- if (!eos)
- eos = s + strlen(s);
-
- area = memarea_new();
- if (tokenize_string(area,s, eos, tokens,
- networkstatus_detached_signature_token_table, 0)) {
- log_warn(LD_DIR, "Error tokenizing detached networkstatus signatures");
- goto err;
- }
-
- /* Grab all the digest-like tokens. */
- SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, _tok) {
- const char *algname;
- digest_algorithm_t alg;
- const char *flavor;
- const char *hexdigest;
- size_t expected_length, digest_length;
-
- tok = _tok;
-
- if (tok->tp == K_CONSENSUS_DIGEST) {
- algname = "sha1";
- alg = DIGEST_SHA1;
- flavor = "ns";
- hexdigest = tok->args[0];
- } else if (tok->tp == K_ADDITIONAL_DIGEST) {
- int a = crypto_digest_algorithm_parse_name(tok->args[1]);
- if (a<0) {
- log_warn(LD_DIR, "Unrecognized algorithm name %s", tok->args[0]);
- continue;
- }
- alg = (digest_algorithm_t) a;
- flavor = tok->args[0];
- algname = tok->args[1];
- hexdigest = tok->args[2];
- } else {
- continue;
- }
-
- digest_length = crypto_digest_algorithm_get_length(alg);
- expected_length = digest_length * 2; /* hex encoding */
-
- if (strlen(hexdigest) != expected_length) {
- log_warn(LD_DIR, "Wrong length on consensus-digest in detached "
- "networkstatus signatures");
- goto err;
- }
- digests = detached_get_digests(sigs, flavor);
- tor_assert(digests);
- if (!tor_mem_is_zero(digests->d[alg], digest_length)) {
- log_warn(LD_DIR, "Multiple digests for %s with %s on detached "
- "signatures document", flavor, algname);
- continue;
- }
- if (base16_decode(digests->d[alg], digest_length,
- hexdigest, strlen(hexdigest)) != (int) digest_length) {
- log_warn(LD_DIR, "Bad encoding on consensus-digest in detached "
- "networkstatus signatures");
- goto err;
- }
- } SMARTLIST_FOREACH_END(_tok);
-
- tok = find_by_keyword(tokens, K_VALID_AFTER);
- if (parse_iso_time(tok->args[0], &sigs->valid_after)) {
- log_warn(LD_DIR, "Bad valid-after in detached networkstatus signatures");
- goto err;
- }
-
- tok = find_by_keyword(tokens, K_FRESH_UNTIL);
- if (parse_iso_time(tok->args[0], &sigs->fresh_until)) {
- log_warn(LD_DIR, "Bad fresh-until in detached networkstatus signatures");
- goto err;
- }
-
- tok = find_by_keyword(tokens, K_VALID_UNTIL);
- if (parse_iso_time(tok->args[0], &sigs->valid_until)) {
- log_warn(LD_DIR, "Bad valid-until in detached networkstatus signatures");
- goto err;
- }
-
- SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, _tok) {
- const char *id_hexdigest;
- const char *sk_hexdigest;
- const char *algname;
- const char *flavor;
- digest_algorithm_t alg;
-
- char id_digest[DIGEST_LEN];
- char sk_digest[DIGEST_LEN];
- smartlist_t *siglist;
- document_signature_t *sig;
- int is_duplicate;
-
- tok = _tok;
- if (tok->tp == K_DIRECTORY_SIGNATURE) {
- tor_assert(tok->n_args >= 2);
- flavor = "ns";
- algname = "sha1";
- id_hexdigest = tok->args[0];
- sk_hexdigest = tok->args[1];
- } else if (tok->tp == K_ADDITIONAL_SIGNATURE) {
- tor_assert(tok->n_args >= 4);
- flavor = tok->args[0];
- algname = tok->args[1];
- id_hexdigest = tok->args[2];
- sk_hexdigest = tok->args[3];
- } else {
- continue;
- }
-
- {
- int a = crypto_digest_algorithm_parse_name(algname);
- if (a<0) {
- log_warn(LD_DIR, "Unrecognized algorithm name %s", algname);
- continue;
- }
- alg = (digest_algorithm_t) a;
- }
-
- if (!tok->object_type ||
- strcmp(tok->object_type, "SIGNATURE") ||
- tok->object_size < 128 || tok->object_size > 512) {
- log_warn(LD_DIR, "Bad object type or length on directory-signature");
- goto err;
- }
-
- if (strlen(id_hexdigest) != HEX_DIGEST_LEN ||
- base16_decode(id_digest, sizeof(id_digest),
- id_hexdigest, HEX_DIGEST_LEN) != sizeof(id_digest)) {
- log_warn(LD_DIR, "Error decoding declared identity %s in "
- "network-status vote.", escaped(id_hexdigest));
- goto err;
- }
- if (strlen(sk_hexdigest) != HEX_DIGEST_LEN ||
- base16_decode(sk_digest, sizeof(sk_digest),
- sk_hexdigest, HEX_DIGEST_LEN) != sizeof(sk_digest)) {
- log_warn(LD_DIR, "Error decoding declared signing key digest %s in "
- "network-status vote.", escaped(sk_hexdigest));
- goto err;
- }
-
- siglist = detached_get_signatures(sigs, flavor);
- is_duplicate = 0;
- SMARTLIST_FOREACH(siglist, document_signature_t *, dsig, {
- if (dsig->alg == alg &&
- tor_memeq(id_digest, dsig->identity_digest, DIGEST_LEN) &&
- tor_memeq(sk_digest, dsig->signing_key_digest, DIGEST_LEN)) {
- is_duplicate = 1;
- }
- });
- if (is_duplicate) {
- log_warn(LD_DIR, "Two signatures with identical keys and algorithm "
- "found.");
- continue;
- }
-
- sig = tor_malloc_zero(sizeof(document_signature_t));
- sig->alg = alg;
- memcpy(sig->identity_digest, id_digest, DIGEST_LEN);
- memcpy(sig->signing_key_digest, sk_digest, DIGEST_LEN);
- if (tok->object_size >= INT_MAX || tok->object_size >= SIZE_T_CEILING) {
- tor_free(sig);
- goto err;
- }
- sig->signature = tor_memdup(tok->object_body, tok->object_size);
- sig->signature_len = (int) tok->object_size;
-
- smartlist_add(siglist, sig);
- } SMARTLIST_FOREACH_END(_tok);
-
- goto done;
- err:
- ns_detached_signatures_free(sigs);
- sigs = NULL;
- done:
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- if (area) {
- DUMP_AREA(area, "detached signatures");
- memarea_drop_all(area);
- }
- return sigs;
-}
-
/** Parse the addr policy in the string <b>s</b> and return it. If
* assume_action is nonnegative, then insert its action (ADDR_POLICY_ACCEPT or
* ADDR_POLICY_REJECT) for items that specify no action.
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index 00020936e..eb0ca880f 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -51,8 +51,6 @@ int networkstatus_verify_bw_weights(networkstatus_t *ns, int);
networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
const char **eos_out,
enum networkstatus_type_t ns_type);
-ns_detached_signatures_t *networkstatus_parse_detached_signatures(
- const char *s, const char *eos);
int find_single_ipv6_orport(const smartlist_t *list,
tor_addr_t *addr_out,
diff --git a/src/feature/nodelist/networkstatus.c b/src/feature/nodelist/networkstatus.c
index e20d543db..d214e6470 100644
--- a/src/feature/nodelist/networkstatus.c
+++ b/src/feature/nodelist/networkstatus.c
@@ -2705,25 +2705,6 @@ networkstatus_check_required_protocols(const networkstatus_t *ns,
return 0;
}
-/** Release all storage held in <b>s</b>. */
-void
-ns_detached_signatures_free_(ns_detached_signatures_t *s)
-{
- if (!s)
- return;
- if (s->signatures) {
- STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
- SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
- document_signature_free(sig));
- smartlist_free(sigs);
- } STRMAP_FOREACH_END;
- strmap_free(s->signatures, NULL);
- strmap_free(s->digests, tor_free_);
- }
-
- tor_free(s);
-}
-
/** Free all storage held locally in this module. */
void
networkstatus_free_all(void)
diff --git a/src/feature/nodelist/networkstatus.h b/src/feature/nodelist/networkstatus.h
index cc6badf0b..6f8b2dc96 100644
--- a/src/feature/nodelist/networkstatus.h
+++ b/src/feature/nodelist/networkstatus.h
@@ -24,9 +24,6 @@ void routerstatus_free_(routerstatus_t *rs);
void networkstatus_vote_free_(networkstatus_t *ns);
#define networkstatus_vote_free(ns) \
FREE_AND_NULL(networkstatus_t, networkstatus_vote_free_, (ns))
-void ns_detached_signatures_free_(ns_detached_signatures_t *s);
-#define ns_detached_signatures_free(s) \
- FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
networkstatus_voter_info_t *networkstatus_get_voter_by_id(
networkstatus_t *vote,
const char *identity);
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index 23a1d1ba4..59464022b 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -34,6 +34,7 @@
#include "feature/control/control.h"
#include "feature/dirauth/bwauth.h"
#include "feature/dirauth/dirvote.h"
+#include "feature/dirauth/dsigs_parse.h"
#include "feature/dirauth/process_descs.h"
#include "feature/dirauth/recommend_pkg.h"
#include "feature/dirauth/shared_random_state.h"
1
0

12 Oct '18
commit 2be35f4d61fb8d05007ce65d0421a2ef71c07eb4
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Sep 30 23:51:47 2018 -0500
Split microdescriptor parser into its own file.
---
src/core/include.am | 2 +
src/feature/dirauth/dirvote.c | 1 +
src/feature/dirparse/microdesc_parse.c | 267 +++++++++++++++++++++++++++++++++
src/feature/dirparse/microdesc_parse.h | 20 +++
src/feature/dirparse/routerparse.c | 242 +-----------------------------
src/feature/dirparse/routerparse.h | 7 +-
src/feature/nodelist/microdesc.c | 15 +-
src/test/fuzz/fuzz_microdesc.c | 4 +-
src/test/test_microdesc.c | 5 +-
9 files changed, 307 insertions(+), 256 deletions(-)
diff --git a/src/core/include.am b/src/core/include.am
index febc1d821..89b12e958 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -74,6 +74,7 @@ LIBTOR_APP_A_SOURCES = \
src/feature/dircommon/fp_pair.c \
src/feature/dircommon/voting_schedule.c \
src/feature/dirparse/authcert_parse.c \
+ src/feature/dirparse/microdesc_parse.c \
src/feature/dirparse/parsecommon.c \
src/feature/dirparse/routerparse.c \
src/feature/dirparse/sigcommon.c \
@@ -294,6 +295,7 @@ noinst_HEADERS += \
src/feature/dircommon/voting_schedule.h \
src/feature/dirparse/authcert_members.i \
src/feature/dirparse/authcert_parse.h \
+ src/feature/dirparse/microdesc_parse.h \
src/feature/dirparse/parsecommon.h \
src/feature/dirparse/routerparse.h \
src/feature/dirparse/sigcommon.h \
diff --git a/src/feature/dirauth/dirvote.c b/src/feature/dirauth/dirvote.c
index f5546abd4..27a85de8a 100644
--- a/src/feature/dirauth/dirvote.c
+++ b/src/feature/dirauth/dirvote.c
@@ -28,6 +28,7 @@
#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/microdesc_parse.h"
#include "feature/dirparse/signing.h"
#include "feature/nodelist/fmt_routerstatus.h"
#include "feature/client/entrynodes.h" /* needed for guardfraction methods */
diff --git a/src/feature/dirparse/microdesc_parse.c b/src/feature/dirparse/microdesc_parse.c
new file mode 100644
index 000000000..005d2c53d
--- /dev/null
+++ b/src/feature/dirparse/microdesc_parse.c
@@ -0,0 +1,267 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file microdesc_parse.c
+ * \brief Code to parse and validate microdescriptors.
+ **/
+
+#include "core/or/or.h"
+
+#include "app/config/config.h"
+#include "core/or/policies.h"
+#include "feature/dirparse/microdesc_parse.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/routerparse.h"
+#include "feature/nodelist/microdesc.h"
+#include "feature/nodelist/nickname.h"
+#include "feature/relay/router.h"
+#include "lib/crypt_ops/crypto_curve25519.h"
+#include "lib/crypt_ops/crypto_ed25519.h"
+#include "lib/crypt_ops/crypto_format.h"
+#include "lib/memarea/memarea.h"
+
+#include "feature/nodelist/microdesc_st.h"
+
+/** List of tokens recognized in microdescriptors */
+static token_rule_t microdesc_token_table[] = {
+ T1_START("onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024),
+ T01("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
+ T0N("id", K_ID, GE(2), NO_OBJ ),
+ T0N("a", K_A, GE(1), NO_OBJ ),
+ T01("family", K_FAMILY, ARGS, NO_OBJ ),
+ T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
+ T01("p6", K_P6, CONCAT_ARGS, NO_OBJ ),
+ A01("@last-listed", A_LAST_LISTED, CONCAT_ARGS, NO_OBJ ),
+ END_OF_TABLE
+};
+
+/** Assuming that s starts with a microdesc, return the start of the
+ * *NEXT* one. Return NULL on "not found." */
+static const char *
+find_start_of_next_microdesc(const char *s, const char *eos)
+{
+ int started_with_annotations;
+ s = eat_whitespace_eos(s, eos);
+ if (!s)
+ return NULL;
+
+#define CHECK_LENGTH() STMT_BEGIN \
+ if (s+32 > eos) \
+ return NULL; \
+ STMT_END
+
+#define NEXT_LINE() STMT_BEGIN \
+ s = memchr(s, '\n', eos-s); \
+ if (!s || s+1 >= eos) \
+ return NULL; \
+ s++; \
+ STMT_END
+
+ CHECK_LENGTH();
+
+ started_with_annotations = (*s == '@');
+
+ if (started_with_annotations) {
+ /* Start by advancing to the first non-annotation line. */
+ while (*s == '@')
+ NEXT_LINE();
+ }
+ CHECK_LENGTH();
+
+ /* Now we should be pointed at an onion-key line. If we are, then skip
+ * it. */
+ if (!strcmpstart(s, "onion-key"))
+ NEXT_LINE();
+
+ /* Okay, now we're pointed at the first line of the microdescriptor which is
+ not an annotation or onion-key. The next line that _is_ an annotation or
+ onion-key is the start of the next microdescriptor. */
+ while (s+32 < eos) {
+ if (*s == '@' || !strcmpstart(s, "onion-key"))
+ return s;
+ NEXT_LINE();
+ }
+ return NULL;
+
+#undef CHECK_LENGTH
+#undef NEXT_LINE
+}
+
+/** Parse as many microdescriptors as are found from the string starting at
+ * <b>s</b> and ending at <b>eos</b>. If allow_annotations is set, read any
+ * annotations we recognize and ignore ones we don't.
+ *
+ * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
+ * descriptor in the body field of each microdesc_t.
+ *
+ * Return all newly parsed microdescriptors in a newly allocated
+ * smartlist_t. If <b>invalid_disgests_out</b> is provided, add a SHA256
+ * microdesc digest to it for every microdesc that we found to be badly
+ * formed. (This may cause duplicates) */
+smartlist_t *
+microdescs_parse_from_string(const char *s, const char *eos,
+ int allow_annotations,
+ saved_location_t where,
+ smartlist_t *invalid_digests_out)
+{
+ smartlist_t *tokens;
+ smartlist_t *result;
+ microdesc_t *md = NULL;
+ memarea_t *area;
+ const char *start = s;
+ const char *start_of_next_microdesc;
+ int flags = allow_annotations ? TS_ANNOTATIONS_OK : 0;
+ const int copy_body = (where != SAVED_IN_CACHE);
+
+ directory_token_t *tok;
+
+ if (!eos)
+ eos = s + strlen(s);
+
+ s = eat_whitespace_eos(s, eos);
+ area = memarea_new();
+ result = smartlist_new();
+ tokens = smartlist_new();
+
+ while (s < eos) {
+ int okay = 0;
+
+ start_of_next_microdesc = find_start_of_next_microdesc(s, eos);
+ if (!start_of_next_microdesc)
+ start_of_next_microdesc = eos;
+
+ md = tor_malloc_zero(sizeof(microdesc_t));
+ {
+ const char *cp = tor_memstr(s, start_of_next_microdesc-s,
+ "onion-key");
+ const int no_onion_key = (cp == NULL);
+ if (no_onion_key) {
+ cp = s; /* So that we have *some* junk to put in the body */
+ }
+
+ md->bodylen = start_of_next_microdesc - cp;
+ md->saved_location = where;
+ if (copy_body)
+ md->body = tor_memdup_nulterm(cp, md->bodylen);
+ else
+ md->body = (char*)cp;
+ md->off = cp - start;
+ crypto_digest256(md->digest, md->body, md->bodylen, DIGEST_SHA256);
+ if (no_onion_key) {
+ log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Malformed or truncated descriptor");
+ goto next;
+ }
+ }
+
+ if (tokenize_string(area, s, start_of_next_microdesc, tokens,
+ microdesc_token_table, flags)) {
+ log_warn(LD_DIR, "Unparseable microdescriptor");
+ goto next;
+ }
+
+ if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
+ if (parse_iso_time(tok->args[0], &md->last_listed)) {
+ log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
+ goto next;
+ }
+ }
+
+ tok = find_by_keyword(tokens, K_ONION_KEY);
+ if (!crypto_pk_public_exponent_ok(tok->key)) {
+ log_warn(LD_DIR,
+ "Relay's onion key had invalid exponent.");
+ goto next;
+ }
+ router_set_rsa_onion_pkey(tok->key, &md->onion_pkey,
+ &md->onion_pkey_len);
+ crypto_pk_free(tok->key);
+
+ if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
+ curve25519_public_key_t k;
+ tor_assert(tok->n_args >= 1);
+ if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
+ log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
+ goto next;
+ }
+ md->onion_curve25519_pkey =
+ tor_memdup(&k, sizeof(curve25519_public_key_t));
+ }
+
+ smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
+ if (id_lines) {
+ SMARTLIST_FOREACH_BEGIN(id_lines, directory_token_t *, t) {
+ tor_assert(t->n_args >= 2);
+ if (!strcmp(t->args[0], "ed25519")) {
+ if (md->ed25519_identity_pkey) {
+ log_warn(LD_DIR, "Extra ed25519 key in microdesc");
+ smartlist_free(id_lines);
+ goto next;
+ }
+ ed25519_public_key_t k;
+ if (ed25519_public_from_base64(&k, t->args[1])<0) {
+ log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
+ smartlist_free(id_lines);
+ goto next;
+ }
+ md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
+ }
+ } SMARTLIST_FOREACH_END(t);
+ smartlist_free(id_lines);
+ }
+
+ {
+ smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
+ if (a_lines) {
+ find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
+ smartlist_free(a_lines);
+ }
+ }
+
+ if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
+ int i;
+ md->family = smartlist_new();
+ for (i=0;i<tok->n_args;++i) {
+ if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
+ log_warn(LD_DIR, "Illegal nickname %s in family line",
+ escaped(tok->args[i]));
+ goto next;
+ }
+ smartlist_add_strdup(md->family, tok->args[i]);
+ }
+ }
+
+ if ((tok = find_opt_by_keyword(tokens, K_P))) {
+ md->exit_policy = parse_short_policy(tok->args[0]);
+ }
+ if ((tok = find_opt_by_keyword(tokens, K_P6))) {
+ md->ipv6_exit_policy = parse_short_policy(tok->args[0]);
+ }
+
+ smartlist_add(result, md);
+ okay = 1;
+
+ md = NULL;
+ next:
+ if (! okay && invalid_digests_out) {
+ smartlist_add(invalid_digests_out,
+ tor_memdup(md->digest, DIGEST256_LEN));
+ }
+ microdesc_free(md);
+ md = NULL;
+
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ memarea_clear(area);
+ smartlist_clear(tokens);
+ s = start_of_next_microdesc;
+ }
+
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ memarea_drop_all(area);
+ smartlist_free(tokens);
+
+ return result;
+}
diff --git a/src/feature/dirparse/microdesc_parse.h b/src/feature/dirparse/microdesc_parse.h
new file mode 100644
index 000000000..e8f8b2e17
--- /dev/null
+++ b/src/feature/dirparse/microdesc_parse.h
@@ -0,0 +1,20 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file microdesc_parse.h
+ * \brief Header file for microdesc_parse.c.
+ **/
+
+#ifndef TOR_MICRODESC_PARSE_H
+#define TOR_MICRODESC_PARSE_H
+
+smartlist_t *microdescs_parse_from_string(const char *s, const char *eos,
+ int allow_annotations,
+ saved_location_t where,
+ smartlist_t *invalid_digests_out);
+
+#endif
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index f661042fe..599f2147c 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -309,19 +309,6 @@ static token_rule_t networkstatus_detached_signature_token_table[] = {
END_OF_TABLE
};
-/** List of tokens recognized in microdescriptors */
-static token_rule_t microdesc_token_table[] = {
- T1_START("onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY_1024),
- T01("ntor-onion-key", K_ONION_KEY_NTOR, GE(1), NO_OBJ ),
- T0N("id", K_ID, GE(2), NO_OBJ ),
- T0N("a", K_A, GE(1), NO_OBJ ),
- T01("family", K_FAMILY, ARGS, NO_OBJ ),
- T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
- T01("p6", K_P6, CONCAT_ARGS, NO_OBJ ),
- A01("@last-listed", A_LAST_LISTED, CONCAT_ARGS, NO_OBJ ),
- END_OF_TABLE
-};
-
#undef T
/* static function prototypes */
@@ -541,7 +528,7 @@ router_parse_list_from_string(const char **s, const char *eos,
* with at least one argument (use GE(1) in setup). If found, store
* address and port number to <b>addr_out</b> and
* <b>port_out</b>. Return number of OR ports found. */
-static int
+int
find_single_ipv6_orport(const smartlist_t *list,
tor_addr_t *addr_out,
uint16_t *port_out)
@@ -3387,233 +3374,6 @@ find_all_exitpolicy(smartlist_t *s)
return out;
}
-/** Assuming that s starts with a microdesc, return the start of the
- * *NEXT* one. Return NULL on "not found." */
-static const char *
-find_start_of_next_microdesc(const char *s, const char *eos)
-{
- int started_with_annotations;
- s = eat_whitespace_eos(s, eos);
- if (!s)
- return NULL;
-
-#define CHECK_LENGTH() STMT_BEGIN \
- if (s+32 > eos) \
- return NULL; \
- STMT_END
-
-#define NEXT_LINE() STMT_BEGIN \
- s = memchr(s, '\n', eos-s); \
- if (!s || s+1 >= eos) \
- return NULL; \
- s++; \
- STMT_END
-
- CHECK_LENGTH();
-
- started_with_annotations = (*s == '@');
-
- if (started_with_annotations) {
- /* Start by advancing to the first non-annotation line. */
- while (*s == '@')
- NEXT_LINE();
- }
- CHECK_LENGTH();
-
- /* Now we should be pointed at an onion-key line. If we are, then skip
- * it. */
- if (!strcmpstart(s, "onion-key"))
- NEXT_LINE();
-
- /* Okay, now we're pointed at the first line of the microdescriptor which is
- not an annotation or onion-key. The next line that _is_ an annotation or
- onion-key is the start of the next microdescriptor. */
- while (s+32 < eos) {
- if (*s == '@' || !strcmpstart(s, "onion-key"))
- return s;
- NEXT_LINE();
- }
- return NULL;
-
-#undef CHECK_LENGTH
-#undef NEXT_LINE
-}
-
-/** Parse as many microdescriptors as are found from the string starting at
- * <b>s</b> and ending at <b>eos</b>. If allow_annotations is set, read any
- * annotations we recognize and ignore ones we don't.
- *
- * If <b>saved_location</b> isn't SAVED_IN_CACHE, make a local copy of each
- * descriptor in the body field of each microdesc_t.
- *
- * Return all newly parsed microdescriptors in a newly allocated
- * smartlist_t. If <b>invalid_disgests_out</b> is provided, add a SHA256
- * microdesc digest to it for every microdesc that we found to be badly
- * formed. (This may cause duplicates) */
-smartlist_t *
-microdescs_parse_from_string(const char *s, const char *eos,
- int allow_annotations,
- saved_location_t where,
- smartlist_t *invalid_digests_out)
-{
- smartlist_t *tokens;
- smartlist_t *result;
- microdesc_t *md = NULL;
- memarea_t *area;
- const char *start = s;
- const char *start_of_next_microdesc;
- int flags = allow_annotations ? TS_ANNOTATIONS_OK : 0;
- const int copy_body = (where != SAVED_IN_CACHE);
-
- directory_token_t *tok;
-
- if (!eos)
- eos = s + strlen(s);
-
- s = eat_whitespace_eos(s, eos);
- area = memarea_new();
- result = smartlist_new();
- tokens = smartlist_new();
-
- while (s < eos) {
- int okay = 0;
-
- start_of_next_microdesc = find_start_of_next_microdesc(s, eos);
- if (!start_of_next_microdesc)
- start_of_next_microdesc = eos;
-
- md = tor_malloc_zero(sizeof(microdesc_t));
- {
- const char *cp = tor_memstr(s, start_of_next_microdesc-s,
- "onion-key");
- const int no_onion_key = (cp == NULL);
- if (no_onion_key) {
- cp = s; /* So that we have *some* junk to put in the body */
- }
-
- md->bodylen = start_of_next_microdesc - cp;
- md->saved_location = where;
- if (copy_body)
- md->body = tor_memdup_nulterm(cp, md->bodylen);
- else
- md->body = (char*)cp;
- md->off = cp - start;
- crypto_digest256(md->digest, md->body, md->bodylen, DIGEST_SHA256);
- if (no_onion_key) {
- log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Malformed or truncated descriptor");
- goto next;
- }
- }
-
- if (tokenize_string(area, s, start_of_next_microdesc, tokens,
- microdesc_token_table, flags)) {
- log_warn(LD_DIR, "Unparseable microdescriptor");
- goto next;
- }
-
- if ((tok = find_opt_by_keyword(tokens, A_LAST_LISTED))) {
- if (parse_iso_time(tok->args[0], &md->last_listed)) {
- log_warn(LD_DIR, "Bad last-listed time in microdescriptor");
- goto next;
- }
- }
-
- tok = find_by_keyword(tokens, K_ONION_KEY);
- if (!crypto_pk_public_exponent_ok(tok->key)) {
- log_warn(LD_DIR,
- "Relay's onion key had invalid exponent.");
- goto next;
- }
- router_set_rsa_onion_pkey(tok->key, &md->onion_pkey,
- &md->onion_pkey_len);
- crypto_pk_free(tok->key);
-
- if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
- curve25519_public_key_t k;
- tor_assert(tok->n_args >= 1);
- if (curve25519_public_from_base64(&k, tok->args[0]) < 0) {
- log_warn(LD_DIR, "Bogus ntor-onion-key in microdesc");
- goto next;
- }
- md->onion_curve25519_pkey =
- tor_memdup(&k, sizeof(curve25519_public_key_t));
- }
-
- smartlist_t *id_lines = find_all_by_keyword(tokens, K_ID);
- if (id_lines) {
- SMARTLIST_FOREACH_BEGIN(id_lines, directory_token_t *, t) {
- tor_assert(t->n_args >= 2);
- if (!strcmp(t->args[0], "ed25519")) {
- if (md->ed25519_identity_pkey) {
- log_warn(LD_DIR, "Extra ed25519 key in microdesc");
- smartlist_free(id_lines);
- goto next;
- }
- ed25519_public_key_t k;
- if (ed25519_public_from_base64(&k, t->args[1])<0) {
- log_warn(LD_DIR, "Bogus ed25519 key in microdesc");
- smartlist_free(id_lines);
- goto next;
- }
- md->ed25519_identity_pkey = tor_memdup(&k, sizeof(k));
- }
- } SMARTLIST_FOREACH_END(t);
- smartlist_free(id_lines);
- }
-
- {
- smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
- if (a_lines) {
- find_single_ipv6_orport(a_lines, &md->ipv6_addr, &md->ipv6_orport);
- smartlist_free(a_lines);
- }
- }
-
- if ((tok = find_opt_by_keyword(tokens, K_FAMILY))) {
- int i;
- md->family = smartlist_new();
- for (i=0;i<tok->n_args;++i) {
- if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
- log_warn(LD_DIR, "Illegal nickname %s in family line",
- escaped(tok->args[i]));
- goto next;
- }
- smartlist_add_strdup(md->family, tok->args[i]);
- }
- }
-
- if ((tok = find_opt_by_keyword(tokens, K_P))) {
- md->exit_policy = parse_short_policy(tok->args[0]);
- }
- if ((tok = find_opt_by_keyword(tokens, K_P6))) {
- md->ipv6_exit_policy = parse_short_policy(tok->args[0]);
- }
-
- smartlist_add(result, md);
- okay = 1;
-
- md = NULL;
- next:
- if (! okay && invalid_digests_out) {
- smartlist_add(invalid_digests_out,
- tor_memdup(md->digest, DIGEST256_LEN));
- }
- microdesc_free(md);
- md = NULL;
-
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- memarea_clear(area);
- smartlist_clear(tokens);
- s = start_of_next_microdesc;
- }
-
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- memarea_drop_all(area);
- smartlist_free(tokens);
-
- return result;
-}
-
/** Called on startup; right now we just handle scanning the unparseable
* descriptor dumps, but hang anything else we might need to do in the
* future here as well.
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index f90ad08a9..00020936e 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -54,10 +54,9 @@ networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
ns_detached_signatures_t *networkstatus_parse_detached_signatures(
const char *s, const char *eos);
-smartlist_t *microdescs_parse_from_string(const char *s, const char *eos,
- int allow_annotations,
- saved_location_t where,
- smartlist_t *invalid_digests_out);
+int find_single_ipv6_orport(const smartlist_t *list,
+ tor_addr_t *addr_out,
+ uint16_t *port_out);
void routerparse_init(void);
void routerparse_free_all(void);
diff --git a/src/feature/nodelist/microdesc.c b/src/feature/nodelist/microdesc.c
index 85bc59812..3716513f0 100644
--- a/src/feature/nodelist/microdesc.c
+++ b/src/feature/nodelist/microdesc.c
@@ -12,20 +12,21 @@
#include "lib/fdio/fdio.h"
-#include "core/or/circuitbuild.h"
#include "app/config/config.h"
-#include "feature/dircommon/directory.h"
+#include "core/or/circuitbuild.h"
+#include "core/or/policies.h"
+#include "feature/client/entrynodes.h"
#include "feature/dircache/dirserv.h"
#include "feature/dirclient/dlstatus.h"
-#include "feature/client/entrynodes.h"
+#include "feature/dircommon/directory.h"
+#include "feature/dirparse/microdesc_parse.h"
+#include "feature/dirparse/routerparse.h"
+#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
-#include "core/or/policies.h"
-#include "feature/relay/router.h"
-#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/routerlist.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/relay/router.h"
#include "feature/nodelist/microdesc_st.h"
#include "feature/nodelist/networkstatus_st.h"
diff --git a/src/test/fuzz/fuzz_microdesc.c b/src/test/fuzz/fuzz_microdesc.c
index 789e522af..ab54cf2a3 100644
--- a/src/test/fuzz/fuzz_microdesc.c
+++ b/src/test/fuzz/fuzz_microdesc.c
@@ -1,8 +1,8 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
+
#include "core/or/or.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/microdesc_parse.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/microdesc.h"
#include "lib/crypt_ops/crypto_ed25519.h"
diff --git a/src/test/test_microdesc.c b/src/test/test_microdesc.c
index bec8053f6..8ede2690e 100644
--- a/src/test/test_microdesc.c
+++ b/src/test/test_microdesc.c
@@ -4,13 +4,14 @@
#include "orconfig.h"
#include "core/or/or.h"
-#include "app/config/config.h"
#define DIRVOTE_PRIVATE
+#include "app/config/config.h"
#include "feature/dirauth/dirvote.h"
+#include "feature/dirparse/microdesc_parse.h"
+#include "feature/dirparse/routerparse.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/routerlist.h"
-#include "feature/dirparse/routerparse.h"
#include "feature/nodelist/torcert.h"
#include "feature/nodelist/microdesc_st.h"
1
0

12 Oct '18
commit 95e2eb9083d2cd9c79c3f4151850c86cbeaf4cc4
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Mon Oct 1 10:48:55 2018 -0500
Move summarize_protover_flags to versions.c
---
src/core/or/versions.c | 45 ++++++++++++++++++++++++++++++++++++++
src/core/or/versions.h | 4 ++++
src/feature/dirparse/routerparse.c | 44 -------------------------------------
src/feature/dirparse/routerparse.h | 4 ----
4 files changed, 49 insertions(+), 48 deletions(-)
diff --git a/src/core/or/versions.c b/src/core/or/versions.c
index 2d2486298..06274996a 100644
--- a/src/core/or/versions.c
+++ b/src/core/or/versions.c
@@ -10,6 +10,7 @@
*/
#include "core/or/or.h"
+#include "core/or/protover.h"
#include "core/or/versions.h"
#include "lib/crypt_ops/crypto_util.h"
@@ -375,3 +376,47 @@ sort_version_list(smartlist_t *versions, int remove_duplicates)
if (remove_duplicates)
smartlist_uniq(versions, compare_tor_version_str_ptr_, tor_free_);
}
+
+/** Summarize the protocols listed in <b>protocols</b> into <b>out</b>,
+ * falling back or correcting them based on <b>version</b> as appropriate.
+ */
+void
+summarize_protover_flags(protover_summary_flags_t *out,
+ const char *protocols,
+ const char *version)
+{
+ tor_assert(out);
+ memset(out, 0, sizeof(*out));
+ if (protocols) {
+ out->protocols_known = 1;
+ out->supports_extend2_cells =
+ protocol_list_supports_protocol(protocols, PRT_RELAY, 2);
+ out->supports_ed25519_link_handshake_compat =
+ protocol_list_supports_protocol(protocols, PRT_LINKAUTH, 3);
+ out->supports_ed25519_link_handshake_any =
+ protocol_list_supports_protocol_or_later(protocols, PRT_LINKAUTH, 3);
+ out->supports_ed25519_hs_intro =
+ protocol_list_supports_protocol(protocols, PRT_HSINTRO, 4);
+ out->supports_v3_hsdir =
+ protocol_list_supports_protocol(protocols, PRT_HSDIR,
+ PROTOVER_HSDIR_V3);
+ out->supports_v3_rendezvous_point =
+ protocol_list_supports_protocol(protocols, PRT_HSREND,
+ PROTOVER_HS_RENDEZVOUS_POINT_V3);
+ }
+ if (version && !strcmpstart(version, "Tor ")) {
+ if (!out->protocols_known) {
+ /* The version is a "Tor" version, and where there is no
+ * list of protocol versions that we should be looking at instead. */
+
+ out->supports_extend2_cells =
+ tor_version_as_new_as(version, "0.2.4.8-alpha");
+ out->protocols_known = 1;
+ } else {
+ /* Bug #22447 forces us to filter on this version. */
+ if (!tor_version_as_new_as(version, "0.3.0.8")) {
+ out->supports_v3_hsdir = 0;
+ }
+ }
+ }
+}
diff --git a/src/core/or/versions.h b/src/core/or/versions.h
index a2353bcae..0c773f3f4 100644
--- a/src/core/or/versions.h
+++ b/src/core/or/versions.h
@@ -37,4 +37,8 @@ int tor_version_compare(tor_version_t *a, tor_version_t *b);
int tor_version_same_series(tor_version_t *a, tor_version_t *b);
void sort_version_list(smartlist_t *lst, int remove_duplicates);
+void summarize_protover_flags(protover_summary_flags_t *out,
+ const char *protocols,
+ const char *version);
+
#endif /* !defined(TOR_VERSIONS_H) */
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index 956e8f3ea..0e9773564 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -1201,50 +1201,6 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
return extrainfo;
}
-/** Summarize the protocols listed in <b>protocols</b> into <b>out</b>,
- * falling back or correcting them based on <b>version</b> as appropriate.
- */
-void
-summarize_protover_flags(protover_summary_flags_t *out,
- const char *protocols,
- const char *version)
-{
- tor_assert(out);
- memset(out, 0, sizeof(*out));
- if (protocols) {
- out->protocols_known = 1;
- out->supports_extend2_cells =
- protocol_list_supports_protocol(protocols, PRT_RELAY, 2);
- out->supports_ed25519_link_handshake_compat =
- protocol_list_supports_protocol(protocols, PRT_LINKAUTH, 3);
- out->supports_ed25519_link_handshake_any =
- protocol_list_supports_protocol_or_later(protocols, PRT_LINKAUTH, 3);
- out->supports_ed25519_hs_intro =
- protocol_list_supports_protocol(protocols, PRT_HSINTRO, 4);
- out->supports_v3_hsdir =
- protocol_list_supports_protocol(protocols, PRT_HSDIR,
- PROTOVER_HSDIR_V3);
- out->supports_v3_rendezvous_point =
- protocol_list_supports_protocol(protocols, PRT_HSREND,
- PROTOVER_HS_RENDEZVOUS_POINT_V3);
- }
- if (version && !strcmpstart(version, "Tor ")) {
- if (!out->protocols_known) {
- /* The version is a "Tor" version, and where there is no
- * list of protocol versions that we should be looking at instead. */
-
- out->supports_extend2_cells =
- tor_version_as_new_as(version, "0.2.4.8-alpha");
- out->protocols_known = 1;
- } else {
- /* Bug #22447 forces us to filter on this version. */
- if (!tor_version_as_new_as(version, "0.3.0.8")) {
- out->supports_v3_hsdir = 0;
- }
- }
- }
-}
-
/** Parse the addr policy in the string <b>s</b> and return it. If
* assume_action is nonnegative, then insert its action (ADDR_POLICY_ACCEPT or
* ADDR_POLICY_REJECT) for items that specify no action.
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index 80c974e06..72f812ee4 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -44,10 +44,6 @@ int find_single_ipv6_orport(const smartlist_t *list,
void routerparse_init(void);
void routerparse_free_all(void);
-void summarize_protover_flags(protover_summary_flags_t *out,
- const char *protocols,
- const char *version);
-
#define ED_DESC_SIGNATURE_PREFIX "Tor router descriptor signature v1"
#endif /* !defined(TOR_ROUTERPARSE_H) */
1
0

12 Oct '18
commit 4201203845fa563a7f7410609267b96f2c319720
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Mon Oct 1 10:44:59 2018 -0500
extract networkstatus parsing to its own file.
---
src/core/include.am | 2 +
src/feature/dirauth/bwauth.c | 2 +-
src/feature/dirauth/dirvote.c | 33 +-
src/feature/dirauth/guardfraction.c | 2 +-
src/feature/dircache/consdiffmgr.c | 2 +-
src/feature/dircommon/consdiff.c | 2 +-
src/feature/dirparse/ns_parse.c | 1684 ++++++++++++++++++++++++++++++++++
src/feature/dirparse/ns_parse.h | 45 +
src/feature/dirparse/routerparse.c | 1646 +--------------------------------
src/feature/dirparse/routerparse.h | 33 +-
src/feature/nodelist/networkstatus.c | 3 +-
src/test/fuzz/fuzz_consensus.c | 2 +-
src/test/fuzz/fuzz_vrs.c | 4 +-
src/test/test_consdiffmgr.c | 2 +-
src/test/test_dir.c | 2 +
src/test/test_dir_common.c | 1 +
src/test/test_guardfraction.c | 4 +-
src/test/test_routerlist.c | 1 +
18 files changed, 1767 insertions(+), 1703 deletions(-)
diff --git a/src/core/include.am b/src/core/include.am
index 6815f327f..1cecd8ba7 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -75,6 +75,7 @@ LIBTOR_APP_A_SOURCES = \
src/feature/dircommon/voting_schedule.c \
src/feature/dirparse/authcert_parse.c \
src/feature/dirparse/microdesc_parse.c \
+ src/feature/dirparse/ns_parse.c \
src/feature/dirparse/parsecommon.c \
src/feature/dirparse/routerparse.c \
src/feature/dirparse/sigcommon.c \
@@ -298,6 +299,7 @@ noinst_HEADERS += \
src/feature/dirparse/authcert_members.i \
src/feature/dirparse/authcert_parse.h \
src/feature/dirparse/microdesc_parse.h \
+ src/feature/dirparse/ns_parse.h \
src/feature/dirparse/parsecommon.h \
src/feature/dirparse/routerparse.h \
src/feature/dirparse/sigcommon.h \
diff --git a/src/feature/dirauth/bwauth.c b/src/feature/dirauth/bwauth.c
index ec23f629c..29e0c9096 100644
--- a/src/feature/dirauth/bwauth.c
+++ b/src/feature/dirauth/bwauth.c
@@ -15,7 +15,7 @@
#include "app/config/config.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/routerlist.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/vote_routerstatus_st.h"
diff --git a/src/feature/dirauth/dirvote.c b/src/feature/dirauth/dirvote.c
index 0322c9cae..80a5b5473 100644
--- a/src/feature/dirauth/dirvote.c
+++ b/src/feature/dirauth/dirvote.c
@@ -6,32 +6,33 @@
#define DIRVOTE_PRIVATE
#include "core/or/or.h"
#include "app/config/config.h"
+#include "core/or/policies.h"
+#include "core/or/protover.h"
+#include "core/or/tor_version_st.h"
+#include "core/or/versions.h"
+#include "feature/dirauth/bwauth.h"
#include "feature/dirauth/dircollate.h"
#include "feature/dirauth/dsigs_parse.h"
+#include "feature/dirauth/guardfraction.h"
#include "feature/dirauth/recommend_pkg.h"
#include "feature/dirauth/voteflags.h"
-#include "feature/dircommon/directory.h"
-#include "feature/dirclient/dirclient.h"
-#include "feature/dirauth/bwauth.h"
#include "feature/dircache/dirserv.h"
-#include "feature/dirauth/guardfraction.h"
+#include "feature/dirclient/dirclient.h"
+#include "feature/dircommon/directory.h"
+#include "feature/dirparse/microdesc_parse.h"
+#include "feature/dirparse/ns_parse.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/signing.h"
+#include "feature/nodelist/authcert.h"
+#include "feature/nodelist/dirlist.h"
+#include "feature/nodelist/fmt_routerstatus.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
-#include "feature/dirparse/parsecommon.h"
-#include "core/or/policies.h"
-#include "core/or/protover.h"
-#include "core/or/tor_version_st.h"
-#include "feature/stats/rephist.h"
+#include "feature/nodelist/routerlist.h"
#include "feature/relay/router.h"
#include "feature/relay/routerkeys.h"
-#include "feature/nodelist/authcert.h"
-#include "feature/nodelist/dirlist.h"
-#include "feature/nodelist/routerlist.h"
-#include "feature/dirparse/routerparse.h"
-#include "feature/dirparse/microdesc_parse.h"
-#include "feature/dirparse/signing.h"
-#include "feature/nodelist/fmt_routerstatus.h"
+#include "feature/stats/rephist.h"
#include "feature/client/entrynodes.h" /* needed for guardfraction methods */
#include "feature/nodelist/torcert.h"
#include "feature/dircommon/voting_schedule.h"
diff --git a/src/feature/dirauth/guardfraction.c b/src/feature/dirauth/guardfraction.c
index 812cb7df2..1734d0a13 100644
--- a/src/feature/dirauth/guardfraction.c
+++ b/src/feature/dirauth/guardfraction.c
@@ -12,7 +12,7 @@
#include "core/or/or.h"
#include "feature/dirauth/guardfraction.h"
#include "feature/nodelist/networkstatus.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/nodelist/vote_routerstatus_st.h"
diff --git a/src/feature/dircache/consdiffmgr.c b/src/feature/dircache/consdiffmgr.c
index 7dc56f4f1..e79aad6ef 100644
--- a/src/feature/dircache/consdiffmgr.c
+++ b/src/feature/dircache/consdiffmgr.c
@@ -20,7 +20,7 @@
#include "feature/dircache/consdiffmgr.h"
#include "core/mainloop/cpuworker.h"
#include "feature/nodelist/networkstatus.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "lib/evloop/compat_libevent.h"
#include "lib/evloop/workqueue.h"
#include "lib/compress/compress.h"
diff --git a/src/feature/dircommon/consdiff.c b/src/feature/dircommon/consdiff.c
index 944ddd552..f8ced8503 100644
--- a/src/feature/dircommon/consdiff.c
+++ b/src/feature/dircommon/consdiff.c
@@ -41,7 +41,7 @@
#include "core/or/or.h"
#include "feature/dircommon/consdiff.h"
#include "lib/memarea/memarea.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
static const char* ns_diff_version = "network-status-diff-version 1";
static const char* hash_token = "hash";
diff --git a/src/feature/dirparse/ns_parse.c b/src/feature/dirparse/ns_parse.c
new file mode 100644
index 000000000..e356c0c11
--- /dev/null
+++ b/src/feature/dirparse/ns_parse.c
@@ -0,0 +1,1684 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file routerparse.c
+ * \brief Code to parse and validate consensus documents and votes.
+ */
+
+#define NS_PARSE_PRIVATE
+
+#include "core/or/or.h"
+#include "app/config/config.h"
+#include "feature/client/entrynodes.h"
+#include "feature/dirauth/dirvote.h"
+#include "feature/dirparse/authcert_parse.h"
+#include "feature/dirparse/ns_parse.h"
+#include "feature/dirparse/parsecommon.h"
+#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/sigcommon.h"
+#include "feature/dirparse/unparseable.h"
+#include "feature/hs_common/shared_random_client.h"
+#include "feature/nodelist/authcert.h"
+#include "feature/nodelist/describe.h"
+#include "feature/nodelist/networkstatus.h"
+#include "feature/nodelist/nickname.h"
+#include "lib/crypt_ops/crypto_format.h"
+#include "lib/memarea/memarea.h"
+
+#include "feature/dirauth/vote_microdesc_hash_st.h"
+#include "feature/nodelist/authority_cert_st.h"
+#include "feature/nodelist/document_signature_st.h"
+#include "feature/nodelist/networkstatus_st.h"
+#include "feature/nodelist/networkstatus_voter_info_st.h"
+#include "feature/nodelist/vote_routerstatus_st.h"
+
+#undef log
+#include <math.h>
+
+/** List of tokens recognized in the body part of v3 networkstatus
+ * documents. */
+static token_rule_t rtrstatus_token_table[] = {
+ T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
+ T1( "r", K_R, GE(7), NO_OBJ ),
+ T0N("a", K_A, GE(1), NO_OBJ ),
+ T1( "s", K_S, ARGS, NO_OBJ ),
+ T01("v", K_V, CONCAT_ARGS, NO_OBJ ),
+ T01("w", K_W, ARGS, NO_OBJ ),
+ T0N("m", K_M, CONCAT_ARGS, NO_OBJ ),
+ T0N("id", K_ID, GE(2), NO_OBJ ),
+ T01("pr", K_PROTO, CONCAT_ARGS, NO_OBJ ),
+ T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
+ END_OF_TABLE
+};
+
+/** List of tokens recognized in V3 networkstatus votes. */
+static token_rule_t networkstatus_token_table[] = {
+ T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
+ GE(1), NO_OBJ ),
+ T1("vote-status", K_VOTE_STATUS, GE(1), NO_OBJ ),
+ T1("published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
+ T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
+ T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
+ T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
+ T1("voting-delay", K_VOTING_DELAY, GE(2), NO_OBJ ),
+ T1("known-flags", K_KNOWN_FLAGS, ARGS, NO_OBJ ),
+ T01("params", K_PARAMS, ARGS, NO_OBJ ),
+ T( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
+ T01("signing-ed25519", K_SIGNING_CERT_ED, NO_ARGS , NEED_OBJ ),
+ T01("shared-rand-participate",K_SR_FLAG, NO_ARGS, NO_OBJ ),
+ T0N("shared-rand-commit", K_COMMIT, GE(3), NO_OBJ ),
+ T01("shared-rand-previous-value", K_PREVIOUS_SRV,EQ(2), NO_OBJ ),
+ T01("shared-rand-current-value", K_CURRENT_SRV, EQ(2), NO_OBJ ),
+ T0N("package", K_PACKAGE, CONCAT_ARGS, NO_OBJ ),
+ T01("recommended-client-protocols", K_RECOMMENDED_CLIENT_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+ T01("recommended-relay-protocols", K_RECOMMENDED_RELAY_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+ T01("required-client-protocols", K_REQUIRED_CLIENT_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+ T01("required-relay-protocols", K_REQUIRED_RELAY_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+
+#include "feature/dirparse/authcert_members.i"
+
+ T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
+ T1( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
+ T1( "dir-source", K_DIR_SOURCE, GE(6), NO_OBJ ),
+ T01("legacy-dir-key", K_LEGACY_DIR_KEY, GE(1), NO_OBJ ),
+ T1( "known-flags", K_KNOWN_FLAGS, CONCAT_ARGS, NO_OBJ ),
+ T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
+ T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
+ T1( "consensus-methods", K_CONSENSUS_METHODS, GE(1), NO_OBJ ),
+
+ END_OF_TABLE
+};
+
+/** List of tokens recognized in V3 networkstatus consensuses. */
+static token_rule_t networkstatus_consensus_token_table[] = {
+ T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
+ GE(1), NO_OBJ ),
+ T1("vote-status", K_VOTE_STATUS, GE(1), NO_OBJ ),
+ T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
+ T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
+ T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
+ T1("voting-delay", K_VOTING_DELAY, GE(2), NO_OBJ ),
+
+ T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
+
+ T1N("dir-source", K_DIR_SOURCE, GE(6), NO_OBJ ),
+ T1N("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
+ T1N("vote-digest", K_VOTE_DIGEST, GE(1), NO_OBJ ),
+
+ T1( "known-flags", K_KNOWN_FLAGS, CONCAT_ARGS, NO_OBJ ),
+
+ T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
+ T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
+ T01("consensus-method", K_CONSENSUS_METHOD, EQ(1), NO_OBJ),
+ T01("params", K_PARAMS, ARGS, NO_OBJ ),
+
+ T01("shared-rand-previous-value", K_PREVIOUS_SRV, EQ(2), NO_OBJ ),
+ T01("shared-rand-current-value", K_CURRENT_SRV, EQ(2), NO_OBJ ),
+
+ T01("recommended-client-protocols", K_RECOMMENDED_CLIENT_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+ T01("recommended-relay-protocols", K_RECOMMENDED_RELAY_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+ T01("required-client-protocols", K_REQUIRED_CLIENT_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+ T01("required-relay-protocols", K_REQUIRED_RELAY_PROTOCOLS,
+ CONCAT_ARGS, NO_OBJ ),
+
+ END_OF_TABLE
+};
+
+/** List of tokens recognized in the footer of v1 directory footers. */
+static token_rule_t networkstatus_vote_footer_token_table[] = {
+ T01("directory-footer", K_DIRECTORY_FOOTER, NO_ARGS, NO_OBJ ),
+ T01("bandwidth-weights", K_BW_WEIGHTS, ARGS, NO_OBJ ),
+ T( "directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
+ END_OF_TABLE
+};
+
+/** Try to find the start and end of the signed portion of a networkstatus
+ * document in <b>s</b>. On success, set <b>start_out</b> to the first
+ * character of the document, and <b>end_out</b> to a position one after the
+ * final character of the signed document, and return 0. On failure, return
+ * -1. */
+int
+router_get_networkstatus_v3_signed_boundaries(const char *s,
+ const char **start_out,
+ const char **end_out)
+{
+ return router_get_hash_impl_helper(s, strlen(s),
+ "network-status-version",
+ "\ndirectory-signature",
+ ' ', LOG_INFO,
+ start_out, end_out);
+}
+
+/** Set <b>digest_out</b> to the SHA3-256 digest of the signed portion of the
+ * networkstatus vote in <b>s</b> -- or of the entirety of <b>s</b> if no
+ * signed portion can be identified. Return 0 on success, -1 on failure. */
+int
+router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
+ const char *s)
+{
+ const char *start, *end;
+ if (router_get_networkstatus_v3_signed_boundaries(s, &start, &end) < 0) {
+ start = s;
+ end = s + strlen(s);
+ }
+ tor_assert(start);
+ tor_assert(end);
+ return crypto_digest256((char*)digest_out, start, end-start,
+ DIGEST_SHA3_256);
+}
+
+/** Set <b>digests</b> to all the digests of the consensus document in
+ * <b>s</b> */
+int
+router_get_networkstatus_v3_hashes(const char *s, common_digests_t *digests)
+{
+ return router_get_hashes_impl(s,strlen(s),digests,
+ "network-status-version",
+ "\ndirectory-signature",
+ ' ');
+}
+
+/** Helper: given a string <b>s</b>, return the start of the next router-status
+ * object (starting with "r " at the start of a line). If none is found,
+ * return the start of the directory footer, or the next directory signature.
+ * If none is found, return the end of the string. */
+static inline const char *
+find_start_of_next_routerstatus(const char *s)
+{
+ const char *eos, *footer, *sig;
+ if ((eos = strstr(s, "\nr ")))
+ ++eos;
+ else
+ eos = s + strlen(s);
+
+ footer = tor_memstr(s, eos-s, "\ndirectory-footer");
+ sig = tor_memstr(s, eos-s, "\ndirectory-signature");
+
+ if (footer && sig)
+ return MIN(footer, sig) + 1;
+ else if (footer)
+ return footer+1;
+ else if (sig)
+ return sig+1;
+ else
+ return eos;
+}
+
+/** Parse the GuardFraction string from a consensus or vote.
+ *
+ * If <b>vote</b> or <b>vote_rs</b> are set the document getting
+ * parsed is a vote routerstatus. Otherwise it's a consensus. This is
+ * the same semantic as in routerstatus_parse_entry_from_string(). */
+STATIC int
+routerstatus_parse_guardfraction(const char *guardfraction_str,
+ networkstatus_t *vote,
+ vote_routerstatus_t *vote_rs,
+ routerstatus_t *rs)
+{
+ int ok;
+ const char *end_of_header = NULL;
+ int is_consensus = !vote_rs;
+ uint32_t guardfraction;
+
+ tor_assert(bool_eq(vote, vote_rs));
+
+ /* If this info comes from a consensus, but we should't apply
+ guardfraction, just exit. */
+ if (is_consensus && !should_apply_guardfraction(NULL)) {
+ return 0;
+ }
+
+ end_of_header = strchr(guardfraction_str, '=');
+ if (!end_of_header) {
+ return -1;
+ }
+
+ guardfraction = (uint32_t)tor_parse_ulong(end_of_header+1,
+ 10, 0, 100, &ok, NULL);
+ if (!ok) {
+ log_warn(LD_DIR, "Invalid GuardFraction %s", escaped(guardfraction_str));
+ return -1;
+ }
+
+ log_debug(LD_GENERAL, "[*] Parsed %s guardfraction '%s' for '%s'.",
+ is_consensus ? "consensus" : "vote",
+ guardfraction_str, rs->nickname);
+
+ if (!is_consensus) { /* We are parsing a vote */
+ vote_rs->status.guardfraction_percentage = guardfraction;
+ vote_rs->status.has_guardfraction = 1;
+ } else {
+ /* We are parsing a consensus. Only apply guardfraction to guards. */
+ if (rs->is_possible_guard) {
+ rs->guardfraction_percentage = guardfraction;
+ rs->has_guardfraction = 1;
+ } else {
+ log_warn(LD_BUG, "Got GuardFraction for non-guard %s. "
+ "This is not supposed to happen. Not applying. ", rs->nickname);
+ }
+ }
+
+ return 0;
+}
+
+/** Given a string at *<b>s</b>, containing a routerstatus object, and an
+ * empty smartlist at <b>tokens</b>, parse and return the first router status
+ * object in the string, and advance *<b>s</b> to just after the end of the
+ * router status. Return NULL and advance *<b>s</b> on error.
+ *
+ * If <b>vote</b> and <b>vote_rs</b> are provided, don't allocate a fresh
+ * routerstatus but use <b>vote_rs</b> instead.
+ *
+ * If <b>consensus_method</b> is nonzero, this routerstatus is part of a
+ * consensus, and we should parse it according to the method used to
+ * make that consensus.
+ *
+ * Parse according to the syntax used by the consensus flavor <b>flav</b>.
+ **/
+STATIC routerstatus_t *
+routerstatus_parse_entry_from_string(memarea_t *area,
+ const char **s, smartlist_t *tokens,
+ networkstatus_t *vote,
+ vote_routerstatus_t *vote_rs,
+ int consensus_method,
+ consensus_flavor_t flav)
+{
+ const char *eos, *s_dup = *s;
+ routerstatus_t *rs = NULL;
+ directory_token_t *tok;
+ char timebuf[ISO_TIME_LEN+1];
+ struct in_addr in;
+ int offset = 0;
+ tor_assert(tokens);
+ tor_assert(bool_eq(vote, vote_rs));
+
+ if (!consensus_method)
+ flav = FLAV_NS;
+ tor_assert(flav == FLAV_NS || flav == FLAV_MICRODESC);
+
+ eos = find_start_of_next_routerstatus(*s);
+
+ if (tokenize_string(area,*s, eos, tokens, rtrstatus_token_table,0)) {
+ log_warn(LD_DIR, "Error tokenizing router status");
+ goto err;
+ }
+ if (smartlist_len(tokens) < 1) {
+ log_warn(LD_DIR, "Impossibly short router status");
+ goto err;
+ }
+ tok = find_by_keyword(tokens, K_R);
+ tor_assert(tok->n_args >= 7); /* guaranteed by GE(7) in K_R setup */
+ if (flav == FLAV_NS) {
+ if (tok->n_args < 8) {
+ log_warn(LD_DIR, "Too few arguments to r");
+ goto err;
+ }
+ } else if (flav == FLAV_MICRODESC) {
+ offset = -1; /* There is no descriptor digest in an md consensus r line */
+ }
+
+ if (vote_rs) {
+ rs = &vote_rs->status;
+ } else {
+ rs = tor_malloc_zero(sizeof(routerstatus_t));
+ }
+
+ if (!is_legal_nickname(tok->args[0])) {
+ log_warn(LD_DIR,
+ "Invalid nickname %s in router status; skipping.",
+ escaped(tok->args[0]));
+ goto err;
+ }
+ strlcpy(rs->nickname, tok->args[0], sizeof(rs->nickname));
+
+ if (digest_from_base64(rs->identity_digest, tok->args[1])) {
+ log_warn(LD_DIR, "Error decoding identity digest %s",
+ escaped(tok->args[1]));
+ goto err;
+ }
+
+ if (flav == FLAV_NS) {
+ if (digest_from_base64(rs->descriptor_digest, tok->args[2])) {
+ log_warn(LD_DIR, "Error decoding descriptor digest %s",
+ escaped(tok->args[2]));
+ goto err;
+ }
+ }
+
+ if (tor_snprintf(timebuf, sizeof(timebuf), "%s %s",
+ tok->args[3+offset], tok->args[4+offset]) < 0 ||
+ parse_iso_time(timebuf, &rs->published_on)<0) {
+ log_warn(LD_DIR, "Error parsing time '%s %s' [%d %d]",
+ tok->args[3+offset], tok->args[4+offset],
+ offset, (int)flav);
+ goto err;
+ }
+
+ if (tor_inet_aton(tok->args[5+offset], &in) == 0) {
+ log_warn(LD_DIR, "Error parsing router address in network-status %s",
+ escaped(tok->args[5+offset]));
+ goto err;
+ }
+ rs->addr = ntohl(in.s_addr);
+
+ rs->or_port = (uint16_t) tor_parse_long(tok->args[6+offset],
+ 10,0,65535,NULL,NULL);
+ rs->dir_port = (uint16_t) tor_parse_long(tok->args[7+offset],
+ 10,0,65535,NULL,NULL);
+
+ {
+ smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
+ if (a_lines) {
+ find_single_ipv6_orport(a_lines, &rs->ipv6_addr, &rs->ipv6_orport);
+ smartlist_free(a_lines);
+ }
+ }
+
+ tok = find_opt_by_keyword(tokens, K_S);
+ if (tok && vote) {
+ int i;
+ vote_rs->flags = 0;
+ for (i=0; i < tok->n_args; ++i) {
+ int p = smartlist_string_pos(vote->known_flags, tok->args[i]);
+ if (p >= 0) {
+ vote_rs->flags |= (UINT64_C(1)<<p);
+ } else {
+ log_warn(LD_DIR, "Flags line had a flag %s not listed in known_flags.",
+ escaped(tok->args[i]));
+ goto err;
+ }
+ }
+ } else if (tok) {
+ /* This is a consensus, not a vote. */
+ int i;
+ for (i=0; i < tok->n_args; ++i) {
+ if (!strcmp(tok->args[i], "Exit"))
+ rs->is_exit = 1;
+ else if (!strcmp(tok->args[i], "Stable"))
+ rs->is_stable = 1;
+ else if (!strcmp(tok->args[i], "Fast"))
+ rs->is_fast = 1;
+ else if (!strcmp(tok->args[i], "Running"))
+ rs->is_flagged_running = 1;
+ else if (!strcmp(tok->args[i], "Named"))
+ rs->is_named = 1;
+ else if (!strcmp(tok->args[i], "Valid"))
+ rs->is_valid = 1;
+ else if (!strcmp(tok->args[i], "Guard"))
+ rs->is_possible_guard = 1;
+ else if (!strcmp(tok->args[i], "BadExit"))
+ rs->is_bad_exit = 1;
+ else if (!strcmp(tok->args[i], "Authority"))
+ rs->is_authority = 1;
+ else if (!strcmp(tok->args[i], "Unnamed") &&
+ consensus_method >= 2) {
+ /* Unnamed is computed right by consensus method 2 and later. */
+ rs->is_unnamed = 1;
+ } else if (!strcmp(tok->args[i], "HSDir")) {
+ rs->is_hs_dir = 1;
+ } else if (!strcmp(tok->args[i], "V2Dir")) {
+ rs->is_v2_dir = 1;
+ }
+ }
+ /* These are implied true by having been included in a consensus made
+ * with a given method */
+ rs->is_flagged_running = 1; /* Starting with consensus method 4. */
+ rs->is_valid = 1; /* Starting with consensus method 24. */
+ }
+ {
+ const char *protocols = NULL, *version = NULL;
+ if ((tok = find_opt_by_keyword(tokens, K_PROTO))) {
+ tor_assert(tok->n_args == 1);
+ protocols = tok->args[0];
+ }
+ if ((tok = find_opt_by_keyword(tokens, K_V))) {
+ tor_assert(tok->n_args == 1);
+ version = tok->args[0];
+ if (vote_rs) {
+ vote_rs->version = tor_strdup(tok->args[0]);
+ }
+ }
+
+ summarize_protover_flags(&rs->pv, protocols, version);
+ }
+
+ /* handle weighting/bandwidth info */
+ if ((tok = find_opt_by_keyword(tokens, K_W))) {
+ int i;
+ for (i=0; i < tok->n_args; ++i) {
+ if (!strcmpstart(tok->args[i], "Bandwidth=")) {
+ int ok;
+ rs->bandwidth_kb =
+ (uint32_t)tor_parse_ulong(strchr(tok->args[i], '=')+1,
+ 10, 0, UINT32_MAX,
+ &ok, NULL);
+ if (!ok) {
+ log_warn(LD_DIR, "Invalid Bandwidth %s", escaped(tok->args[i]));
+ goto err;
+ }
+ rs->has_bandwidth = 1;
+ } else if (!strcmpstart(tok->args[i], "Measured=") && vote_rs) {
+ int ok;
+ vote_rs->measured_bw_kb =
+ (uint32_t)tor_parse_ulong(strchr(tok->args[i], '=')+1,
+ 10, 0, UINT32_MAX, &ok, NULL);
+ if (!ok) {
+ log_warn(LD_DIR, "Invalid Measured Bandwidth %s",
+ escaped(tok->args[i]));
+ goto err;
+ }
+ vote_rs->has_measured_bw = 1;
+ vote->has_measured_bws = 1;
+ } else if (!strcmpstart(tok->args[i], "Unmeasured=1")) {
+ rs->bw_is_unmeasured = 1;
+ } else if (!strcmpstart(tok->args[i], "GuardFraction=")) {
+ if (routerstatus_parse_guardfraction(tok->args[i],
+ vote, vote_rs, rs) < 0) {
+ goto err;
+ }
+ }
+ }
+ }
+
+ /* parse exit policy summaries */
+ if ((tok = find_opt_by_keyword(tokens, K_P))) {
+ tor_assert(tok->n_args == 1);
+ if (strcmpstart(tok->args[0], "accept ") &&
+ strcmpstart(tok->args[0], "reject ")) {
+ log_warn(LD_DIR, "Unknown exit policy summary type %s.",
+ escaped(tok->args[0]));
+ goto err;
+ }
+ /* XXX weasel: parse this into ports and represent them somehow smart,
+ * maybe not here but somewhere on if we need it for the client.
+ * we should still parse it here to check it's valid tho.
+ */
+ rs->exitsummary = tor_strdup(tok->args[0]);
+ rs->has_exitsummary = 1;
+ }
+
+ if (vote_rs) {
+ SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, t) {
+ if (t->tp == K_M && t->n_args) {
+ vote_microdesc_hash_t *line =
+ tor_malloc(sizeof(vote_microdesc_hash_t));
+ line->next = vote_rs->microdesc;
+ line->microdesc_hash_line = tor_strdup(t->args[0]);
+ vote_rs->microdesc = line;
+ }
+ if (t->tp == K_ID) {
+ tor_assert(t->n_args >= 2);
+ if (!strcmp(t->args[0], "ed25519")) {
+ vote_rs->has_ed25519_listing = 1;
+ if (strcmp(t->args[1], "none") &&
+ digest256_from_base64((char*)vote_rs->ed25519_id,
+ t->args[1])<0) {
+ log_warn(LD_DIR, "Bogus ed25519 key in networkstatus vote");
+ goto err;
+ }
+ }
+ }
+ if (t->tp == K_PROTO) {
+ tor_assert(t->n_args == 1);
+ vote_rs->protocols = tor_strdup(t->args[0]);
+ }
+ } SMARTLIST_FOREACH_END(t);
+ } else if (flav == FLAV_MICRODESC) {
+ tok = find_opt_by_keyword(tokens, K_M);
+ if (tok) {
+ tor_assert(tok->n_args);
+ if (digest256_from_base64(rs->descriptor_digest, tok->args[0])) {
+ log_warn(LD_DIR, "Error decoding microdescriptor digest %s",
+ escaped(tok->args[0]));
+ goto err;
+ }
+ } else {
+ log_info(LD_BUG, "Found an entry in networkstatus with no "
+ "microdescriptor digest. (Router %s ($%s) at %s:%d.)",
+ rs->nickname, hex_str(rs->identity_digest, DIGEST_LEN),
+ fmt_addr32(rs->addr), rs->or_port);
+ }
+ }
+
+ if (!strcasecmp(rs->nickname, UNNAMED_ROUTER_NICKNAME))
+ rs->is_named = 0;
+
+ goto done;
+ err:
+ dump_desc(s_dup, "routerstatus entry");
+ if (rs && !vote_rs)
+ routerstatus_free(rs);
+ rs = NULL;
+ done:
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_clear(tokens);
+ if (area) {
+ DUMP_AREA(area, "routerstatus entry");
+ memarea_clear(area);
+ }
+ *s = eos;
+
+ return rs;
+}
+
+int
+compare_vote_routerstatus_entries(const void **_a, const void **_b)
+{
+ const vote_routerstatus_t *a = *_a, *b = *_b;
+ return fast_memcmp(a->status.identity_digest, b->status.identity_digest,
+ DIGEST_LEN);
+}
+
+/** Verify the bandwidth weights of a network status document */
+int
+networkstatus_verify_bw_weights(networkstatus_t *ns, int consensus_method)
+{
+ int64_t G=0, M=0, E=0, D=0, T=0;
+ double Wgg, Wgm, Wgd, Wmg, Wmm, Wme, Wmd, Weg, Wem, Wee, Wed;
+ double Gtotal=0, Mtotal=0, Etotal=0;
+ const char *casename = NULL;
+ int valid = 1;
+ (void) consensus_method;
+
+ const int64_t weight_scale = networkstatus_get_weight_scale_param(ns);
+ tor_assert(weight_scale >= 1);
+ Wgg = networkstatus_get_bw_weight(ns, "Wgg", -1);
+ Wgm = networkstatus_get_bw_weight(ns, "Wgm", -1);
+ Wgd = networkstatus_get_bw_weight(ns, "Wgd", -1);
+ Wmg = networkstatus_get_bw_weight(ns, "Wmg", -1);
+ Wmm = networkstatus_get_bw_weight(ns, "Wmm", -1);
+ Wme = networkstatus_get_bw_weight(ns, "Wme", -1);
+ Wmd = networkstatus_get_bw_weight(ns, "Wmd", -1);
+ Weg = networkstatus_get_bw_weight(ns, "Weg", -1);
+ Wem = networkstatus_get_bw_weight(ns, "Wem", -1);
+ Wee = networkstatus_get_bw_weight(ns, "Wee", -1);
+ Wed = networkstatus_get_bw_weight(ns, "Wed", -1);
+
+ if (Wgg<0 || Wgm<0 || Wgd<0 || Wmg<0 || Wmm<0 || Wme<0 || Wmd<0 || Weg<0
+ || Wem<0 || Wee<0 || Wed<0) {
+ log_warn(LD_BUG, "No bandwidth weights produced in consensus!");
+ return 0;
+ }
+
+ // First, sanity check basic summing properties that hold for all cases
+ // We use > 1 as the check for these because they are computed as integers.
+ // Sometimes there are rounding errors.
+ if (fabs(Wmm - weight_scale) > 1) {
+ log_warn(LD_BUG, "Wmm=%f != %"PRId64,
+ Wmm, (weight_scale));
+ valid = 0;
+ }
+
+ if (fabs(Wem - Wee) > 1) {
+ log_warn(LD_BUG, "Wem=%f != Wee=%f", Wem, Wee);
+ valid = 0;
+ }
+
+ if (fabs(Wgm - Wgg) > 1) {
+ log_warn(LD_BUG, "Wgm=%f != Wgg=%f", Wgm, Wgg);
+ valid = 0;
+ }
+
+ if (fabs(Weg - Wed) > 1) {
+ log_warn(LD_BUG, "Wed=%f != Weg=%f", Wed, Weg);
+ valid = 0;
+ }
+
+ if (fabs(Wgg + Wmg - weight_scale) > 0.001*weight_scale) {
+ log_warn(LD_BUG, "Wgg=%f != %"PRId64" - Wmg=%f", Wgg,
+ (weight_scale), Wmg);
+ valid = 0;
+ }
+
+ if (fabs(Wee + Wme - weight_scale) > 0.001*weight_scale) {
+ log_warn(LD_BUG, "Wee=%f != %"PRId64" - Wme=%f", Wee,
+ (weight_scale), Wme);
+ valid = 0;
+ }
+
+ if (fabs(Wgd + Wmd + Wed - weight_scale) > 0.001*weight_scale) {
+ log_warn(LD_BUG, "Wgd=%f + Wmd=%f + Wed=%f != %"PRId64,
+ Wgd, Wmd, Wed, (weight_scale));
+ valid = 0;
+ }
+
+ Wgg /= weight_scale;
+ Wgm /= weight_scale; (void) Wgm; // unused from here on.
+ Wgd /= weight_scale;
+
+ Wmg /= weight_scale;
+ Wmm /= weight_scale;
+ Wme /= weight_scale;
+ Wmd /= weight_scale;
+
+ Weg /= weight_scale; (void) Weg; // unused from here on.
+ Wem /= weight_scale; (void) Wem; // unused from here on.
+ Wee /= weight_scale;
+ Wed /= weight_scale;
+
+ // Then, gather G, M, E, D, T to determine case
+ SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
+ int is_exit = 0;
+ /* Bug #2203: Don't count bad exits as exits for balancing */
+ is_exit = rs->is_exit && !rs->is_bad_exit;
+ if (rs->has_bandwidth) {
+ T += rs->bandwidth_kb;
+ if (is_exit && rs->is_possible_guard) {
+ D += rs->bandwidth_kb;
+ Gtotal += Wgd*rs->bandwidth_kb;
+ Mtotal += Wmd*rs->bandwidth_kb;
+ Etotal += Wed*rs->bandwidth_kb;
+ } else if (is_exit) {
+ E += rs->bandwidth_kb;
+ Mtotal += Wme*rs->bandwidth_kb;
+ Etotal += Wee*rs->bandwidth_kb;
+ } else if (rs->is_possible_guard) {
+ G += rs->bandwidth_kb;
+ Gtotal += Wgg*rs->bandwidth_kb;
+ Mtotal += Wmg*rs->bandwidth_kb;
+ } else {
+ M += rs->bandwidth_kb;
+ Mtotal += Wmm*rs->bandwidth_kb;
+ }
+ } else {
+ log_warn(LD_BUG, "Missing consensus bandwidth for router %s",
+ routerstatus_describe(rs));
+ }
+ } SMARTLIST_FOREACH_END(rs);
+
+ // Finally, check equality conditions depending upon case 1, 2 or 3
+ // Full equality cases: 1, 3b
+ // Partial equality cases: 2b (E=G), 3a (M=E)
+ // Fully unknown: 2a
+ if (3*E >= T && 3*G >= T) {
+ // Case 1: Neither are scarce
+ casename = "Case 1";
+ if (fabs(Etotal-Mtotal) > 0.01*MAX(Etotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Mtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Mtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (fabs(Gtotal-Mtotal) > 0.01*MAX(Gtotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Mtotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Mtotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ } else if (3*E < T && 3*G < T) {
+ int64_t R = MIN(E, G);
+ int64_t S = MAX(E, G);
+ /*
+ * Case 2: Both Guards and Exits are scarce
+ * Balance D between E and G, depending upon
+ * D capacity and scarcity. Devote no extra
+ * bandwidth to middle nodes.
+ */
+ if (R+D < S) { // Subcase a
+ double Rtotal, Stotal;
+ if (E < G) {
+ Rtotal = Etotal;
+ Stotal = Gtotal;
+ } else {
+ Rtotal = Gtotal;
+ Stotal = Etotal;
+ }
+ casename = "Case 2a";
+ // Rtotal < Stotal
+ if (Rtotal > Stotal) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Rtotal %f > Stotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Rtotal, Stotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ // Rtotal < T/3
+ if (3*Rtotal > T) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: 3*Rtotal %f > T "
+ "%"PRId64". G=%"PRId64" M=%"PRId64" E=%"PRId64
+ " D=%"PRId64" T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Rtotal*3, (T),
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ // Stotal < T/3
+ if (3*Stotal > T) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: 3*Stotal %f > T "
+ "%"PRId64". G=%"PRId64" M=%"PRId64" E=%"PRId64
+ " D=%"PRId64" T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Stotal*3, (T),
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ // Mtotal > T/3
+ if (3*Mtotal < T) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: 3*Mtotal %f < T "
+ "%"PRId64". "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Mtotal*3, (T),
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ } else { // Subcase b: R+D > S
+ casename = "Case 2b";
+
+ /* Check the rare-M redirect case. */
+ if (D != 0 && 3*M < T) {
+ casename = "Case 2b (balanced)";
+ if (fabs(Etotal-Mtotal) > 0.01*MAX(Etotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Mtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Mtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (fabs(Gtotal-Mtotal) > 0.01*MAX(Gtotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Mtotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Mtotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ } else {
+ if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ }
+ }
+ } else { // if (E < T/3 || G < T/3) {
+ int64_t S = MIN(E, G);
+ int64_t NS = MAX(E, G);
+ if (3*(S+D) < T) { // Subcase a:
+ double Stotal;
+ double NStotal;
+ if (G < E) {
+ casename = "Case 3a (G scarce)";
+ Stotal = Gtotal;
+ NStotal = Etotal;
+ } else { // if (G >= E) {
+ casename = "Case 3a (E scarce)";
+ NStotal = Gtotal;
+ Stotal = Etotal;
+ }
+ // Stotal < T/3
+ if (3*Stotal > T) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: 3*Stotal %f > T "
+ "%"PRId64". G=%"PRId64" M=%"PRId64" E=%"PRId64
+ " D=%"PRId64" T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Stotal*3, (T),
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (NS >= M) {
+ if (fabs(NStotal-Mtotal) > 0.01*MAX(NStotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: NStotal %f != Mtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, NStotal, Mtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ } else {
+ // if NS < M, NStotal > T/3 because only one of G or E is scarce
+ if (3*NStotal < T) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: 3*NStotal %f < T "
+ "%"PRId64". G=%"PRId64" M=%"PRId64
+ " E=%"PRId64" D=%"PRId64" T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, NStotal*3, (T),
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ }
+ } else { // Subcase b: S+D >= T/3
+ casename = "Case 3b";
+ if (fabs(Etotal-Mtotal) > 0.01*MAX(Etotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Mtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Mtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Etotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ if (fabs(Gtotal-Mtotal) > 0.01*MAX(Gtotal,Mtotal)) {
+ log_warn(LD_DIR,
+ "Bw Weight Failure for %s: Mtotal %f != Gtotal %f. "
+ "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
+ " T=%"PRId64". "
+ "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
+ casename, Mtotal, Gtotal,
+ (G), (M), (E),
+ (D), (T),
+ Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
+ valid = 0;
+ }
+ }
+ }
+
+ if (valid)
+ log_notice(LD_DIR, "Bandwidth-weight %s is verified and valid.",
+ casename);
+
+ return valid;
+}
+
+/** Check if a shared random value of type <b>srv_type</b> is in
+ * <b>tokens</b>. If there is, parse it and set it to <b>srv_out</b>. Return
+ * -1 on failure, 0 on success. The resulting srv is allocated on the heap and
+ * it's the responsibility of the caller to free it. */
+static int
+extract_one_srv(smartlist_t *tokens, directory_keyword srv_type,
+ sr_srv_t **srv_out)
+{
+ int ret = -1;
+ directory_token_t *tok;
+ sr_srv_t *srv = NULL;
+ smartlist_t *chunks;
+
+ tor_assert(tokens);
+
+ chunks = smartlist_new();
+ tok = find_opt_by_keyword(tokens, srv_type);
+ if (!tok) {
+ /* That's fine, no SRV is allowed. */
+ ret = 0;
+ goto end;
+ }
+ for (int i = 0; i < tok->n_args; i++) {
+ smartlist_add(chunks, tok->args[i]);
+ }
+ srv = sr_parse_srv(chunks);
+ if (srv == NULL) {
+ log_warn(LD_DIR, "SR: Unparseable SRV %s", escaped(tok->object_body));
+ goto end;
+ }
+ /* All is good. */
+ *srv_out = srv;
+ ret = 0;
+ end:
+ smartlist_free(chunks);
+ return ret;
+}
+
+/** Extract any shared random values found in <b>tokens</b> and place them in
+ * the networkstatus <b>ns</b>. */
+static void
+extract_shared_random_srvs(networkstatus_t *ns, smartlist_t *tokens)
+{
+ const char *voter_identity;
+ networkstatus_voter_info_t *voter;
+
+ tor_assert(ns);
+ tor_assert(tokens);
+ /* Can be only one of them else code flow. */
+ tor_assert(ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_CONSENSUS);
+
+ if (ns->type == NS_TYPE_VOTE) {
+ voter = smartlist_get(ns->voters, 0);
+ tor_assert(voter);
+ voter_identity = hex_str(voter->identity_digest,
+ sizeof(voter->identity_digest));
+ } else {
+ /* Consensus has multiple voters so no specific voter. */
+ voter_identity = "consensus";
+ }
+
+ /* We extract both, and on error everything is stopped because it means
+ * the vote is malformed for the shared random value(s). */
+ if (extract_one_srv(tokens, K_PREVIOUS_SRV, &ns->sr_info.previous_srv) < 0) {
+ log_warn(LD_DIR, "SR: Unable to parse previous SRV from %s",
+ voter_identity);
+ /* Maybe we have a chance with the current SRV so let's try it anyway. */
+ }
+ if (extract_one_srv(tokens, K_CURRENT_SRV, &ns->sr_info.current_srv) < 0) {
+ log_warn(LD_DIR, "SR: Unable to parse current SRV from %s",
+ voter_identity);
+ }
+}
+
+/** Parse a v3 networkstatus vote, opinion, or consensus (depending on
+ * ns_type), from <b>s</b>, and return the result. Return NULL on failure. */
+networkstatus_t *
+networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
+ networkstatus_type_t ns_type)
+{
+ smartlist_t *tokens = smartlist_new();
+ smartlist_t *rs_tokens = NULL, *footer_tokens = NULL;
+ networkstatus_voter_info_t *voter = NULL;
+ networkstatus_t *ns = NULL;
+ common_digests_t ns_digests;
+ uint8_t sha3_as_signed[DIGEST256_LEN];
+ const char *cert, *end_of_header, *end_of_footer, *s_dup = s;
+ directory_token_t *tok;
+ struct in_addr in;
+ int i, inorder, n_signatures = 0;
+ memarea_t *area = NULL, *rs_area = NULL;
+ consensus_flavor_t flav = FLAV_NS;
+ char *last_kwd=NULL;
+
+ tor_assert(s);
+
+ if (eos_out)
+ *eos_out = NULL;
+
+ if (router_get_networkstatus_v3_hashes(s, &ns_digests) ||
+ router_get_networkstatus_v3_sha3_as_signed(sha3_as_signed, s)<0) {
+ log_warn(LD_DIR, "Unable to compute digest of network-status");
+ goto err;
+ }
+
+ area = memarea_new();
+ end_of_header = find_start_of_next_routerstatus(s);
+ if (tokenize_string(area, s, end_of_header, tokens,
+ (ns_type == NS_TYPE_CONSENSUS) ?
+ networkstatus_consensus_token_table :
+ networkstatus_token_table, 0)) {
+ log_warn(LD_DIR, "Error tokenizing network-status header");
+ goto err;
+ }
+
+ ns = tor_malloc_zero(sizeof(networkstatus_t));
+ memcpy(&ns->digests, &ns_digests, sizeof(ns_digests));
+ memcpy(&ns->digest_sha3_as_signed, sha3_as_signed, sizeof(sha3_as_signed));
+
+ tok = find_by_keyword(tokens, K_NETWORK_STATUS_VERSION);
+ tor_assert(tok);
+ if (tok->n_args > 1) {
+ int flavor = networkstatus_parse_flavor_name(tok->args[1]);
+ if (flavor < 0) {
+ log_warn(LD_DIR, "Can't parse document with unknown flavor %s",
+ escaped(tok->args[1]));
+ goto err;
+ }
+ ns->flavor = flav = flavor;
+ }
+ if (flav != FLAV_NS && ns_type != NS_TYPE_CONSENSUS) {
+ log_warn(LD_DIR, "Flavor found on non-consensus networkstatus.");
+ goto err;
+ }
+
+ if (ns_type != NS_TYPE_CONSENSUS) {
+ const char *end_of_cert = NULL;
+ if (!(cert = strstr(s, "\ndir-key-certificate-version")))
+ goto err;
+ ++cert;
+ ns->cert = authority_cert_parse_from_string(cert, &end_of_cert);
+ if (!ns->cert || !end_of_cert || end_of_cert > end_of_header)
+ goto err;
+ }
+
+ tok = find_by_keyword(tokens, K_VOTE_STATUS);
+ tor_assert(tok->n_args);
+ if (!strcmp(tok->args[0], "vote")) {
+ ns->type = NS_TYPE_VOTE;
+ } else if (!strcmp(tok->args[0], "consensus")) {
+ ns->type = NS_TYPE_CONSENSUS;
+ } else if (!strcmp(tok->args[0], "opinion")) {
+ ns->type = NS_TYPE_OPINION;
+ } else {
+ log_warn(LD_DIR, "Unrecognized vote status %s in network-status",
+ escaped(tok->args[0]));
+ goto err;
+ }
+ if (ns_type != ns->type) {
+ log_warn(LD_DIR, "Got the wrong kind of v3 networkstatus.");
+ goto err;
+ }
+
+ if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
+ tok = find_by_keyword(tokens, K_PUBLISHED);
+ if (parse_iso_time(tok->args[0], &ns->published))
+ goto err;
+
+ ns->supported_methods = smartlist_new();
+ tok = find_opt_by_keyword(tokens, K_CONSENSUS_METHODS);
+ if (tok) {
+ for (i=0; i < tok->n_args; ++i)
+ smartlist_add_strdup(ns->supported_methods, tok->args[i]);
+ } else {
+ smartlist_add_strdup(ns->supported_methods, "1");
+ }
+ } else {
+ tok = find_opt_by_keyword(tokens, K_CONSENSUS_METHOD);
+ if (tok) {
+ int num_ok;
+ ns->consensus_method = (int)tor_parse_long(tok->args[0], 10, 1, INT_MAX,
+ &num_ok, NULL);
+ if (!num_ok)
+ goto err;
+ } else {
+ ns->consensus_method = 1;
+ }
+ }
+
+ if ((tok = find_opt_by_keyword(tokens, K_RECOMMENDED_CLIENT_PROTOCOLS)))
+ ns->recommended_client_protocols = tor_strdup(tok->args[0]);
+ if ((tok = find_opt_by_keyword(tokens, K_RECOMMENDED_RELAY_PROTOCOLS)))
+ ns->recommended_relay_protocols = tor_strdup(tok->args[0]);
+ if ((tok = find_opt_by_keyword(tokens, K_REQUIRED_CLIENT_PROTOCOLS)))
+ ns->required_client_protocols = tor_strdup(tok->args[0]);
+ if ((tok = find_opt_by_keyword(tokens, K_REQUIRED_RELAY_PROTOCOLS)))
+ ns->required_relay_protocols = tor_strdup(tok->args[0]);
+
+ tok = find_by_keyword(tokens, K_VALID_AFTER);
+ if (parse_iso_time(tok->args[0], &ns->valid_after))
+ goto err;
+
+ tok = find_by_keyword(tokens, K_FRESH_UNTIL);
+ if (parse_iso_time(tok->args[0], &ns->fresh_until))
+ goto err;
+
+ tok = find_by_keyword(tokens, K_VALID_UNTIL);
+ if (parse_iso_time(tok->args[0], &ns->valid_until))
+ goto err;
+
+ tok = find_by_keyword(tokens, K_VOTING_DELAY);
+ tor_assert(tok->n_args >= 2);
+ {
+ int ok;
+ ns->vote_seconds =
+ (int) tor_parse_long(tok->args[0], 10, 0, INT_MAX, &ok, NULL);
+ if (!ok)
+ goto err;
+ ns->dist_seconds =
+ (int) tor_parse_long(tok->args[1], 10, 0, INT_MAX, &ok, NULL);
+ if (!ok)
+ goto err;
+ }
+ if (ns->valid_after +
+ (get_options()->TestingTorNetwork ?
+ MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) > ns->fresh_until) {
+ log_warn(LD_DIR, "Vote/consensus freshness interval is too short");
+ goto err;
+ }
+ if (ns->valid_after +
+ (get_options()->TestingTorNetwork ?
+ MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL)*2 > ns->valid_until) {
+ log_warn(LD_DIR, "Vote/consensus liveness interval is too short");
+ goto err;
+ }
+ if (ns->vote_seconds < MIN_VOTE_SECONDS) {
+ log_warn(LD_DIR, "Vote seconds is too short");
+ goto err;
+ }
+ if (ns->dist_seconds < MIN_DIST_SECONDS) {
+ log_warn(LD_DIR, "Dist seconds is too short");
+ goto err;
+ }
+
+ if ((tok = find_opt_by_keyword(tokens, K_CLIENT_VERSIONS))) {
+ ns->client_versions = tor_strdup(tok->args[0]);
+ }
+ if ((tok = find_opt_by_keyword(tokens, K_SERVER_VERSIONS))) {
+ ns->server_versions = tor_strdup(tok->args[0]);
+ }
+
+ {
+ smartlist_t *package_lst = find_all_by_keyword(tokens, K_PACKAGE);
+ ns->package_lines = smartlist_new();
+ if (package_lst) {
+ SMARTLIST_FOREACH(package_lst, directory_token_t *, t,
+ smartlist_add_strdup(ns->package_lines, t->args[0]));
+ }
+ smartlist_free(package_lst);
+ }
+
+ tok = find_by_keyword(tokens, K_KNOWN_FLAGS);
+ ns->known_flags = smartlist_new();
+ inorder = 1;
+ for (i = 0; i < tok->n_args; ++i) {
+ smartlist_add_strdup(ns->known_flags, tok->args[i]);
+ if (i>0 && strcmp(tok->args[i-1], tok->args[i])>= 0) {
+ log_warn(LD_DIR, "%s >= %s", tok->args[i-1], tok->args[i]);
+ inorder = 0;
+ }
+ }
+ if (!inorder) {
+ log_warn(LD_DIR, "known-flags not in order");
+ goto err;
+ }
+ if (ns->type != NS_TYPE_CONSENSUS &&
+ smartlist_len(ns->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
+ /* If we allowed more than 64 flags in votes, then parsing them would make
+ * us invoke undefined behavior whenever we used 1<<flagnum to do a
+ * bit-shift. This is only for votes and opinions: consensus users don't
+ * care about flags they don't recognize, and so don't build a bitfield
+ * for them. */
+ log_warn(LD_DIR, "Too many known-flags in consensus vote or opinion");
+ goto err;
+ }
+
+ tok = find_opt_by_keyword(tokens, K_PARAMS);
+ if (tok) {
+ int any_dups = 0;
+ inorder = 1;
+ ns->net_params = smartlist_new();
+ for (i = 0; i < tok->n_args; ++i) {
+ int ok=0;
+ char *eq = strchr(tok->args[i], '=');
+ size_t eq_pos;
+ if (!eq) {
+ log_warn(LD_DIR, "Bad element '%s' in params", escaped(tok->args[i]));
+ goto err;
+ }
+ eq_pos = eq-tok->args[i];
+ tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
+ if (!ok) {
+ log_warn(LD_DIR, "Bad element '%s' in params", escaped(tok->args[i]));
+ goto err;
+ }
+ if (i > 0 && strcmp(tok->args[i-1], tok->args[i]) >= 0) {
+ log_warn(LD_DIR, "%s >= %s", tok->args[i-1], tok->args[i]);
+ inorder = 0;
+ }
+ if (last_kwd && eq_pos == strlen(last_kwd) &&
+ fast_memeq(last_kwd, tok->args[i], eq_pos)) {
+ log_warn(LD_DIR, "Duplicate value for %s parameter",
+ escaped(tok->args[i]));
+ any_dups = 1;
+ }
+ tor_free(last_kwd);
+ last_kwd = tor_strndup(tok->args[i], eq_pos);
+ smartlist_add_strdup(ns->net_params, tok->args[i]);
+ }
+ if (!inorder) {
+ log_warn(LD_DIR, "params not in order");
+ goto err;
+ }
+ if (any_dups) {
+ log_warn(LD_DIR, "Duplicate in parameters");
+ goto err;
+ }
+ }
+
+ ns->voters = smartlist_new();
+
+ SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, _tok) {
+ tok = _tok;
+ if (tok->tp == K_DIR_SOURCE) {
+ tor_assert(tok->n_args >= 6);
+
+ if (voter)
+ smartlist_add(ns->voters, voter);
+ voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
+ voter->sigs = smartlist_new();
+ if (ns->type != NS_TYPE_CONSENSUS)
+ memcpy(voter->vote_digest, ns_digests.d[DIGEST_SHA1], DIGEST_LEN);
+
+ voter->nickname = tor_strdup(tok->args[0]);
+ if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
+ base16_decode(voter->identity_digest, sizeof(voter->identity_digest),
+ tok->args[1], HEX_DIGEST_LEN)
+ != sizeof(voter->identity_digest)) {
+ log_warn(LD_DIR, "Error decoding identity digest %s in "
+ "network-status document.", escaped(tok->args[1]));
+ goto err;
+ }
+ if (ns->type != NS_TYPE_CONSENSUS &&
+ tor_memneq(ns->cert->cache_info.identity_digest,
+ voter->identity_digest, DIGEST_LEN)) {
+ log_warn(LD_DIR,"Mismatch between identities in certificate and vote");
+ goto err;
+ }
+ if (ns->type != NS_TYPE_CONSENSUS) {
+ if (authority_cert_is_blacklisted(ns->cert)) {
+ log_warn(LD_DIR, "Rejecting vote signature made with blacklisted "
+ "signing key %s",
+ hex_str(ns->cert->signing_key_digest, DIGEST_LEN));
+ goto err;
+ }
+ }
+ voter->address = tor_strdup(tok->args[2]);
+ if (!tor_inet_aton(tok->args[3], &in)) {
+ log_warn(LD_DIR, "Error decoding IP address %s in network-status.",
+ escaped(tok->args[3]));
+ goto err;
+ }
+ voter->addr = ntohl(in.s_addr);
+ int ok;
+ voter->dir_port = (uint16_t)
+ tor_parse_long(tok->args[4], 10, 0, 65535, &ok, NULL);
+ if (!ok)
+ goto err;
+ voter->or_port = (uint16_t)
+ tor_parse_long(tok->args[5], 10, 0, 65535, &ok, NULL);
+ if (!ok)
+ goto err;
+ } else if (tok->tp == K_CONTACT) {
+ if (!voter || voter->contact) {
+ log_warn(LD_DIR, "contact element is out of place.");
+ goto err;
+ }
+ voter->contact = tor_strdup(tok->args[0]);
+ } else if (tok->tp == K_VOTE_DIGEST) {
+ tor_assert(ns->type == NS_TYPE_CONSENSUS);
+ tor_assert(tok->n_args >= 1);
+ if (!voter || ! tor_digest_is_zero(voter->vote_digest)) {
+ log_warn(LD_DIR, "vote-digest element is out of place.");
+ goto err;
+ }
+ if (strlen(tok->args[0]) != HEX_DIGEST_LEN ||
+ base16_decode(voter->vote_digest, sizeof(voter->vote_digest),
+ tok->args[0], HEX_DIGEST_LEN)
+ != sizeof(voter->vote_digest)) {
+ log_warn(LD_DIR, "Error decoding vote digest %s in "
+ "network-status consensus.", escaped(tok->args[0]));
+ goto err;
+ }
+ }
+ } SMARTLIST_FOREACH_END(_tok);
+ if (voter) {
+ smartlist_add(ns->voters, voter);
+ voter = NULL;
+ }
+ if (smartlist_len(ns->voters) == 0) {
+ log_warn(LD_DIR, "Missing dir-source elements in a networkstatus.");
+ goto err;
+ } else if (ns->type != NS_TYPE_CONSENSUS && smartlist_len(ns->voters) != 1) {
+ log_warn(LD_DIR, "Too many dir-source elements in a vote networkstatus.");
+ goto err;
+ }
+
+ if (ns->type != NS_TYPE_CONSENSUS &&
+ (tok = find_opt_by_keyword(tokens, K_LEGACY_DIR_KEY))) {
+ int bad = 1;
+ if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
+ networkstatus_voter_info_t *voter_0 = smartlist_get(ns->voters, 0);
+ if (base16_decode(voter_0->legacy_id_digest, DIGEST_LEN,
+ tok->args[0], HEX_DIGEST_LEN) != DIGEST_LEN)
+ bad = 1;
+ else
+ bad = 0;
+ }
+ if (bad) {
+ log_warn(LD_DIR, "Invalid legacy key digest %s on vote.",
+ escaped(tok->args[0]));
+ }
+ }
+
+ /* If this is a vote document, check if information about the shared
+ randomness protocol is included, and extract it. */
+ if (ns->type == NS_TYPE_VOTE) {
+ dirvote_parse_sr_commits(ns, tokens);
+ }
+ /* For both a vote and consensus, extract the shared random values. */
+ if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_CONSENSUS) {
+ extract_shared_random_srvs(ns, tokens);
+ }
+
+ /* Parse routerstatus lines. */
+ rs_tokens = smartlist_new();
+ rs_area = memarea_new();
+ s = end_of_header;
+ ns->routerstatus_list = smartlist_new();
+
+ while (!strcmpstart(s, "r ")) {
+ if (ns->type != NS_TYPE_CONSENSUS) {
+ vote_routerstatus_t *rs = tor_malloc_zero(sizeof(vote_routerstatus_t));
+ if (routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens, ns,
+ rs, 0, 0)) {
+ smartlist_add(ns->routerstatus_list, rs);
+ } else {
+ vote_routerstatus_free(rs);
+ }
+ } else {
+ routerstatus_t *rs;
+ if ((rs = routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens,
+ NULL, NULL,
+ ns->consensus_method,
+ flav))) {
+ /* Use exponential-backoff scheduling when downloading microdescs */
+ smartlist_add(ns->routerstatus_list, rs);
+ }
+ }
+ }
+ for (i = 1; i < smartlist_len(ns->routerstatus_list); ++i) {
+ routerstatus_t *rs1, *rs2;
+ if (ns->type != NS_TYPE_CONSENSUS) {
+ vote_routerstatus_t *a = smartlist_get(ns->routerstatus_list, i-1);
+ vote_routerstatus_t *b = smartlist_get(ns->routerstatus_list, i);
+ rs1 = &a->status; rs2 = &b->status;
+ } else {
+ rs1 = smartlist_get(ns->routerstatus_list, i-1);
+ rs2 = smartlist_get(ns->routerstatus_list, i);
+ }
+ if (fast_memcmp(rs1->identity_digest, rs2->identity_digest, DIGEST_LEN)
+ >= 0) {
+ log_warn(LD_DIR, "Networkstatus entries not sorted by identity digest");
+ goto err;
+ }
+ }
+ if (ns_type != NS_TYPE_CONSENSUS) {
+ digest256map_t *ed_id_map = digest256map_new();
+ SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, vote_routerstatus_t *,
+ vrs) {
+ if (! vrs->has_ed25519_listing ||
+ tor_mem_is_zero((const char *)vrs->ed25519_id, DIGEST256_LEN))
+ continue;
+ if (digest256map_get(ed_id_map, vrs->ed25519_id) != NULL) {
+ log_warn(LD_DIR, "Vote networkstatus ed25519 identities were not "
+ "unique");
+ digest256map_free(ed_id_map, NULL);
+ goto err;
+ }
+ digest256map_set(ed_id_map, vrs->ed25519_id, (void*)1);
+ } SMARTLIST_FOREACH_END(vrs);
+ digest256map_free(ed_id_map, NULL);
+ }
+
+ /* Parse footer; check signature. */
+ footer_tokens = smartlist_new();
+ if ((end_of_footer = strstr(s, "\nnetwork-status-version ")))
+ ++end_of_footer;
+ else
+ end_of_footer = s + strlen(s);
+ if (tokenize_string(area,s, end_of_footer, footer_tokens,
+ networkstatus_vote_footer_token_table, 0)) {
+ log_warn(LD_DIR, "Error tokenizing network-status vote footer.");
+ goto err;
+ }
+
+ {
+ int found_sig = 0;
+ SMARTLIST_FOREACH_BEGIN(footer_tokens, directory_token_t *, _tok) {
+ tok = _tok;
+ if (tok->tp == K_DIRECTORY_SIGNATURE)
+ found_sig = 1;
+ else if (found_sig) {
+ log_warn(LD_DIR, "Extraneous token after first directory-signature");
+ goto err;
+ }
+ } SMARTLIST_FOREACH_END(_tok);
+ }
+
+ if ((tok = find_opt_by_keyword(footer_tokens, K_DIRECTORY_FOOTER))) {
+ if (tok != smartlist_get(footer_tokens, 0)) {
+ log_warn(LD_DIR, "Misplaced directory-footer token");
+ goto err;
+ }
+ }
+
+ tok = find_opt_by_keyword(footer_tokens, K_BW_WEIGHTS);
+ if (tok) {
+ ns->weight_params = smartlist_new();
+ for (i = 0; i < tok->n_args; ++i) {
+ int ok=0;
+ char *eq = strchr(tok->args[i], '=');
+ if (!eq) {
+ log_warn(LD_DIR, "Bad element '%s' in weight params",
+ escaped(tok->args[i]));
+ goto err;
+ }
+ tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
+ if (!ok) {
+ log_warn(LD_DIR, "Bad element '%s' in params", escaped(tok->args[i]));
+ goto err;
+ }
+ smartlist_add_strdup(ns->weight_params, tok->args[i]);
+ }
+ }
+
+ SMARTLIST_FOREACH_BEGIN(footer_tokens, directory_token_t *, _tok) {
+ char declared_identity[DIGEST_LEN];
+ networkstatus_voter_info_t *v;
+ document_signature_t *sig;
+ const char *id_hexdigest = NULL;
+ const char *sk_hexdigest = NULL;
+ digest_algorithm_t alg = DIGEST_SHA1;
+ tok = _tok;
+ if (tok->tp != K_DIRECTORY_SIGNATURE)
+ continue;
+ tor_assert(tok->n_args >= 2);
+ if (tok->n_args == 2) {
+ id_hexdigest = tok->args[0];
+ sk_hexdigest = tok->args[1];
+ } else {
+ const char *algname = tok->args[0];
+ int a;
+ id_hexdigest = tok->args[1];
+ sk_hexdigest = tok->args[2];
+ a = crypto_digest_algorithm_parse_name(algname);
+ if (a<0) {
+ log_warn(LD_DIR, "Unknown digest algorithm %s; skipping",
+ escaped(algname));
+ continue;
+ }
+ alg = a;
+ }
+
+ if (!tok->object_type ||
+ strcmp(tok->object_type, "SIGNATURE") ||
+ tok->object_size < 128 || tok->object_size > 512) {
+ log_warn(LD_DIR, "Bad object type or length on directory-signature");
+ goto err;
+ }
+
+ if (strlen(id_hexdigest) != HEX_DIGEST_LEN ||
+ base16_decode(declared_identity, sizeof(declared_identity),
+ id_hexdigest, HEX_DIGEST_LEN)
+ != sizeof(declared_identity)) {
+ log_warn(LD_DIR, "Error decoding declared identity %s in "
+ "network-status document.", escaped(id_hexdigest));
+ goto err;
+ }
+ if (!(v = networkstatus_get_voter_by_id(ns, declared_identity))) {
+ log_warn(LD_DIR, "ID on signature on network-status document does "
+ "not match any declared directory source.");
+ goto err;
+ }
+ sig = tor_malloc_zero(sizeof(document_signature_t));
+ memcpy(sig->identity_digest, v->identity_digest, DIGEST_LEN);
+ sig->alg = alg;
+ if (strlen(sk_hexdigest) != HEX_DIGEST_LEN ||
+ base16_decode(sig->signing_key_digest, sizeof(sig->signing_key_digest),
+ sk_hexdigest, HEX_DIGEST_LEN)
+ != sizeof(sig->signing_key_digest)) {
+ log_warn(LD_DIR, "Error decoding declared signing key digest %s in "
+ "network-status document.", escaped(sk_hexdigest));
+ tor_free(sig);
+ goto err;
+ }
+
+ if (ns->type != NS_TYPE_CONSENSUS) {
+ if (tor_memneq(declared_identity, ns->cert->cache_info.identity_digest,
+ DIGEST_LEN)) {
+ log_warn(LD_DIR, "Digest mismatch between declared and actual on "
+ "network-status vote.");
+ tor_free(sig);
+ goto err;
+ }
+ }
+
+ if (networkstatus_get_voter_sig_by_alg(v, sig->alg)) {
+ /* We already parsed a vote with this algorithm from this voter. Use the
+ first one. */
+ log_fn(LOG_PROTOCOL_WARN, LD_DIR, "We received a networkstatus "
+ "that contains two signatures from the same voter with the same "
+ "algorithm. Ignoring the second signature.");
+ tor_free(sig);
+ continue;
+ }
+
+ if (ns->type != NS_TYPE_CONSENSUS) {
+ if (check_signature_token(ns_digests.d[DIGEST_SHA1], DIGEST_LEN,
+ tok, ns->cert->signing_key, 0,
+ "network-status document")) {
+ tor_free(sig);
+ goto err;
+ }
+ sig->good_signature = 1;
+ } else {
+ if (tok->object_size >= INT_MAX || tok->object_size >= SIZE_T_CEILING) {
+ tor_free(sig);
+ goto err;
+ }
+ sig->signature = tor_memdup(tok->object_body, tok->object_size);
+ sig->signature_len = (int) tok->object_size;
+ }
+ smartlist_add(v->sigs, sig);
+
+ ++n_signatures;
+ } SMARTLIST_FOREACH_END(_tok);
+
+ if (! n_signatures) {
+ log_warn(LD_DIR, "No signatures on networkstatus document.");
+ goto err;
+ } else if (ns->type == NS_TYPE_VOTE && n_signatures != 1) {
+ log_warn(LD_DIR, "Received more than one signature on a "
+ "network-status vote.");
+ goto err;
+ }
+
+ if (eos_out)
+ *eos_out = end_of_footer;
+
+ goto done;
+ err:
+ dump_desc(s_dup, "v3 networkstatus");
+ networkstatus_vote_free(ns);
+ ns = NULL;
+ done:
+ if (tokens) {
+ SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(tokens);
+ }
+ if (voter) {
+ if (voter->sigs) {
+ SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
+ document_signature_free(sig));
+ smartlist_free(voter->sigs);
+ }
+ tor_free(voter->nickname);
+ tor_free(voter->address);
+ tor_free(voter->contact);
+ tor_free(voter);
+ }
+ if (rs_tokens) {
+ SMARTLIST_FOREACH(rs_tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(rs_tokens);
+ }
+ if (footer_tokens) {
+ SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_clear(t));
+ smartlist_free(footer_tokens);
+ }
+ if (area) {
+ DUMP_AREA(area, "v3 networkstatus");
+ memarea_drop_all(area);
+ }
+ if (rs_area)
+ memarea_drop_all(rs_area);
+ tor_free(last_kwd);
+
+ return ns;
+}
diff --git a/src/feature/dirparse/ns_parse.h b/src/feature/dirparse/ns_parse.h
new file mode 100644
index 000000000..22438d73a
--- /dev/null
+++ b/src/feature/dirparse/ns_parse.h
@@ -0,0 +1,45 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file ns_parse.h
+ * \brief Header file for ns_parse.c.
+ **/
+
+#ifndef TOR_NS_PARSE_H
+#define TOR_NS_PARSE_H
+
+int router_get_networkstatus_v3_hashes(const char *s,
+ common_digests_t *digests);
+int router_get_networkstatus_v3_signed_boundaries(const char *s,
+ const char **start_out,
+ const char **end_out);
+int router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
+ const char *s);
+int compare_vote_routerstatus_entries(const void **_a, const void **_b);
+
+int networkstatus_verify_bw_weights(networkstatus_t *ns, int);
+enum networkstatus_type_t;
+networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
+ const char **eos_out,
+ enum networkstatus_type_t ns_type);
+
+#ifdef NS_PARSE_PRIVATE
+STATIC int routerstatus_parse_guardfraction(const char *guardfraction_str,
+ networkstatus_t *vote,
+ vote_routerstatus_t *vote_rs,
+ routerstatus_t *rs);
+struct memarea_t;
+STATIC routerstatus_t *routerstatus_parse_entry_from_string(
+ struct memarea_t *area,
+ const char **s, smartlist_t *tokens,
+ networkstatus_t *vote,
+ vote_routerstatus_t *vote_rs,
+ int consensus_method,
+ consensus_flavor_t flav);
+#endif
+
+#endif
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index daeb67c91..956e8f3ea 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -193,110 +193,6 @@ static token_rule_t extrainfo_token_table[] = {
END_OF_TABLE
};
-/** List of tokens recognized in the body part of v3 networkstatus
- * documents. */
-static token_rule_t rtrstatus_token_table[] = {
- T01("p", K_P, CONCAT_ARGS, NO_OBJ ),
- T1( "r", K_R, GE(7), NO_OBJ ),
- T0N("a", K_A, GE(1), NO_OBJ ),
- T1( "s", K_S, ARGS, NO_OBJ ),
- T01("v", K_V, CONCAT_ARGS, NO_OBJ ),
- T01("w", K_W, ARGS, NO_OBJ ),
- T0N("m", K_M, CONCAT_ARGS, NO_OBJ ),
- T0N("id", K_ID, GE(2), NO_OBJ ),
- T01("pr", K_PROTO, CONCAT_ARGS, NO_OBJ ),
- T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- END_OF_TABLE
-};
-
-/** List of tokens recognized in V3 networkstatus votes. */
-static token_rule_t networkstatus_token_table[] = {
- T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
- GE(1), NO_OBJ ),
- T1("vote-status", K_VOTE_STATUS, GE(1), NO_OBJ ),
- T1("published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ ),
- T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
- T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
- T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
- T1("voting-delay", K_VOTING_DELAY, GE(2), NO_OBJ ),
- T1("known-flags", K_KNOWN_FLAGS, ARGS, NO_OBJ ),
- T01("params", K_PARAMS, ARGS, NO_OBJ ),
- T( "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
- T01("signing-ed25519", K_SIGNING_CERT_ED, NO_ARGS , NEED_OBJ ),
- T01("shared-rand-participate",K_SR_FLAG, NO_ARGS, NO_OBJ ),
- T0N("shared-rand-commit", K_COMMIT, GE(3), NO_OBJ ),
- T01("shared-rand-previous-value", K_PREVIOUS_SRV,EQ(2), NO_OBJ ),
- T01("shared-rand-current-value", K_CURRENT_SRV, EQ(2), NO_OBJ ),
- T0N("package", K_PACKAGE, CONCAT_ARGS, NO_OBJ ),
- T01("recommended-client-protocols", K_RECOMMENDED_CLIENT_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
- T01("recommended-relay-protocols", K_RECOMMENDED_RELAY_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
- T01("required-client-protocols", K_REQUIRED_CLIENT_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
- T01("required-relay-protocols", K_REQUIRED_RELAY_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
-
-#include "feature/dirparse/authcert_members.i"
-
- T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
- T1( "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
- T1( "dir-source", K_DIR_SOURCE, GE(6), NO_OBJ ),
- T01("legacy-dir-key", K_LEGACY_DIR_KEY, GE(1), NO_OBJ ),
- T1( "known-flags", K_KNOWN_FLAGS, CONCAT_ARGS, NO_OBJ ),
- T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
- T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
- T1( "consensus-methods", K_CONSENSUS_METHODS, GE(1), NO_OBJ ),
-
- END_OF_TABLE
-};
-
-/** List of tokens recognized in V3 networkstatus consensuses. */
-static token_rule_t networkstatus_consensus_token_table[] = {
- T1_START("network-status-version", K_NETWORK_STATUS_VERSION,
- GE(1), NO_OBJ ),
- T1("vote-status", K_VOTE_STATUS, GE(1), NO_OBJ ),
- T1("valid-after", K_VALID_AFTER, CONCAT_ARGS, NO_OBJ ),
- T1("fresh-until", K_FRESH_UNTIL, CONCAT_ARGS, NO_OBJ ),
- T1("valid-until", K_VALID_UNTIL, CONCAT_ARGS, NO_OBJ ),
- T1("voting-delay", K_VOTING_DELAY, GE(2), NO_OBJ ),
-
- T0N("opt", K_OPT, CONCAT_ARGS, OBJ_OK ),
-
- T1N("dir-source", K_DIR_SOURCE, GE(6), NO_OBJ ),
- T1N("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ),
- T1N("vote-digest", K_VOTE_DIGEST, GE(1), NO_OBJ ),
-
- T1( "known-flags", K_KNOWN_FLAGS, CONCAT_ARGS, NO_OBJ ),
-
- T01("client-versions", K_CLIENT_VERSIONS, CONCAT_ARGS, NO_OBJ ),
- T01("server-versions", K_SERVER_VERSIONS, CONCAT_ARGS, NO_OBJ ),
- T01("consensus-method", K_CONSENSUS_METHOD, EQ(1), NO_OBJ),
- T01("params", K_PARAMS, ARGS, NO_OBJ ),
-
- T01("shared-rand-previous-value", K_PREVIOUS_SRV, EQ(2), NO_OBJ ),
- T01("shared-rand-current-value", K_CURRENT_SRV, EQ(2), NO_OBJ ),
-
- T01("recommended-client-protocols", K_RECOMMENDED_CLIENT_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
- T01("recommended-relay-protocols", K_RECOMMENDED_RELAY_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
- T01("required-client-protocols", K_REQUIRED_CLIENT_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
- T01("required-relay-protocols", K_REQUIRED_RELAY_PROTOCOLS,
- CONCAT_ARGS, NO_OBJ ),
-
- END_OF_TABLE
-};
-
-/** List of tokens recognized in the footer of v1 directory footers. */
-static token_rule_t networkstatus_vote_footer_token_table[] = {
- T01("directory-footer", K_DIRECTORY_FOOTER, NO_ARGS, NO_OBJ ),
- T01("bandwidth-weights", K_BW_WEIGHTS, ARGS, NO_OBJ ),
- T( "directory-signature", K_DIRECTORY_SIGNATURE, GE(2), NEED_OBJ ),
- END_OF_TABLE
-};
-
#undef T
/* static function prototypes */
@@ -318,52 +214,6 @@ router_get_router_hash(const char *s, size_t s_len, char *digest)
DIGEST_SHA1);
}
-/** Try to find the start and end of the signed portion of a networkstatus
- * document in <b>s</b>. On success, set <b>start_out</b> to the first
- * character of the document, and <b>end_out</b> to a position one after the
- * final character of the signed document, and return 0. On failure, return
- * -1. */
-int
-router_get_networkstatus_v3_signed_boundaries(const char *s,
- const char **start_out,
- const char **end_out)
-{
- return router_get_hash_impl_helper(s, strlen(s),
- "network-status-version",
- "\ndirectory-signature",
- ' ', LOG_INFO,
- start_out, end_out);
-}
-
-/** Set <b>digest_out</b> to the SHA3-256 digest of the signed portion of the
- * networkstatus vote in <b>s</b> -- or of the entirety of <b>s</b> if no
- * signed portion can be identified. Return 0 on success, -1 on failure. */
-int
-router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
- const char *s)
-{
- const char *start, *end;
- if (router_get_networkstatus_v3_signed_boundaries(s, &start, &end) < 0) {
- start = s;
- end = s + strlen(s);
- }
- tor_assert(start);
- tor_assert(end);
- return crypto_digest256((char*)digest_out, start, end-start,
- DIGEST_SHA3_256);
-}
-
-/** Set <b>digests</b> to all the digests of the consensus document in
- * <b>s</b> */
-int
-router_get_networkstatus_v3_hashes(const char *s, common_digests_t *digests)
-{
- return router_get_hashes_impl(s,strlen(s),digests,
- "network-status-version",
- "\ndirectory-signature",
- ' ');
-}
-
/** Set <b>digest</b> to the SHA-1 digest of the hash of the <b>s_len</b>-byte
* extrainfo string at <b>s</b>. Return 0 on success, -1 on failure. */
int
@@ -1351,93 +1201,10 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
return extrainfo;
}
-/** Helper: given a string <b>s</b>, return the start of the next router-status
- * object (starting with "r " at the start of a line). If none is found,
- * return the start of the directory footer, or the next directory signature.
- * If none is found, return the end of the string. */
-static inline const char *
-find_start_of_next_routerstatus(const char *s)
-{
- const char *eos, *footer, *sig;
- if ((eos = strstr(s, "\nr ")))
- ++eos;
- else
- eos = s + strlen(s);
-
- footer = tor_memstr(s, eos-s, "\ndirectory-footer");
- sig = tor_memstr(s, eos-s, "\ndirectory-signature");
-
- if (footer && sig)
- return MIN(footer, sig) + 1;
- else if (footer)
- return footer+1;
- else if (sig)
- return sig+1;
- else
- return eos;
-}
-
-/** Parse the GuardFraction string from a consensus or vote.
- *
- * If <b>vote</b> or <b>vote_rs</b> are set the document getting
- * parsed is a vote routerstatus. Otherwise it's a consensus. This is
- * the same semantic as in routerstatus_parse_entry_from_string(). */
-STATIC int
-routerstatus_parse_guardfraction(const char *guardfraction_str,
- networkstatus_t *vote,
- vote_routerstatus_t *vote_rs,
- routerstatus_t *rs)
-{
- int ok;
- const char *end_of_header = NULL;
- int is_consensus = !vote_rs;
- uint32_t guardfraction;
-
- tor_assert(bool_eq(vote, vote_rs));
-
- /* If this info comes from a consensus, but we should't apply
- guardfraction, just exit. */
- if (is_consensus && !should_apply_guardfraction(NULL)) {
- return 0;
- }
-
- end_of_header = strchr(guardfraction_str, '=');
- if (!end_of_header) {
- return -1;
- }
-
- guardfraction = (uint32_t)tor_parse_ulong(end_of_header+1,
- 10, 0, 100, &ok, NULL);
- if (!ok) {
- log_warn(LD_DIR, "Invalid GuardFraction %s", escaped(guardfraction_str));
- return -1;
- }
-
- log_debug(LD_GENERAL, "[*] Parsed %s guardfraction '%s' for '%s'.",
- is_consensus ? "consensus" : "vote",
- guardfraction_str, rs->nickname);
-
- if (!is_consensus) { /* We are parsing a vote */
- vote_rs->status.guardfraction_percentage = guardfraction;
- vote_rs->status.has_guardfraction = 1;
- } else {
- /* We are parsing a consensus. Only apply guardfraction to guards. */
- if (rs->is_possible_guard) {
- rs->guardfraction_percentage = guardfraction;
- rs->has_guardfraction = 1;
- } else {
- log_warn(LD_BUG, "Got GuardFraction for non-guard %s. "
- "This is not supposed to happen. Not applying. ", rs->nickname);
- }
- }
-
- return 0;
-}
-
/** Summarize the protocols listed in <b>protocols</b> into <b>out</b>,
* falling back or correcting them based on <b>version</b> as appropriate.
*/
-STATIC void
+void
summarize_protover_flags(protover_summary_flags_t *out,
const char *protocols,
const char *version)
@@ -1478,1417 +1245,6 @@ summarize_protover_flags(protover_summary_flags_t *out,
}
}
-/** Given a string at *<b>s</b>, containing a routerstatus object, and an
- * empty smartlist at <b>tokens</b>, parse and return the first router status
- * object in the string, and advance *<b>s</b> to just after the end of the
- * router status. Return NULL and advance *<b>s</b> on error.
- *
- * If <b>vote</b> and <b>vote_rs</b> are provided, don't allocate a fresh
- * routerstatus but use <b>vote_rs</b> instead.
- *
- * If <b>consensus_method</b> is nonzero, this routerstatus is part of a
- * consensus, and we should parse it according to the method used to
- * make that consensus.
- *
- * Parse according to the syntax used by the consensus flavor <b>flav</b>.
- **/
-STATIC routerstatus_t *
-routerstatus_parse_entry_from_string(memarea_t *area,
- const char **s, smartlist_t *tokens,
- networkstatus_t *vote,
- vote_routerstatus_t *vote_rs,
- int consensus_method,
- consensus_flavor_t flav)
-{
- const char *eos, *s_dup = *s;
- routerstatus_t *rs = NULL;
- directory_token_t *tok;
- char timebuf[ISO_TIME_LEN+1];
- struct in_addr in;
- int offset = 0;
- tor_assert(tokens);
- tor_assert(bool_eq(vote, vote_rs));
-
- if (!consensus_method)
- flav = FLAV_NS;
- tor_assert(flav == FLAV_NS || flav == FLAV_MICRODESC);
-
- eos = find_start_of_next_routerstatus(*s);
-
- if (tokenize_string(area,*s, eos, tokens, rtrstatus_token_table,0)) {
- log_warn(LD_DIR, "Error tokenizing router status");
- goto err;
- }
- if (smartlist_len(tokens) < 1) {
- log_warn(LD_DIR, "Impossibly short router status");
- goto err;
- }
- tok = find_by_keyword(tokens, K_R);
- tor_assert(tok->n_args >= 7); /* guaranteed by GE(7) in K_R setup */
- if (flav == FLAV_NS) {
- if (tok->n_args < 8) {
- log_warn(LD_DIR, "Too few arguments to r");
- goto err;
- }
- } else if (flav == FLAV_MICRODESC) {
- offset = -1; /* There is no descriptor digest in an md consensus r line */
- }
-
- if (vote_rs) {
- rs = &vote_rs->status;
- } else {
- rs = tor_malloc_zero(sizeof(routerstatus_t));
- }
-
- if (!is_legal_nickname(tok->args[0])) {
- log_warn(LD_DIR,
- "Invalid nickname %s in router status; skipping.",
- escaped(tok->args[0]));
- goto err;
- }
- strlcpy(rs->nickname, tok->args[0], sizeof(rs->nickname));
-
- if (digest_from_base64(rs->identity_digest, tok->args[1])) {
- log_warn(LD_DIR, "Error decoding identity digest %s",
- escaped(tok->args[1]));
- goto err;
- }
-
- if (flav == FLAV_NS) {
- if (digest_from_base64(rs->descriptor_digest, tok->args[2])) {
- log_warn(LD_DIR, "Error decoding descriptor digest %s",
- escaped(tok->args[2]));
- goto err;
- }
- }
-
- if (tor_snprintf(timebuf, sizeof(timebuf), "%s %s",
- tok->args[3+offset], tok->args[4+offset]) < 0 ||
- parse_iso_time(timebuf, &rs->published_on)<0) {
- log_warn(LD_DIR, "Error parsing time '%s %s' [%d %d]",
- tok->args[3+offset], tok->args[4+offset],
- offset, (int)flav);
- goto err;
- }
-
- if (tor_inet_aton(tok->args[5+offset], &in) == 0) {
- log_warn(LD_DIR, "Error parsing router address in network-status %s",
- escaped(tok->args[5+offset]));
- goto err;
- }
- rs->addr = ntohl(in.s_addr);
-
- rs->or_port = (uint16_t) tor_parse_long(tok->args[6+offset],
- 10,0,65535,NULL,NULL);
- rs->dir_port = (uint16_t) tor_parse_long(tok->args[7+offset],
- 10,0,65535,NULL,NULL);
-
- {
- smartlist_t *a_lines = find_all_by_keyword(tokens, K_A);
- if (a_lines) {
- find_single_ipv6_orport(a_lines, &rs->ipv6_addr, &rs->ipv6_orport);
- smartlist_free(a_lines);
- }
- }
-
- tok = find_opt_by_keyword(tokens, K_S);
- if (tok && vote) {
- int i;
- vote_rs->flags = 0;
- for (i=0; i < tok->n_args; ++i) {
- int p = smartlist_string_pos(vote->known_flags, tok->args[i]);
- if (p >= 0) {
- vote_rs->flags |= (UINT64_C(1)<<p);
- } else {
- log_warn(LD_DIR, "Flags line had a flag %s not listed in known_flags.",
- escaped(tok->args[i]));
- goto err;
- }
- }
- } else if (tok) {
- /* This is a consensus, not a vote. */
- int i;
- for (i=0; i < tok->n_args; ++i) {
- if (!strcmp(tok->args[i], "Exit"))
- rs->is_exit = 1;
- else if (!strcmp(tok->args[i], "Stable"))
- rs->is_stable = 1;
- else if (!strcmp(tok->args[i], "Fast"))
- rs->is_fast = 1;
- else if (!strcmp(tok->args[i], "Running"))
- rs->is_flagged_running = 1;
- else if (!strcmp(tok->args[i], "Named"))
- rs->is_named = 1;
- else if (!strcmp(tok->args[i], "Valid"))
- rs->is_valid = 1;
- else if (!strcmp(tok->args[i], "Guard"))
- rs->is_possible_guard = 1;
- else if (!strcmp(tok->args[i], "BadExit"))
- rs->is_bad_exit = 1;
- else if (!strcmp(tok->args[i], "Authority"))
- rs->is_authority = 1;
- else if (!strcmp(tok->args[i], "Unnamed") &&
- consensus_method >= 2) {
- /* Unnamed is computed right by consensus method 2 and later. */
- rs->is_unnamed = 1;
- } else if (!strcmp(tok->args[i], "HSDir")) {
- rs->is_hs_dir = 1;
- } else if (!strcmp(tok->args[i], "V2Dir")) {
- rs->is_v2_dir = 1;
- }
- }
- /* These are implied true by having been included in a consensus made
- * with a given method */
- rs->is_flagged_running = 1; /* Starting with consensus method 4. */
- rs->is_valid = 1; /* Starting with consensus method 24. */
- }
- {
- const char *protocols = NULL, *version = NULL;
- if ((tok = find_opt_by_keyword(tokens, K_PROTO))) {
- tor_assert(tok->n_args == 1);
- protocols = tok->args[0];
- }
- if ((tok = find_opt_by_keyword(tokens, K_V))) {
- tor_assert(tok->n_args == 1);
- version = tok->args[0];
- if (vote_rs) {
- vote_rs->version = tor_strdup(tok->args[0]);
- }
- }
-
- summarize_protover_flags(&rs->pv, protocols, version);
- }
-
- /* handle weighting/bandwidth info */
- if ((tok = find_opt_by_keyword(tokens, K_W))) {
- int i;
- for (i=0; i < tok->n_args; ++i) {
- if (!strcmpstart(tok->args[i], "Bandwidth=")) {
- int ok;
- rs->bandwidth_kb =
- (uint32_t)tor_parse_ulong(strchr(tok->args[i], '=')+1,
- 10, 0, UINT32_MAX,
- &ok, NULL);
- if (!ok) {
- log_warn(LD_DIR, "Invalid Bandwidth %s", escaped(tok->args[i]));
- goto err;
- }
- rs->has_bandwidth = 1;
- } else if (!strcmpstart(tok->args[i], "Measured=") && vote_rs) {
- int ok;
- vote_rs->measured_bw_kb =
- (uint32_t)tor_parse_ulong(strchr(tok->args[i], '=')+1,
- 10, 0, UINT32_MAX, &ok, NULL);
- if (!ok) {
- log_warn(LD_DIR, "Invalid Measured Bandwidth %s",
- escaped(tok->args[i]));
- goto err;
- }
- vote_rs->has_measured_bw = 1;
- vote->has_measured_bws = 1;
- } else if (!strcmpstart(tok->args[i], "Unmeasured=1")) {
- rs->bw_is_unmeasured = 1;
- } else if (!strcmpstart(tok->args[i], "GuardFraction=")) {
- if (routerstatus_parse_guardfraction(tok->args[i],
- vote, vote_rs, rs) < 0) {
- goto err;
- }
- }
- }
- }
-
- /* parse exit policy summaries */
- if ((tok = find_opt_by_keyword(tokens, K_P))) {
- tor_assert(tok->n_args == 1);
- if (strcmpstart(tok->args[0], "accept ") &&
- strcmpstart(tok->args[0], "reject ")) {
- log_warn(LD_DIR, "Unknown exit policy summary type %s.",
- escaped(tok->args[0]));
- goto err;
- }
- /* XXX weasel: parse this into ports and represent them somehow smart,
- * maybe not here but somewhere on if we need it for the client.
- * we should still parse it here to check it's valid tho.
- */
- rs->exitsummary = tor_strdup(tok->args[0]);
- rs->has_exitsummary = 1;
- }
-
- if (vote_rs) {
- SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, t) {
- if (t->tp == K_M && t->n_args) {
- vote_microdesc_hash_t *line =
- tor_malloc(sizeof(vote_microdesc_hash_t));
- line->next = vote_rs->microdesc;
- line->microdesc_hash_line = tor_strdup(t->args[0]);
- vote_rs->microdesc = line;
- }
- if (t->tp == K_ID) {
- tor_assert(t->n_args >= 2);
- if (!strcmp(t->args[0], "ed25519")) {
- vote_rs->has_ed25519_listing = 1;
- if (strcmp(t->args[1], "none") &&
- digest256_from_base64((char*)vote_rs->ed25519_id,
- t->args[1])<0) {
- log_warn(LD_DIR, "Bogus ed25519 key in networkstatus vote");
- goto err;
- }
- }
- }
- if (t->tp == K_PROTO) {
- tor_assert(t->n_args == 1);
- vote_rs->protocols = tor_strdup(t->args[0]);
- }
- } SMARTLIST_FOREACH_END(t);
- } else if (flav == FLAV_MICRODESC) {
- tok = find_opt_by_keyword(tokens, K_M);
- if (tok) {
- tor_assert(tok->n_args);
- if (digest256_from_base64(rs->descriptor_digest, tok->args[0])) {
- log_warn(LD_DIR, "Error decoding microdescriptor digest %s",
- escaped(tok->args[0]));
- goto err;
- }
- } else {
- log_info(LD_BUG, "Found an entry in networkstatus with no "
- "microdescriptor digest. (Router %s ($%s) at %s:%d.)",
- rs->nickname, hex_str(rs->identity_digest, DIGEST_LEN),
- fmt_addr32(rs->addr), rs->or_port);
- }
- }
-
- if (!strcasecmp(rs->nickname, UNNAMED_ROUTER_NICKNAME))
- rs->is_named = 0;
-
- goto done;
- err:
- dump_desc(s_dup, "routerstatus entry");
- if (rs && !vote_rs)
- routerstatus_free(rs);
- rs = NULL;
- done:
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_clear(tokens);
- if (area) {
- DUMP_AREA(area, "routerstatus entry");
- memarea_clear(area);
- }
- *s = eos;
-
- return rs;
-}
-
-int
-compare_vote_routerstatus_entries(const void **_a, const void **_b)
-{
- const vote_routerstatus_t *a = *_a, *b = *_b;
- return fast_memcmp(a->status.identity_digest, b->status.identity_digest,
- DIGEST_LEN);
-}
-
-/** Verify the bandwidth weights of a network status document */
-int
-networkstatus_verify_bw_weights(networkstatus_t *ns, int consensus_method)
-{
- int64_t G=0, M=0, E=0, D=0, T=0;
- double Wgg, Wgm, Wgd, Wmg, Wmm, Wme, Wmd, Weg, Wem, Wee, Wed;
- double Gtotal=0, Mtotal=0, Etotal=0;
- const char *casename = NULL;
- int valid = 1;
- (void) consensus_method;
-
- const int64_t weight_scale = networkstatus_get_weight_scale_param(ns);
- tor_assert(weight_scale >= 1);
- Wgg = networkstatus_get_bw_weight(ns, "Wgg", -1);
- Wgm = networkstatus_get_bw_weight(ns, "Wgm", -1);
- Wgd = networkstatus_get_bw_weight(ns, "Wgd", -1);
- Wmg = networkstatus_get_bw_weight(ns, "Wmg", -1);
- Wmm = networkstatus_get_bw_weight(ns, "Wmm", -1);
- Wme = networkstatus_get_bw_weight(ns, "Wme", -1);
- Wmd = networkstatus_get_bw_weight(ns, "Wmd", -1);
- Weg = networkstatus_get_bw_weight(ns, "Weg", -1);
- Wem = networkstatus_get_bw_weight(ns, "Wem", -1);
- Wee = networkstatus_get_bw_weight(ns, "Wee", -1);
- Wed = networkstatus_get_bw_weight(ns, "Wed", -1);
-
- if (Wgg<0 || Wgm<0 || Wgd<0 || Wmg<0 || Wmm<0 || Wme<0 || Wmd<0 || Weg<0
- || Wem<0 || Wee<0 || Wed<0) {
- log_warn(LD_BUG, "No bandwidth weights produced in consensus!");
- return 0;
- }
-
- // First, sanity check basic summing properties that hold for all cases
- // We use > 1 as the check for these because they are computed as integers.
- // Sometimes there are rounding errors.
- if (fabs(Wmm - weight_scale) > 1) {
- log_warn(LD_BUG, "Wmm=%f != %"PRId64,
- Wmm, (weight_scale));
- valid = 0;
- }
-
- if (fabs(Wem - Wee) > 1) {
- log_warn(LD_BUG, "Wem=%f != Wee=%f", Wem, Wee);
- valid = 0;
- }
-
- if (fabs(Wgm - Wgg) > 1) {
- log_warn(LD_BUG, "Wgm=%f != Wgg=%f", Wgm, Wgg);
- valid = 0;
- }
-
- if (fabs(Weg - Wed) > 1) {
- log_warn(LD_BUG, "Wed=%f != Weg=%f", Wed, Weg);
- valid = 0;
- }
-
- if (fabs(Wgg + Wmg - weight_scale) > 0.001*weight_scale) {
- log_warn(LD_BUG, "Wgg=%f != %"PRId64" - Wmg=%f", Wgg,
- (weight_scale), Wmg);
- valid = 0;
- }
-
- if (fabs(Wee + Wme - weight_scale) > 0.001*weight_scale) {
- log_warn(LD_BUG, "Wee=%f != %"PRId64" - Wme=%f", Wee,
- (weight_scale), Wme);
- valid = 0;
- }
-
- if (fabs(Wgd + Wmd + Wed - weight_scale) > 0.001*weight_scale) {
- log_warn(LD_BUG, "Wgd=%f + Wmd=%f + Wed=%f != %"PRId64,
- Wgd, Wmd, Wed, (weight_scale));
- valid = 0;
- }
-
- Wgg /= weight_scale;
- Wgm /= weight_scale; (void) Wgm; // unused from here on.
- Wgd /= weight_scale;
-
- Wmg /= weight_scale;
- Wmm /= weight_scale;
- Wme /= weight_scale;
- Wmd /= weight_scale;
-
- Weg /= weight_scale; (void) Weg; // unused from here on.
- Wem /= weight_scale; (void) Wem; // unused from here on.
- Wee /= weight_scale;
- Wed /= weight_scale;
-
- // Then, gather G, M, E, D, T to determine case
- SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
- int is_exit = 0;
- /* Bug #2203: Don't count bad exits as exits for balancing */
- is_exit = rs->is_exit && !rs->is_bad_exit;
- if (rs->has_bandwidth) {
- T += rs->bandwidth_kb;
- if (is_exit && rs->is_possible_guard) {
- D += rs->bandwidth_kb;
- Gtotal += Wgd*rs->bandwidth_kb;
- Mtotal += Wmd*rs->bandwidth_kb;
- Etotal += Wed*rs->bandwidth_kb;
- } else if (is_exit) {
- E += rs->bandwidth_kb;
- Mtotal += Wme*rs->bandwidth_kb;
- Etotal += Wee*rs->bandwidth_kb;
- } else if (rs->is_possible_guard) {
- G += rs->bandwidth_kb;
- Gtotal += Wgg*rs->bandwidth_kb;
- Mtotal += Wmg*rs->bandwidth_kb;
- } else {
- M += rs->bandwidth_kb;
- Mtotal += Wmm*rs->bandwidth_kb;
- }
- } else {
- log_warn(LD_BUG, "Missing consensus bandwidth for router %s",
- routerstatus_describe(rs));
- }
- } SMARTLIST_FOREACH_END(rs);
-
- // Finally, check equality conditions depending upon case 1, 2 or 3
- // Full equality cases: 1, 3b
- // Partial equality cases: 2b (E=G), 3a (M=E)
- // Fully unknown: 2a
- if (3*E >= T && 3*G >= T) {
- // Case 1: Neither are scarce
- casename = "Case 1";
- if (fabs(Etotal-Mtotal) > 0.01*MAX(Etotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Mtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Mtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (fabs(Gtotal-Mtotal) > 0.01*MAX(Gtotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Mtotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Mtotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- } else if (3*E < T && 3*G < T) {
- int64_t R = MIN(E, G);
- int64_t S = MAX(E, G);
- /*
- * Case 2: Both Guards and Exits are scarce
- * Balance D between E and G, depending upon
- * D capacity and scarcity. Devote no extra
- * bandwidth to middle nodes.
- */
- if (R+D < S) { // Subcase a
- double Rtotal, Stotal;
- if (E < G) {
- Rtotal = Etotal;
- Stotal = Gtotal;
- } else {
- Rtotal = Gtotal;
- Stotal = Etotal;
- }
- casename = "Case 2a";
- // Rtotal < Stotal
- if (Rtotal > Stotal) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Rtotal %f > Stotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Rtotal, Stotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- // Rtotal < T/3
- if (3*Rtotal > T) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: 3*Rtotal %f > T "
- "%"PRId64". G=%"PRId64" M=%"PRId64" E=%"PRId64
- " D=%"PRId64" T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Rtotal*3, (T),
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- // Stotal < T/3
- if (3*Stotal > T) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: 3*Stotal %f > T "
- "%"PRId64". G=%"PRId64" M=%"PRId64" E=%"PRId64
- " D=%"PRId64" T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Stotal*3, (T),
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- // Mtotal > T/3
- if (3*Mtotal < T) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: 3*Mtotal %f < T "
- "%"PRId64". "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Mtotal*3, (T),
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- } else { // Subcase b: R+D > S
- casename = "Case 2b";
-
- /* Check the rare-M redirect case. */
- if (D != 0 && 3*M < T) {
- casename = "Case 2b (balanced)";
- if (fabs(Etotal-Mtotal) > 0.01*MAX(Etotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Mtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Mtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (fabs(Gtotal-Mtotal) > 0.01*MAX(Gtotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Mtotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Mtotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- } else {
- if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- }
- }
- } else { // if (E < T/3 || G < T/3) {
- int64_t S = MIN(E, G);
- int64_t NS = MAX(E, G);
- if (3*(S+D) < T) { // Subcase a:
- double Stotal;
- double NStotal;
- if (G < E) {
- casename = "Case 3a (G scarce)";
- Stotal = Gtotal;
- NStotal = Etotal;
- } else { // if (G >= E) {
- casename = "Case 3a (E scarce)";
- NStotal = Gtotal;
- Stotal = Etotal;
- }
- // Stotal < T/3
- if (3*Stotal > T) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: 3*Stotal %f > T "
- "%"PRId64". G=%"PRId64" M=%"PRId64" E=%"PRId64
- " D=%"PRId64" T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Stotal*3, (T),
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (NS >= M) {
- if (fabs(NStotal-Mtotal) > 0.01*MAX(NStotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: NStotal %f != Mtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, NStotal, Mtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- } else {
- // if NS < M, NStotal > T/3 because only one of G or E is scarce
- if (3*NStotal < T) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: 3*NStotal %f < T "
- "%"PRId64". G=%"PRId64" M=%"PRId64
- " E=%"PRId64" D=%"PRId64" T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, NStotal*3, (T),
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- }
- } else { // Subcase b: S+D >= T/3
- casename = "Case 3b";
- if (fabs(Etotal-Mtotal) > 0.01*MAX(Etotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Mtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Mtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (fabs(Etotal-Gtotal) > 0.01*MAX(Etotal,Gtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Etotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Etotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- if (fabs(Gtotal-Mtotal) > 0.01*MAX(Gtotal,Mtotal)) {
- log_warn(LD_DIR,
- "Bw Weight Failure for %s: Mtotal %f != Gtotal %f. "
- "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
- " T=%"PRId64". "
- "Wgg=%f Wgd=%f Wmg=%f Wme=%f Wmd=%f Wee=%f Wed=%f",
- casename, Mtotal, Gtotal,
- (G), (M), (E),
- (D), (T),
- Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed);
- valid = 0;
- }
- }
- }
-
- if (valid)
- log_notice(LD_DIR, "Bandwidth-weight %s is verified and valid.",
- casename);
-
- return valid;
-}
-
-/** Check if a shared random value of type <b>srv_type</b> is in
- * <b>tokens</b>. If there is, parse it and set it to <b>srv_out</b>. Return
- * -1 on failure, 0 on success. The resulting srv is allocated on the heap and
- * it's the responsibility of the caller to free it. */
-static int
-extract_one_srv(smartlist_t *tokens, directory_keyword srv_type,
- sr_srv_t **srv_out)
-{
- int ret = -1;
- directory_token_t *tok;
- sr_srv_t *srv = NULL;
- smartlist_t *chunks;
-
- tor_assert(tokens);
-
- chunks = smartlist_new();
- tok = find_opt_by_keyword(tokens, srv_type);
- if (!tok) {
- /* That's fine, no SRV is allowed. */
- ret = 0;
- goto end;
- }
- for (int i = 0; i < tok->n_args; i++) {
- smartlist_add(chunks, tok->args[i]);
- }
- srv = sr_parse_srv(chunks);
- if (srv == NULL) {
- log_warn(LD_DIR, "SR: Unparseable SRV %s", escaped(tok->object_body));
- goto end;
- }
- /* All is good. */
- *srv_out = srv;
- ret = 0;
- end:
- smartlist_free(chunks);
- return ret;
-}
-
-/** Extract any shared random values found in <b>tokens</b> and place them in
- * the networkstatus <b>ns</b>. */
-static void
-extract_shared_random_srvs(networkstatus_t *ns, smartlist_t *tokens)
-{
- const char *voter_identity;
- networkstatus_voter_info_t *voter;
-
- tor_assert(ns);
- tor_assert(tokens);
- /* Can be only one of them else code flow. */
- tor_assert(ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_CONSENSUS);
-
- if (ns->type == NS_TYPE_VOTE) {
- voter = smartlist_get(ns->voters, 0);
- tor_assert(voter);
- voter_identity = hex_str(voter->identity_digest,
- sizeof(voter->identity_digest));
- } else {
- /* Consensus has multiple voters so no specific voter. */
- voter_identity = "consensus";
- }
-
- /* We extract both, and on error everything is stopped because it means
- * the vote is malformed for the shared random value(s). */
- if (extract_one_srv(tokens, K_PREVIOUS_SRV, &ns->sr_info.previous_srv) < 0) {
- log_warn(LD_DIR, "SR: Unable to parse previous SRV from %s",
- voter_identity);
- /* Maybe we have a chance with the current SRV so let's try it anyway. */
- }
- if (extract_one_srv(tokens, K_CURRENT_SRV, &ns->sr_info.current_srv) < 0) {
- log_warn(LD_DIR, "SR: Unable to parse current SRV from %s",
- voter_identity);
- }
-}
-
-/** Parse a v3 networkstatus vote, opinion, or consensus (depending on
- * ns_type), from <b>s</b>, and return the result. Return NULL on failure. */
-networkstatus_t *
-networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
- networkstatus_type_t ns_type)
-{
- smartlist_t *tokens = smartlist_new();
- smartlist_t *rs_tokens = NULL, *footer_tokens = NULL;
- networkstatus_voter_info_t *voter = NULL;
- networkstatus_t *ns = NULL;
- common_digests_t ns_digests;
- uint8_t sha3_as_signed[DIGEST256_LEN];
- const char *cert, *end_of_header, *end_of_footer, *s_dup = s;
- directory_token_t *tok;
- struct in_addr in;
- int i, inorder, n_signatures = 0;
- memarea_t *area = NULL, *rs_area = NULL;
- consensus_flavor_t flav = FLAV_NS;
- char *last_kwd=NULL;
-
- tor_assert(s);
-
- if (eos_out)
- *eos_out = NULL;
-
- if (router_get_networkstatus_v3_hashes(s, &ns_digests) ||
- router_get_networkstatus_v3_sha3_as_signed(sha3_as_signed, s)<0) {
- log_warn(LD_DIR, "Unable to compute digest of network-status");
- goto err;
- }
-
- area = memarea_new();
- end_of_header = find_start_of_next_routerstatus(s);
- if (tokenize_string(area, s, end_of_header, tokens,
- (ns_type == NS_TYPE_CONSENSUS) ?
- networkstatus_consensus_token_table :
- networkstatus_token_table, 0)) {
- log_warn(LD_DIR, "Error tokenizing network-status header");
- goto err;
- }
-
- ns = tor_malloc_zero(sizeof(networkstatus_t));
- memcpy(&ns->digests, &ns_digests, sizeof(ns_digests));
- memcpy(&ns->digest_sha3_as_signed, sha3_as_signed, sizeof(sha3_as_signed));
-
- tok = find_by_keyword(tokens, K_NETWORK_STATUS_VERSION);
- tor_assert(tok);
- if (tok->n_args > 1) {
- int flavor = networkstatus_parse_flavor_name(tok->args[1]);
- if (flavor < 0) {
- log_warn(LD_DIR, "Can't parse document with unknown flavor %s",
- escaped(tok->args[1]));
- goto err;
- }
- ns->flavor = flav = flavor;
- }
- if (flav != FLAV_NS && ns_type != NS_TYPE_CONSENSUS) {
- log_warn(LD_DIR, "Flavor found on non-consensus networkstatus.");
- goto err;
- }
-
- if (ns_type != NS_TYPE_CONSENSUS) {
- const char *end_of_cert = NULL;
- if (!(cert = strstr(s, "\ndir-key-certificate-version")))
- goto err;
- ++cert;
- ns->cert = authority_cert_parse_from_string(cert, &end_of_cert);
- if (!ns->cert || !end_of_cert || end_of_cert > end_of_header)
- goto err;
- }
-
- tok = find_by_keyword(tokens, K_VOTE_STATUS);
- tor_assert(tok->n_args);
- if (!strcmp(tok->args[0], "vote")) {
- ns->type = NS_TYPE_VOTE;
- } else if (!strcmp(tok->args[0], "consensus")) {
- ns->type = NS_TYPE_CONSENSUS;
- } else if (!strcmp(tok->args[0], "opinion")) {
- ns->type = NS_TYPE_OPINION;
- } else {
- log_warn(LD_DIR, "Unrecognized vote status %s in network-status",
- escaped(tok->args[0]));
- goto err;
- }
- if (ns_type != ns->type) {
- log_warn(LD_DIR, "Got the wrong kind of v3 networkstatus.");
- goto err;
- }
-
- if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
- tok = find_by_keyword(tokens, K_PUBLISHED);
- if (parse_iso_time(tok->args[0], &ns->published))
- goto err;
-
- ns->supported_methods = smartlist_new();
- tok = find_opt_by_keyword(tokens, K_CONSENSUS_METHODS);
- if (tok) {
- for (i=0; i < tok->n_args; ++i)
- smartlist_add_strdup(ns->supported_methods, tok->args[i]);
- } else {
- smartlist_add_strdup(ns->supported_methods, "1");
- }
- } else {
- tok = find_opt_by_keyword(tokens, K_CONSENSUS_METHOD);
- if (tok) {
- int num_ok;
- ns->consensus_method = (int)tor_parse_long(tok->args[0], 10, 1, INT_MAX,
- &num_ok, NULL);
- if (!num_ok)
- goto err;
- } else {
- ns->consensus_method = 1;
- }
- }
-
- if ((tok = find_opt_by_keyword(tokens, K_RECOMMENDED_CLIENT_PROTOCOLS)))
- ns->recommended_client_protocols = tor_strdup(tok->args[0]);
- if ((tok = find_opt_by_keyword(tokens, K_RECOMMENDED_RELAY_PROTOCOLS)))
- ns->recommended_relay_protocols = tor_strdup(tok->args[0]);
- if ((tok = find_opt_by_keyword(tokens, K_REQUIRED_CLIENT_PROTOCOLS)))
- ns->required_client_protocols = tor_strdup(tok->args[0]);
- if ((tok = find_opt_by_keyword(tokens, K_REQUIRED_RELAY_PROTOCOLS)))
- ns->required_relay_protocols = tor_strdup(tok->args[0]);
-
- tok = find_by_keyword(tokens, K_VALID_AFTER);
- if (parse_iso_time(tok->args[0], &ns->valid_after))
- goto err;
-
- tok = find_by_keyword(tokens, K_FRESH_UNTIL);
- if (parse_iso_time(tok->args[0], &ns->fresh_until))
- goto err;
-
- tok = find_by_keyword(tokens, K_VALID_UNTIL);
- if (parse_iso_time(tok->args[0], &ns->valid_until))
- goto err;
-
- tok = find_by_keyword(tokens, K_VOTING_DELAY);
- tor_assert(tok->n_args >= 2);
- {
- int ok;
- ns->vote_seconds =
- (int) tor_parse_long(tok->args[0], 10, 0, INT_MAX, &ok, NULL);
- if (!ok)
- goto err;
- ns->dist_seconds =
- (int) tor_parse_long(tok->args[1], 10, 0, INT_MAX, &ok, NULL);
- if (!ok)
- goto err;
- }
- if (ns->valid_after +
- (get_options()->TestingTorNetwork ?
- MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) > ns->fresh_until) {
- log_warn(LD_DIR, "Vote/consensus freshness interval is too short");
- goto err;
- }
- if (ns->valid_after +
- (get_options()->TestingTorNetwork ?
- MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL)*2 > ns->valid_until) {
- log_warn(LD_DIR, "Vote/consensus liveness interval is too short");
- goto err;
- }
- if (ns->vote_seconds < MIN_VOTE_SECONDS) {
- log_warn(LD_DIR, "Vote seconds is too short");
- goto err;
- }
- if (ns->dist_seconds < MIN_DIST_SECONDS) {
- log_warn(LD_DIR, "Dist seconds is too short");
- goto err;
- }
-
- if ((tok = find_opt_by_keyword(tokens, K_CLIENT_VERSIONS))) {
- ns->client_versions = tor_strdup(tok->args[0]);
- }
- if ((tok = find_opt_by_keyword(tokens, K_SERVER_VERSIONS))) {
- ns->server_versions = tor_strdup(tok->args[0]);
- }
-
- {
- smartlist_t *package_lst = find_all_by_keyword(tokens, K_PACKAGE);
- ns->package_lines = smartlist_new();
- if (package_lst) {
- SMARTLIST_FOREACH(package_lst, directory_token_t *, t,
- smartlist_add_strdup(ns->package_lines, t->args[0]));
- }
- smartlist_free(package_lst);
- }
-
- tok = find_by_keyword(tokens, K_KNOWN_FLAGS);
- ns->known_flags = smartlist_new();
- inorder = 1;
- for (i = 0; i < tok->n_args; ++i) {
- smartlist_add_strdup(ns->known_flags, tok->args[i]);
- if (i>0 && strcmp(tok->args[i-1], tok->args[i])>= 0) {
- log_warn(LD_DIR, "%s >= %s", tok->args[i-1], tok->args[i]);
- inorder = 0;
- }
- }
- if (!inorder) {
- log_warn(LD_DIR, "known-flags not in order");
- goto err;
- }
- if (ns->type != NS_TYPE_CONSENSUS &&
- smartlist_len(ns->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
- /* If we allowed more than 64 flags in votes, then parsing them would make
- * us invoke undefined behavior whenever we used 1<<flagnum to do a
- * bit-shift. This is only for votes and opinions: consensus users don't
- * care about flags they don't recognize, and so don't build a bitfield
- * for them. */
- log_warn(LD_DIR, "Too many known-flags in consensus vote or opinion");
- goto err;
- }
-
- tok = find_opt_by_keyword(tokens, K_PARAMS);
- if (tok) {
- int any_dups = 0;
- inorder = 1;
- ns->net_params = smartlist_new();
- for (i = 0; i < tok->n_args; ++i) {
- int ok=0;
- char *eq = strchr(tok->args[i], '=');
- size_t eq_pos;
- if (!eq) {
- log_warn(LD_DIR, "Bad element '%s' in params", escaped(tok->args[i]));
- goto err;
- }
- eq_pos = eq-tok->args[i];
- tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
- if (!ok) {
- log_warn(LD_DIR, "Bad element '%s' in params", escaped(tok->args[i]));
- goto err;
- }
- if (i > 0 && strcmp(tok->args[i-1], tok->args[i]) >= 0) {
- log_warn(LD_DIR, "%s >= %s", tok->args[i-1], tok->args[i]);
- inorder = 0;
- }
- if (last_kwd && eq_pos == strlen(last_kwd) &&
- fast_memeq(last_kwd, tok->args[i], eq_pos)) {
- log_warn(LD_DIR, "Duplicate value for %s parameter",
- escaped(tok->args[i]));
- any_dups = 1;
- }
- tor_free(last_kwd);
- last_kwd = tor_strndup(tok->args[i], eq_pos);
- smartlist_add_strdup(ns->net_params, tok->args[i]);
- }
- if (!inorder) {
- log_warn(LD_DIR, "params not in order");
- goto err;
- }
- if (any_dups) {
- log_warn(LD_DIR, "Duplicate in parameters");
- goto err;
- }
- }
-
- ns->voters = smartlist_new();
-
- SMARTLIST_FOREACH_BEGIN(tokens, directory_token_t *, _tok) {
- tok = _tok;
- if (tok->tp == K_DIR_SOURCE) {
- tor_assert(tok->n_args >= 6);
-
- if (voter)
- smartlist_add(ns->voters, voter);
- voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
- voter->sigs = smartlist_new();
- if (ns->type != NS_TYPE_CONSENSUS)
- memcpy(voter->vote_digest, ns_digests.d[DIGEST_SHA1], DIGEST_LEN);
-
- voter->nickname = tor_strdup(tok->args[0]);
- if (strlen(tok->args[1]) != HEX_DIGEST_LEN ||
- base16_decode(voter->identity_digest, sizeof(voter->identity_digest),
- tok->args[1], HEX_DIGEST_LEN)
- != sizeof(voter->identity_digest)) {
- log_warn(LD_DIR, "Error decoding identity digest %s in "
- "network-status document.", escaped(tok->args[1]));
- goto err;
- }
- if (ns->type != NS_TYPE_CONSENSUS &&
- tor_memneq(ns->cert->cache_info.identity_digest,
- voter->identity_digest, DIGEST_LEN)) {
- log_warn(LD_DIR,"Mismatch between identities in certificate and vote");
- goto err;
- }
- if (ns->type != NS_TYPE_CONSENSUS) {
- if (authority_cert_is_blacklisted(ns->cert)) {
- log_warn(LD_DIR, "Rejecting vote signature made with blacklisted "
- "signing key %s",
- hex_str(ns->cert->signing_key_digest, DIGEST_LEN));
- goto err;
- }
- }
- voter->address = tor_strdup(tok->args[2]);
- if (!tor_inet_aton(tok->args[3], &in)) {
- log_warn(LD_DIR, "Error decoding IP address %s in network-status.",
- escaped(tok->args[3]));
- goto err;
- }
- voter->addr = ntohl(in.s_addr);
- int ok;
- voter->dir_port = (uint16_t)
- tor_parse_long(tok->args[4], 10, 0, 65535, &ok, NULL);
- if (!ok)
- goto err;
- voter->or_port = (uint16_t)
- tor_parse_long(tok->args[5], 10, 0, 65535, &ok, NULL);
- if (!ok)
- goto err;
- } else if (tok->tp == K_CONTACT) {
- if (!voter || voter->contact) {
- log_warn(LD_DIR, "contact element is out of place.");
- goto err;
- }
- voter->contact = tor_strdup(tok->args[0]);
- } else if (tok->tp == K_VOTE_DIGEST) {
- tor_assert(ns->type == NS_TYPE_CONSENSUS);
- tor_assert(tok->n_args >= 1);
- if (!voter || ! tor_digest_is_zero(voter->vote_digest)) {
- log_warn(LD_DIR, "vote-digest element is out of place.");
- goto err;
- }
- if (strlen(tok->args[0]) != HEX_DIGEST_LEN ||
- base16_decode(voter->vote_digest, sizeof(voter->vote_digest),
- tok->args[0], HEX_DIGEST_LEN)
- != sizeof(voter->vote_digest)) {
- log_warn(LD_DIR, "Error decoding vote digest %s in "
- "network-status consensus.", escaped(tok->args[0]));
- goto err;
- }
- }
- } SMARTLIST_FOREACH_END(_tok);
- if (voter) {
- smartlist_add(ns->voters, voter);
- voter = NULL;
- }
- if (smartlist_len(ns->voters) == 0) {
- log_warn(LD_DIR, "Missing dir-source elements in a networkstatus.");
- goto err;
- } else if (ns->type != NS_TYPE_CONSENSUS && smartlist_len(ns->voters) != 1) {
- log_warn(LD_DIR, "Too many dir-source elements in a vote networkstatus.");
- goto err;
- }
-
- if (ns->type != NS_TYPE_CONSENSUS &&
- (tok = find_opt_by_keyword(tokens, K_LEGACY_DIR_KEY))) {
- int bad = 1;
- if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
- networkstatus_voter_info_t *voter_0 = smartlist_get(ns->voters, 0);
- if (base16_decode(voter_0->legacy_id_digest, DIGEST_LEN,
- tok->args[0], HEX_DIGEST_LEN) != DIGEST_LEN)
- bad = 1;
- else
- bad = 0;
- }
- if (bad) {
- log_warn(LD_DIR, "Invalid legacy key digest %s on vote.",
- escaped(tok->args[0]));
- }
- }
-
- /* If this is a vote document, check if information about the shared
- randomness protocol is included, and extract it. */
- if (ns->type == NS_TYPE_VOTE) {
- dirvote_parse_sr_commits(ns, tokens);
- }
- /* For both a vote and consensus, extract the shared random values. */
- if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_CONSENSUS) {
- extract_shared_random_srvs(ns, tokens);
- }
-
- /* Parse routerstatus lines. */
- rs_tokens = smartlist_new();
- rs_area = memarea_new();
- s = end_of_header;
- ns->routerstatus_list = smartlist_new();
-
- while (!strcmpstart(s, "r ")) {
- if (ns->type != NS_TYPE_CONSENSUS) {
- vote_routerstatus_t *rs = tor_malloc_zero(sizeof(vote_routerstatus_t));
- if (routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens, ns,
- rs, 0, 0)) {
- smartlist_add(ns->routerstatus_list, rs);
- } else {
- vote_routerstatus_free(rs);
- }
- } else {
- routerstatus_t *rs;
- if ((rs = routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens,
- NULL, NULL,
- ns->consensus_method,
- flav))) {
- /* Use exponential-backoff scheduling when downloading microdescs */
- smartlist_add(ns->routerstatus_list, rs);
- }
- }
- }
- for (i = 1; i < smartlist_len(ns->routerstatus_list); ++i) {
- routerstatus_t *rs1, *rs2;
- if (ns->type != NS_TYPE_CONSENSUS) {
- vote_routerstatus_t *a = smartlist_get(ns->routerstatus_list, i-1);
- vote_routerstatus_t *b = smartlist_get(ns->routerstatus_list, i);
- rs1 = &a->status; rs2 = &b->status;
- } else {
- rs1 = smartlist_get(ns->routerstatus_list, i-1);
- rs2 = smartlist_get(ns->routerstatus_list, i);
- }
- if (fast_memcmp(rs1->identity_digest, rs2->identity_digest, DIGEST_LEN)
- >= 0) {
- log_warn(LD_DIR, "Networkstatus entries not sorted by identity digest");
- goto err;
- }
- }
- if (ns_type != NS_TYPE_CONSENSUS) {
- digest256map_t *ed_id_map = digest256map_new();
- SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, vote_routerstatus_t *,
- vrs) {
- if (! vrs->has_ed25519_listing ||
- tor_mem_is_zero((const char *)vrs->ed25519_id, DIGEST256_LEN))
- continue;
- if (digest256map_get(ed_id_map, vrs->ed25519_id) != NULL) {
- log_warn(LD_DIR, "Vote networkstatus ed25519 identities were not "
- "unique");
- digest256map_free(ed_id_map, NULL);
- goto err;
- }
- digest256map_set(ed_id_map, vrs->ed25519_id, (void*)1);
- } SMARTLIST_FOREACH_END(vrs);
- digest256map_free(ed_id_map, NULL);
- }
-
- /* Parse footer; check signature. */
- footer_tokens = smartlist_new();
- if ((end_of_footer = strstr(s, "\nnetwork-status-version ")))
- ++end_of_footer;
- else
- end_of_footer = s + strlen(s);
- if (tokenize_string(area,s, end_of_footer, footer_tokens,
- networkstatus_vote_footer_token_table, 0)) {
- log_warn(LD_DIR, "Error tokenizing network-status vote footer.");
- goto err;
- }
-
- {
- int found_sig = 0;
- SMARTLIST_FOREACH_BEGIN(footer_tokens, directory_token_t *, _tok) {
- tok = _tok;
- if (tok->tp == K_DIRECTORY_SIGNATURE)
- found_sig = 1;
- else if (found_sig) {
- log_warn(LD_DIR, "Extraneous token after first directory-signature");
- goto err;
- }
- } SMARTLIST_FOREACH_END(_tok);
- }
-
- if ((tok = find_opt_by_keyword(footer_tokens, K_DIRECTORY_FOOTER))) {
- if (tok != smartlist_get(footer_tokens, 0)) {
- log_warn(LD_DIR, "Misplaced directory-footer token");
- goto err;
- }
- }
-
- tok = find_opt_by_keyword(footer_tokens, K_BW_WEIGHTS);
- if (tok) {
- ns->weight_params = smartlist_new();
- for (i = 0; i < tok->n_args; ++i) {
- int ok=0;
- char *eq = strchr(tok->args[i], '=');
- if (!eq) {
- log_warn(LD_DIR, "Bad element '%s' in weight params",
- escaped(tok->args[i]));
- goto err;
- }
- tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
- if (!ok) {
- log_warn(LD_DIR, "Bad element '%s' in params", escaped(tok->args[i]));
- goto err;
- }
- smartlist_add_strdup(ns->weight_params, tok->args[i]);
- }
- }
-
- SMARTLIST_FOREACH_BEGIN(footer_tokens, directory_token_t *, _tok) {
- char declared_identity[DIGEST_LEN];
- networkstatus_voter_info_t *v;
- document_signature_t *sig;
- const char *id_hexdigest = NULL;
- const char *sk_hexdigest = NULL;
- digest_algorithm_t alg = DIGEST_SHA1;
- tok = _tok;
- if (tok->tp != K_DIRECTORY_SIGNATURE)
- continue;
- tor_assert(tok->n_args >= 2);
- if (tok->n_args == 2) {
- id_hexdigest = tok->args[0];
- sk_hexdigest = tok->args[1];
- } else {
- const char *algname = tok->args[0];
- int a;
- id_hexdigest = tok->args[1];
- sk_hexdigest = tok->args[2];
- a = crypto_digest_algorithm_parse_name(algname);
- if (a<0) {
- log_warn(LD_DIR, "Unknown digest algorithm %s; skipping",
- escaped(algname));
- continue;
- }
- alg = a;
- }
-
- if (!tok->object_type ||
- strcmp(tok->object_type, "SIGNATURE") ||
- tok->object_size < 128 || tok->object_size > 512) {
- log_warn(LD_DIR, "Bad object type or length on directory-signature");
- goto err;
- }
-
- if (strlen(id_hexdigest) != HEX_DIGEST_LEN ||
- base16_decode(declared_identity, sizeof(declared_identity),
- id_hexdigest, HEX_DIGEST_LEN)
- != sizeof(declared_identity)) {
- log_warn(LD_DIR, "Error decoding declared identity %s in "
- "network-status document.", escaped(id_hexdigest));
- goto err;
- }
- if (!(v = networkstatus_get_voter_by_id(ns, declared_identity))) {
- log_warn(LD_DIR, "ID on signature on network-status document does "
- "not match any declared directory source.");
- goto err;
- }
- sig = tor_malloc_zero(sizeof(document_signature_t));
- memcpy(sig->identity_digest, v->identity_digest, DIGEST_LEN);
- sig->alg = alg;
- if (strlen(sk_hexdigest) != HEX_DIGEST_LEN ||
- base16_decode(sig->signing_key_digest, sizeof(sig->signing_key_digest),
- sk_hexdigest, HEX_DIGEST_LEN)
- != sizeof(sig->signing_key_digest)) {
- log_warn(LD_DIR, "Error decoding declared signing key digest %s in "
- "network-status document.", escaped(sk_hexdigest));
- tor_free(sig);
- goto err;
- }
-
- if (ns->type != NS_TYPE_CONSENSUS) {
- if (tor_memneq(declared_identity, ns->cert->cache_info.identity_digest,
- DIGEST_LEN)) {
- log_warn(LD_DIR, "Digest mismatch between declared and actual on "
- "network-status vote.");
- tor_free(sig);
- goto err;
- }
- }
-
- if (networkstatus_get_voter_sig_by_alg(v, sig->alg)) {
- /* We already parsed a vote with this algorithm from this voter. Use the
- first one. */
- log_fn(LOG_PROTOCOL_WARN, LD_DIR, "We received a networkstatus "
- "that contains two signatures from the same voter with the same "
- "algorithm. Ignoring the second signature.");
- tor_free(sig);
- continue;
- }
-
- if (ns->type != NS_TYPE_CONSENSUS) {
- if (check_signature_token(ns_digests.d[DIGEST_SHA1], DIGEST_LEN,
- tok, ns->cert->signing_key, 0,
- "network-status document")) {
- tor_free(sig);
- goto err;
- }
- sig->good_signature = 1;
- } else {
- if (tok->object_size >= INT_MAX || tok->object_size >= SIZE_T_CEILING) {
- tor_free(sig);
- goto err;
- }
- sig->signature = tor_memdup(tok->object_body, tok->object_size);
- sig->signature_len = (int) tok->object_size;
- }
- smartlist_add(v->sigs, sig);
-
- ++n_signatures;
- } SMARTLIST_FOREACH_END(_tok);
-
- if (! n_signatures) {
- log_warn(LD_DIR, "No signatures on networkstatus document.");
- goto err;
- } else if (ns->type == NS_TYPE_VOTE && n_signatures != 1) {
- log_warn(LD_DIR, "Received more than one signature on a "
- "network-status vote.");
- goto err;
- }
-
- if (eos_out)
- *eos_out = end_of_footer;
-
- goto done;
- err:
- dump_desc(s_dup, "v3 networkstatus");
- networkstatus_vote_free(ns);
- ns = NULL;
- done:
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(tokens);
- }
- if (voter) {
- if (voter->sigs) {
- SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
- document_signature_free(sig));
- smartlist_free(voter->sigs);
- }
- tor_free(voter->nickname);
- tor_free(voter->address);
- tor_free(voter->contact);
- tor_free(voter);
- }
- if (rs_tokens) {
- SMARTLIST_FOREACH(rs_tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(rs_tokens);
- }
- if (footer_tokens) {
- SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_clear(t));
- smartlist_free(footer_tokens);
- }
- if (area) {
- DUMP_AREA(area, "v3 networkstatus");
- memarea_drop_all(area);
- }
- if (rs_area)
- memarea_drop_all(rs_area);
- tor_free(last_kwd);
-
- return ns;
-}
-
/** Parse the addr policy in the string <b>s</b> and return it. If
* assume_action is nonnegative, then insert its action (ADDR_POLICY_ACCEPT or
* ADDR_POLICY_REJECT) for items that specify no action.
diff --git a/src/feature/dirparse/routerparse.h b/src/feature/dirparse/routerparse.h
index eb0ca880f..80c974e06 100644
--- a/src/feature/dirparse/routerparse.h
+++ b/src/feature/dirparse/routerparse.h
@@ -14,16 +14,7 @@
#include "core/or/versions.h"
-enum networkstatus_type_t;
-
int router_get_router_hash(const char *s, size_t s_len, char *digest);
-int router_get_networkstatus_v3_hashes(const char *s,
- common_digests_t *digests);
-int router_get_networkstatus_v3_signed_boundaries(const char *s,
- const char **start_out,
- const char **end_out);
-int router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
- const char *s);
int router_get_extrainfo_hash(const char *s, size_t s_len, char *digest);
int router_parse_list_from_string(const char **s, const char *eos,
@@ -46,12 +37,6 @@ extrainfo_t *extrainfo_parse_entry_from_string(const char *s, const char *end,
MOCK_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
(const char *s, int assume_action, int *malformed_list));
-int compare_vote_routerstatus_entries(const void **_a, const void **_b);
-int networkstatus_verify_bw_weights(networkstatus_t *ns, int);
-networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
- const char **eos_out,
- enum networkstatus_type_t ns_type);
-
int find_single_ipv6_orport(const smartlist_t *list,
tor_addr_t *addr_out,
uint16_t *port_out);
@@ -59,23 +44,9 @@ int find_single_ipv6_orport(const smartlist_t *list,
void routerparse_init(void);
void routerparse_free_all(void);
-#ifdef ROUTERPARSE_PRIVATE
-STATIC int routerstatus_parse_guardfraction(const char *guardfraction_str,
- networkstatus_t *vote,
- vote_routerstatus_t *vote_rs,
- routerstatus_t *rs);
-struct memarea_t;
-STATIC routerstatus_t *routerstatus_parse_entry_from_string(
- struct memarea_t *area,
- const char **s, smartlist_t *tokens,
- networkstatus_t *vote,
- vote_routerstatus_t *vote_rs,
- int consensus_method,
- consensus_flavor_t flav);
-STATIC void summarize_protover_flags(protover_summary_flags_t *out,
- const char *protocols,
+void summarize_protover_flags(protover_summary_flags_t *out,
+ const char *protocols,
const char *version);
-#endif /* defined(ROUTERPARSE_PRIVATE) */
#define ED_DESC_SIGNATURE_PREFIX "Tor router descriptor signature v1"
diff --git a/src/feature/nodelist/networkstatus.c b/src/feature/nodelist/networkstatus.c
index d214e6470..de2451b79 100644
--- a/src/feature/nodelist/networkstatus.c
+++ b/src/feature/nodelist/networkstatus.c
@@ -53,6 +53,7 @@
#include "core/or/protover.h"
#include "core/or/relay.h"
#include "core/or/scheduler.h"
+#include "core/or/versions.h"
#include "feature/client/bridges.h"
#include "feature/client/entrynodes.h"
#include "feature/client/transports.h"
@@ -64,7 +65,7 @@
#include "feature/dirclient/dlstatus.h"
#include "feature/dircommon/directory.h"
#include "feature/dircommon/voting_schedule.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/hibernate/hibernate.h"
#include "feature/nodelist/authcert.h"
#include "feature/nodelist/dirlist.h"
diff --git a/src/test/fuzz/fuzz_consensus.c b/src/test/fuzz/fuzz_consensus.c
index 64507c67e..b56702a65 100644
--- a/src/test/fuzz/fuzz_consensus.c
+++ b/src/test/fuzz/fuzz_consensus.c
@@ -2,7 +2,7 @@
/* See LICENSE for licensing information */
#define SIGCOMMON_PRIVATE
#include "core/or/or.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/networkstatus.h"
diff --git a/src/test/fuzz/fuzz_vrs.c b/src/test/fuzz/fuzz_vrs.c
index 8fb6e6ef2..0b869aa5c 100644
--- a/src/test/fuzz/fuzz_vrs.c
+++ b/src/test/fuzz/fuzz_vrs.c
@@ -1,9 +1,9 @@
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#define ROUTERPARSE_PRIVATE
+#define NS_PARSE_PRIVATE
#define NETWORKSTATUS_PRIVATE
#include "core/or/or.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/dirparse/unparseable.h"
#include "lib/memarea/memarea.h"
#include "feature/nodelist/microdesc.h"
diff --git a/src/test/test_consdiffmgr.c b/src/test/test_consdiffmgr.c
index dc05c351b..6c451da68 100644
--- a/src/test/test_consdiffmgr.c
+++ b/src/test/test_consdiffmgr.c
@@ -11,7 +11,7 @@
#include "core/mainloop/cpuworker.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "feature/nodelist/networkstatus.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "lib/evloop/workqueue.h"
#include "lib/compress/compress.h"
#include "lib/encoding/confline.h"
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index 59464022b..3b5d6dc3d 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -16,6 +16,7 @@
#define DLSTATUS_PRIVATE
#define HIBERNATE_PRIVATE
#define NETWORKSTATUS_PRIVATE
+#define NS_PARSE_PRIVATE
#define NODE_SELECT_PRIVATE
#define RELAY_PRIVATE
#define ROUTERLIST_PRIVATE
@@ -54,6 +55,7 @@
#include "feature/nodelist/node_select.h"
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/authcert_parse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/routerset.h"
diff --git a/src/test/test_dir_common.c b/src/test/test_dir_common.c
index 5a9659ad4..86d283894 100644
--- a/src/test/test_dir_common.c
+++ b/src/test/test_dir_common.c
@@ -11,6 +11,7 @@
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/authcert_parse.h"
+#include "feature/dirparse/ns_parse.h"
#include "test/test_dir_common.h"
#include "feature/dircommon/voting_schedule.h"
diff --git a/src/test/test_guardfraction.c b/src/test/test_guardfraction.c
index c7266be66..d6f4cd63f 100644
--- a/src/test/test_guardfraction.c
+++ b/src/test/test_guardfraction.c
@@ -2,15 +2,15 @@
/* See LICENSE for licensing information */
#define GUARDFRACTION_PRIVATE
-#define ROUTERPARSE_PRIVATE
#define NETWORKSTATUS_PRIVATE
+#define NS_PARSE_PRIVATE
#include "orconfig.h"
#include "core/or/or.h"
#include "app/config/config.h"
#include "feature/dirauth/guardfraction.h"
#include "feature/client/entrynodes.h"
-#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/networkstatus_st.h"
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index d4606c227..b386ad529 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -35,6 +35,7 @@
#include "feature/nodelist/routerset.h"
#include "feature/dirparse/authcert_parse.h"
#include "feature/dirparse/routerparse.h"
+#include "feature/dirparse/ns_parse.h"
#include "feature/dirauth/shared_random.h"
#include "app/config/statefile.h"
1
0
commit 5f9839ee424476de83da8533955da58068e57037
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Mon Oct 1 11:17:33 2018 -0500
Remove unused headers from routerparse.c
---
src/feature/dirparse/routerparse.c | 41 ++++----------------------------------
1 file changed, 4 insertions(+), 37 deletions(-)
diff --git a/src/feature/dirparse/routerparse.c b/src/feature/dirparse/routerparse.c
index 54c0e8948..2aa6bef80 100644
--- a/src/feature/dirparse/routerparse.c
+++ b/src/feature/dirparse/routerparse.c
@@ -50,65 +50,32 @@
* </ul>
**/
-#define ROUTERPARSE_PRIVATE
#define EXPOSE_ROUTERDESC_TOKEN_TABLE
#include "core/or/or.h"
#include "app/config/config.h"
-#include "core/or/circuitstats.h"
#include "core/or/policies.h"
-#include "core/or/protover.h"
-#include "feature/client/entrynodes.h"
-#include "feature/dirauth/shared_random.h"
-#include "feature/dircommon/voting_schedule.h"
#include "feature/dirparse/parsecommon.h"
#include "feature/dirparse/policy_parse.h"
#include "feature/dirparse/routerparse.h"
-#include "feature/hs_common/shared_random_client.h"
-#include "feature/nodelist/authcert.h"
+#include "feature/dirparse/sigcommon.h"
+#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/describe.h"
-#include "feature/nodelist/microdesc.h"
-#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nickname.h"
#include "feature/nodelist/routerinfo.h"
#include "feature/nodelist/routerlist.h"
#include "feature/nodelist/torcert.h"
#include "feature/relay/router.h"
-#include "feature/relay/routerkeys.h"
-#include "feature/rend/rendcommon.h"
-#include "feature/stats/rephist.h"
+#include "lib/crypt_ops/crypto_curve25519.h"
+#include "lib/crypt_ops/crypto_ed25519.h"
#include "lib/crypt_ops/crypto_format.h"
-#include "lib/crypt_ops/crypto_util.h"
#include "lib/memarea/memarea.h"
#include "lib/sandbox/sandbox.h"
-#include "feature/dirparse/authcert_parse.h"
-#include "feature/dirparse/sigcommon.h"
-#include "feature/dirparse/unparseable.h"
-
-#include "feature/dirauth/dirvote.h"
#include "core/or/addr_policy_st.h"
-#include "feature/nodelist/authority_cert_st.h"
-#include "feature/nodelist/document_signature_st.h"
-#include "core/or/extend_info_st.h"
#include "feature/nodelist/extrainfo_st.h"
-#include "feature/nodelist/microdesc_st.h"
-#include "feature/nodelist/networkstatus_st.h"
-#include "feature/nodelist/networkstatus_voter_info_st.h"
-#include "feature/dirauth/ns_detached_signatures_st.h"
-#include "feature/rend/rend_authorized_client_st.h"
-#include "feature/rend/rend_intro_point_st.h"
-#include "feature/rend/rend_service_descriptor_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerlist_st.h"
-#include "core/or/tor_version_st.h"
-#include "feature/dirauth/vote_microdesc_hash_st.h"
-#include "feature/nodelist/vote_routerstatus_st.h"
-
-#include "lib/container/bloomfilt.h"
-
-#undef log
-#include <math.h>
/****************************************************************************/
1
0